Setting Color of Chart Data Points

I’m trying to set the color of the data points for my Chart, but it’s not working. The data points stay the default blue color instead of changing to green.

Here is my code below.

ChartCollection charts = worksheets.get(0).getCharts();
int index = charts.add(ChartType.SCATTER_CONNECTED_BY_LINES_WITH_DATA_MARKER,5,0,15,5);
Chart chart = charts.get(index);

SeriesCollection series = chart.getNSeries();
series.add(“U1:U33”, true);
series.get(0).setValues(“U1:U33”);
series.setCategoryData(“T1:T33”);

for (int i = 0; i < chartPoints.getCount(); i++) {
ChartPoint point = chartPoints.get(i);
point.getArea().setFormatting(FormattingType.CUSTOM);
point.getArea().setForegroundColor(Color.getGreen());
}

Hi,


Thanks for providing us some details and sample code segment.

Could you provide us the output Excel file that contains the chart, we will check it soon.

Thank you.

Sure, here is a sample graph generated from the code.

Upon testing the code some more, it works when I use a different chart, like a pie chart. But it doesn’t work for the scatter chart.

Hi Trinh,


Thank you for sharing the sample spreadsheet.

Please check the following piece of code to change the series’ marker color to green. Also attached is the resultant spreadsheet for your reference. Please note, the code changes the color of markers to Red for visual discrimination from original Blue. You may change it to any desired color.

Java

Workbook book = new Workbook(dir + “sample.xlsx”);
ChartCollection charts = book.getWorksheets().get(0).getCharts();
Chart chart = charts.get(0);
for(int i = 0; i < chart.getNSeries().getCount(); i++)
{
Series series = chart.getNSeries().get(i);
Marker marker = series.getMarker();
marker.setMarkerStyle(ChartMarkerType.AUTOMATIC);
marker.getArea().getFillFormat().setType(FillType.SOLID);
marker.getArea().getFillFormat().getSolidFill().setColor(Color.getRed());
}
book.save(dir + “output.xlsx”);

Hi,


Hopefully the code snippet shared by Babar helps you to accomplish your task. Also, if you need to change/modify the Series’ line color, you may try the following line of code:
e.g
Sample code:

//Set the series line color to green.
series.getBorder().setColor(Color.getGreen());

Let us know if we can be of any further help.

Thank you.
babar.raza:
Hi Trinh,

Thank you for sharing the sample spreadsheet.

Please check the following piece of code to change the series' marker color to green. Also attached is the resultant spreadsheet for your reference. Please note, the code changes the color of markers to Red for visual discrimination from original Blue. You may change it to any desired color.

Java

Workbook book = new Workbook(dir + "sample.xlsx");
ChartCollection charts = book.getWorksheets().get(0).getCharts();
Chart chart = charts.get(0);
for(int i = 0; i < chart.getNSeries().getCount(); i++)
{
Series series = chart.getNSeries().get(i);
Marker marker = series.getMarker();
marker.setMarkerStyle(ChartMarkerType.AUTOMATIC);
marker.getArea().getFillFormat().setType(FillType.SOLID);
marker.getArea().getFillFormat().getSolidFill().setColor(Color.getRed());
}
book.save(dir + "output.xlsx");

Thanks, that works for all the points in the series. Is there a way to make it so that different points in the series has different colors.... so let's say one point might be colored green while another is colored red.

Hi,


Sure, you can do it. See the sample updated code below for your reference:
e.g
Sample code:

Workbook book = new Workbook(“f:\files\sample.xlsx”);
ChartCollection charts = book.getWorksheets().get(0).getCharts();
Chart chart = charts.get(0);
for(int i = 0; i < chart.getNSeries().getCount(); i++)
{
Series series = chart.getNSeries().get(i);
for (int j = 0; j <series.getPoints().getCount(); j++)
{
ChartPoint p = series.getPoints().get(j);

Marker marker = p.getMarker();
marker.setMarkerStyle(ChartMarkerType.AUTOMATIC);
marker.getArea().getFillFormat().setType(FillType.SOLID);
if (j % 2 == 0)
{
marker.getArea().getFillFormat().getSolidFill().setColor(Color.getRed());
p.getBorder().setColor(Color.getGreen());

}
else
{

marker.getArea().getFillFormat().getSolidFill().setColor(Color.getGreen());
p.getBorder().setColor(Color.getRed());

}
}
}
book.save(“f:\files\out1.xlsx”);

Hope, this helps a bit.

Thank you.

Thanks, that helps!

Hi,

It is good to know that you are up & running. Please feel free to contact us back in case you need our further assistance with Aspose APIs.