I have a JFreeChart XYPlot with many series, the number of different series depends on input parameters. I wish to colour the lines in groups of two, so that series_0 and series_1 are the same colour then series_2 and series_3 are the same colour. I tried the following solution but it didn't work

XYPlot combinedPlot = createPlot(yaxis, new NumberAxis(xaxis), collection, logScale);

for (int j = 1; j < combinedPlot.getSeriesCount(); j++) { 
if (j % 2 == 1) combinedPlot.getRenderer().setSeriesPaint(j, combinedPlot.getRenderer().getSeriesPaint(j - 1)); 

I don't know why this doesn't work, if I replace combinedPlot.getRenderer().getSeriesPaint(j - 1) by Color.RED it correctly sets every second line to red. Would appreciate any help if you have come accross this before.

Recommended Answers

All 3 Replies

I tried your code with one of the demo plots and it worked just fine. Are you setting the series paint colors after you have added all of the series to your dataset?

Yes I am setting the colours after all the series are added to the dataset, all of the series are in 'collection' a XYSeriesCollection. Hence they are added to the plot in the command

XYPlot combinedPlot = createPlot(yaxis, new NumberAxis(xaxis), collection, logScale);

before I try to overright the default colours. But this is before I add combinedPlot to a ChartPanel, or a JFreeChart so maybe this is causing an issue?

It worked fine when I set the colors after creating the chart with JFreeChart chart = ChartFactory.createXYLineChart(... and then got the plot reference with XYPlot plot = chart.getXYPlot(); Give that a try and see if it helps.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, learning, and sharing knowledge.