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.swing.chart;
18  
19  import org.jfree.chart.ChartFactory;
20  import org.jfree.chart.ChartMouseEvent;
21  import org.jfree.chart.ChartMouseListener;
22  import org.jfree.chart.ChartPanel;
23  import org.jfree.chart.JFreeChart;
24  import org.jfree.chart.entity.ChartEntity;
25  import org.jfree.chart.entity.PieSectionEntity;
26  import org.jfree.chart.plot.PiePlot;
27  import org.jfree.data.general.DefaultPieDataset;
28  
29  import io.github.tonywasher.joceanus.tethys.core.chart.TethysUICorePieChart;
30  import io.github.tonywasher.joceanus.tethys.core.factory.TethysUICoreFactory;
31  import io.github.tonywasher.joceanus.tethys.swing.base.TethysUISwingNode;
32  
33  /**
34   * Swing Pie Chart.
35   */
36  public class TethysUISwingPieChart
37          extends TethysUICorePieChart {
38      /**
39       * The Node.
40       */
41      private final TethysUISwingNode theNode;
42  
43      /**
44       * The dataSet.
45       */
46      private final DefaultPieDataset<String> theDataSet;
47  
48      /**
49       * The chart.
50       */
51      private final JFreeChart theChart;
52  
53      /**
54       * The panel.
55       */
56      private final ChartPanel thePanel;
57  
58      /**
59       * Constructor.
60       *
61       * @param pFactory the Gui factory
62       */
63      TethysUISwingPieChart(final TethysUICoreFactory<?> pFactory) {
64          /* initialise underlying class */
65          super(pFactory);
66  
67          /* Create the dataSet */
68          theDataSet = new DefaultPieDataset<>();
69  
70          /* Create the chart */
71          theChart = ChartFactory.createPieChart(
72                  null,
73                  theDataSet,
74                  true,
75                  true,
76                  false);
77          @SuppressWarnings("unchecked") final PiePlot<String> myPlot = (PiePlot<String>) theChart.getPlot();
78          myPlot.setStartAngle(0);
79          myPlot.setToolTipGenerator((pDataSet, pKey) -> getToolTip((String) pKey));
80  
81          /* Create the panel */
82          thePanel = new ChartPanel(theChart);
83          thePanel.setOpaque(true);
84          thePanel.addChartMouseListener(new ChartMouseListener() {
85              @Override
86              public void chartMouseMoved(final ChartMouseEvent e) {
87                  /* NoOp */
88              }
89  
90              @Override
91              public void chartMouseClicked(final ChartMouseEvent e) {
92                  final ChartEntity entity = e.getEntity();
93                  if (entity instanceof PieSectionEntity section) {
94                      selectSection((String) section.getSectionKey());
95                  }
96              }
97          });
98  
99          /* Create the node */
100         theNode = new TethysUISwingNode(thePanel);
101     }
102 
103     @Override
104     public TethysUISwingNode getNode() {
105         return theNode;
106     }
107 
108     @Override
109     public void setVisible(final boolean pVisible) {
110         theNode.setVisible(pVisible);
111     }
112 
113     @Override
114     public void setEnabled(final boolean pEnabled) {
115         thePanel.setEnabled(pEnabled);
116     }
117 
118     @Override
119     public void setPreferredWidth(final Integer pWidth) {
120         theNode.setPreferredWidth(pWidth);
121     }
122 
123     @Override
124     public void setPreferredHeight(final Integer pHeight) {
125         theNode.setPreferredHeight(pHeight);
126     }
127 
128     @Override
129     public void updatePieChart(final TethysUIPieChartData pData) {
130         /* Update underlying data */
131         super.updatePieChart(pData);
132 
133         /* Set the chart title */
134         theChart.setTitle(pData.getTitle());
135 
136         /* Declare changes */
137         theChart.fireChartChanged();
138     }
139 
140     @Override
141     protected void resetData() {
142         /* Clear existing data  */
143         theDataSet.clear();
144 
145         /* Clear underlying data  */
146         super.resetData();
147     }
148 
149     @Override
150     protected void createSlice(final TethysUIPieChartSection pSection) {
151         /* Create the slice */
152         theDataSet.setValue(pSection.getName(), pSection.getValue().doubleValue());
153 
154         /* Add to underlying data  */
155         super.createSlice(pSection);
156     }
157 }