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.javafx.chart;
18  
19  import io.github.tonywasher.joceanus.tethys.core.chart.TethysUICoreBarChart;
20  import io.github.tonywasher.joceanus.tethys.core.factory.TethysUICoreFactory;
21  import io.github.tonywasher.joceanus.tethys.javafx.base.TethysUIFXNode;
22  import javafx.collections.ObservableList;
23  import javafx.scene.Node;
24  import javafx.scene.chart.CategoryAxis;
25  import javafx.scene.chart.NumberAxis;
26  import javafx.scene.chart.StackedBarChart;
27  import javafx.scene.chart.XYChart.Data;
28  import javafx.scene.chart.XYChart.Series;
29  import javafx.scene.control.Tooltip;
30  import javafx.scene.input.MouseEvent;
31  import javafx.util.StringConverter;
32  
33  import java.util.HashMap;
34  import java.util.Map;
35  
36  /**
37   * javaFX barChart.
38   */
39  public class TethysUIFXBarChart
40          extends TethysUICoreBarChart {
41      /**
42       * The Node.
43       */
44      private final TethysUIFXNode theNode;
45  
46      /**
47       * The chart.
48       */
49      private final StackedBarChart<String, Number> theChart;
50  
51      /**
52       * The series map.
53       */
54      private final Map<String, Series<String, Number>> theSeries;
55  
56      /**
57       * Constructor.
58       *
59       * @param pFactory the Gui Factory
60       */
61      TethysUIFXBarChart(final TethysUICoreFactory<?> pFactory) {
62          /* initialise underlying class */
63          super(pFactory);
64  
65          /* Create chart */
66          final CategoryAxis myXAxis = new CategoryAxis();
67          final NumberAxis myYAxis = new NumberAxis();
68          myYAxis.setTickLabelFormatter(new StringConverter<>() {
69              @Override
70              public String toString(final Number pValue) {
71                  return getFormatter().formatMoney(getParser().parseMoneyValue(pValue.toString()));
72              }
73  
74              @Override
75              public Number fromString(final String pValue) {
76                  return null;
77              }
78          });
79          theChart = new StackedBarChart<>(myXAxis, myYAxis);
80  
81          /* Create the map */
82          theSeries = new HashMap<>();
83  
84          /* Create Node */
85          theNode = new TethysUIFXNode(theChart);
86      }
87  
88      @Override
89      public TethysUIFXNode getNode() {
90          return theNode;
91      }
92  
93      @Override
94      public void setVisible(final boolean pVisible) {
95          theNode.setManaged(pVisible);
96          theNode.setVisible(pVisible);
97      }
98  
99      @Override
100     public void setEnabled(final boolean pEnabled) {
101         theChart.setDisable(!pEnabled);
102     }
103 
104     @Override
105     public void setPreferredWidth(final Integer pWidth) {
106         theChart.setPrefWidth(pWidth);
107     }
108 
109     @Override
110     public void setPreferredHeight(final Integer pHeight) {
111         theChart.setPrefHeight(pHeight);
112     }
113 
114     @Override
115     public void updateBarChart(final TethysUIBarChartData pData) {
116         /* Update underlying data */
117         super.updateBarChart(pData);
118 
119         /* Set the chart title and Axis labels */
120         theChart.setTitle(pData.getTitle());
121         theChart.getXAxis().setLabel(pData.getXAxisLabel());
122         theChart.getYAxis().setLabel(pData.getYAxisLabel());
123     }
124 
125     @Override
126     protected void resetData() {
127         /* Clear the data */
128         final ObservableList<Series<String, Number>> myData = theChart.getData();
129         myData.clear();
130         theSeries.clear();
131 
132         /* Clear underlying data  */
133         super.resetData();
134     }
135 
136     @Override
137     protected void createSection(final String pName,
138                                  final TethysUIBarChartDataSection pSection) {
139         /* Add to underlying data  */
140         super.createSection(pName, pSection);
141 
142         /* Access the series */
143         final Series<String, Number> mySeries = theSeries.computeIfAbsent(pName, n -> {
144             final Series<String, Number> s = new Series<>();
145             s.setName(n);
146             final ObservableList<Series<String, Number>> myData = theChart.getData();
147             myData.add(s);
148             return s;
149         });
150 
151         /* Create section */
152         final ObservableList<Data<String, Number>> mySections = mySeries.getData();
153         final Data<String, Number> myData = new Data<>(pSection.getReference(), pSection.getValue().doubleValue());
154         mySections.add(myData);
155         final Node myNode = myData.getNode();
156 
157         /* Build name */
158         final String myName = pName + ":" + myData.getXValue();
159 
160         /* Create the toolTip */
161         final String myTooltip = getToolTip(myName);
162         Tooltip.install(myNode, new Tooltip(myTooltip));
163 
164         /* Install click handler for node */
165         myNode.addEventHandler(MouseEvent.MOUSE_CLICKED, e -> selectSection(myName));
166     }
167 }