本文介绍了用JFreeChart创建一个正态分布图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I am trying to make a normal distribution graph using the JFreeChart library. I am successful if I try to get one area under the graph. However I did not find a way on how get 2 areas under the graph.

这是一侧的代码:

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

这是上面代码的外观

这就是我需要的样子:

我已经尝试过用正负来翻转值.但是相反,该图的线变为绿色.

I already tried flipping the values with positive and negative. But instead the line of the graph turns green.

这是我尝试过的

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

谢谢您的回答.

推荐答案

您可以使用多个数据集.

You can use multiple datasets.

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

这篇关于用JFreeChart创建一个正态分布图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-28 22:39