Hi,
Please run the following sample and see that image generated is not perfect.
Please copy the file - pareto.xml - to the same folder.
Please find the image attached for details.
Thanks
Muhammed
import java.io.ByteArrayOutputStream;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.NodeList;
import com.aspose.cells.ASeries;
import com.aspose.cells.CellsHelper;
import com.aspose.cells.Chart;
import com.aspose.cells.ChartType;
import com.aspose.cells.Color;
import com.aspose.cells.FileFormatType;
import com.aspose.cells.ImageFormat;
import com.aspose.cells.ImageOptions;
import com.aspose.cells.PatternFill;
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;
public class ParetoToImage {
public static void main(String args[]) {
try {
System.out.println(CellsHelper.getReleaseVersion());
Workbook wb = new Workbook();
Worksheet dataSheet = wb.getWorksheets().addSheet();
String dataSheetName = "DataSheet";
dataSheet.setName(dataSheetName);
//Add a chart sheet
Worksheet chartSheet = wb.getWorksheets().addSheet();
chartSheet.setName("ChartSheet");
int chartSheetIdx = chartSheet.getIndex();
int rowIndex = 1;
int colIndex = 1;
int colCount = 1;
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
DocumentBuilder db = dbf.newDocumentBuilder();
Document doc = db.parse(ParetoToImage.class.getResourceAsStream("pareto.xml"));
NodeList valList = doc.getElementsByTagName("Value");
int rowCount = valList.getLength();
for (int i = 0; i < valList.getLength(); i++) {
Element eltValue = (Element)valList.item(i);
double cellValue = Double.parseDouble(eltValue.getFirstChild().getNodeValue());
//Each row contains diff series data. Note that first row contain category data.
dataSheet.getCells().getCell(rowIndex, colIndex).setValue(cellValue);
rowIndex++;
}
//Find Percent on Total
String sumFormula = "SUM(" + CellsHelper.convertCellIndexToName(1, colCount + 1) + ":" + CellsHelper.convertCellIndexToName(rowCount, colCount + 1) + ")";
for (int i = 0; i < rowCount; i++) {
String firstDataCell = CellsHelper.convertCellIndexToName(i + 1, 1);
String lastDataCell = CellsHelper.convertCellIndexToName(i + 1, colCount);
String rowSumFormula = "=SUM(" + firstDataCell + ":" + lastDataCell + ")";
dataSheet.getCells().getCell(i + 1, colCount + 1).setFormula(rowSumFormula);
String dataCell = CellsHelper.convertCellIndexToName(i + 1, colCount + 1);
//String percentCellFormula = "=100*(" + dataCell + "/" + sumFormula + ")";
String percentCellFormula = "=(" + dataCell + "/" + sumFormula + ")";
dataSheet.getCells().getCell(i + 1, colCount + 2).setFormula(percentCellFormula);
String cumulativePercentCellFormula = "=SUM(" + CellsHelper.convertCellIndexToName(1, colCount + 2) + ":" + CellsHelper.convertCellIndexToName(i + 1, colCount + 2) + ")";
dataSheet.getCells().getCell(i + 1, colCount + 3).setFormula(cumulativePercentCellFormula);
}
String barDataRef = dataSheet.getName() + "!" + CellsHelper.convertCellIndexToName(1, colCount + 1) + ":" + CellsHelper.convertCellIndexToName(rowCount, colCount + 1);
String lineDataRef = dataSheet.getName() + "!" + CellsHelper.convertCellIndexToName(1, colCount + 3) + ":" + CellsHelper.convertCellIndexToName(rowCount, colCount + 3);
String dataRef = barDataRef + "," + lineDataRef;
Chart chart = chartSheet.getCharts().addChart(ChartType.COLUMN_CLUSTERED, 0, 0, 1, 1, 600, 400);
chart.getNSeries().add(dataRef, true);
PatternFill pf = new PatternFill();
pf.setBackgroundColor(new Color(0xA5, 0x2A, 0x2A));
pf.setForegroundColor(new Color(0xA5, 0x2A, 0x2A));
chart.getNSeries().get(0).getArea().setFill(pf);
chart.getNSeries().get(0).getBorder().setVisible(false);
chart.getNSeries().get(1).setType(ChartType.LINE);
chart.getNSeries().get(1).setPlotOnSecondAxis(true);
chart.getSecondValueAxis().setMinValue(0);
chart.getSecondValueAxis().setMaxValue(1);
chart.getSecondValueAxis().setNumberFormat("0%");
for (int i = 0; i < chart.getNSeries().size(); i++) {
ASeries series = chart.getNSeries().get(i);
series.setGapWidth(25);
}
int chartRows = 25, chartCols = 15;
wb.getWorksheets().setActiveSheet(chartSheetIdx);
wb.setOleSize(0, chartRows, 0, chartCols);
//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);
System.out.println("Pareto successfully completed!!!");
}
catch (Exception e) {
e.printStackTrace();
}
}
}