Scatter plot with JFreeChart graph

A discussion forum for JFreeChart (a 2D chart library for the Java platform).
Locked
yannJFreeChart
Posts: 13
Joined: Tue May 15, 2012 2:17 pm
antibot: No, of course not.

Scatter plot with JFreeChart graph

Post by yannJFreeChart » Mon May 21, 2012 2:23 pm

Hello,

I would like to use JFreeChart in order to plot several series of points on the same graph.
Do you know how to do that ?

I did this code but just my first serie is a scatter plot and for the others series the point are linked and I don't want that.

I give you my code :

Code: Select all

XYSeries seriesGeo = new XYSeries("Geo1");
XYSeries seriesGeo2 = new XYSeries("Geo2");
XYSeries seriesGeo3 = new XYSeries("Geo3");

for (int i=minimumSize ; i<maximumSize ; i+=19)
		{
		double var1= Double.parseDouble(bufferLecture[i]);
		double var2= Double.parseDouble(bufferLecture[i+1]);
		
		double test = ( Double.parseDouble(bufferLecture[i-10]) + Double.parseDouble(bufferLecture[i-11])) /2;
		
		if (test == 0) seriesGeo.add(var1, var2);
		else if ( test > 0 && test <= 3) seriesGeo2.add(var1, var2);
		else seriesGeo3.add(var1, var2);
		} 

XYDataset xyDatasetGeo = new XYSeriesCollection(seriesGeo);
XYDataset xyDatasetGeo2 = new XYSeriesCollection(seriesGeo2);
XYDataset xyDatasetGeo3 = new XYSeriesCollection(seriesGeo3);

chartGeo = ChartFactory.createScatterPlot
		("XYLine Chart using JFreeChart", "Différentes mesures", "Débit (l/min)",
		xyDatasetGeo, PlotOrientation.VERTICAL, true, true, false);

XYPlot plot2=(XYPlot) chartGeo.getPlot();
plot2.setDataset(2, (XYDataset) xyDatasetGeo2);
XYItemRenderer render2= new XYLineAndShapeRenderer();
plot2.setRenderer(2,render2);

XYPlot plot3=(XYPlot) chartGeo.getPlot();
plot3.setDataset(3, (XYDataset) xyDatasetGeo3);
XYItemRenderer render3= new XYLineAndShapeRenderer();
plot3.setRenderer(3,render3);

frameGeo=new ChartFrame("XYLine Chart",chartGeo);
frameGeo.setSize(700,500);
frameGeo.setLocation(700,0);
frameGeo.setVisible(true);

Thank you.
Have a good day.

matinh
Posts: 483
Joined: Fri Aug 11, 2006 10:08 am
Location: Austria

Re: Scatter plot with JFreeChart graph

Post by matinh » Mon May 21, 2012 2:32 pm

You explicitly configure a new XYLineAndShapeRenderer for the second and third dataset. There is no need for this.
But if you want separate renderers, you should call setBaseLinesVisible(false) for those renderers.

hth,
- martin

yannJFreeChart
Posts: 13
Joined: Tue May 15, 2012 2:17 pm
antibot: No, of course not.

Re: Scatter plot with JFreeChart graph

Post by yannJFreeChart » Mon May 21, 2012 3:03 pm

Thak you for you reply.

You are right. But if I don't use XYLineAndShapeRenderer, all series have the same color, so I can't differentiate them.

I try to use :

Code: Select all

render2.setBaseLinesVisible(false);
but this doesn't work.

Do you know how can I do ?

yannJFreeChart
Posts: 13
Joined: Tue May 15, 2012 2:17 pm
antibot: No, of course not.

Re: Scatter plot with JFreeChart graph

Post by yannJFreeChart » Mon May 21, 2012 3:16 pm

Ok, I find that I wanted :

I just have to do :

Code: Select all

XYItemRenderer render2= new XYLineAndShapeRenderer(false,true);
Thank you for your help Martin.

steveb
Posts: 4
Joined: Wed Jun 13, 2012 1:30 pm
antibot: No, of course not.

Re: Scatter plot with JFreeChart graph

Post by steveb » Thu Dec 27, 2012 10:52 pm

If you were trying to get the different series to show up with different colors, another way would be to add each of the 3 series to your initial dataset, and then could skip the extra work of creating more datasets and renderers.

If your original code had tried adding a new dataset for each series, all with the same renderer, each would have been the 'first/base' series in the dataset and had the same color. By being different series within the same dataset, they will get different colors assigned.

Code: Select all

// XYDataset xyDatasetGeo = new XYSeriesCollection(seriesGeo);
// XYDataset xyDatasetGeo2 = new XYSeriesCollection(seriesGeo2);
// XYDataset xyDatasetGeo3 = new XYSeriesCollection(seriesGeo3);

XYSeriesCollection xyDatasetGeo = new XYSeriesCollection(seriesGeo);
xyDatasetGeo.addSeries(seriesGeo2);
xyDatasetGeo.addSeries(seriesGeo3);

Locked