1 39 40 package org.jfree.chart.demo; 41 42 import java.io.IOException ; 43 import java.util.Random ; 44 45 import org.jfree.chart.ChartFactory; 46 import org.jfree.chart.ChartPanel; 47 import org.jfree.chart.JFreeChart; 48 import org.jfree.chart.plot.PlotOrientation; 49 import org.jfree.data.HistogramDataset; 50 import org.jfree.data.IntervalXYDataset; 51 import org.jfree.ui.ApplicationFrame; 52 import org.jfree.ui.RefineryUtilities; 53 54 59 public class HistogramDemo extends ApplicationFrame { 60 61 62 static Random random = new Random (); 63 64 69 public HistogramDemo(String title) { 70 super(title); 71 IntervalXYDataset dataset = createDataset(); 72 JFreeChart chart = createChart(dataset); 73 ChartPanel chartPanel = new ChartPanel(chart); 74 chartPanel.setPreferredSize(new java.awt.Dimension (500, 270)); 75 chartPanel.setMouseZoomable(true, false); 76 setContentPane(chartPanel); 77 } 78 79 84 private IntervalXYDataset createDataset() { 85 HistogramDataset dataset = new HistogramDataset(); 86 dataset.setType(HistogramDataset.RELATIVE_FREQUENCY); 87 dataset.addSeries("H1", gaussianData(1000, 3.0), 20); 88 dataset.addSeries("H0", gaussianData(1000, 0), 20); 89 return dataset; 90 } 91 92 103 110 private JFreeChart createChart(IntervalXYDataset dataset) { 111 JFreeChart chart = ChartFactory.createHistogram( 112 "Histogram Demo", 113 null, 114 null, 115 dataset, 116 PlotOrientation.VERTICAL, 117 true, 118 false, 119 false 120 ); 121 chart.getXYPlot().setForegroundAlpha(0.75f); 122 return chart; 123 } 124 125 133 private static double[] gaussianData(int size, double shift) { 134 double[] d = new double[size]; 135 for (int i = 0; i < d.length; i++) { 136 d[i] = random.nextGaussian() + shift; 137 } 138 return d; 139 } 140 141 148 public static void main(String [] args) throws IOException { 149 150 HistogramDemo demo = new HistogramDemo("Histogram Demo"); 151 demo.pack(); 152 RefineryUtilities.centerFrameOnScreen(demo); 153 demo.setVisible(true); 154 155 } 156 157 } 158 | Popular Tags |