1

I am new to jfreechart. I have code like http://www.java2s.com/Code/Java/Chart/JFreeChartXYLogAxesDemo.htm that plots 4 functions in a chart.

However, the x values for each xyseries are all the same. This seems redundant.

Is there a way to eliminate this duplicate data?

I would prefer to generate one x series and many y series, and then add these to some kind of series collection.

edit: implementing the answer was pretty easy (please see below).

not sure what to do with getSeriesKey() though?

import java.util.*;
import org.jfree.chart.*;
import org.jfree.chart.plot.PlotOrientation;
import org.jfree.data.xy.AbstractXYDataset;
import org.jfree.ui.ApplicationFrame;
@SuppressWarnings("serial") class XYDatasetWithCommonX extends AbstractXYDataset {
    @SuppressWarnings("unchecked") XYDatasetWithCommonX(int n) {
        this.n=n;
        y=new ArrayList[n];
        for(int i=0;i<n;i++)
            y[i]=new ArrayList<Number>();
    }
    public void add(double x,double[] y) {
        this.x.add(x);
        for(int i=0;i<n;i++)
            this.y[i].add(y[i]);
    }
    @Override public int getSeriesCount() {
        return n;
    }
    @Override public Comparable getSeriesKey(int series) {
        return "Unit";
    }
    @Override public int getItemCount(int series) {
        return y[series].size();
    }
    @Override public Number getX(int series,int item) {
        return x.get(item);
    }
    @Override public Number getY(int series,int item) {
        return y[series].get(item);
    }
    final int n;
    final List<Number> x=new ArrayList<Number>();
    final List<Number>[] y;
}
@SuppressWarnings("serial") public class Main extends ApplicationFrame {
    Main() {
        super("demo");
    }
    public static void main(String[] args) {
        XYDatasetWithCommonX x=new XYDatasetWithCommonX(3);
        double[] y=new double[3];
        for(int i=1;i<=50;i++) {
            y[0]=10*Math.exp(i/5.0);
            y[1]=20*Math.exp(i/5.0);
            y[2]=30*Math.exp(i/5.0);
            x.add(i,y);
        }
        Main main=new Main();
        final JFreeChart chart=ChartFactory.createXYLineChart("my demo","x","y",x,PlotOrientation.VERTICAL,true,true,false);
        final ChartPanel chartPanel=new ChartPanel(chart);
        chartPanel.setPreferredSize(new java.awt.Dimension(500,270));
        main.setContentPane(chartPanel);
        main.pack();
        main.setVisible(true);
    }
}
Ray Tayek
  • 9,841
  • 8
  • 50
  • 90
  • 2
    The example you linked plots **3** series, and I do not see the redundant values anywhere. Could you elaborate a little more? – SJuan76 Feb 01 '13 at 21:49
  • the xvalues of each series are duplicate sequences. i.e. the i's in each of the three statements like s1.add(i, 10 * Math.exp(i / 5.0)); are the same – Ray Tayek Feb 01 '13 at 22:15

1 Answers1

1

This seems redundant.

Yes, the chosen implementation of XYDataset, XYSeriesCollection, is just one of many concrete implementations. For a large number of identical domain values, consider a custom XYDataset that contains a single List<Number> of domain values shared by all series. The approach is illustrated here for a single series.

Community
  • 1
  • 1
trashgod
  • 203,806
  • 29
  • 246
  • 1,045