1

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.

Here is the code for one side :

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

Here is how it looks with the above code

And here is how I need it to look :

enter image description here

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

This is what I tried

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

Thank you for your answer.

akemalFirdaus
  • 606
  • 1
  • 7
  • 15

1 Answers1

0

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);
}
Jhnen
  • 99
  • 1
  • 5