Changing color.

A discussion forum for JFreeChart (a 2D chart library for the Java platform).
Locked
anupamsr
Posts: 1
Joined: Wed May 31, 2006 4:44 pm
Location: Stuttgart
Contact:

Changing color.

Post by anupamsr » Tue Aug 08, 2006 8:00 am

Hi all!
I want to know if changing color of a series is possible right when it is created. I have to write a program in which user can select from a list of variables, and those variables will be plotted. I want to assign a specific color to such variables.

The program creates all the requested series and then adds them to the chart. The color is changed by making the list of variables sorted and keeping a track of which item number corresponds to which series so that corresponding color can be added:

Code: Select all

xyseries[i].add(xData[i]), yData[i]);
xyseriescollection.addSeries(xyseries[i]);
JFreeChart chart = ChartFactory.createXYLineChart(null,	"", ytitle, xyseriescollection, org.jfree.chart.plot.PlotOrientation.VERTICAL, true, false, false);
XYPlot xyplot = chart.getXYPlot();
XYLineAndShapeRenderer rr = (XYLineAndShapeRenderer)xyplot.getRenderer();
rr.setSeriesPaint(i, green, true);
But now I have to modify the program so that variables will appear in random order. So I HAVE to assign the color to the series prior to adding it into xyseriescollection.

Any idea how to do that?

SeanMcCrohan
Posts: 18
Joined: Thu May 11, 2006 11:22 pm

Post by SeanMcCrohan » Tue Aug 08, 2006 5:49 pm

The user is selecting the variables from a list, right? So this doesn't sound like a problem. You have a list of available variables (indexed). You have a list of colors (using the same index). There's nothing that says that index has to match the index of the series in the renderer. Just map the two - variable index X is rendered as series index Y - and set the appropriate color in the renderer when you add the series. Very very roughly:

Code: Select all

public void addSeries(seriesIndex i)
{
  xyseriescollection.addSeries(xyseries[i]);
  XYPlot xyplot = chart.getXYPlot();
  XYLineAndShapeRenderer rr = (XYLineAndShapeRenderer)xyplot.getRenderer();
  rr.setSeriesPaint(xyplot.getSeriesCount()-1, seriesColors[i], true);
}
Alternately, add all of the series to the chart at the beginning, and just toggle their visibility when the user makes their selections.

Locked