View Javadoc

1   /*
2    * @(#) XYSeries.java Sep 26, 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  
11  package nl.tudelft.simulation.jstats.charts.xy;
12  
13  import java.io.Serializable;
14  import java.util.ArrayList;
15  import java.util.List;
16  
17  import nl.tudelft.simulation.event.EventInterface;
18  import nl.tudelft.simulation.event.EventListenerInterface;
19  import nl.tudelft.simulation.event.EventType;
20  import nl.tudelft.simulation.event.TimedEvent;
21  import nl.tudelft.simulation.logger.Logger;
22  
23  import org.jfree.data.AbstractDataset;
24  
25  /***
26   * The xySerie specifies an xySerie for XY Plots in DSOL.
27   * <p>
28   * (c) copyright 2003 <a href="http://www.simulation.tudelft.nl">Delft
29   * University of Technology </a>, the Netherlands. <br>
30   * See for project information <a href="http://www.simulation.tudelft.nl">
31   * www.simulation.tudelft.nl </a> <br>
32   * License of use: <a href="http://www.gnu.org/copyleft/gpl.html">General Public
33   * License (GPL) </a>, no warranty <br>
34   * 
35   * @author <a href="http://www.simulation.tudelft.nl/people/jacobs.html">Peter
36   *         Jacobs </a>
37   * @version 1.8 2004-03-18
38   * @since 1.0
39   */
40  public class XYSeries extends AbstractDataset implements EventListenerInterface
41  {
42  	/*** LOWER_RANGE_EVENT is fired on a range change */
43  	public static final EventType LOWER_RANGE_EVENT = new EventType(
44  			"LOWER_RANGE_EVENT");
45  
46  	/*** UPPER_RANGE_EVENT is fired on a range change */
47  	public static final EventType UPPER_RANGE_EVENT = new EventType(
48  			"UPPER_RANGE_EVENT");
49  
50  	/*** name refers to the name of the serie */
51  	private String name = null;
52  
53  	/*** the entries of the serie */
54  	private List entries = new ArrayList();
55  
56  	/*** the axisType (default, logarithmic) */
57  	private short axisType = XYChart.XLINEAR_YLINEAR;
58  
59  	/***
60  	 * constructs a new XYSeries.
61  	 * 
62  	 * @param name the name of the series.
63  	 * @param axisType whether this serie is logarithmic (x=0 & y=0 are
64  	 *        neglected)
65  	 */
66  	public XYSeries(final String name, final short axisType)
67  	{
68  		super();
69  		this.axisType = axisType;
70  		this.name = name;
71  		this.fireDatasetChanged();
72  	}
73  
74  	/***
75  	 * @see nl.tudelft.simulation.event.EventListenerInterface
76  	 *      #notify(nl.tudelft.simulation.event.EventInterface)
77  	 */
78  	public synchronized void notify(final EventInterface event)
79  	{
80  		TimedEvent timedEvent = (TimedEvent) event;
81  
82  		//We have chosen to simply neglect <=0.0 values on logarithmic axis
83  		if (this.axisType == XYChart.XLOGARITHMIC_YLINEAR
84  				|| this.axisType == XYChart.XLOGARITHMIC_YLOGARITHMIC)
85  		{
86  			if (timedEvent.getTimeStamp() <= 0.0)
87  			{
88  				Logger.warning(this, "notify", "refusing xvalue of " + event
89  						+ " on logrithmic chart");
90  				return;
91  			}
92  		}
93  		if (this.axisType == XYChart.XLINEAR_YLOGARITHMIC
94  				|| this.axisType == XYChart.XLOGARITHMIC_YLOGARITHMIC)
95  		{
96  			if (((Number) timedEvent.getContent()).doubleValue() <= 0.0)
97  			{
98  				Logger.warning(this, "notify", "refusing yValue of " + event
99  						+ " on logrithmic chart");
100 				return;
101 			}
102 		}
103 		this.entries.add(new Tuple(timedEvent.getTimeStamp(),
104 				(Number) timedEvent.getContent()));
105 		this.fireDatasetChanged();
106 	}
107 
108 	/***
109 	 * returns the number of items in this seris
110 	 * 
111 	 * @return int the number
112 	 */
113 	public int getItemCount()
114 	{
115 		return this.entries.size();
116 	}
117 
118 	/***
119 	 * returns the X value
120 	 * 
121 	 * @param item the item
122 	 * @return Number the xValue
123 	 */
124 	public Number getXValue(final int item)
125 	{
126 		return ((Tuple) this.entries.get(item)).getXValue();
127 	}
128 
129 	/***
130 	 * returns the yValue
131 	 * 
132 	 * @param item the item
133 	 * @return Number
134 	 */
135 	public Number getYValue(final int item)
136 	{
137 		return ((Tuple) this.entries.get(item)).getYValue();
138 	}
139 
140 	/***
141 	 * returns the name of this serie
142 	 * 
143 	 * @return String name
144 	 */
145 	public String getSeriesName()
146 	{
147 		return this.name;
148 	}
149 
150 	/***
151 	 * holds the x-y value pairs as tuple
152 	 */
153 	private class Tuple implements Serializable
154 	{
155 		/*** x-value is the xValue */
156 		private double xValue = Double.NaN;
157 
158 		/*** yValue is the yValue */
159 		private double yValue = Double.NaN;
160 
161 		/***
162 		 * constructs a new tuple constructs a new Tuple
163 		 * 
164 		 * @param xValue the x-value.
165 		 * @param yValueNumber the y-value.
166 		 */
167 		public Tuple(final double xValue, final Number yValueNumber)
168 		{
169 			this.xValue = xValue;
170 			this.yValue = yValueNumber.doubleValue();
171 		}
172 
173 		/***
174 		 * returns the xValue.
175 		 * 
176 		 * @return Number the xValue
177 		 */
178 		public Number getXValue()
179 		{
180 			return new Double(this.xValue);
181 		}
182 
183 		/***
184 		 * returns the yValue.
185 		 * 
186 		 * @return Number the yValue
187 		 */
188 		public Number getYValue()
189 		{
190 			return new Double(this.yValue);
191 		}
192 	}
193 }