0

I have my first class

public class GetResults{

public double[] tableOfresults() {
double[] outputArray = new double[100];
double time;
double results

for(time = 0; time<outputArray.length; i++) {
  //Some values are calculated and then stored in an array as below
  outputArray[(int)Time] = Results
}
return outputarray;

This class just calculates some values I want to plot on a graph and stores them in the array.

This next class is what actually plots the points on my graph. I am unsure of a simple way to get points to plot on my X axis which are the Values of time(Time being the array index 0,1,2,3 ect) and my Y axis which are my Results. I am currently having to put all the positions in myself.

public class graph{
GetResults gr = new GetResuts();
public XYSeries inputOutputGraph() {
    XYSeries graph = new XYSeries("My graph");      
    XYDataset xyDataset = new XYSeriesCollection(graph);

    graph.add(1, gr.tableOfResults()[1]); 
    graph.add(2, gr.tableOfResults()[2]);
    graph.add(3, gr.tableOfResults()[3]);
    graph.add(4, gr.tableOfResults()[4]);

Here I am having to add the values myself(I have 1000 to do). I need something that looks like this graph.add(gr.tableOfResults()[gr.Time], gr.tableOfResults()[gr.Results]); So that as my time goes up 0,1,2,3 it will plot my Results which is stored at index 0,1,2,3 at that position. How could I do this?? I have tried that code^^ and I got an array index out of bounds with the vaue my array size was set to

    JFreeChart chart = ChartFactory.createXYLineChart(
        "Graph", "Time", "results",
        xyDataset, PlotOrientation.VERTICAL, true, true, false);
    ChartFrame graphFrame = new ChartFrame("XYLine Chart", chart);
    graphFrame.setVisible(true);
    graphFrame.setSize(300, 300);
    return graph;
}

}

user2041029
  • 21
  • 1
  • 6

1 Answers1

0

You could just put your calculated values directly in an XYSeries and then let GetResults have a method that returns that series.

class GetResults {

    public XYSeries getSeries() {
        XYSeries series = new XYSeries("Series");
        for (int i = 0; i < 10; i++) {
            series.add(i, Math.pow(2, i));
        }
        return series;
    }
}

Here's how you would use getSeries() in this example.

dataset.addSeries(new GetResults().getSeries());

IMAGE

Community
  • 1
  • 1
trashgod
  • 203,806
  • 29
  • 246
  • 1,045
  • Thanks this has helped with sorting out my x axis problem.. However how do I now read in my Y values which are stored inside my array at positions [1][2][3] ect. So inside that loop it should have something that says as i goes up by 1 then the next value in the array will be plotted at that point. Thanks – user2041029 Feb 11 '13 at 17:39
  • Add a `getCollection()` method that returns a `XYSeriesCollection` containing all the individual `XYSeries`; since there's more than one, you could add an index parameter, e.g. `getSeries(int i)`, that calls `XYSeriesCollection#getSeries()`. – trashgod Feb 11 '13 at 19:48