Hi,
Please run below sample and see the following exception when we create image out of it. The reason is one series contains all zeros. Note that the excel chart is properly created.
java.lang.NumberFormatException: For input string: "N"
at java.lang.NumberFormatException.forInputString(NumberFormatException.java:62)
at java.lang.Integer.parseInt(Integer.java:448)
at java.lang.Integer.parseInt(Integer.java:498)
at com.aspose.a.d.s(Unknown Source)
at com.aspose.a.d.b(Unknown Source)
at com.aspose.a.d.aC(Unknown Source)
at com.aspose.a.d.h(Unknown Source)
at com.aspose.a.g.g(Unknown Source)
at com.aspose.a.a.a(Unknown Source)
at com.aspose.a.h.a(Unknown Source)
at com.aspose.a.h.b(Unknown Source)
at com.aspose.a.h.a(Unknown Source)
at com.aspose.a.h.c(Unknown Source)
at com.aspose.a.h.a(Unknown Source)
at com.aspose.a.c.a.a(Unknown Source)
at com.aspose.cells.Chart.toImage(Unknown Source)
import java.io.ByteArrayOutputStream;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.lang.reflect.Array;
import com.aspose.cells.ASeries;
import com.aspose.cells.Chart;
import com.aspose.cells.ChartType;
import com.aspose.cells.DisplayUnitType;
import com.aspose.cells.FileFormatType;
import com.aspose.cells.GradientColorType;
import com.aspose.cells.GradientDirectionType;
import com.aspose.cells.GradientFill;
import com.aspose.cells.GradientFillType;
import com.aspose.cells.GradientStyleType;
import com.aspose.cells.ImageFormat;
import com.aspose.cells.ImageOptions;
import com.aspose.cells.PatternFill;
import com.aspose.cells.TickLabelPositionType;
import com.aspose.cells.TrendlineType;
import com.aspose.cells.Workbook;
import com.aspose.cells.Worksheet;
import com.aspose.slides.OleObjectFrame;
import com.aspose.slides.Presentation;
import com.aspose.slides.Slide;
/**
* @author mkarattu
*
* To change the template for this generated type comment go to
* Window>Preferences>Java>Code Generation>Code and Comments
*/
public class StackedBarChartMaker {
public static void main(String args[]) {
try {
String[] cellsName = new String[] { "B1", "B2", "B3", "B4",
"C1", "C2", "C3", "C4",
"D1", "D2", "D3", "D4",
"E1", "E2", "E3", "E4",
"F1", "F2", "F3", "F4"
};
//Array of cell data
int[] cellsValue = new int[] { 67, 86, 68, 91,
44, 64, 89, 48,
0, 0, 0, 0, 0,
43, 29, 69, 26,
24, 40, 38, 25 };
Workbook wb = new Workbook();
Worksheet dataSheet = wb.getWorksheets().getSheet(0);
String dataSheetName = "DataSheet";
dataSheet.setName(dataSheetName);
dataSheet.getCells().getCell("A1").setValue("IN");
dataSheet.getCells().getCell("A2").setValue("US");
dataSheet.getCells().getCell("A3").setValue("UK");
dataSheet.getCells().getCell("A4").setValue("JP");
dataSheet.getCells().getCell("A5").setValue("CH");
//Populate DataSheet with data
int size = Array.getLength(cellsName);
for (int i = 0; i < size; i++) {
String cellName = cellsName[i];
int cellValue = cellsValue[i];
dataSheet.getCells().getCell(cellName).setValue(cellValue);
}
//Add a chart sheet
Worksheet chartSheet = wb.getWorksheets().addSheet();
chartSheet.setName("ChartSheet");
int chartSheetIdx = chartSheet.getIndex();
//Add a chart in ChartSheet with data series from DataSheet
int chartWidth = 600;
int chartHeight = 400;
int chartHeightInRows = 0;
int chartWidthInCols = 0;
int cellWidth = dataSheet.getCells().getColumnWidthPixel(0);
int cellHeight = dataSheet.getCells().getRowHeightPixel(0);
chartWidthInCols = (int)(chartWidth / cellWidth);
chartHeightInRows = (int)(chartHeight / cellHeight);
chartWidth = chartWidthInCols * cellWidth;
chartHeight = chartHeightInRows * cellHeight;
System.out.println("OLE SIZE = Cols: " + chartWidthInCols + ", Rows: " + chartHeightInRows);
System.out.println("CHART SIZE = chartWidth: " + chartWidth + ", chartHeight: " + chartHeight);
Chart chart = chartSheet.getCharts().addChart(ChartType.COLUMN_100PERCENT_STACKED, 0, 0, 0, 0, chartWidth, chartHeight);
chart.getNSeries().add(dataSheetName + "!B1:F4", false);
for (int i = 0; i < chart.getNSeries().size(); i++) {
ASeries series = chart.getNSeries().get(i);
PatternFill pf = new PatternFill();
pf.setBackgroundColor(wb.getPalette().getColor(i));
pf.setForegroundColor(wb.getPalette().getColor(i));
series.getArea().setFill(pf);
}
String categoryAreaRef = dataSheet.getName() + "!A1:A5";
chart.getNSeries().setCategoryData(categoryAreaRef);
chart.getCategoryAxis().setDisplayUnitLabelShown(true);
chart.getCategoryAxis().setTickLabelPosition(TickLabelPositionType.NEXT_TO_AXIS);
chart.getTitle().setText("Aspose.Cells");
//Trendlines
chart.getNSeries().get(0).getTrendlines().addTrendline(TrendlineType.LINEAR);
wb.getWorksheets().setActiveSheet(chartSheetIdx);
wb.setOleSize(0, chartHeightInRows - 1, 0, chartWidthInCols - 1);
//Save the workbook to stream
ByteArrayOutputStream bout = new ByteArrayOutputStream();
wb.save(bout);
wb.save(new FileOutputStream("D:\\Temp\\output.xls"));
//Get Chart as image.
ImageOptions imgOpts = new ImageOptions();
imgOpts.setImageFormat(ImageFormat.PNG);
imgOpts.setFashion(FileFormatType.EXCEL2003);
chart.toImage(new FileOutputStream("D:\\Temp\\chart.png"), imgOpts);
Presentation pres = new Presentation();
Slide sld = pres.getSlideByPosition(1);
int slideWidth = (int) pres.getSlideSize().getX();
int slideHeight = (int) pres.getSlideSize().getY();
int x = (slideWidth - chartWidth * 8) / 2;
int y = (slideHeight - chartHeight * 8) / 2;
OleObjectFrame oof = sld.getShapes().addOleObjectFrame(800, 500, chartWidth * 8, chartHeight * 8, "Excel.Sheet.8", bout.toByteArray());
com.aspose.slides.Picture pic = new com.aspose.slides.Picture(pres, new FileInputStream("D:\\Temp\\chart.png"));
int picId = pres.getPictures().add(pic);
oof.setPictureId(picId);
//Write the presentation on disk
pres.write(new FileOutputStream("D:\\Temp\\output.ppt"));
System.out.println("successfully completed!!!");
}
catch (Exception e) {
e.printStackTrace();
}
}
}
- Thanks
Muhammed