Converting Chart to Image

Skip to end of metadata
Go to start of metadata
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 the release of version v2.1.2, Aspose.Cells for Java supports to convert charts in Excel files to image files.

The toImage method of the Chart class converts the chart to an image file, so, you can save the image file to disk/stream for you need.

Example:

The following example shows how to convert a chart to render an image file.

[Java]
//Create a new Workbook.
Workbook workbook = new Workbook();

//Get the first worksheet.
Worksheet sheet = workbook.getWorksheets().get(0);

//Set the name of worksheet
sheet.setName("Data");

//Get the cells collection in the sheet.
Cells cells = workbook.getWorksheets().get(0).getCells();

//Put some values into a cells of the Data sheet.
cells.get("A1").setValue("Region");
cells.get("A2").setValue("France");
cells.get("A3").setValue("Germany");
cells.get("A4").setValue("England");
cells.get("A5").setValue("Sweden");
cells.get("A6").setValue("Italy");
cells.get("A7").setValue("Spain");
cells.get("A8").setValue("Portugal");
cells.get("B1").setValue("Sale");
cells.get("B2").setValue(70000);
cells.get("B3").setValue(55000);
cells.get("B4").setValue(30000);
cells.get("B5").setValue(40000);
cells.get("B6").setValue(35000);
cells.get("B7").setValue(32000);
cells.get("B8").setValue(10000);

//Create chart
int chartIndex = sheet.getCharts().add(ChartType.COLUMN, 12, 1, 33, 12);
Chart chart = sheet.getCharts().get(chartIndex);

//Set properties of chart title
chart.getTitle().setText("Sales By Region");
chart.getTitle().getTextFont().setBold(true);
chart.getTitle().getTextFont().setSize(12);

//Set properties of nseries
chart.getNSeries().add("Data!B2:B8", true);
chart.getNSeries().setCategoryData("Data!A2:A8");

//Set the fill colors for the series's data points (France - Portugal(7 points))
ChartPointCollection chartPoints = chart.getNSeries().get(0).getPoints();

ChartPoint point = chartPoints.get(0);
point.getArea().setForegroundColor(Color.getCyan());

point = chartPoints.get(1);
point.getArea().setForegroundColor(Color.getBlue());

point = chartPoints.get(2);
point.getArea().setForegroundColor(Color.getYellow());

point = chartPoints.get(3);
point.getArea().setForegroundColor(Color.getRed());

point = chartPoints.get(4);
point.getArea().setForegroundColor(Color.getBlack());

point = chartPoints.get(5);
point.getArea().setForegroundColor(Color.getGreen());

point = chartPoints.get(6);
point.getArea().setForegroundColor(Color.getMaroon());

//Set the legend invisible
chart.setShowLegend(false);

//Get the Chart mage
ImageOrPrintOptions imgOpts = new ImageOrPrintOptions();
imgOpts.setImageFormat(ImageFormat.getPng());

//Save the chart image file.
chart.toImage(new FileOutputStream("D:\\Files\\MyChartImage.png"), imgOpts);
 
Supported Charts List

Following is the list of supported charts. There are certain charts/attributes which are not supported yet. We always work to extend and enhance the supported charts list and attributes with every new version of Aspose.Cells .
Y – Yes, N – No.

Chart type Chart sub-type Is supported
Column Column Y
  ColumnStacked Y
  Column100PercentStacked Y
  Column3DClustered Y
  Column3DStacked Y
  Column3D100PercentStacked Y
  Column3D Y
Bar Bar Y
  BarStacked Y
  Bar100PercentStacked Y
  Bar3DClustered Y
  Bar3DStacked Y
  Bar3D100PercentStacked Y
Line Line Y
  LineStacked Y
  Line100PercentStacked Y
  LineWithDataMarkers Y
  LineStackedWithDataMarkers Y
  Line100PercentStackedWithDataMarkers Y
  Line3D N
Pie Pie Y
  Pie3D Y
  PiePie N
  PieExploded Y
  Pie3DExploded Y
  PieBar N
Scatter Scatter Y
  ScatterConnectedByCurvesWithDataMarker N
  ScatterConnectedByCurvesWithoutDataMarker N
  ScatterConnectedByLinesWithDataMarker Y
  ScatterConnectedByLinesWithoutDataMarker Y
Area Area Y
  AreaStacked Y
  Area100PercentStacked Y
  Area3D N
  Area3DStacked N
  Area3D100PercentStacked N
Doughnut Doughnut Y
  DoughnutExploded Y
Radar Radar N
  RadarWithDataMarkers N
  RadarFilled N
Surface Surface3D N
  SurfaceWireframe3D N
  SurfaceContour N
  SurfaceContourWireframe N
Bubble Bubble Y
  Bubble3D N
Stock StockHighLowClose N
  StockOpenHighLowClose N
  StockVolumeHighLowClose N
  StockVolumeOpenHighLowClose N
Cylinder Cylinder N
  CylinderStacked N
  Cylinder100PercentStacked N
  CylindricalBar N
  CylindricalBarStacked N
  CylindricalBar100PercentStacked N
  CylindricalColumn3D N
Cone Cone N
  ConeStacked N
  Cone100PercentStacked N
  ConicalBar N
  ConicalBarStacked N
  ConicalBar100PercentStacked N
  ConicalColumn3D N
Pyramid Pyramid N
  PyramidStacked N
  Pyramid100PercentStacked N
  PyramidBar N
  PyramidBarStacked N
  PyramidBar100PercentStacked N
  PyramidColumn3D N

There are some other chart’s attributes/objects that are not supported:

  • ErrorBars, Trendlines, Up/DownBars, DropLine, HiLowLine,
  • ChartDataTable
  • Font effects: Underline, Strikeout
Enter labels to add to this page:
Please wait 
Looking for a label? Just start typing.