How to Mix ScatterPlot and Lines

A discussion forum for JFreeChart (a 2D chart library for the Java platform).
Locked
HSG
Posts: 14
Joined: Wed May 06, 2009 10:35 am

How to Mix ScatterPlot and Lines

Post by HSG » Wed May 06, 2009 10:39 am

Hi,

I'm a french student, and I develop an application using JFreeChart.
I have to make a chart with plots and lines, how I can do that ?

Sorry for my english :(

Regards,

Gaël

david.gilbert
JFreeChart Project Leader
Posts: 11734
Joined: Fri Mar 14, 2003 10:29 am
antibot: No, of course not.
Contact:

Re: How to Mix ScatterPlot and Lines

Post by david.gilbert » Thu May 07, 2009 7:43 am

Your english is fine! In JFreeChart, you can overlay multiple datasets on the same plot. First, call setDataset(int, dataset), then call setRenderer(int, renderer) to add an appropriate renderer for the dataset. You'll find these methods in the CategoryPlot and XYPlot classes (with slightly different parameter types reflecting the type of data that each plot supports).
David Gilbert
JFreeChart Project Leader

:idea: Read my blog
:idea: Support JFree via the Github sponsorship program

HSG
Posts: 14
Joined: Wed May 06, 2009 10:35 am

Re: How to Mix ScatterPlot and Lines

Post by HSG » Thu May 07, 2009 9:50 am

Ok I understand that, but I use which plot ?
But a chart could have many plots ??
I use which plot ?

MitchBuell
Posts: 47
Joined: Thu Dec 11, 2008 7:59 pm

Re: How to Mix ScatterPlot and Lines

Post by MitchBuell » Fri May 08, 2009 8:18 pm

Here's my test code I used to figure this out myself. This example will create two separate charts that are overlaid on top of each other. It's like making two separate JFreeCharts then stacking them together.

Code: Select all

// Create a single plot containing both the scatter and line
XYPlot plot = new XYPlot();

/* SETUP SCATTER */

// Create the scatter data, renderer, and axis
XYDataset collection1 = getScatterPlotData();
XYItemRenderer renderer1 = new XYLineAndShapeRenderer(false, true);	// Shapes only
ValueAxis domain1 = new NumberAxis("Domain1");
ValueAxis range1 = new NumberAxis("Range1");

// Set the scatter data, renderer, and axis into plot
plot.setDataset(0, collection1);
plot.setRenderer(0, renderer1);
plot.setDomainAxis(0, domain1);
plot.setRangeAxis(0, range1);

// Map the scatter to the first Domain and first Range
plot.mapDatasetToDomainAxis(0, 0);
plot.mapDatasetToRangeAxis(0, 0);

/* SETUP LINE */

// Create the line data, renderer, and axis
XYDataset collection2 = getLinePlotData();
XYItemRenderer renderer2 = new XYLineAndShapeRenderer(true, false);	// Lines only
ValueAxis domain2 = new NumberAxis("Domain2");
ValueAxis range2 = new NumberAxis("Range2");

// Set the line data, renderer, and axis into plot
plot.setDataset(1, collection2);
plot.setRenderer(1, renderer2);
plot.setDomainAxis(1, domain2);
plot.setRangeAxis(1, range2);

// Map the line to the second Domain and second Range
plot.mapDatasetToDomainAxis(1, 1);
plot.mapDatasetToRangeAxis(1, 1);

// Create the chart with the plot and a legend
JFreeChart chart = new JFreeChart("Multi Dataset Chart", JFreeChart.DEFAULT_TITLE_FONT, plot, true);
From here you can tweak the plot.mapDatasetToXXX methods to your liking. If you want both the scatter and line graphs to share the same Domain (example: share the same time axis) then just change this:

Code: Select all

// Map the line to the FIRST Domain and second Range
plot.mapDatasetToDomainAxis(1, 0);
plot.mapDatasetToRangeAxis(1, 1);
Now both the scatter and line graphs share the same Domain axis. You can also share the same Range, or share both. Just mix and match until you have what you want.

HSG
Posts: 14
Joined: Wed May 06, 2009 10:35 am

Re: How to Mix ScatterPlot and Lines

Post by HSG » Mon May 11, 2009 3:53 pm

Thanks a lot,

I'm using XYDotsRenderer and XYSpline and it works.

Perfect :)

ninetty
Posts: 4
Joined: Sun Mar 11, 2012 5:53 pm
antibot: No, of course not.

Re: How to Mix ScatterPlot and Lines

Post by ninetty » Wed Apr 11, 2012 11:50 pm

there is some error if i use your source code, there is no getScatterPlotData() and getLineChart in my netbeans..
i using jfreechart-1.0.14 and jcommon-1.0.16
can you explain to me?

is it possible if i want to mix polar chart and lines using jfreechart?
thank you very much..

*i'm an indonesian, sorry maybe i can't write english well...
:)

Gibbster
Posts: 1
Joined: Thu Sep 27, 2012 2:06 pm
antibot: No, of course not.

Re: How to Mix ScatterPlot and Lines

Post by Gibbster » Thu Sep 27, 2012 2:12 pm

Hello everyone: I have a question related to the topic.

I intend to draw X bar charts overlayed.
I can plot them bto video but the colors of the bars in each chart component are not displayed correctly. Simply they aren't the colors i've selected.
Before posting the code I would be grateful to those who suggest vaguely what can go wrong even according to their own experiences.

Thank you very much

Gib.

jahjeremy
Posts: 31
Joined: Tue Sep 25, 2012 2:49 am
antibot: No, of course not.

Re: How to Mix ScatterPlot and Lines

Post by jahjeremy » Wed Oct 03, 2012 8:07 pm

Hi, Gibbster.

Is this thread at all relevant? (started by myself)

viewtopic.php?f=3&t=115807

I found that setting the "base" methods on the bar renderers does NOT work as I would expect.

For instance, these methods don't work as I would expect:

Code: Select all

renderer.setBasePaint(Color.blue);
renderer.setBaseFillPaint(Color.blue);
Instead, you have to call methods like renderer.setSeriesPaint(0, Color.blue) for each of your series in your collection. Then your colors should be correct.

Does that help?

I'm not sure your reply belongs on this thread but just thought I'd try to help. :mrgreen:

sanchit
Posts: 1
Joined: Tue Nov 22, 2016 3:34 pm
antibot: No, of course not.

Re: How to Mix ScatterPlot and Lines

Post by sanchit » Tue Nov 22, 2016 3:43 pm

Hello All,

I am doing a project in which I have to build a Jfreechart application.
I want a combo box with two types of chart selection: x-y plot and scatter plot.
Both will have same dataset but will not overlay each other.


Any help would be really helpful.

Regards
Sanchit

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

Re: How to Mix ScatterPlot and Lines

Post by paradoxoff » Thu Nov 24, 2016 11:12 am

Do you want to switch from one chart to the next, or do you want to display the two charts simultaneously?
Technically, a scatter plot is an XYPlot. The only "special feature" of a scatter plot is that an XYItemRenderer is used that only draws shapes at the coordinates of the x/y data values.

Locked