1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package io.github.tonywasher.joceanus.tethys.core.chart;
18
19 import io.github.tonywasher.joceanus.oceanus.decimal.OceanusDecimalFormatter;
20 import io.github.tonywasher.joceanus.oceanus.decimal.OceanusDecimalParser;
21 import io.github.tonywasher.joceanus.oceanus.decimal.OceanusMoney;
22 import io.github.tonywasher.joceanus.oceanus.event.OceanusEventManager;
23 import io.github.tonywasher.joceanus.oceanus.event.OceanusEventRegistrar;
24 import io.github.tonywasher.joceanus.tethys.api.base.TethysUIEvent;
25 import io.github.tonywasher.joceanus.tethys.api.chart.TethysUIBarChart;
26 import io.github.tonywasher.joceanus.tethys.core.base.TethysUICoreComponent;
27 import io.github.tonywasher.joceanus.tethys.core.chart.TethysUICoreAreaChart.TethysUICoreAreaChartData;
28 import io.github.tonywasher.joceanus.tethys.core.factory.TethysUICoreFactory;
29
30 import java.util.HashMap;
31 import java.util.Iterator;
32 import java.util.LinkedHashMap;
33 import java.util.Map;
34
35
36
37
38
39
40
41
42
43 public abstract class TethysUICoreBarChart
44 extends TethysUICoreComponent
45 implements TethysUIBarChart {
46
47
48
49 private final OceanusDecimalFormatter theFormatter;
50
51
52
53
54 private final OceanusDecimalParser theParser;
55
56
57
58
59 private final Integer theId;
60
61
62
63
64 private final OceanusEventManager<TethysUIEvent> theEventManager;
65
66
67
68
69 private final Map<String, TethysUIBarChartDataSection> theSectionMap;
70
71
72
73
74
75
76 protected TethysUICoreBarChart(final TethysUICoreFactory<?> pFactory) {
77
78 theId = pFactory.getNextId();
79 theEventManager = new OceanusEventManager<>();
80 theFormatter = pFactory.getDataFormatter().getDecimalFormatter();
81 theParser = pFactory.getDataFormatter().getDecimalParser();
82
83
84 theSectionMap = new HashMap<>();
85 }
86
87 @Override
88 public Integer getId() {
89 return theId;
90 }
91
92 @Override
93 public OceanusEventRegistrar<TethysUIEvent> getEventRegistrar() {
94 return theEventManager.getEventRegistrar();
95 }
96
97
98
99
100
101
102 protected OceanusDecimalFormatter getFormatter() {
103 return theFormatter;
104 }
105
106
107
108
109
110
111 protected OceanusDecimalParser getParser() {
112 return theParser;
113 }
114
115 @Override
116 public void updateBarChart(final TethysUIBarChartData pData) {
117
118 resetData();
119
120
121 final Iterator<TethysUIBarChartSeries> myIterator = pData.seriesIterator();
122 while (myIterator.hasNext()) {
123 final TethysUIBarChartSeries myBase = myIterator.next();
124 final String myName = myBase.getName();
125
126
127 final Iterator<TethysUIBarChartDataSection> mySectIterator = myBase.sectionIterator();
128 while (mySectIterator.hasNext()) {
129 final TethysUIBarChartDataSection mySection = mySectIterator.next();
130
131
132 createSection(myName, mySection);
133 }
134 }
135 }
136
137
138
139
140 protected void resetData() {
141
142 theSectionMap.clear();
143 }
144
145
146
147
148
149
150
151 protected void createSection(final String pName,
152 final TethysUIBarChartDataSection pSection) {
153 final String myKey = pName + ":" + pSection.getReference();
154 theSectionMap.put(myKey, pSection);
155 }
156
157
158
159
160
161
162
163 protected String getToolTip(final String pName) {
164 final TethysUIBarChartDataSection mySection = theSectionMap.get(pName);
165 final OceanusMoney myValue = mySection.getValue();
166 return pName + " = " + theFormatter.formatMoney(myValue);
167 }
168
169
170
171
172
173
174 protected void selectSection(final String pName) {
175 final TethysUIBarChartDataSection mySection = theSectionMap.get(pName);
176 theEventManager.fireEvent(TethysUIEvent.PRESSED, mySection);
177 }
178
179
180
181
182 public static final class TethysUICoreBarChartData
183 implements TethysUIBarChartData {
184
185
186
187 static final String XAXIS_LABEL = TethysUICoreAreaChartData.XAXIS_LABEL;
188
189
190
191
192 static final String YAXIS_LABEL = TethysUICoreAreaChartData.YAXIS_LABEL;
193
194
195
196
197 private final String theTitle;
198
199
200
201
202 private String theXAxisLabel;
203
204
205
206
207 private String theYAxisLabel;
208
209
210
211
212 private final Map<String, TethysUIBarChartSeries> theSeriesMap;
213
214
215
216
217
218
219 TethysUICoreBarChartData(final String pTitle) {
220 this(pTitle, XAXIS_LABEL, YAXIS_LABEL);
221 }
222
223
224
225
226
227
228
229
230 private TethysUICoreBarChartData(final String pTitle,
231 final String pXAxisLabel,
232 final String pYAxisLabel) {
233
234 theTitle = pTitle;
235 theXAxisLabel = pXAxisLabel;
236 theYAxisLabel = pYAxisLabel;
237
238
239 theSeriesMap = new LinkedHashMap<>();
240 }
241
242 @Override
243 public TethysUIBarChartData setXAxisLabel(final String pLabel) {
244 theXAxisLabel = pLabel;
245 return this;
246 }
247
248 @Override
249 public TethysUIBarChartData setYAxisLabel(final String pLabel) {
250 theYAxisLabel = pLabel;
251 return this;
252 }
253
254 @Override
255 public String getTitle() {
256 return theTitle;
257 }
258
259 @Override
260 public String getXAxisLabel() {
261 return theXAxisLabel;
262 }
263
264 @Override
265 public String getYAxisLabel() {
266 return theYAxisLabel;
267 }
268
269 @Override
270 public Iterator<TethysUIBarChartSeries> seriesIterator() {
271 return theSeriesMap.values().iterator();
272 }
273
274 @Override
275 public TethysUIBarChartSeries createSeries(final String pName) {
276 final TethysUIBarChartSeries mySeries = new TethysUICoreBarChartSeries(pName);
277 theSeriesMap.put(pName, mySeries);
278 return mySeries;
279 }
280 }
281
282
283
284
285 public static final class TethysUICoreBarChartSeries
286 implements TethysUIBarChartSeries {
287
288
289
290 private final String theName;
291
292
293
294
295 private final Map<String, TethysUIBarChartDataSection> theSectionMap;
296
297
298
299
300
301
302 TethysUICoreBarChartSeries(final String pName) {
303 theName = pName;
304 theSectionMap = new LinkedHashMap<>();
305 }
306
307 @Override
308 public void addSection(final String pRef,
309 final OceanusMoney pValue) {
310 addSection(pRef, pValue, theName + ":" + pRef);
311 }
312
313 @Override
314 public void addSection(final String pRef,
315 final OceanusMoney pValue,
316 final Object pSource) {
317 theSectionMap.put(pRef, new TethysUICoreBarChartDataSection(this, pRef, pValue, pSource));
318 }
319
320 @Override
321 public String getName() {
322 return theName;
323 }
324
325 @Override
326 public Iterator<TethysUIBarChartDataSection> sectionIterator() {
327 return theSectionMap.values().iterator();
328 }
329 }
330
331
332
333
334 public static final class TethysUICoreBarChartDataSection
335 implements TethysUIBarChartDataSection {
336
337
338
339 private final TethysUIBarChartSeries theSeries;
340
341
342
343
344 private final String theRef;
345
346
347
348
349 private final OceanusMoney theValue;
350
351
352
353
354 private final Object theSource;
355
356
357
358
359
360
361
362
363
364 TethysUICoreBarChartDataSection(final TethysUIBarChartSeries pSeries,
365 final String pRef,
366 final OceanusMoney pValue,
367 final Object pSource) {
368 theSeries = pSeries;
369 theRef = pRef;
370 theValue = pValue;
371 theSource = pSource;
372 }
373
374 @Override
375 public TethysUIBarChartSeries getSeries() {
376 return theSeries;
377 }
378
379 @Override
380 public String getReference() {
381 return theRef;
382 }
383
384 @Override
385 public OceanusMoney getValue() {
386 return theValue;
387 }
388
389 @Override
390 public Object getSource() {
391 return theSource;
392 }
393 }
394 }