1 package org.opentrafficsim.graphs;
2
3 import java.io.Serializable;
4
5 import org.djunits.value.vdouble.scalar.DoubleScalarInterface;
6
7
8
9
10
11
12
13
14
15
16
17 public class Axis implements Serializable
18 {
19
20 private static final long serialVersionUID = 20140000L;
21
22
23 private final DoubleScalarInterface minimumValue;
24
25
26 private DoubleScalarInterface 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 DoubleScalarInterface minimumValue, final DoubleScalarInterface maximumValue,
55 final double[] granularities, final double initialGranularity, final String name, final String shortName,
56 final String format)
57 {
58 this.minimumValue = minimumValue;
59 this.setMaximumValue(maximumValue);
60 this.granularities = granularities;
61 if (null != granularities)
62 {
63 this.setCurrentGranularity(initialGranularity);
64 }
65 this.name = name;
66 this.shortName = shortName;
67 this.format = format;
68 }
69
70
71
72
73
74
75 public double getRelativeBin(final DoubleScalarInterface value)
76 {
77 return (value.getSI() - this.getMinimumValue().getSI()) / this.getGranularities()[0];
78 }
79
80
81
82
83
84 public void adjustMaximumValue(final DoubleScalarInterface newMaximum)
85 {
86
87 this.setMaximumValue(newMaximum);
88 }
89
90
91
92
93
94
95 public double getValue(final int aggregatedBin)
96 {
97 return this.getMinimumValue().getSI() + 1.0 * aggregatedBin * this.getCurrentGranularity();
98 }
99
100
101
102
103 public int getAggregatedBinCount()
104 {
105 return (int) Math
106 .ceil((this.getMaximumValue().getSI() - this.getMinimumValue().getSI()) / this.getCurrentGranularity());
107 }
108
109
110
111
112 public int getBinCount()
113 {
114 return (int) Math.ceil((this.getMaximumValue().getSI() - this.getMinimumValue().getSI()) / 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 DoubleScalarInterface getMaximumValue()
149 {
150 return this.maximumValue;
151 }
152
153
154
155
156
157
158 public void setMaximumValue(final DoubleScalarInterface 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 DoubleScalarInterface 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 }