1

I have a function which produces a load of doubles up to the size of the array. What I am curious to know is how I can then plot these values using a XYLineChart? I am unsure of how to put these doubles into the correct format so that they can then be plotted. How do I change my double[] variables into a suitable format that the XYSeries will accept and then plot. My code listing is below.

public XYSeries inputOutputGraph() {
    XYSeries graph = new XYSeries();
    XYDataset xyDataset = new XYSeriesCollection(graph);
    graph.add(valuesToPlot.outputArray); //This line here is the issue
    JFreeChart chart = ChartFactory.createXYLineChart(
            " Array values", "Time", "values",
            xyDataset, PlotOrientation.VERTICAL, true, true, false);
    ChartFrame graphFrame = new ChartFrame("XYLine Chart", chart);
    graphFrame.setVisible(true);
    graphFrame.setSize(300, 300); 
    return graph;
}   

This is the error message I get:

no suitable method found for add(double[]) method org.jfree.data.xy.XYSeries.add(org.jfree.data.xy.XYDataItem,boolean) is not applicable

(actual and formal argument lists differ in length)

syb0rg
  • 8,057
  • 9
  • 41
  • 81
user2041029
  • 21
  • 1
  • 6

1 Answers1

2

Look at the docs for XYSeries. There is no add method for handling a double array of input data as your compilation error indicates.

As the name suggests XYSeries is comprised of a series of X & Y co-ordinates. Your double array would suggest that you only have one set of points, probably the Y coordinates.

If this is the case you could change your array from a 1D to a 2-dimensional array of size n X 2. Use the first and second columns of the array for the X & Y co-ordinates respectively. E.g.

double[][] xyValues = new double[100][2];

Then, to add, you could do:

for (int i = 0; i < xyValues.length; i++) {
   graph.add(xyValues[i][0], xyValues[i][0]);
}
Reimeus
  • 158,255
  • 15
  • 216
  • 276
  • To minimize copying, also consider creating you own implementation of `XYDataset`, as shown [here](http://stackoverflow.com/a/14459322/230513). – trashgod Feb 06 '13 at 02:54