Introduction
Charts are visually appealing and make it easy for users to see comparisons, patterns, and trends in data. For instance, rather than having to analyze several columns of worksheet numbers, you can see at a glance whether sales are falling or rising over quarterly periods, or how the actual sales compare to the projected sales.
Sometimes, you do need to present the chart in your applications or web pages. You might need to insert it into a Word Document, a PDF file, a Power Point Presentation or in some other scenario. Simply you want the chart should be rendered as an image, so that you may paste it into your applications with ease. A Picture is worthwhile. Frequently, in the course of work, one has to present statistical and graphical information in an easy to understand and an easy to maintain manner
Since v4.2, Aspose.Cells supports to convert charts in Excel files to images.
The ToImage method of the Chart class converts a chart to an image, it returns System.Drawing.Bitmap object. This object has a Save method used to save the image file to disk.
Example:
The following example shows how to convert a chart to render an image file.
[C#]
//Instantiating a Workbook object
Workbook workbook = new Workbook();
//Adding a new worksheet to the Excel object
int sheetIndex=workbook.Worksheets.Add();
//Obtaining the reference of the newly added worksheet by
//passing its sheet index
Worksheet worksheet=workbook.Worksheets[sheetIndex];
//Adding a sample value to "A1" cell
worksheet.Cells["A1"].PutValue(50);
//Adding a sample value to "A2" cell
worksheet.Cells["A2"].PutValue(100);
//Adding a sample value to "A3" cell
worksheet.Cells["A3"].PutValue(150);
//Adding a sample value to "B1" cell
worksheet.Cells["B1"].PutValue(4);
//Adding a sample value to "B2" cell
worksheet.Cells["B2"].PutValue(20);
//Adding a sample value to "B3" cell
worksheet.Cells["B3"].PutValue(50);
//Adding a chart to the worksheet
int chartIndex=worksheet.Charts.Add(ChartType.Column,5,0,15,5);
//Accessing the instance of the newly added chart
Chart chart=worksheet.Charts[chartIndex];
//Adding NSeries (chart data source) to the chart ranging from "A1" cell to "B3"
chart.NSeries.Add("A1:B3",true);
//Converting chart to image.
Bitmap bitmap = chart.ToImage();
//Saving the chart image.
bitmap.Save("d:\\Chart.jpg",System.Drawing.Imaging.ImageFormat.Jpeg);
[VB.NET]
'Instantiating a Workbook object
Dim workbook As Workbook = New Workbook()
'Adding a new worksheet to the Excel object
Dim sheetIndex As Integer = workbook.Worksheets.Add()
'Obtaining the reference of the newly added worksheet by passing its sheet index
Dim worksheet As Worksheet = workbook.Worksheets(sheetIndex)
'Adding a sample value to "A1" cell
worksheet.Cells("A1").PutValue(50)
'Adding a sample value to "A2" cell
worksheet.Cells("A2").PutValue(100)
'Adding a sample value to "A3" cell
worksheet.Cells("A3").PutValue(150)
'Adding a sample value to "B1" cell
worksheet.Cells("B1").PutValue(4)
'Adding a sample value to "B2" cell
worksheet.Cells("B2").PutValue(20)
'Adding a sample value to "B3" cell
worksheet.Cells("B3").PutValue(50)
'Adding a chart to the worksheet
Dim chartIndex As Integer = worksheet.Charts.Add(ChartType.Column,5,0,15,5)
'Accessing the instance of the newly added chart
Dim chart As Chart = worksheet.Charts(chartIndex)
'Adding NSeries (chart data source) to the chart ranging from "A1" cell to "B3"
chart.NSeries.Add("A1:B3",True)
'Converting chart to image.
Dim bitmap As Bitmap = chart.ToImage()
'Saving the chart image.
bitmap.Save("d:\Chart.jpg",System.Drawing.Imaging.ImageFormat.Jpeg)
[JAVA]
Currently this feature is not available in Java version, we will support it in the future.
Supported Charts List
Following is the list of supported charts. There are certain charts which are not supported yet. We always work to extend and enhance the supported charts list with every new version of Aspose.Cells.
|
Chart type
|
Chart sub-type
|
Is supported
|
|
Column
|
Column
|
Y
|
|
|
ColumnStacked
|
Y
|
|
|
Column100PercentStacked
|
Y
|
|
|
Column3DClustered
|
N
|
|
|
Column3DStacked
|
N
|
|
|
Column3D100PercentStacked
|
N
|
|
|
Column3D
|
N
|
|
Bar
|
Bar
|
Y
|
|
|
BarStacked
|
Y
|
|
|
Bar100PercentStacked
|
Y
|
|
|
Bar3DClustered
|
N
|
|
|
Bar3DStacked
|
N
|
|
|
Bar3D100PercentStacked
|
N
|
|
Line
|
Line
|
Y
|
|
|
LineStacked
|
Y
|
|
|
Line100PercentStacked
|
Y
|
|
|
LineWithDataMarkers
|
Y
|
|
|
LineStackedWithDataMarkers
|
Y
|
|
|
Line100PercentStackedWithDataMarkers
|
Y
|
|
|
Line3D
|
N
|
|
Pie
|
Pie
|
Y
|
|
|
Pie3D< |