2013-02-11 55 views
0

我有我的第一類在另一個類返回填充值的數組繪製

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; 

在圖上我如何繪製點從功能該類只是計算一些值我想在圖形繪製並將它們存儲在數組中。

這下一個班級是實際繪製我的圖上的點。我不確定一個簡單的方法來獲取點在我的X軸上繪製的時間值(時間是數組索引0,1,2,3 ect)和我的Y軸,這些是我的結果。我目前不得不把自己的所有職位。

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]); 

在這裏,我不得不自己添加值(我有1000做)。我需要的東西看起來像這樣graph.add(gr.tableOfResults()[gr.Time],gr.tableOfResults()[gr.Results]); 因此,當我的時間上升到0,1,2,3時,它會繪製我的結果,該結果存儲在該位置的索引0,1,2,3處。我怎麼能這樣做?我曾嘗試代碼^^,我得到了一個數組索引越界與我的數組大小設置爲

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; 
} 

}的vaue

回答

0

你可以只把你的計算值直接在XYSeries,然後讓GetResults有一個返回該系列的方法。

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; 
    } 
} 

這裏是你將如何在這個example使用getSeries()

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

IMAGE

+0

由於這有助於與整理我的x軸的問題。但是現在如何在其中的位置處存儲在我的數組內我的Y值讀[1] [2] [3 ]等。所以在這個循環中,它應該有一些說,當我上升1,然後數組中的下一個值將繪製在那一點。謝謝 – user2041029 2013-02-11 17:39:38

+0

添加一個'getCollection()'方法,該方法返回包含所有單個'XYSeries'的'XYSeriesCollection';因爲有不止一個,你可以添加一個索引參數,例如'getSeries(int i)',它調用'XYSeriesCollection#getSeries()'。 – trashgod 2013-02-11 19:48:15