We're sorry Aspose doesn't work properply without JavaScript enabled.

Free Support Forum - aspose.com

Converting chart from a sheet to an image

I have current problem where I am creating charts and trying to insert them into ppt format with aspose.slides. All works fine if I use the aspect ratio the image is given from ToImage() function.

I have traced this to be a problem of creating chart sheet to 30 row * 30 columns of size

Dim chartIdx As Integer = chartSheet.Charts.Add(Aspose.Cells.Charts.ChartType.Column, 0, 0, sheetRows, sheetCols)


This and

Dim opts As New Aspose.Cells.Rendering.ImageOrPrintOptions
opts.HorizontalResolution = 100
opts.VerticalResolution = 100


Seem to be the only places to affect the actual size of the image im getting from

wb.Worksheets(0).Charts(0).ToImage(opts)

What I want to do is to give a box area (2 points in which the image should be displayed with correct aspect ratio). Is the only way to get chart as a certain size by defining the size with rows and columns and not using calculation with horizontal/vertical resolutions?


Sample code below:

Imports Aspose.Cells
Imports Aspose.Cells.Charts
Imports System.Drawing

Public Class Test2
Inherits System.Web.UI.Page

Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
'Put user code to initialize the page here
Dim wb As New workbook()
createstaticData(wb)
CreateStaticReport(wb)

Dim opts As New Aspose.Cells.Rendering.ImageOrPrintOptions
opts.Quality = 80
opts.HorizontalResolution = 100
opts.VerticalResolution = 100
opts.ImageFormat = System.Drawing.Imaging.ImageFormat.Jpeg

Dim oBitmap As Bitmap = wb.Worksheets(0).Charts(0).ToImage(opts)

Response.ContentType = “image/jpeg”
oBitmap.Save(Response.OutputStream, System.Drawing.Imaging.ImageFormat.Jpeg)

End Sub

Private Sub CreateStaticData(ByVal workbook As Workbook)
Dim cells As cells = workbook.Worksheets(0).Cells

'Put a string into a cell
cells(“A1”).PutValue(“Region”)
cells(“A2”).PutValue(“France”)
cells(“A3”).PutValue(“Germany”)
cells(“A4”).PutValue(“England”)

cells(“B1”).PutValue(2002)
cells(“C1”).PutValue(2003)
cells(“D1”).PutValue(2004)
cells(“E1”).PutValue(2005)
cells(“F1”).PutValue(2006)

cells(“B2”).PutValue(40000)
cells(“C2”).PutValue(45000)
cells(“D2”).PutValue(50000)
cells(“E2”).PutValue(55000)
cells(“F2”).PutValue(70000)

cells(“B3”).PutValue(10000)
cells(“C3”).PutValue(25000)
cells(“D3”).PutValue(40000)
cells(“E3”).PutValue(55000)
cells(“F3”).PutValue(80000)

cells(“B4”).PutValue(65000)
cells(“C4”).PutValue(50000)
cells(“D4”).PutValue(35000)
cells(“E4”).PutValue(30000)
cells(“F4”).PutValue(20000)
End Sub

Private Sub CreateStaticReport(ByVal workbook As Workbook)
Dim sheet As Worksheet = workbook.Worksheets(0)
'Set the name of worksheet
sheet.Name = “Line”
sheet.IsGridlinesVisible = False

'Create chart
Dim chartIndex As Integer = 0
chartIndex = sheet.Charts.Add(ChartType.Column, 0, 0, 30, 30)

Dim chart As chart = sheet.Charts(chartIndex)
chart.CategoryAxis.MajorGridLines.IsVisible = False

'Set Properties of chart title
chart.Title.Text = “Sales By Region For Years”
chart.Title.TextFont.Color = Color.Black
chart.Title.TextFont.IsBold = True
chart.Title.TextFont.Size = 12

'Set Properties of nseries
chart.NSeries.Add(“B2:F4”, False)
chart.NSeries.CategoryData = “B1:F1”
chart.NSeries.IsColorVaried = True

Dim cells As cells = workbook.Worksheets(0).Cells
Dim i As Integer
For i = 0 To chart.NSeries.Count - 1 Step i + 1
chart.NSeries(i).Name = cells(i + 1, 0).Value.ToString()
chart.NSeries(i).MarkerStyle = ChartMarkerType.Automatic

'Set Properties of categoryaxis title
chart.CategoryAxis.Title.Text = “Year(2002-2006)”
chart.CategoryAxis.Title.TextFont.Color = Color.Black
chart.CategoryAxis.Title.TextFont.IsBold = True
chart.CategoryAxis.Title.TextFont.Size = 10

'Set the legend position type.
chart.Legend.Position = LegendPositionType.Top
Next
End Sub

End Class

Hi,


Well, I think you can make use of Chart.ChartObject.Width and Chart.ChartObject.Height attributes etc. to set your desired size. Also, you can use ChartObject.UpperDeltaX/Y attribute(s) etc. to set the chart position in between row/column.


chart.ChartObject.Width = 430;
chart.ChartObject.Height = 325;
chart.ChartObject.UpperDeltaX = 100;
chart.ChartObject.UpperDeltaY = 50;


Thank you.

Oh, did not realize that there was the ChartObject to work with the actual size of the chart. Was trying to fix the size with ChartArea. Seems that this way I can achieve what I am trying to, thanks.