dataset with multiple series color

A discussion forum for JFreeChart (a 2D chart library for the Java platform).
Locked
hcardoso
Posts: 6
Joined: Wed Nov 13, 2013 5:00 pm
antibot: No, of course not.

dataset with multiple series color

Post by hcardoso » Wed Nov 13, 2013 5:09 pm

Hi!

This is my first topic here so try and go easy on me.

My problem is that i have a XYSeriesCollection called dataset which has multiple XYSeries series.

I have a XYAreaChart and i want every serie displayed on the chart to have a different color. I tried a number of things to do this but it keeps drawing only one color and one serie that i assume is the first.

Code: Select all

		for(int i = 0; i<dataset.getSeriesCount();i++){
			XYAreaRenderer r = new XYAreaRenderer();
			r.setSeriesFillPaint(i, java.awt.Color.getHSBColor(j, 0.5f, 0.5f));
			chart.getXYPlot().setRenderer(r);
			chart.getXYPlot().getDomainAxis().setRange(0, 360);
			chart.getXYPlot().setDataset( dataset );
		}
This is my first day messing with JFreeChart but i have experience in java.


Any help will be much appreciated

medloh
Posts: 16
Joined: Tue May 07, 2013 11:26 pm
antibot: No, of course not.

Re: dataset with multiple series color

Post by medloh » Wed Nov 13, 2013 5:34 pm

Here's the code we use for having a different color for each Y axis. Our use case has multiple Y axes, and only one X axis.

Toward the bottom is an example of setting different colors in same TimeSeriesCollection.

Code: Select all

		Color colorData = ...
		TimeSeriesCollection tsc = ... // each axis requires TimeSeriesCollection

		// setting axis colors
		NumberAxis axis = ...
		axis.setLabelPaint(colorAxis);
		axis.setTickLabelPaint(colorAxis);

		// setting line color
		XYLineAndShapeRenderer renderer = new XYLineAndShapeRenderer();
		renderer.setBaseShapesVisible(true);
		renderer.setBaseShapesFilled(true);
		renderer.setSeriesPaint(0, colorData);

		<snip/>

		// for setting color and stroke of other lines in TimeSeriesCollection
		renderer.setSeriesPaint(fcastIndex, new Color(200, 0, 200) );
		renderer.setSeriesStroke(
			    fcastIndex, 
			    new BasicStroke(
			        1.0f, BasicStroke.CAP_ROUND, BasicStroke.JOIN_ROUND,
			        1.0f, new float[] {6.0f, 6.0f}, 0.0f
			    ));
		}
		
		plot.setRenderer(axisIndex, renderer);


paradoxoff
Posts: 1634
Joined: Sat Feb 17, 2007 1:51 pm

Re: dataset with multiple series color

Post by paradoxoff » Thu Nov 14, 2013 11:03 pm

hcardoso wrote:

Code: Select all

		for(int i = 0; i<dataset.getSeriesCount();i++){
			XYAreaRenderer r = new XYAreaRenderer();
			r.setSeriesFillPaint(i, java.awt.Color.getHSBColor(j, 0.5f, 0.5f));
			chart.getXYPlot().setRenderer(r);
			chart.getXYPlot().getDomainAxis().setRange(0, 360);
			chart.getXYPlot().setDataset( dataset );
		}
This code snippet will do the following:
For each series in the dataset, you are creating a new renderer, assign that as renderer for the primary dataset, and then set the dataset as primary dataset for the plot.
What you probably want to do:
Create a dataset and a single renderer, and set those as primary dataset/renderer for the XYPlot. THe renderer should be automatically configured to show each of the series in a different color.

Locked