2013-06-22 57 views
1

我試圖讓使用的JFreeChart庫正態分佈圖。如果我嘗試在圖表下方找到一個區域,我很成功。但是,我沒有找到如何讓圖下2個區域的方法。創建一個正常的分佈圖使用JFreeChart

下面是一面代碼:

public GrafHujungKanan() { 

     Function2D normal = new NormalDistributionFunction2D(0.0, 1.0); 
     dataset = DatasetUtilities.sampleFunction2D(normal, -4, 4, 100, 
       "Normal"); 

     XYSeries fLine = new XYSeries("fLine"); 
     fLine.add(nilaiKritikal, 0); 
     fLine.add(4, 0); 
     ((XYSeriesCollection) dataset).addSeries(fLine); 

     NumberAxis xAxis = new NumberAxis(null); 
     NumberAxis yAxis = new NumberAxis(null); 
     XYDifferenceRenderer renderer = new XYDifferenceRenderer(); 
     xAxis.setRange(0, 5); 
     plot = new XYPlot(dataset, xAxis, yAxis, renderer); 

     chart = new JFreeChart(plot); 
     chart.removeLegend(); 

     ChartPanel cp = new ChartPanel(chart); 
     this.add(cp); 
    } 

這裏是如何看起來與上面的代碼

Here is how it looks with the above code

這裏是我需要它來看看:

enter image description here

我ALRE ady嘗試用正面和負面翻轉價值觀。但是,圖表的線條變成了綠色。

這是我試過

public GrafDuaHujung() { 

    Function2D normal = new NormalDistributionFunction2D(0.0, 1.0); 
    dataset = DatasetUtilities.sampleFunction2D(normal, -4, 4, 100, 
      "Normal"); 

    // line on right side 
    XYSeries fLine = new XYSeries("fLine"); 
    fLine.add(2, 0); 
    fLine.add(4, 0); 
    ((XYSeriesCollection) dataset).addSeries(fLine); 

    // line on left side 
    XYSeries dLine = new XYSeries("dLine"); 
    dLine.add(-2, 0); 
    dLine.add(-4, 0); 
    ((XYSeriesCollection) dataset).addSeries(dLine); 

    NumberAxis xAxis = new NumberAxis(null); 
    NumberAxis yAxis = new NumberAxis(null); 
    XYDifferenceRenderer renderer = new XYDifferenceRenderer(); 
    xAxis.setRange(0, 5); 
    plot = new XYPlot(dataset, xAxis, yAxis, renderer); 

    chart = new JFreeChart(plot); 
    chart.removeLegend(); 

    ChartPanel cp = new ChartPanel(chart); 
    this.add(cp); 
} 

謝謝您的回答。

+0

交叉貼[這裏](http://www.jfree.org/forum/viewtopic.php?f=3&t=116545)。 – trashgod

+0

檢查類似的問題[here](http://www.jfree.org/forum/viewtopic.php?f=3&t=117702)。 – trashgod

回答

0

您可以使用多個數據集。

public GrafDuaHujung() { 

    Function2D normal = new NormalDistributionFunction2D(0.0, 1.0); 
    dataset = DatasetUtilities.sampleFunction2D(normal, -4, 4, 100, "Normal"); 
    dataset2 = DatasetUtilities.sampleFunction2D(normal, -4, 4, 100, "Normal"); //New 

    // line on right side 
    XYSeries fLine = new XYSeries("fLine"); 
    fLine.add(2, 0); 
    fLine.add(4, 0); 
    ((XYSeriesCollection) dataset).addSeries(fLine); 

    // line on left side 
    XYSeries dLine = new XYSeries("dLine"); 
    dLine.add(-2, 0); 
    dLine.add(-4, 0); 
    ((XYSeriesCollection) dataset2).addSeries(dLine); //Changed 

    XYDifferenceRenderer renderer = new XYDifferenceRenderer(); 
    plot = new XYPlot(); //New 
    plot.setDataset(0, dataset); //New 
    plot.setDataset(1, dataset2); //New 
    plot.setRenderer(renderer); //New 

    chart = new JFreeChart(plot); 
    chart.removeLegend(); 

    ChartPanel cp = new ChartPanel(chart); 
    this.add(cp); 
}