Creating a normal distribution graph with JFreeChart

A discussion forum for JFreeChart (a 2D chart library for the Java platform).
Locked
akemalfirdaus
Posts: 1
Joined: Sun Jun 23, 2013 9:27 pm
antibot: No, of course not.

Creating a normal distribution graph with JFreeChart

Post by akemalfirdaus » Sun Jun 23, 2013 9:31 pm

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 :

Code: Select all

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

Image

And here is how I need it to look :

Image

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

This is what I tried

Code: Select all

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 your your response :)

John Matthews
Posts: 513
Joined: Wed Sep 12, 2007 3:18 pm

Re: Creating a normal distribution graph with JFreeChart

Post by John Matthews » Mon Jun 24, 2013 12:38 pm

Cross-posted here.

Locked