使用JFreeChart创建正态分布图

时间:2013-06-22 19:55:42

标签: java graph jfreechart normal-distribution

我正在尝试使用JFreeChart库制作正态分布图。如果我试图在图表下面找到一个区域,我就会成功。但是我没有找到如何在图表下获得2个区域的方法。

以下是一方的代码:

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

我已经尝试用积极和消极的方式翻转价值观。但相反,图表的行变为绿色。

这就是我试过的

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

感谢您的回答。

1 个答案:

答案 0 :(得分: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);
}