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.OceanusMoney;
21 import io.github.tonywasher.joceanus.oceanus.decimal.OceanusRate;
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.TethysUIPieChart;
26 import io.github.tonywasher.joceanus.tethys.core.base.TethysUICoreComponent;
27 import io.github.tonywasher.joceanus.tethys.core.factory.TethysUICoreFactory;
28
29 import java.util.HashMap;
30 import java.util.Iterator;
31 import java.util.LinkedHashMap;
32 import java.util.Map;
33
34
35
36
37 public abstract class TethysUICorePieChart
38 extends TethysUICoreComponent
39 implements TethysUIPieChart {
40
41
42
43 private final OceanusDecimalFormatter theFormatter;
44
45
46
47
48 private final Map<String, TethysUIPieChartSection> theSectionMap;
49
50
51
52
53 private final Integer theId;
54
55
56
57
58 private final OceanusEventManager<TethysUIEvent> theEventManager;
59
60
61
62
63 private OceanusMoney theTotal;
64
65
66
67
68
69
70 protected TethysUICorePieChart(final TethysUICoreFactory<?> pFactory) {
71
72 theId = pFactory.getNextId();
73 theEventManager = new OceanusEventManager<>();
74 theFormatter = pFactory.getDataFormatter().getDecimalFormatter();
75
76
77 theSectionMap = new HashMap<>();
78 }
79
80 @Override
81 public Integer getId() {
82 return theId;
83 }
84
85 @Override
86 public OceanusEventRegistrar<TethysUIEvent> getEventRegistrar() {
87 return theEventManager.getEventRegistrar();
88 }
89
90 @Override
91 public void updatePieChart(final TethysUIPieChartData pData) {
92
93 resetData();
94
95
96 final Iterator<TethysUIPieChartSection> myIterator = pData.sectionIterator();
97 while (myIterator.hasNext()) {
98 final TethysUIPieChartSection mySection = myIterator.next();
99
100
101 createSlice(mySection);
102 }
103 }
104
105
106
107
108 protected void resetData() {
109
110 theSectionMap.clear();
111 theTotal = null;
112 }
113
114
115
116
117
118
119 protected void createSlice(final TethysUIPieChartSection pSection) {
120
121 theSectionMap.put(pSection.getName(), pSection);
122
123
124 if (theTotal == null) {
125 theTotal = new OceanusMoney(pSection.getValue());
126 } else {
127 theTotal.addAmount(pSection.getValue());
128 }
129 }
130
131
132
133
134
135
136
137 protected String getToolTip(final String pName) {
138 final TethysUIPieChartSection mySection = theSectionMap.get(pName);
139 final OceanusMoney myValue = mySection.getValue();
140 final OceanusRate myPerCent = new OceanusRate(myValue, theTotal);
141 return pName + ": ("
142 + theFormatter.formatMoney(myValue) + ", "
143 + theFormatter.formatRate(myPerCent) + ")";
144 }
145
146
147
148
149
150
151 protected void selectSection(final String pName) {
152 final TethysUIPieChartSection mySection = theSectionMap.get(pName);
153 theEventManager.fireEvent(TethysUIEvent.PRESSED, mySection);
154 }
155
156
157
158
159 public static final class TethysUICorePieChartData
160 implements TethysUIPieChartData {
161
162
163
164 private final String theTitle;
165
166
167
168
169 private final Map<String, TethysUIPieChartSection> theSectionMap;
170
171
172
173
174
175
176 TethysUICorePieChartData(final String pTitle) {
177 theTitle = pTitle;
178 theSectionMap = new LinkedHashMap<>();
179 }
180
181 @Override
182 public String getTitle() {
183 return theTitle;
184 }
185
186 @Override
187 public Iterator<TethysUIPieChartSection> sectionIterator() {
188 return theSectionMap.values().iterator();
189 }
190
191 @Override
192 public void addSection(final String pName,
193 final OceanusMoney pValue,
194 final Object pSource) {
195 final TethysUIPieChartSection mySection = new TethysUICorePieChartSection(pName, pValue, pSource);
196 theSectionMap.put(pName, mySection);
197 }
198 }
199
200
201
202
203 public static final class TethysUICorePieChartSection
204 implements TethysUIPieChartSection {
205
206
207
208 private final String theName;
209
210
211
212
213 private final OceanusMoney theValue;
214
215
216
217
218 private final Object theSource;
219
220
221
222
223
224
225
226
227 private TethysUICorePieChartSection(final String pName,
228 final OceanusMoney pValue,
229 final Object pSource) {
230 theName = pName;
231 theValue = pValue;
232 theSource = pSource;
233 }
234
235 @Override
236 public String getName() {
237 return theName;
238 }
239
240 @Override
241 public OceanusMoney getValue() {
242 return theValue;
243 }
244
245 @Override
246 public Object getSource() {
247 return theSource;
248 }
249 }
250 }