Hi,
I am using aspose cells 7.0.3 for java. Is there any way by which we can add different CUSTOM color to the pie chart.
The following options did not work.
- By default the pie chart created uses the excel default template. i.e if I don’t specify any color
- When I apply color to the series, all the slices have same color.
My requirement is to have my defined colors in the chart. Attached the desired output.
Please let me know.
Hi,
You need to set the color of individual chart point for this purpose. For example:
SeriesCollection nSeries = chart.getNSeries();
//Get the Chart Series
Series aSeries = nSeries.get(0);
ChartPointCollection chartPoints = aSeries.getPoints();
ChartPoint point = chartPoints.get(0);
point.getArea().setForegroundColor(Color.getCyan());
Please let us know if we can be of any additional help to you.
Thanks
Hi,
Thanks for your posting and using Aspose.Cells for Java.
You need to set foreground color of the point area inside the series.
For your full understanding, I have written the complete runnable code, which you can run and generate exactly the same pie chart as you have shown in your Pie-Slice-Colors.xlsx file.
Please see the code below. The code generates the full chart from scratch using Aspose.Cells for Java code.
The code is fully commented so you will not have any trouble understanding it.
Please download and use the latest version:
Aspose.Cells
for Java v7.2.0.6
I have attached the template and output xlsx file. Please also see the screenshot shown below.
If you get any question, please feel free to ask, we will help you asap.
Java
String filePath = "F:\\template.xlsx";
//Create a workbook object from the template file
Workbook workbook = new Workbook(filePath);
//Access the first worksheet.
Worksheet worksheet = workbook.getWorksheets().get(0);
//Put the sample values used in a pie chart
worksheet.getCells().get("C3").putValue("India");
worksheet.getCells().get("C4").putValue("China");
worksheet.getCells().get("C5").putValue("United States");
worksheet.getCells().get("C6").putValue("Russia");
worksheet.getCells().get("C7").putValue("United Kingdom");
worksheet.getCells().get("C8").putValue("Others");
//Put the sample values used in a pie chart
worksheet.getCells().get("D2").putValue("% of world population");
worksheet.getCells().get("D3").putValue(25);
worksheet.getCells().get("D4").putValue(30);
worksheet.getCells().get("D5").putValue(10);
worksheet.getCells().get("D6").putValue(13);
worksheet.getCells().get("D7").putValue(9);
worksheet.getCells().get("D8").putValue(13);
//Create a pie chart with desired length and width
int pieIdx = worksheet.getCharts().add(ChartType.PIE, 1, 6, 15, 14);
//Access the pie chart
Chart pie = worksheet.getCharts().get(pieIdx);
//Set the pie chart series
pie.getNSeries().add("D3:D8", true);
//Set the category data
pie.getNSeries().setCategoryData("=Sheet1!$C$3:$C$8");
//Set the chart title that is linked to cell D2
pie.getTitle().setLinkedSource("D2");
//Set the legend position at the bottom.
pie.getLegend().setPosition(LegendPositionType.BOTTOM);
//Set the chart title's font name and color
pie.getTitle().getFont().setName("Calibri");
pie.getTitle().getFont().setSize(18);
//Access the chart series
Series srs = pie.getNSeries().get(0);
//Color the indvidual points with custom colors
srs.getPoints().get(0).getArea().setForegroundColor(Color.fromArgb(0, 246, 22, 219));
srs.getPoints().get(1).getArea().setForegroundColor(Color.fromArgb(0, 51, 34, 84));
srs.getPoints().get(2).getArea().setForegroundColor(Color.fromArgb(0, 46, 74, 44));
srs.getPoints().get(3).getArea().setForegroundColor(Color.fromArgb(0, 19, 99, 44));
srs.getPoints().get(4).getArea().setForegroundColor(Color.fromArgb(0, 208, 223, 7));
srs.getPoints().get(5).getArea().setForegroundColor(Color.fromArgb(0, 222, 69, 8));
//Autofit all columns
worksheet.autoFitColumns();
//Save the workbook
workbook.save("output.xlsx");
Screenshot:
Seems like you have put a lot of effort into this. Thanks.
Actually I needed to iterate through the
ChartPointCollection as I never know the number of dataPoints.
Earlier I was trying to use the iterator on the ChartPointCollection. This iterator.hasNext() is always returning false. Is it a known bug??
However i see that ChartPointCollection.getCount() is returning actual number of of data points. I can iterate using this.
Thank you for your help. Please look into the iterator issue.
Hi,
We, it is not a bug. The iterator and CountOfPointObjects property of ChartPointCollection only take those instantiated ChartPoint objects into count. For a newly created chart, all chartpoints are using the default settings according to the chart type and serieses settings. There is no custom chartpoint object in the collection. When user customize one chartpoint’s settings explicitly (just as what you want to do with every chartpoint), the chartpoint object will be instantiated and put into the collection. So, to access those chartpoints that have custom settings, you should use the iterator. To customize one specified chart point, just as you are doing now, you should use ChartPointCollection[i] to get the specified chart point (if the specified chartpoint does not exist in the collection, a new object will be created and put into the collection) and use ChartPointCollection.Count/Seriese.CountOfDataValues property to limit the chart point index.
Thank you.
Thanks Amzad,
That clarifies. Bit unusual; but makes sense when we take into account memory usage. You must be saving some memory by this approach when the ChartPointCollection is not used for a chart.
Thanks again. Please consider this closed.
Hi,
Thanks for your feedback.
We have now closed this thread.
Let us know if you have any other questions or you face any other issue relating to Aspose.Cells. We will be glad to help you.