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)

1 Answer 1

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]);
}
Sign up to request clarification or add additional context in comments.

1 Comment

To minimize copying, also consider creating you own implementation of XYDataset, as shown here.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.