1 package org.opentrafficsim.graphs;
2
3 import java.io.Serializable;
4
5 import org.djunits.value.vdouble.scalar.DoubleScalar;
6
7
8
9
10
11
12
13
14
15
16
17 class Axis implements Serializable
18 {
19
20 private static final long serialVersionUID = 20140000L;
21
22
23 private final DoubleScalar<?> minimumValue;
24
25
26 private DoubleScalar<?> maximumValue;
27
28
29 private final double[] granularities;
30
31
32 private double currentGranularity;
33
34
35 private final String name;
36
37
38 private final String shortName;
39
40
41 private String format;
42
43
44
45
46
47
48
49
50
51
52
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
71
72
73
74 public double getRelativeBin(final DoubleScalar<?> value)
75 {
76 return (value.getSI() - this.getMinimumValue().getSI()) / this.getGranularities()[0];
77 }
78
79
80
81
82
83 public void adjustMaximumValue(final DoubleScalar<?> newMaximum)
84 {
85
86 this.setMaximumValue(newMaximum);
87 }
88
89
90
91
92
93
94 public double getValue(final int aggregatedBin)
95 {
96 return this.getMinimumValue().getSI() + 1.0 * aggregatedBin * this.getCurrentGranularity();
97 }
98
99
100
101
102 public int getAggregatedBinCount()
103 {
104 return (int) Math.ceil((this.getMaximumValue().getSI() - this.getMinimumValue().getSI())
105 / this.getCurrentGranularity());
106 }
107
108
109
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
119
120
121 public double getCurrentGranularity()
122 {
123 return this.currentGranularity;
124 }
125
126
127
128
129
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
146
147
148 public DoubleScalar<?> getMaximumValue()
149 {
150 return this.maximumValue;
151 }
152
153
154
155
156
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
169
170
171 public DoubleScalar<?> getMinimumValue()
172 {
173 return this.minimumValue;
174 }
175
176
177
178
179
180 public final double[] getGranularities()
181 {
182 return this.granularities;
183 }
184
185
186
187
188
189 public String getFormat()
190 {
191 return this.format;
192 }
193
194
195
196
197
198 public String getShortName()
199 {
200 return this.shortName;
201 }
202
203
204
205
206
207 public String getName()
208 {
209 return this.name;
210 }
211
212
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 }