View Javadoc

1   /*
2    * @(#) Histogram.java Sep 22, 2003
3    * 
4    * Copyright (c) 2003 Delft University of Technology Jaffalaan 5, 2628 BX Delft,
5    * the Netherlands All rights reserved.
6    * 
7    * This software is proprietary information of Delft University of Technology
8    * The code is published under the General Public License
9    */
10  package nl.tudelft.simulation.jstats.charts.histogram;
11  
12  import java.awt.Color;
13  import java.awt.Container;
14  import java.awt.GradientPaint;
15  import java.rmi.RemoteException;
16  
17  import nl.tudelft.simulation.event.EventProducerInterface;
18  import nl.tudelft.simulation.event.EventType;
19  import nl.tudelft.simulation.jstats.Swingable;
20  import nl.tudelft.simulation.jstats.statistics.Counter;
21  import nl.tudelft.simulation.logger.Logger;
22  
23  import org.jfree.chart.ChartFactory;
24  import org.jfree.chart.ChartPanel;
25  import org.jfree.chart.JFreeChart;
26  import org.jfree.chart.axis.NumberAxis;
27  import org.jfree.chart.plot.PlotOrientation;
28  
29  /***
30   * The histogram specifies a histogram chart for the DSOL framework.
31   * <p>
32   * (c) copyright 2002-2004 <a href="http://www.simulation.tudelft.nl">Delft
33   * University of Technology </a>, the Netherlands. <br>
34   * See for project information <a href="http://www.simulation.tudelft.nl">
35   * www.simulation.tudelft.nl </a> <br>
36   * License of use: <a href="http://www.gnu.org/copyleft/gpl.html">General Public
37   * License (GPL) </a>, no warranty <br>
38   * 
39   * @author <a href="http://www.tbm.tudelft.nl/webstaf/alexandv/index.htm">
40   *         Alexander Verbraeck </a> <br>
41   *         <a href="http://www.tbm.tudelft.nl/webstaf/peterja/index.htm"> Peter
42   *         Jacobs </a>
43   * @version 1.9 2004-03-22
44   * @since 1.2
45   */
46  public class Histogram implements Swingable
47  {
48  	/*** LABEL_X_AXIS is the label on the X-axis */
49  	public static final String LABEL_X_AXIS = "X";
50  
51  	/*** LABEL_Y_AXIS is the label on the Y-axis */
52  	public static final String LABEL_Y_AXIS = "#";
53  
54  	/*** chart refers to the chart */
55  	protected JFreeChart chart = null;
56  
57  	/*** dataset refers to the dataset */
58  	protected HistogramDataset dataset = null;
59  
60  	/***
61  	 * constructs a new Histogram
62  	 * 
63  	 * @param title the title
64  	 * @param domain the domain
65  	 * @param numberofBins the numberofbins
66  	 */
67  	public Histogram(final String title, final double[] domain,
68  			final int numberofBins)
69  	{
70  		this(title, domain, null, numberofBins);
71  	}
72  
73  	/***
74  	 * constructs a new Histogram.
75  	 * 
76  	 * @param title the title. The title of the histogram
77  	 * @param domain the domain of the x-axis.
78  	 * @param range the y-axis range of the histogram.
79  	 * @param numberofBins the numberofbins of this histogram.
80  	 */
81  	public Histogram(final String title, final double[] domain,
82  			final double[] range, final int numberofBins)
83  	{
84  		super();
85  		this.dataset = new HistogramDataset(domain, range, numberofBins);
86  
87  		this.chart = ChartFactory.createHistogram(title, LABEL_X_AXIS,
88  				LABEL_Y_AXIS, this.dataset, PlotOrientation.VERTICAL, true,
89  				true, true);
90  		this.chart.setBackgroundPaint(new GradientPaint(0.0F, 0.0F,
91  				Color.white, 1000F, 0.0F, Color.blue));
92  
93  		this.chart.getXYPlot().setRangeAxis(
94  				new NumberAxis(Histogram.LABEL_Y_AXIS));
95  		this.chart.getXYPlot().getRangeAxis().setAutoRange(true);
96  		this.chart.getXYPlot().setDomainAxis(
97  				new HistogramDomainAxis(this.chart.getXYPlot(),
98  						Histogram.LABEL_X_AXIS, domain, numberofBins));
99  		this.dataset.addChangeListener(this.chart.getXYPlot());
100 	}
101 
102 	/***
103 	 * adds a counter to the histogramdataset. This histogram then subscribes
104 	 * its dataset to the <code>Counter.COUNT_EVENT</code>.
105 	 * 
106 	 * @param counter the counter to add.
107 	 */
108 	public synchronized void add(final Counter counter)
109 	{
110 		HistogramSeries set = this.getDataset().addSeries(
111 				counter.getDescription());
112 		counter.addListener(set, Counter.COUNT_EVENT, false);
113 	}
114 
115 	/***
116 	 * adds an eventProducer to the histogram dataset. The histogram subscribes
117 	 * its dataset subsequentially to the specified event.
118 	 * 
119 	 * @param description the description of the eventProducer
120 	 * @param source the eventproducer which functions as source for this
121 	 *        histogram.
122 	 * @param eventType the eventType.
123 	 */
124 	public synchronized void add(final String description,
125 			final EventProducerInterface source, final EventType eventType)
126 	{
127 		HistogramSeries set = this.getDataset().addSeries(description);
128 		try
129 		{
130 			source.addListener(set, eventType, false);
131 		} catch (RemoteException exception)
132 		{
133 			Logger.warning(this, "add", exception);
134 		}
135 	}
136 
137 	/***
138 	 * returns the chart
139 	 * 
140 	 * @return JFreeChart
141 	 */
142 	public JFreeChart getChart()
143 	{
144 		return this.chart;
145 	}
146 
147 	/***
148 	 * returns the chartPanel of this histogram.
149 	 * 
150 	 * @return ChartPanel
151 	 */
152 	public Container getSwingPanel()
153 	{
154 		ChartPanel result = new ChartPanel(this.chart);
155 		result.setMouseZoomable(true, false);
156 		return result;
157 	}
158 
159 	/***
160 	 * returns the dataset of a histogram.
161 	 * 
162 	 * @return the HistogramDataset containing all series.
163 	 */
164 	public HistogramDataset getDataset()
165 	{
166 		return this.dataset;
167 	}
168 }