1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
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
35
36 public class TethysUISwingPieChart
37 extends TethysUICorePieChart {
38
39
40
41 private final TethysUISwingNode theNode;
42
43
44
45
46 private final DefaultPieDataset<String> theDataSet;
47
48
49
50
51 private final JFreeChart theChart;
52
53
54
55
56 private final ChartPanel thePanel;
57
58
59
60
61
62
63 TethysUISwingPieChart(final TethysUICoreFactory<?> pFactory) {
64
65 super(pFactory);
66
67
68 theDataSet = new DefaultPieDataset<>();
69
70
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
82 thePanel = new ChartPanel(theChart);
83 thePanel.setOpaque(true);
84 thePanel.addChartMouseListener(new ChartMouseListener() {
85 @Override
86 public void chartMouseMoved(final ChartMouseEvent e) {
87
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
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
131 super.updatePieChart(pData);
132
133
134 theChart.setTitle(pData.getTitle());
135
136
137 theChart.fireChartChanged();
138 }
139
140 @Override
141 protected void resetData() {
142
143 theDataSet.clear();
144
145
146 super.resetData();
147 }
148
149 @Override
150 protected void createSlice(final TethysUIPieChartSection pSection) {
151
152 theDataSet.setValue(pSection.getName(), pSection.getValue().doubleValue());
153
154
155 super.createSlice(pSection);
156 }
157 }