我似乎无法想象这个JFreeChart。我已经阅读了很多问题,但似乎没有任何问题。我只需要在顶部显示一个正态分布曲线的直方图。
XYPlot plot = new XYPlot();
XYDataset dataset = createDataset();
XYBarRenderer renderer = (XYBarRenderer) plot.getRenderer();
plot.setDataset(0, dataset);
plot.setRenderer(0, renderer);
XYDataset datasetNormal = createDatasetNormal();
XYLineAndShapeRenderer lineRenderer = new XYLineAndShapeRenderer();
plot.setDataset(1, datasetNormal);
plot.setRenderer(1, lineRenderer);
JFreeChart chart = new JFreeChart(plot);
chartPanel.setLayout(null);
ChartPanel chartPanel1 = new ChartPanel(chart);
chartPanel1.setBounds(10, 15, 680, 420);
chartPanel.add(chartPanel1);
以下是创建直方图+正常曲线的方法,temp
是一个数组
1
private XYDataset createDatasetNormal() {
Function2D normal = new NormalDistributionFunction2D(Double.parseDouble(dataCalc.getMean()),
Double.parseDouble(dataCalc.getStnDev()));
XYDataset dataset = DatasetUtilities.sampleFunction2D(normal, minValue(temp), maxValue(temp),
(int) difference - 1, "Normal");
return dataset;
}
2
private IntervalXYDataset createDataset() {
HistogramDataset dataset = new HistogramDataset();
double max = maxValue(temp);
double min = minValue(temp);
dataset.addSeries("Frequency", temp, (int) (difference - 1), min - 1, max + 1);
return dataset;
}
我整个星期一直坚持这一点,所以非常感谢任何帮助!