Richard,
I am trying to use the method you suggested here. I have had success drawing the line underneath the curve, but I cannot get it to fill in area between the two datasets. I have used XYDifferenceRenderer as suggested but it does not work!
Here is some code giving a basic example:
Code: Select all
package main;
import javax.swing.JFrame;
import org.jfree.chart.ChartPanel;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.axis.NumberAxis;
import org.jfree.chart.plot.XYPlot;
import org.jfree.chart.renderer.xy.XYDifferenceRenderer;
import org.jfree.data.function.Function2D;
import org.jfree.data.general.DatasetUtilities;
import org.jfree.data.xy.XYSeries;
import org.jfree.data.xy.XYSeriesCollection;
public class Test extends JFrame {
private JFreeChart chart;
private XYSeriesCollection dataset;
private XYPlot plot;
public Test() {
Function2D group = new FDistributionFunction(10, 10);
dataset = (XYSeriesCollection)DatasetUtilities.sampleFunction2D(group, 0, 5, 2000, "F");
double f = 2;
XYSeries fLine = new XYSeries("fLine");
fLine.add(f, 0);
fLine.add(f, group.getValue(f));
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);
}
public static void main(String[] args) {
Test m = new Test();
m.setVisible(true);
m.pack();
}
}
And here is the FDistributionFunction class used above which I have created by implementing the Function2D class:
Code: Select all
package main;
import org.jfree.data.function.Function2D;
public class FDistributionFunction implements Function2D {
private double df1;
private double df2;
public FDistributionFunction(double df1, double df2) {
this.df1 = df1;
this.df2 = df2;
}
public double getValue(double x) {
double top = (Math.pow((df1*x), df1))*(Math.pow(df2, df2));
double bottom = Math.pow(((df1*x) + df2), (df1 + df2));
double divideTop = Math.sqrt(top/bottom);
double divideBottom = x*beta(0.5*df1, 0.5*df2);
double pdf = divideTop / divideBottom;
return pdf;
}
public double beta(double a, double b) {
return Math.exp((gammaln(a)) + (gammaln(b)) - (gammaln(a+b)));
}
public double gammaln(double w) {
double[] cof = {76.18009172947146, -86.50532032941677,
24.01409824083091, -1.231739572450155, 0.1208650973866179 * Math.pow(10,-2), -0.5395239384953 * Math.pow(10,-5)};
double y = w;
double x = w;
double tmp = x+5.5;
tmp -= (x+0.5)*Math.log(tmp);
double ser = 1.000000000190015;
for(int i = 0; i<+5;i++)
ser += cof[i]/++y;
return -tmp+Math.log(2.5066282746310005*ser/x);
}
}