Free Support Forum - aspose.com

Chart.toImage - Pie Chart colors are not reflected in image

Hi,
Please find sample below and see that the following issues are happening in the image.

1. DataPoint colors are not reflected in image
2. Pie radius is not same

Thanks
Muhammed


import java.io.ByteArrayOutputStream;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.lang.reflect.Array;
import java.util.ArrayList;
import java.util.List;

import com.aspose.cells.ASeries;
import com.aspose.cells.Chart;
import com.aspose.cells.ChartMarkerType;
import com.aspose.cells.ChartPoint;
import com.aspose.cells.ChartPoints;
import com.aspose.cells.ChartType;
import com.aspose.cells.Color;
import com.aspose.cells.FileFormatType;
import com.aspose.cells.Fill;
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 PieChartIssues {
//private Color[] defaultColors = new Color[]{Color.CYAN, Color.GRAY, Color.GREEN, Color.LIME, Color.MAGENTA, Color.MAROON, Color.NAVY, Color.OLIVE, Color.PURPLE, Color.RED , Color.SILVER, Color.TEAL, Color.WHITE, Color.BLACK, Color.BLUE, Color.YELLOW};
// private int paletteIndex;
//private List paletteColors;

public static void main(String args[]) {

try {
String[] cellsName = new String[] { "B2","C2","D2","E2","F2","G2","H2","I2","J2",
"B3","C3","D3","E3","F3","G3","H3","I3","J3",
"B4","C4","D4","E4","F4","G4","H4","I4","J4",
};

//Array of cell data
double[] cellsValue = new double[] {7.027279E7,1744253.0,1473.0,395085.7,7.019342E7,17.0,1111239.0,0.0,5.218978E7,
9.530519E7,2848223.0,6885.9,395216.4,9.729046E7,18.5,900626.8,0.0,4.484255E7,
9.898876E7,1076122.0,1393.5,790165.4,8.850956E7,12.8,1151062.0,0.0,7.406841E7};

Workbook wb = new Workbook();

Worksheet dataSheet = wb.getWorksheets().getSheet(0);
String dataSheetName = "DataSheet";
dataSheet.setName(dataSheetName);

dataSheet.getCells().getCell("A2").setValue("Unit Sales");
dataSheet.getCells().getCell("A3").setValue("Value Sales");
dataSheet.getCells().getCell("A4").setValue("Volume Sales");
dataSheet.getCells().getCell("B1").setValue("BrandA Rinse Conditioners");
dataSheet.getCells().getCell("C1").setValue("BrandB Ltd Rinse Conditioners");
dataSheet.getCells().getCell("D1").setValue("BrandC Rinse Conditioners");
dataSheet.getCells().getCell("E1").setValue("BrandD Rinse Conditioners");
dataSheet.getCells().getCell("F1").setValue("BrandE Rinse Conditioners");
dataSheet.getCells().getCell("G1").setValue("BrandF Rinse Conditioners");
dataSheet.getCells().getCell("H1").setValue("BrandG Rinse Conditioners");
dataSheet.getCells().getCell("I1").setValue("BrandH Rinse Conditioners");
dataSheet.getCells().getCell("J1").setValue("BrandJ Rinse Conditioners");

//Populate DataSheet with data
int size = Array.getLength(cellsName);
for (int i = 0; i < size; i++) {
String cellName = cellsName[i];
double 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 = 300;
int chartHeight = 300;

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);

int chartStartRow = 0;
int chartStartColumn = 0;
int chartLeft = 0;
int chartTop = 0;

Chart chart = chartSheet.getCharts().addChart(ChartType.PIE, chartStartRow, chartStartColumn, chartLeft, chartTop, chartWidth, chartHeight);
chart.getNSeries().add(dataSheetName + "!B2:J2", false);

//String categoryAreaRef = dataSheet.getName() + "!A2:A4";
String categoryAreaRef = dataSheet.getName() + "!B1:J1";
chart.getNSeries().setCategoryData(categoryAreaRef);

String chartTitle = dataSheet.getCells().getCell("A2").getValue().toString();
chart.getTitle().setText(chartTitle);

for (int j = 0; j < chart.getNSeries().size(); j++) {
ASeries series = chart.getNSeries().get(j);
ChartPoints chartPoints = series.getChartPoints();
for (int k = 0; k < chartPoints.size(); k++) {
ChartPoint chartPoint = chartPoints.getChartPoint(k);
PatternFill seriesFillObj = new PatternFill();
seriesFillObj.setForegroundColor(wb.getPalette().getColor(k));
seriesFillObj.setBackgroundColor(wb.getPalette().getColor(k));
chartPoint.getArea().setFill(seriesFillObj);
}
}

chart.setLegendShown(false);

//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);

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"));

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\\chart1.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();
}
}
}

Hi,

We found your mentioned issues after an initial test. We will figure out your issue(s) soon.

We have logged your issue as an id: CELLSJAVA-15336 into our issue tracking system.

Thank you.

Hi,<?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" />

Please try the attached latest version. We have fixed your mentioned issue regarding Chart2Image.

Thank You & Best Regards,

Hi,<?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" />

Please try the attached latest version of Aspose.Cells for Java. We have fixed your mentioned issue.

Thank You & Best Regards,

Hi,

Working fine.

Thanks
Muhammed