1 package org.opentrafficsim.graphs;
2
3 import java.io.Serializable;
4
5 import org.djunits.value.vdouble.scalar.DoubleScalar;
6
7 /**
8 * Definition of one axis for a ContourPlot.
9 * <p>
10 * Copyright (c) 2013-2016 Delft University of Technology, PO Box 5, 2600 AA, Delft, the Netherlands. All rights reserved. <br>
11 * BSD-style license. See <a href="http://opentrafficsim.org/docs/license.html">OpenTrafficSim License</a>.
12 * <p>
13 * $LastChangedDate: 2015-08-30 00:16:51 +0200 (Sun, 30 Aug 2015) $, @version $Revision: 1329 $, by $Author: averbraeck $,
14 * initial version Jul 28, 2014 <br>
15 * @author <a href="http://www.tudelft.nl/pknoppers">Peter Knoppers</a>
16 */
17 class Axis implements Serializable
18 {
19 /** */
20 private static final long serialVersionUID = 20140000L;
21
22 /** Lowest value along this axis. */
23 private final DoubleScalar<?> minimumValue;
24
25 /** Highest value along this axis. */
26 private DoubleScalar<?> maximumValue;
27
28 /** Aggregation values along this axis (all values must be an integer multiple of the first value). */
29 private final double[] granularities;
30
31 /** Current aggregation value (must be one of the values in granularities). */
32 private double currentGranularity;
33
34 /** Name to describe the axis and to name the pop up menu that changes the current granularity. */
35 private final String name;
36
37 /** Name to identify this axis. */
38 private final String shortName;
39
40 /** Format for rendering a value along this axis. */
41 private String format;
42
43 /**
44 * Create a new AxisDefinition.
45 * @param minimumValue DoubleScalar; the minimum value along this axis
46 * @param maximumValue DoubleScalar; the maximum value along this axis
47 * @param granularities double[]; the aggregation values along this axis (all values must be an integer multiple of the
48 * first value)
49 * @param initialGranularity double; initial aggregation value (must be one of the values in granularities)
50 * @param name String; the name to describe the axis and to name the pop up menu that changes the current granularity
51 * @param shortName String; the name identifying this axis for use in a menu
52 * @param format String; format string for rendering a value along this axis
53 */
54 public Axis(final DoubleScalar<?> minimumValue, final DoubleScalar<?> maximumValue, final double[] granularities,
55 final double initialGranularity, final String name, final String shortName, final String format)
56 {
57 this.minimumValue = minimumValue;
58 this.setMaximumValue(maximumValue);
59 this.granularities = granularities;
60 if (null != granularities)
61 {
62 this.setCurrentGranularity(initialGranularity);
63 }
64 this.name = name;
65 this.shortName = shortName;
66 this.format = format;
67 }
68
69 /**
70 * Compute the floating point bin number for a value.
71 * @param value DoubleScalar; the value
72 * @return double; the bin that belongs to the value
73 */
74 public double getRelativeBin(final DoubleScalar<?> value)
75 {
76 return (value.getSI() - this.getMinimumValue().getSI()) / this.getGranularities()[0];
77 }
78
79 /**
80 * Adjust (increase) the range of this AxisDefinition.
81 * @param newMaximum DoubleScalar; the new maximum value of the axis
82 */
83 public void adjustMaximumValue(final DoubleScalar<?> newMaximum)
84 {
85 // System.out.println("extending axis " + this.name + " from " + this.maximumValue + " to " + newMaximum);
86 this.setMaximumValue(newMaximum);
87 }
88
89 /**
90 * Return the value for an aggregated bin number.
91 * @param aggregatedBin Integer; the number of a bin
92 * @return Double; the value corresponding to the center of aggregateBin
93 */
94 public double getValue(final int aggregatedBin)
95 {
96 return this.getMinimumValue().getSI() + 1.0 * aggregatedBin * this.getCurrentGranularity();
97 }
98
99 /**
100 * @return Integer; the number of bins along this axis
101 */
102 public int getAggregatedBinCount()
103 {
104 return (int) Math.ceil((this.getMaximumValue().getSI() - this.getMinimumValue().getSI())
105 / this.getCurrentGranularity());
106 }
107
108 /**
109 * @return Integer; the number of aggregated bins along this axis
110 */
111 public int getBinCount()
112 {
113 return (int) Math.ceil((this.getMaximumValue().getSI() - this.getMinimumValue().getSI())
114 / this.getGranularities()[0]);
115 }
116
117 /**
118 * Get the granularity of this axis.
119 * @return double; the granularity of this axis
120 */
121 public double getCurrentGranularity()
122 {
123 return this.currentGranularity;
124 }
125
126 /**
127 * Change the granularity for this axis. <br>
128 * The new value must be present in the granularities.
129 * @param newGranularity double; the new value for the granularity of this axis
130 */
131 public void setCurrentGranularity(final double newGranularity)
132 {
133 for (double g : this.getGranularities())
134 {
135 if (g == newGranularity)
136 {
137 this.currentGranularity = newGranularity;
138 return;
139 }
140 }
141 throw new RuntimeException("Illegal granularity " + newGranularity);
142 }
143
144 /**
145 * Get the maximum value of this axis.
146 * @return DoubleScalar; the current maximum value of this axis
147 */
148 public DoubleScalar<?> getMaximumValue()
149 {
150 return this.maximumValue;
151 }
152
153 /**
154 * Change the maximum value of this axis. <br>
155 * The maximum value can only be increased.
156 * @param newMaximumValue DoubleScalar; the new maximum value of this axis
157 */
158 public void setMaximumValue(final DoubleScalar<?> newMaximumValue)
159 {
160 if (null != this.maximumValue && newMaximumValue.getSI() < this.maximumValue.getSI())
161 {
162 throw new RuntimeException("maximum value may not be decreased");
163 }
164 this.maximumValue = newMaximumValue;
165 }
166
167 /**
168 * Get the minimum value of this axis.
169 * @return DoubleScalar; the minimum value of this axis
170 */
171 public DoubleScalar<?> getMinimumValue()
172 {
173 return this.minimumValue;
174 }
175
176 /**
177 * Retrieve the possible granularities for this Axis.
178 * @return granularities
179 */
180 public final double[] getGranularities()
181 {
182 return this.granularities;
183 }
184
185 /**
186 * Retrieve the format for displaying values along this Axis.
187 * @return format
188 */
189 public String getFormat()
190 {
191 return this.format;
192 }
193
194 /**
195 * Retrieve the short name for this Axis.
196 * @return String; the short name for this Axis
197 */
198 public String getShortName()
199 {
200 return this.shortName;
201 }
202
203 /**
204 * Retrieve the name of this Axis.
205 * @return String; the name of this Axis
206 */
207 public String getName()
208 {
209 return this.name;
210 }
211
212 /** {@inheritDoc} */
213 @Override
214 public final String toString()
215 {
216 return "Axis [minimumValue=" + this.minimumValue + ", maximumValue=" + this.maximumValue + ", currentGranularity="
217 + this.currentGranularity + ", name=" + this.name + ", shortName=" + this.shortName + "]";
218 }
219
220 }