1

I have an ArrayList that gets data from user input, and I have created a button which when activated launches an XY Line graph. The problem I have is I keep getting errors and my LineGraph wont create.

I don't really know how to get the data from my ArrayList and put it in an XYSeries.

Iterator it = dataXY.iterator();
    XYSeries p1 = new XYSeries("XY");
    while( it.hasNext()) {
        p1.add((XYDataItem) it.next());
    }


    XYSeriesCollection xydata = new XYSeriesCollection();
    xydata.addSeries(p1);

    JFreeChart lineChart = ChartFactory.createXYLineChart("XY chart", "X", "Y", xydata, PlotOrientation.HORIZONTAL, true, true, false);
    lineChart.setBackgroundPaint(Color.WHITE);

    final XYPlot plot = lineChart.getXYPlot();
    XYLineAndShapeRenderer renderer = new XYLineAndShapeRenderer();
    renderer.setSeriesPaint(0, Color.BLACK);
    renderer.setSeriesStroke( 0 , new BasicStroke( 4.0f ) );
    plot.setRenderer( renderer ); 
    plot.setBackgroundPaint(Color.WHITE);


    ChartFrame frame = new ChartFrame ("XY Line Graph", lineChart);
    frame.setVisible(true); 
    frame.setSize(700,500);

All this code is in the action event of my "create a graph" button. Any help on how to make this chart is greatly appreciated.

trashgod
  • 203,806
  • 29
  • 246
  • 1,045
Alex.11
  • 29
  • 1
  • 5
  • 3
    What's not working with your current approach? Please edit your question to include a [complete example](http://stackoverflow.com/help/mcve). – trashgod Aug 26 '15 at 21:32
  • my current approach is not working. A line graph is not being created but my arraylist is working. Ergo i think the problem is between the arraylist and making the dataset! – Alex.11 Aug 27 '15 at 08:40
  • The chartframe isnt even opening either – Alex.11 Aug 27 '15 at 08:44

1 Answers1

1

In the end I didnt even need to convert to an array although i managed to do that this way:

// converting arraylist to array
    double [][] p1 = new double[dataXY.size()][dataXY.size()];
   for (int i=0; i<dataXY.size(); i++) {
        int x = dataXY.get(i).getX(); int y = dataXY.get(i).getY();
         p1[i][0] = x;
         p1[i][1] = y;
    }

Anyway what i did was usean XYSeries and used a for loop to add all my values into the series like so:

XYSeriesCollection ds = new XYSeriesCollection();
    XYSeries s1 = new XYSeries("XY data");
    for (int i=0; i<dataXY.size(); i++) {
         int x = dataXY.get(i).getX(); int y = dataXY.get(i).getY();
         s1.add(y, x);
    }
    ds.addSeries(s1);
Alex.11
  • 29
  • 1
  • 5