Plotting problem: different variable range

A discussion forum for JFreeChart (a 2D chart library for the Java platform).
Locked
YJ

Plotting problem: different variable range

Post by YJ » Thu May 23, 2002 6:23 pm

Hi, I would like to know if JFreeChart can handle different ranges of X variables in a plot. I need to plot (scatter plot) two variables whose x range are different from each other.

Both variables come from a scientific calculation and the x axis is a time. But the problem is that one of the variable stop evolving earlier than the other variable. As a result, I got the following two sets to create a plot,
{(x1, t1), (x2, t2), ..., (xm, tm)} for X variable and
{(v1, t1), (v2, t2), ..., (vm, tm), ..., (vn, tn)} for V variable.

Can JFreeChart create one scatter plot with these two sets of data?

Thanks in advance and I'm looking forward to your response.

YJ

David Gilbert

Re: Plotting problem: different variable range

Post by David Gilbert » Fri May 24, 2002 7:12 am

Yes, you can. Try creating your dataset like this:

XYSeries series1 = new XYSeries("First");
series1.add(1.0, 1.0);
series1.add(2.0, 4.0);
series1.add(3.0, 3.0);
series1.add(4.0, 5.0);
series1.add(5.0, 5.0);
series1.add(6.0, 7.0);
series1.add(7.0, 7.0);
series1.add(8.0, 8.0);

XYSeries series2 = new XYSeries("Second");
series3.add(3.0, 4.0);
series3.add(4.0, 3.0);
series3.add(5.0, 2.0);
series3.add(6.0, 3.0);
series3.add(7.0, 6.0);
series3.add(8.0, 3.0);
series3.add(9.0, 4.0);
series3.add(10.0, 3.0);

XYSeriesCollection dataset = new XYSeriesCollection();
dataset.addSeries(series1);
dataset.addSeries(series2);

The x-values don't even need to be regularly spaced.

Regards,

DG.

Young-Jin Lee

Re: Plotting problem: different variable range

Post by Young-Jin Lee » Fri May 24, 2002 8:02 pm

I did what you suggested, but I got the exception.
com.jrefinery.data.SeriesException: XYSeries.add(...): x-value already exists.
at com.jrefinery.data.XYSeries.add(Unknown Source)
at com.jrefinery.data.XYSeries.add(Unknown Source)
at main.MultiFreeFall.createTimeForceData(MultiFreeFall.java:375) at main.MultiFreeFall.createPlotData(MultiFreeFall.java:414)
at main.PlotFrame.<init>(PlotFrame.java:55)
at main.MultiFreeFall.createPlotFrame(MultiFreeFall.java:557)

Here is what I've tried.

try {
XYSeries series1 = new XYSeries("Gravitational Acceleration 1: " + g1);
for(int i = 0; i < times1.size(); i++) {
series1.add((Double) times1.get(i),
(Double) yAcceleration1.get(i));
}

XYSeries series2 = new XYSeries("Gravitational Acceleration 2: " + g2);
for(int i = 0; i < times2.size(); i++) {
series2.add((Double) times2.get(i),
(Double) yAcceleration2.get(i));
}

plotData = new XYSeriesCollection();
((XYSeriesCollection) plotData).addSeries(series1);
((XYSeriesCollection) plotData).addSeries(series2);
} catch(SeriesException se) {
se.printStackTrace();
}

David Gilbert

Re: Plotting problem: different variable range

Post by David Gilbert » Sat May 25, 2002 7:22 am

One feature of XYSeries is that is doesn't allow duplicate x-values.

In general though, an XYDataset implementation can return duplicate x-values, generating a scatter plot is one example of when this might be required.

You might need to write your own class that implements XYDataset (or make do with the ugly DefaultXYDataset class).

Regards,

DG.

YJ

Re: Plotting problem: different variable range

Post by YJ » Sun May 26, 2002 11:13 pm

In your first answer, you told me to use XYSeries, not XYDataset. But it seems like your first answer was not correct.

In summary, I need to use XYDataset to generate a xy-plot with a duplicated x-values. Is it correct?

Thanks in advance.

YJ

David Gilbert

Re: Plotting problem: different variable range

Post by David Gilbert » Mon May 27, 2002 7:17 am

YJ wrote:
> In your first answer, you told me to use XYSeries, not
> XYDataset. But it seems like your first answer was not correct.

You've discovered that I make mistakes...and I thought I was going to keep it a secret.

> In summary, I need to use XYDataset to generate a xy-plot
> with a duplicated x-values. Is it correct?

XYDataset is the INTERFACE that XYPlot uses to obtain data for an xy-plot. The interface allows for duplicated x-values and different ranges between series, because both the x and y values are accessed by an index.

There are two classes that implement the XYDataset interface: DefaultXYDataset and XYSeriesCollection. You can also write your own class that implements XYDataset...then you have complete control over the way you store your data.

The XYSeriesCollection class is easier to use than DefaultXYDataset, but because of the way XYSeries is implemented it doesn't allow for duplicated x values.

Regards,

DG.

Locked