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 javafx.collections.ObservableList;
20  import javafx.scene.Node;
21  import javafx.scene.chart.PieChart;
22  import javafx.scene.chart.PieChart.Data;
23  import javafx.scene.control.Tooltip;
24  import javafx.scene.input.MouseEvent;
25  
26  import io.github.tonywasher.joceanus.tethys.core.chart.TethysUICorePieChart;
27  import io.github.tonywasher.joceanus.tethys.core.factory.TethysUICoreFactory;
28  import io.github.tonywasher.joceanus.tethys.javafx.base.TethysUIFXNode;
29  
30  /**
31   * javaFX Pie Chart.
32   */
33  public class TethysUIFXPieChart
34          extends TethysUICorePieChart {
35      /**
36       * The Node.
37       */
38      private final TethysUIFXNode theNode;
39  
40      /**
41       * The chart.
42       */
43      private final PieChart theChart;
44  
45      /**
46       * Constructor.
47       *
48       * @param pFactory the Gui factory
49       */
50      TethysUIFXPieChart(final TethysUICoreFactory<?> pFactory) {
51          /* initialise underlying class */
52          super(pFactory);
53  
54          /* Create chart */
55          theChart = new PieChart();
56          theChart.setLabelsVisible(true);
57          theNode = new TethysUIFXNode(theChart);
58      }
59  
60      @Override
61      public TethysUIFXNode getNode() {
62          return theNode;
63      }
64  
65      @Override
66      public void setVisible(final boolean pVisible) {
67          theNode.setManaged(pVisible);
68          theNode.setVisible(pVisible);
69      }
70  
71      @Override
72      public void setEnabled(final boolean pEnabled) {
73          theChart.setDisable(!pEnabled);
74      }
75  
76      @Override
77      public void setPreferredWidth(final Integer pWidth) {
78          theChart.setPrefWidth(pWidth);
79      }
80  
81      @Override
82      public void setPreferredHeight(final Integer pHeight) {
83          theChart.setPrefHeight(pHeight);
84      }
85  
86      @Override
87      public void updatePieChart(final TethysUIPieChartData pData) {
88          /* Update underlying data */
89          super.updatePieChart(pData);
90  
91          /* Set the chart title */
92          theChart.setTitle(pData.getTitle());
93      }
94  
95      @Override
96      protected void resetData() {
97          /* Clear the data */
98          final ObservableList<Data> myData = theChart.getData();
99          myData.clear();
100 
101         /* Clear underlying data  */
102         super.resetData();
103     }
104 
105     @Override
106     protected void createSlice(final TethysUIPieChartSection pSection) {
107         /* Add to underlying data  */
108         super.createSlice(pSection);
109 
110         /* Create the slice */
111         final String myName = pSection.getName();
112         final Data mySlice = new Data(myName, pSection.getValue().doubleValue());
113         final ObservableList<Data> myData = theChart.getData();
114         myData.add(mySlice);
115         final Node myNode = mySlice.getNode();
116 
117         /* Create the toolTip */
118         final String myTooltip = getToolTip(myName);
119         Tooltip.install(myNode, new Tooltip(myTooltip));
120 
121         /* Install click handler for node */
122         myNode.addEventHandler(MouseEvent.MOUSE_CLICKED, e -> selectSection(myName));
123     }
124 }