如何使折线图从零开始并在最后一个范围JFreeChart结束

时间:2016-07-24 15:53:52

标签: java jfreechart linegraph

我已经制作了这样的折线图。即使蓝线没有零数据,红线也只有100的值。所以我如何设置蓝线从零开始,红色完成直到200,如下所示:

enter image description here

我在textfile(outputAvgGen.txt)中的数据是:

11752.58,HC  
11819.65,HC  
11941.75,HC  
12398.45,HC  
12401.06,HC  
12531.09,HC  
12634.33,HC  
12748.83,HC  
12787.36,HC  
12799.44,HC 
   .  
   .  
   .   
30137.15,P3  
31919.46,P3  
32323.8,P3    
   .  
   .  
   .  
and so on until 200 data  

这是我的代码:

public class Graph2 extends JFrame{
public Graph2(){
    setTitle("P3 Performance Analysis");
    JPanel chartPanel = createChartPanel();
    add(chartPanel, BorderLayout.CENTER);

    setSize(640, 480);
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    setLocationRelativeTo(null);
}
public static void main(String[] args) {
    SwingUtilities.invokeLater(new Runnable() {
        @Override
        public void run() {
            new Graph2().setVisible(true);
        }
    });
}
private JPanel createChartPanel() {
    // creates a line chart object
    // returns the chart panel
    String chartTitle = "Average Fitness against No. of Generations";
    String xAxisLabel = "No. of Generations";
    String yAxisLabel = "Average Fitness";

    XYDataset dataset = createDataset();

    JFreeChart chart = ChartFactory.createXYLineChart(chartTitle,
            xAxisLabel, yAxisLabel, dataset);

    XYPlot plot = chart.getXYPlot();
    NumberAxis yAxis = (NumberAxis) plot.getRangeAxis();
    yAxis.setAutoRangeIncludesZero(true);

    return new ChartPanel(chart);
}

private XYDataset createDataset() {
    ArrayList<PlotData2> dataList = readData();

    XYSeriesCollection dataset = new XYSeriesCollection();
    XYSeries series1 = new XYSeries("Hill Climber");
    XYSeries series2 = new XYSeries("P3");

    for (int i = 0; i < dataList.size(); i++){

        if (dataList.get(i).getAlgo().equals("HC")){
            series1.add(i, dataList.get(i).getAvg());
            System.out.println("HC i=" + i);
        }
        else if (dataList.get(i).getAlgo().equals("P3")){
            series2.add(i, dataList.get(i).getAvg());
            System.out.println("P3 i=" + i);
        }

    }

    /*
    for (int i = 0; i < dataList.size(); i++){
        if (dataList.get(i).getAlgo().equals("HC")){
            System.out.println("i = " + i);
            series1.add(i, dataList.get(i).getAvg());
        }

    }

    for (int j = 0; j < dataList.size(); j++){
        if (dataList.get(j).getAlgo().equals("P3")){
            System.out.println("j = " + j);
            series2.add(j, dataList.get(j).getAvg());
        }
    }*/


    /*
    series1.add(1.0, 2.0);
    series1.add(2.0, 3.0);
    series1.add(3.0, 2.5);
    series1.add(3.5, 2.8);
    series1.add(4.2, 6.0);

    series2.add(2.0, 1.0);
    series2.add(2.5, 2.4);
    series2.add(3.2, 1.2);
    series2.add(3.9, 2.8);
    series2.add(4.6, 3.0); */

    dataset.addSeries(series1);
    dataset.addSeries(series2);

    return dataset;

}

public static ArrayList<PlotData2> readData(){
    ArrayList<PlotData2> dataList = new ArrayList<PlotData2>();

    try {
        BufferedReader in = new BufferedReader( new FileReader("outputAvgGen.txt") );

        String str;
        while ( (str = in.readLine() )!= null ) {
            String[] data = str.split( "," );
            double avg = Double.parseDouble(data[0]);
            String algo = data[1];

           // int gen = Integer.parseInt(data[2]);

            dataList.add(new PlotData2(algo,avg));
        }
        in.close();
    } catch ( IOException ex ) {
        System.err.println( "Error: Can't open the file for reading." );
    }


    return dataList;
  }

}  

实际上我计算的原始数据是在文本文件中成对的HC和P3,如下所示:

15426.35,HC  
38903.93,P3
13777.49,HC
34480.21,P3
15199.38,HC
38559.36,P3
13399.15,HC
32931.49,P3  
     .  
     .  
   and so on..  

但是我对它进行了排序以使图形以递增方式绘制,但在排序时它不会根据对进行绘图。

有人可以帮助代码,至少让它从0开始,并以两条线的最大范围结束,使其同时显示。谢谢。

1 个答案:

答案 0 :(得分:0)

您的代码的主要问题是您同时递增两个系列的x值。你需要单独做。此外,您在createDataset方法中使用硬编码字符串。 请尝试这种方法:

public static HashMap<String, List<Double>> readData() {
    HashMap<String, List<Double>> dataList = new HashMap<String, List<Double>>();
    try {
        BufferedReader in = new BufferedReader(new FileReader("outputAvgGen.txt"));
        String str;
        while ((str = in.readLine()) != null) {
            String[] data = str.split(",");
            String algo = data[1];
            if (dataList.get(algo) == null) {
                dataList.put(algo, new ArrayList<Double>());
            }
            dataList.get(algo).add(Double.parseDouble(data[0]));
        }
        in.close();
    }
    catch (IOException ex) {
        System.err.println("Error: Can't open the file for reading.");
    }
    return dataList
}
private XYDataset createDataset() {
    HashMap<String, List<Double>> dataList = readData();
    XYSeriesCollection dataset = new XYSeriesCollection();
    Set<String> keys = dataList.keySet();
    for (String key : keys) {
        XYSeries series = new XYSeries(key);
        List<Double> numbers = dataList.get(key);
        for (int i = 0; i < numbers.size() - 1; i++) {
            series.add(i, numbers.get(i), false);
        }
        series.add(numbers.size() - 1, numbers.get(numbers.size() - 1),  true);
        dataset.addSeries(series);
    }
    return dataset;
}