View Javadoc
1   /*
2    * Tethys: GUI Utilities
3    * Copyright 2012-2026. Tony Washer
4    *
5    * Licensed under the Apache License, Version 2.0 (the "License"); you may not
6    * use this file except in compliance with the License.  You may obtain a copy
7    * of the License at
8    *
9    *   http://www.apache.org/licenses/LICENSE-2.0
10   *
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
13   * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.  See the
14   * License for the specific language governing permissions and limitations under
15   * the License.
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   * Pie Chart.
36   */
37  public abstract class TethysUICorePieChart
38          extends TethysUICoreComponent
39          implements TethysUIPieChart {
40      /**
41       * The formatter.
42       */
43      private final OceanusDecimalFormatter theFormatter;
44  
45      /**
46       * The sectionMap.
47       */
48      private final Map<String, TethysUIPieChartSection> theSectionMap;
49  
50      /**
51       * The id.
52       */
53      private final Integer theId;
54  
55      /**
56       * The Event Manager.
57       */
58      private final OceanusEventManager<TethysUIEvent> theEventManager;
59  
60      /**
61       * The total.
62       */
63      private OceanusMoney theTotal;
64  
65      /**
66       * Constructor.
67       *
68       * @param pFactory the Gui factory
69       */
70      protected TethysUICorePieChart(final TethysUICoreFactory<?> pFactory) {
71          /* Build standard fields */
72          theId = pFactory.getNextId();
73          theEventManager = new OceanusEventManager<>();
74          theFormatter = pFactory.getDataFormatter().getDecimalFormatter();
75  
76          /* Create the section map */
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          /* reset the existing data  */
93          resetData();
94  
95          /* Iterate through the sections */
96          final Iterator<TethysUIPieChartSection> myIterator = pData.sectionIterator();
97          while (myIterator.hasNext()) {
98              final TethysUIPieChartSection mySection = myIterator.next();
99  
100             /* Create the slice */
101             createSlice(mySection);
102         }
103     }
104 
105     /**
106      * Reset chart data.
107      */
108     protected void resetData() {
109         /* Clear existing data  */
110         theSectionMap.clear();
111         theTotal = null;
112     }
113 
114     /**
115      * Add chart section.
116      *
117      * @param pSection the section to add
118      */
119     protected void createSlice(final TethysUIPieChartSection pSection) {
120         /* Add to the section map */
121         theSectionMap.put(pSection.getName(), pSection);
122 
123         /* Adjust the total */
124         if (theTotal == null) {
125             theTotal = new OceanusMoney(pSection.getValue());
126         } else {
127             theTotal.addAmount(pSection.getValue());
128         }
129     }
130 
131     /**
132      * Obtain tooltip for sectionName.
133      *
134      * @param pName the section name
135      * @return the tooltip
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      * handle selection.
148      *
149      * @param pName the section name
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      * PieChart Data.
158      */
159     public static final class TethysUICorePieChartData
160             implements TethysUIPieChartData {
161         /**
162          * The Chart Title.
163          */
164         private final String theTitle;
165 
166         /**
167          * The SectionMap.
168          */
169         private final Map<String, TethysUIPieChartSection> theSectionMap;
170 
171         /**
172          * Constructor.
173          *
174          * @param pTitle the title
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      * The Section definition.
202      */
203     public static final class TethysUICorePieChartSection
204             implements TethysUIPieChartSection {
205         /**
206          * The name of the section.
207          */
208         private final String theName;
209 
210         /**
211          * The value of the section.
212          */
213         private final OceanusMoney theValue;
214 
215         /**
216          * The source of the section.
217          */
218         private final Object theSource;
219 
220         /**
221          * Constructor.
222          *
223          * @param pName   the name
224          * @param pValue  the value
225          * @param pSource the source
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 }