-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmenusandcharts.java
193 lines (153 loc) · 6.06 KB
/
menusandcharts.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
package menusandcharts;
import java.net.URL;
import java.util.ResourceBundle;
import javafx.beans.binding.Bindings;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.Node;
import javafx.scene.chart.BarChart;
import javafx.scene.chart.CategoryAxis;
import javafx.scene.chart.NumberAxis;
import javafx.scene.chart.PieChart;
import javafx.scene.chart.XYChart;
import javafx.scene.control.ContextMenu;
import javafx.scene.control.MenuItem;
import javafx.scene.control.Tooltip;
import javafx.scene.input.MouseButton;
import javafx.scene.input.MouseEvent;
import javafx.scene.layout.BorderPane;
import javafx.util.Duration;
/**
* FXML Controller class
*
* @author spangsberg
*/
class menusandcharts implements Initializable {
@FXML
private BorderPane borderPane;
/**
* Initializes the controller class.
*/
@Override
public void initialize(URL url, ResourceBundle rb) {
borderPane.setCenter(buildPieChart());
}
@FXML
private void handleShowBarChart() {
borderPane.setCenter(buildBarChart());
}
@FXML
private void handleShowPieChart() {
borderPane.setCenter(buildPieChart());
}
private BarChart buildBarChart() {
CategoryAxis xAxis = new CategoryAxis();
xAxis.setLabel("Most Popular Programming Language");
NumberAxis yAxis = new NumberAxis();
yAxis.setLabel("# of developers x 1000");
BarChart barChart = new BarChart(xAxis, yAxis);
XYChart.Series dataSeries1 = new XYChart.Series();
dataSeries1.setName("Popular programming languages rated by GitHub");
dataSeries1.getData().add(new XYChart.Data("JavaScript", 2300));
dataSeries1.getData().add(new XYChart.Data("Python", 1000));
dataSeries1.getData().add(new XYChart.Data("Java", 986));
dataSeries1.getData().add(new XYChart.Data("Ruby", 870));
dataSeries1.getData().add(new XYChart.Data("C++", 413));
dataSeries1.getData().add(new XYChart.Data("C#", 326));
barChart.getData().add(dataSeries1);
return barChart;
}
private PieChart buildPieChart() {
//Preparing ObservbleList object
ObservableList<PieChart.Data> pieChartData = FXCollections.observableArrayList(
new PieChart.Data("JavaScript", 65),
new PieChart.Data("HTML/CSS", 56),
new PieChart.Data("Python", 48),
new PieChart.Data("SQL", 47),
new PieChart.Data("Java", 35),
new PieChart.Data("TypeScript", 30),
new PieChart.Data("C#", 28),
new PieChart.Data("C++", 24),
new PieChart.Data("PHP", 22),
new PieChart.Data("C#", 21));
PieChart pieChart = new PieChart(pieChartData); //Creating a Pie chart
//attach tooltips
createToolTips(pieChart);
pieChart.setTitle("Most Popular Programming Language %"); //Setting the title of the Pie chart
pieChart.setClockwise(true); //setting the direction to arrange the data
pieChart.setLabelLineLength(50); //Setting the length of the label line
pieChart.setLabelsVisible(true); //Setting the labels of the pie chart visible
pieChart.setLegendVisible(false);
pieChart.setStartAngle(180);
//bind value and label on each pie slice to reflect changes
pieChartData.forEach(data ->
data.nameProperty().bind(Bindings.concat(data.getName(), " ", data.pieValueProperty(), " ")
));
ContextMenu contextMenu = new ContextMenu(); //create context menu
MenuItem miSwitchToBarChart = new MenuItem("Switch to Bar Chart");
contextMenu.getItems().add(miSwitchToBarChart);
//Add event handler to display context menu
pieChart.addEventHandler(MouseEvent.MOUSE_CLICKED,
new EventHandler<MouseEvent>() {
@Override
public void handle(MouseEvent event) {
if (event.getButton() == MouseButton.SECONDARY) {
contextMenu.show(pieChart, event.getScreenX(), event.getScreenY());
}
}
});
//Before Java 8
//Add event handler to change chart type (anonymous inner class)
miSwitchToBarChart.setOnAction(new EventHandler<ActionEvent>() {
@Override
public void handle(ActionEvent event) {
borderPane.setCenter(buildBarChart());
}
});
//Java 8 and newer (lambda expression)
miSwitchToBarChart.setOnAction(event -> { borderPane.setCenter(buildBarChart()); });
return pieChart;
}
/**
*
*/
@FXML
private void handleClose() {
System.exit(0);
}
/**
*
*/
@FXML
private void handleUpdatePieData() {
Node node = borderPane.getCenter();
if (node instanceof PieChart)
{
PieChart pc = (PieChart) node;
double value = pc.getData().get(2).getPieValue();
pc.getData().get(2).setPieValue(value * 1.10);
createToolTips(pc);
}
}
/**
* Creates tooltips for all data entries
* @param pc
*/
private void createToolTips(PieChart pc) {
for (PieChart.Data data: pc.getData()) {
String msg = Double.toString(data.getPieValue());
Tooltip tp = new Tooltip(msg);
tp.setShowDelay(Duration.seconds(0));
Tooltip.install(data.getNode(), tp);
//update tooltip data when changed
data.pieValueProperty().addListener((observable, oldValue, newValue) ->
{
tp.setText(newValue.toString());
});
}
}
}