How to reset the rendered colors used in JFreeChart
I'm using a XYPlot to show a line graph with 1 or more lines in it. I have it combined with a listener pattern so it can receive updates: if so, it removes the lines using a dataset.removeAllSeries()
(dataset is of type XYSeriesCollection
)开发者_JS百科 call, and then adds the new lines using dataset.addSeries(...)
However, a new graph with new lines will have different colors!
So probably the Color renderer continues to distribute new colors.
Is there a possibility to reset the renderer, so it starts out with the first color again?
You can use the setSeriesPaint()
method of the renderer to set a new color for each serie.
example:
renderer.setSeriesPaint(0, Color.red);
renderer.setSeriesPaint(1, Color.blue);
First parameter is the index for serie.
plot.getRenderer().setSeriesPaint(serieindex, null);
This is a substitute for renderer reset, which I discovered myself (which I'm proud of as a beginner :) ). I have many series in my chart, which have to be removed, then added again because of new calculations. I need 1 serie to be always black and other painted in any colors but black. Now, because I receive new series in potentially new order so I can't use:
plot.getRenderer().setSeriesPaint(0, new Color(0,0,0);
just like that because if I set up 1st serie as black in first calculations, every future first serie (in new calculations) will be always black, so I can get not only one particular serie in black (because of various order of adding series), which I dont want to. If I remove all series using:
dataset.removeAllSeries();
it DOESN'T reset fixed colors of series (if You are using setSeriesPaint(serieindex, new Color(RGB, RGB, RGB)) for, say, 3 series, You remove them and add again new series, new series will get their colors fixed by You before for other series with the same indexes). So I was thinking about resetting renderer, I found nothing in internet, but I have tried "null" as a color. With fixed "null" as a color serie's line is barely visible (I dont remember right now how I checked this), but it is not a color for renderer, so it automatically takes a new color from its list (which You can override BTW). So now I can set up a black color for one specific serie. I take its random index using:
int serieindex = dataset.indexOf(seriename);
because I know its name (key). I'm setting null "color" for any other series, so they take a first free color from the renderer list. I guess You can use this for keeping the same new serie colors too, however its of course better to set up fixed colors in the case simpliest than my.
In summary I found out a "color renderer reset" which delete a definition of color made once for a serie with specific number, so after deleting series any new serie with this number can get automatically new color from the list. I hope it will help someone :). It is my first answer so maybe its not the best possible.
精彩评论