A discussion forum for JFreeChart (a 2D chart library for the Java platform).
-
stledger
- Posts: 14
- Joined: Sun Mar 09, 2008 7:39 am
Post
by stledger » Thu Jan 09, 2014 4:49 am
Once again, posting code here. This demonstrates using the ProbabilityAxis to plot normally distributed data.
Code: Select all
/* -------------------
* ProbabilityAxisDemoLinear.java
* -------------------
* (C) Copyright 2004-2008, by Object Refinery Limited.
*
*/
package demo;
import java.util.Arrays;
import java.util.Random;
import javax.swing.JPanel;
import org.jfree.chart.ChartFactory;
import org.jfree.chart.ChartPanel;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.axis.ProbabilityAxis;
import org.jfree.chart.labels.StandardXYToolTipGenerator;
import org.jfree.chart.plot.PlotOrientation;
import org.jfree.chart.plot.XYPlot;
import org.jfree.chart.renderer.xy.XYLineAndShapeRenderer;
import org.jfree.data.xy.XYDataset;
import org.jfree.data.xy.XYSeries;
import org.jfree.data.xy.XYSeriesCollection;
import org.jfree.ui.ApplicationFrame;
import org.jfree.ui.RefineryUtilities;
/**
* A simple demonstration application showing how to create a probability axis
* line chart for a Gaussian distribution using data from a random draw of a
* Gaussian distribution.
*/
public class ProbabilityAxisDemoLinear extends ApplicationFrame
{
/**
* Creates a new demo.
*
* @param title
* the frame title.
*/
public ProbabilityAxisDemoLinear(String title)
{
super(title);
JPanel chartPanel = createDemoPanel();
chartPanel.setPreferredSize(new java.awt.Dimension(500, 270));
setContentPane(chartPanel);
}
/**
* Creates a sample dataset.
*
* @return a sample dataset.
*/
private static XYDataset createDataset()
{
XYSeries series1 = new XYSeries("Random Draw");
Random random = new Random(13);
double median = 10;
double sigma = 2;
double[] gaussian = new double[100];
// the percentage of the cumulative distribution
double[] x = new double[100];
for (int i = 0; i < gaussian.length; i++)
{
gaussian[i] = random.nextGaussian() * sigma + median;
x[i] = i;
}
Arrays.sort(gaussian);
for (int i = 0; i < x.length; i++)
{
series1.add(x[i], gaussian[i]);
}
XYSeries series2 = new XYSeries(
"Gaussian Distribution (median = 10, sigma = 2");
series2.add(0.5, 4.850);
series2.add(50.0, 10.0);
series2.add(99.5, 15.150);
XYSeriesCollection dataset = new XYSeriesCollection();
dataset.addSeries(series1);
dataset.addSeries(series2);
return dataset;
}
/**
* Creates a chart.
*
* @param dataset
* the data for the chart.
*
* @return a chart.
*/
private static JFreeChart createChart(XYDataset dataset)
{
// create the chart...
JFreeChart chart = ChartFactory.createXYLineChart(
"Probability Axis Linear Demo", // chart title
"Cumulative Percent", // x axis label
"Y", // y axis label
dataset, // data
PlotOrientation.VERTICAL, true, // include legend
true, // tooltips
false // urls
);
// get a reference to the plot for further customization...
XYPlot plot = (XYPlot) chart.getPlot();
ProbabilityAxis xAxis = new ProbabilityAxis("Cumulative Percent");
plot.setDomainAxis(xAxis);
XYLineAndShapeRenderer renderer = new XYLineAndShapeRenderer();
renderer.setSeriesLinesVisible(0, false);
renderer.setSeriesShapesVisible(1, false);
renderer.setBaseToolTipGenerator(new StandardXYToolTipGenerator());
plot.setRenderer(renderer);
return chart;
}
/**
* Creates a panel for the demo (used by SuperDemo.java).
*
* @return A panel.
*/
public static JPanel createDemoPanel()
{
JFreeChart chart = createChart(createDataset());
return new ChartPanel(chart);
}
/**
* Starting point for the demonstration application.
*
* @param args
* ignored.
*/
public static void main(String[] args)
{
ProbabilityAxisDemoLinear demo = new ProbabilityAxisDemoLinear(
"JFreeChart: ProbabilityAxisDemoLinear.java");
demo.pack();
RefineryUtilities.centerFrameOnScreen(demo);
demo.setVisible(true);
}
}
John
-
stledger
- Posts: 14
- Joined: Sun Mar 09, 2008 7:39 am
Post
by stledger » Sun Mar 16, 2014 9:58 pm
Made a few minor changes to the demo. It is now easy to change the number of draws. Also, now calculate the cumulative percent, rather than using a value from a table. Basically, took out some magic numbers. This code is free, and submitted for the JFreeChart project use.
John
Code: Select all
/* -------------------
* ProbabilityAxisDemoLinear.java
* -------------------
* (C) Copyright 2004-2008, by Object Refinery Limited.
*
*/
package demo;
import java.util.Arrays;
import java.util.Random;
import javax.swing.JPanel;
import org.jfree.chart.ChartFactory;
import org.jfree.chart.ChartPanel;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.axis.ProbabilityAxis;
import org.jfree.chart.labels.StandardXYToolTipGenerator;
import org.jfree.chart.plot.PlotOrientation;
import org.jfree.chart.plot.XYPlot;
import org.jfree.chart.renderer.xy.XYLineAndShapeRenderer;
import org.jfree.data.xy.XYDataset;
import org.jfree.data.xy.XYSeries;
import org.jfree.data.xy.XYSeriesCollection;
import org.jfree.ui.ApplicationFrame;
import org.jfree.ui.RefineryUtilities;
/**<p>
* A simple demonstration application showing how to create a probability axis
* line chart for a Gaussian distribution using data from a random draw of a
* Gaussian distribution.</p><p>
*
* @version 1.1 Feb 1, 2014
* Fixed a minor error in X axis values. Was plotting the lower side of the percent
* bin, rather than the middle of the bin. Also, now calculate the end point y axis values,
* rather than just pulling the number of standard deviations from a table.</p><p>
*/
public class ProbabilityAxisDemoLinear extends ApplicationFrame
{
/**
* Creates a new demo.
*
* @param title
* the frame title.
*/
public ProbabilityAxisDemoLinear(String title)
{
super(title);
JPanel chartPanel = createDemoPanel();
chartPanel.setPreferredSize(new java.awt.Dimension(500, 270));
setContentPane(chartPanel);
}
/**
* Creates a sample dataset.
*
* @return a sample dataset.
*/
private static XYDataset createDataset()
{
XYSeries series1 = new XYSeries("Random Draw");
Random random = new Random(13);
double median = 10;
double sigma = 2;
int n = 500; // number of random draws
double[] gaussian = new double[n];
// the percentage of the cumulative distribution
double[] x = new double[n];
for (int i = 0; i < gaussian.length; i++)
{
gaussian[i] = random.nextGaussian() * sigma + median;
x[i] = 100.0*(i/((double) n) + 1.0/(2.0*n));
}
Arrays.sort(gaussian);
for (int i = 0; i < x.length; i++)
{
series1.add(x[i], gaussian[i]);
}
// The distribution the random draws are made from
XYSeries series2 = new XYSeries(
"Gaussian Distribution (median = 10, sigma = 2");
series2.add(0.1, median + ProbabilityAxis.calculateInvCNF(0.1)*sigma);
series2.add(50.0, median);
series2.add(99.9, median + ProbabilityAxis.calculateInvCNF(99.9)*sigma);
XYSeriesCollection dataset = new XYSeriesCollection();
dataset.addSeries(series1);
dataset.addSeries(series2);
return dataset;
}
/**
* Creates a chart.
*
* @param dataset
* the data for the chart.
*
* @return a chart.
*/
private static JFreeChart createChart(XYDataset dataset)
{
// create the chart...
JFreeChart chart = ChartFactory.createXYLineChart(
"Probability Axis Linear Demo", // chart title
"Cumulative Percent", // x axis label
"Y", // y axis label
dataset, // data
PlotOrientation.VERTICAL, true, // include legend
true, // tooltips
false // urls
);
// get a reference to the plot for further customization...
XYPlot plot = (XYPlot) chart.getPlot();
ProbabilityAxis xAxis = new ProbabilityAxis("Cumulative Percent");
plot.setDomainAxis(xAxis);
XYLineAndShapeRenderer renderer = new XYLineAndShapeRenderer();
renderer.setSeriesLinesVisible(0, false);
renderer.setSeriesShapesVisible(1, false);
renderer.setBaseToolTipGenerator(new StandardXYToolTipGenerator());
plot.setRenderer(renderer);
return chart;
}
/**
* Creates a panel for the demo (used by SuperDemo.java).
*
* @return A panel.
*/
public static JPanel createDemoPanel()
{
JFreeChart chart = createChart(createDataset());
return new ChartPanel(chart);
}
/**
* Starting point for the demonstration application.
*
* @param args
* ignored.
*/
public static void main(String[] args)
{
ProbabilityAxisDemoLinear demo = new ProbabilityAxisDemoLinear(
"JFreeChart: ProbabilityAxisDemoLinear.java");
demo.pack();
RefineryUtilities.centerFrameOnScreen(demo);
demo.setVisible(true);
}
}