I want to achieve basically this :

Code: Select all

```
public class JFreeChartPanel extends JPanel {
private final XYPlot plot;
double mean = 0.0, sd = 1.0;
XYDataset dataset = initDataset();
NumberAxis domain;
public JFreeChartPanel(){
JFreeChart chart = ChartFactory.createXYLineChart(
"Normal Distribution",
"X",
"PDF",
dataset,
PlotOrientation.VERTICAL,
false,
false,
false
);
plot=chart.getXYPlot();
domain = (NumberAxis) plot.getDomainAxis();
domain.setAutoRangeStickyZero(false); //Fixes the margin issue with 0
domain.setTickUnit(new NumberTickUnit(sd)); //Spacing on X-axis should be standard deviation + mean
XYSeriesCollection dataset1 = new XYSeriesCollection();
XYSeriesCollection dataset2 = new XYSeriesCollection();
XYSeries area1 = new XYSeries("area1");
area1.add(-2, 0);
area1.add(0, 0);
((XYSeriesCollection) dataset1).addSeries(area1);
XYSeries area2 = new XYSeries("area2");
area1.add(2, 0);
area1.add(4, 0);
dataset2.addSeries(area2);
XYDifferenceRenderer renderer1 = new XYDifferenceRenderer();
XYDifferenceRenderer renderer2 = new XYDifferenceRenderer();
plot.setDataset(1, dataset1);
plot.setRenderer(1, renderer1);
plot.setDataset(2, dataset2);
plot.setRenderer(2, renderer2);
plot.setDomainAxis(domain);
final ChartPanel chartPanel = new ChartPanel(chart);
setLayout(new BorderLayout());
add(chartPanel);
}
private XYDataset initDataset() {
double minX=mean-(4*sd),maxX=mean+(4*sd); //Minimum and Maximum values on X-axis (4 deviations)
Function2D normal = new NormalDistributionFunction2D(mean, sd);
XYDataset dataset = DatasetUtilities.sampleFunction2D(normal, minX, maxX, 100, "Normal");
return dataset;
}
```