-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGraphGeneratorAllDevices_magstat_05042019.java
More file actions
423 lines (318 loc) · 14.8 KB
/
Copy pathGraphGeneratorAllDevices_magstat_05042019.java
File metadata and controls
423 lines (318 loc) · 14.8 KB
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
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
package de.gsi.cs.co.ap.app.magstat.utils;
import java.text.DecimalFormat;
import java.text.ParseException;
import java.util.regex.Pattern;
// import cern.accsoft.commons.domain.zones.AcceleratorZone;
import javafx.animation.AnimationTimer;
import javafx.beans.binding.Bindings;
import javafx.beans.property.DoubleProperty;
import javafx.beans.property.SimpleDoubleProperty;
import javafx.beans.value.ChangeListener;
import javafx.beans.value.ObservableValue;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.geometry.Insets;
import javafx.geometry.Pos;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.scene.chart.CategoryAxis;
import javafx.scene.chart.NumberAxis;
import javafx.scene.chart.ScatterChart;
import javafx.scene.chart.XYChart;
import javafx.scene.control.Button;
import javafx.scene.control.ButtonBar;
// import javafx.scene.control.ComboBox;
import javafx.scene.control.Label;
import javafx.scene.control.Separator;
import javafx.scene.control.TextField;
import javafx.scene.input.MouseEvent;
import javafx.scene.input.ScrollEvent;
import javafx.scene.layout.GridPane;
import javafx.scene.layout.HBox;
import javafx.scene.layout.Priority;
import javafx.scene.layout.Region;
import javafx.scene.layout.VBox;
import javafx.stage.Modality;
import javafx.stage.Stage;
import javafx.stage.WindowEvent;
import javafx.util.StringConverter;
// for library loggers
// import org.slf4j.Logger;
// import org.slf4j.LoggerFactory;
// for application loggers
// import de.gsi.cs.co.ap.common.gui.elements.logger.AppLogger;
/**
* @author fschirru
*/
public class GraphGeneratorAllDevices {
// You can choose a logger (needed imports are given in the import section as comments):
// for libraries:
// private static final Logger LOGGER = LoggerFactory.getLogger(GraphGenerator.class);
// for applications:
// private static final AppLogger LOGGER = AppLogger.getLogger();
private final static GraphGeneratorAllDevices INSTANCE = new GraphGeneratorAllDevices();
private final Button graph_zoom_bt = new Button("Zoom");
private final Button graph_exit_bt = new Button("Close");
private final Button graph_reset_bt = new Button("Reset");
private final ButtonBar button_bar = new ButtonBar();
// private final ComboBox<AcceleratorZone> acceleratorZone_cb = new ComboBox<>();
private final DoubleProperty yMin_dp = new SimpleDoubleProperty(0);
private final DoubleProperty yMax_dp = new SimpleDoubleProperty(0);
private final GridPane gridpane = new GridPane();
// private final GridPane gridpaneButton = new GridPane();
private final Group root = new Group();
private final Label acceleratorZone_lb = new Label("Manual Zoom: ");
private final Label yMin_lb = new Label();
private final Label yMax_lb = new Label();
private final Scene scene = new Scene(root);
private final Separator hSeparator = new Separator();
private final Stage graphStage = new Stage();
private final TextField yMin_tf = new TextField();
private final TextField yMax_tf = new TextField();
private final HBox hBox = new HBox();
private final VBox vBox = new VBox();
private final CategoryAxis xAxis = new CategoryAxis();
private final NumberAxis yAxis = new NumberAxis();
private final ScatterChart<String, Number> chart_sc = new ScatterChart<>(xAxis, yAxis);
private final XYChart.Series<String, Number> series = new XYChart.Series<>();
private final DecimalFormat df = new DecimalFormat("#0.000000");
private final Pattern pattern = Pattern.compile("[\\-+]?\\d{0,3}([\\.]\\d{0,6})?");
private final AnimationTimer animationTimer;
private final ObservableList<String> xAxisNames = FXCollections.observableArrayList();
private int numberOfDevices = 0;
private final double SCALE_DELTA = 2;
private final NiceAxisScale nsa = NiceAxisScale.getInstance();
GraphGeneratorAllDevices() {
animationTimer = new AnimationTimer() {
@Override
public void handle(final long now) {
addDataToSeries();
}
};
yMin_tf.textProperty().addListener((observable, oldValue, newValue) -> {
if (newValue.equals("") || newValue.equals("-")) {
yMin_dp.set(Double.parseDouble("0"));
} else {
if (!newValue.matches(pattern.toString())) {
yMin_tf.setText(oldValue);
// yMin_dp.set(Double.parseDouble("0"));
} else {
yMin_dp.set(Double.parseDouble(newValue));
}
}
});
yMax_tf.textProperty().addListener(new ChangeListener<String>() {
@Override
public void changed(final ObservableValue<? extends String> observable, final String oldValue,
final String newValue) {
if (newValue.equals("") || newValue.equals("-")) {
yMax_dp.set(Double.parseDouble("0"));
} else {
if (!newValue.matches(pattern.toString())) {
yMax_tf.setText(oldValue);
// yMax_dp.set(Double.parseDouble("0"));
} else {
yMax_dp.set(Double.parseDouble(newValue));
}
}
}
});
xAxis.setLabel("Device");
yAxis.setLabel("\u0394" + "I (A)");
yAxis.setTickLabelFormatter(new StringConverter<Number>() {
@Override
public String toString(final Number number) {
return df.format(number.doubleValue());
}
@Override
public Number fromString(final String string) {
try {
return df.parse(string);
} catch (final ParseException e) {
e.printStackTrace();
return 0;
}
}
});
chart_sc.getStylesheets().add(this.getClass().getResource("/de/gsi/cs/co/ap/app/magstat/ChartStyle.css").toExternalForm());
// +-chart_sc.getStyleClass().add("chart-symbol");
chart_sc.setTitle("");
// chart_sc.getStylesheets().add(this.getClass().getResource("../TemplateApp.css").toExternalForm());
// chart_sc.setId("pippo");
chart_sc.setPrefWidth(1500);
// chart_sc.setMaxWidth(1400);
chart_sc.setPrefHeight(800);
chart_sc.setStyle("-fx-font-size: 11px;");
chart_sc.getXAxis().setTickLabelRotation(270);
chart_sc.setAnimated(false);
chart_sc.setOnScroll(new EventHandler<ScrollEvent>() {
@Override
public void handle(final ScrollEvent event) {
event.consume();
if (event.getDeltaY() == 0) {
return;
}
// final NumberAxis yAxis = (NumberAxis) chart_sc.getYAxis();
yAxis.setAutoRanging(false);
// yAxis.setLowerBound(0);
// yAxis.setLowerBound(yAxis.getLowerBound() / 2);
// yAxis.setUpperBound(yAxis.getUpperBound() / 2);
// yAxis.setUpperBound(20);
// yAxis.setTickUnit(Math.abs(yAxis.getLowerBound() - yAxis.getUpperBound()) / 10);
// yAxis.setAutoRanging(true);
// final double scaleFactor = event.getDeltaY() > 0 ? SCALE_DELTA : 1 / SCALE_DELTA;
// chart_sc.setScaleX(chart_sc.getScaleX() * scaleFactor);
// chart_sc.setScaleY(chart_sc.getScaleY() * scaleFactor);
if (event.getDeltaY() > 0) {
nsa.setAxisValues(yAxis.getLowerBound() / SCALE_DELTA, yAxis.getUpperBound() / SCALE_DELTA, 20);
} else {
nsa.setAxisValues(yAxis.getLowerBound() * SCALE_DELTA, yAxis.getUpperBound() * SCALE_DELTA, 20);
}
// nsa.setAxisValues(yAxis.getLowerBound() / 2, yAxis.getUpperBound() / 2, 10);
// System.out.println(nsa.getNiceMax());
// System.out.println(nsa.getNiceMin());
// System.out.println(nsa.getTickSpacing());
yAxis.setLowerBound(nsa.getNiceMin());
yAxis.setUpperBound(nsa.getNiceMax());
// yAxis.setUpperBound(20);
yAxis.setTickUnit(nsa.getTickSpacing());
}
});
chart_sc.setOnMousePressed(new EventHandler<MouseEvent>() {
@Override
public void handle(final MouseEvent event) {
if (event.getClickCount() == 2) {
chart_sc.setScaleX(1.0);
chart_sc.setScaleY(1.0);
}
}
});
hSeparator.setStyle("-fx-border-style: solid; -fx-border-width: 0.1px;");
graphStage.setTitle("Graph");
graphStage.setResizable(false);
graphStage.initModality(Modality.APPLICATION_MODAL);
graphStage.setOnCloseRequest(new EventHandler<WindowEvent>() {
@Override
public void handle(final WindowEvent event) {
event.consume();
}
});
graphStage.setScene(scene);
graph_exit_bt.setPrefWidth(100);
graph_exit_bt.setOnAction(new EventHandler<ActionEvent>() {
@Override
public void handle(final ActionEvent event) {
// MagnetDataJoiner.getInstance().getData().get(0).setCurrent_diff(-100);
animationTimer.stop();
graphStage.close();
}
});
graph_reset_bt.setPrefWidth(100);
graph_reset_bt.setOnAction(new EventHandler<ActionEvent>() {
@Override
public void handle(final ActionEvent event) {
yMin_tf.clear();
yMax_tf.clear();
yAxis.setAutoRanging(true);
}
});
graph_zoom_bt.disableProperty()
.bind(Bindings.isEmpty(yMin_tf.textProperty()).or(Bindings.isEmpty(yMax_tf.textProperty()))
.or(Bindings.when(yMax_dp.lessThanOrEqualTo(yMin_dp)).then(true).otherwise(false)));
graph_zoom_bt.setPrefWidth(100);
graph_zoom_bt.setOnAction(new EventHandler<ActionEvent>() {
@Override
public void handle(final ActionEvent event) {
yAxis.setAutoRanging(false);
nsa.setAxisValues(Double.parseDouble(yMin_tf.getText()), Double.parseDouble(yMax_tf.getText()), 20);
yAxis.setLowerBound(nsa.getNiceMin());
yAxis.setUpperBound(nsa.getNiceMax());
yAxis.setTickUnit(nsa.getTickSpacing());
}
});
yMin_tf.setPrefWidth(120);
yMax_tf.setPrefWidth(120);
yMin_lb.setText("Y (min):");
// yMin_lb.setPrefWidth(100);
yMax_lb.setText("Y (max):");
// yMax_lb.setPrefWidth(100);
gridpane.setHgap(10);
// gridpane.setVgap(10);
button_bar.getButtons().addAll(graph_zoom_bt, graph_reset_bt, graph_exit_bt);
gridpane.add(acceleratorZone_lb, 0, 0);
// gridpane.add(acceleratorZone_cb, 1, 0);
gridpane.add(yMin_lb, 2, 0);
gridpane.add(yMin_tf, 3, 0);
gridpane.add(yMax_lb, 4, 0);
gridpane.add(yMax_tf, 5, 0);
// gridpaneButton.add(graph_zoom_bt, 0, 0);
// gridpaneButton.add(graph_reset_bt, 1, 0);
// gridpaneButton.add(graph_exit_bt, 2, 0);
// button_bar.setPrefWidth(400);
// gridpane.add(button_bar, 4, 0);
// GridPane.setHalignment(button_bar, HPos.RIGHT);
// ridpane.add(graph_exit_bt, 5, 0);
final Region filler = new Region();
HBox.setHgrow(filler, Priority.ALWAYS);
hBox.setSpacing(20);
hBox.getChildren().addAll(gridpane, filler, button_bar);
gridpane.setAlignment(Pos.CENTER_LEFT);
// gridpaneButton.setAlignment(Pos.CENTER_RIGHT);
// vBox.setAlignment(Pos.CENTER_LEFT);
vBox.setSpacing(10);
// vBox.setPrefWidth(1200);
vBox.setPadding(new Insets(10, 10, 10, 10));
vBox.getChildren().addAll(chart_sc, hSeparator, hBox);
root.getChildren().add(vBox);
}
public static GraphGeneratorAllDevices getInstance() {
return INSTANCE;
}
public void initGraph() {
animationTimer.stop();
numberOfDevices = MagnetDataJoiner.getInstance().getData().size();
xAxisNames.clear();
series.getData().clear();
chart_sc.getData().clear();
for (int i = 0; i < numberOfDevices; i++) {
xAxisNames.add(MagnetDataJoiner.getInstance().getData().get(i).getId());
series.getData()
.add(new XYChart.Data<String, Number>(MagnetDataJoiner.getInstance().getData().get(i).getId(),
MagnetDataJoiner.getInstance().getData().get(i).getCurrent_diff()));
}
series.setName("\u0394" + "I (A)");
// chart_sc.setLegendSide(Side.RIGHT);
chart_sc.setLegendVisible(false);
chart_sc.getData().add(series);
xAxis.setCategories(xAxisNames);
for (final XYChart.Data<String, Number> data : series.getData()) {
if (data.getXValue().contains("MU")) {
data.getNode().lookup(".chart-symbol").setStyle("-fx-background-color: green;");
}
if (data.getXValue().contains("QD") || data.getXValue().contains("QT")) {
data.getNode().lookup(".chart-symbol").setStyle("-fx-background-color: yellow;");
}
if (data.getXValue().contains("KY") || data.getXValue().contains("KX")) {
data.getNode().lookup(".chart-symbol").setStyle("-fx-background-color: blue;");
}
if (data.getXValue().contains("KS")) {
data.getNode().lookup(".chart-symbol").setStyle("-fx-background-color: red;");
}
}
animationTimer.start();
}
public void setGraphTitle(final String title) {
chart_sc.setTitle(title);
}
public void showGraphStage() {
graphStage.show();
graph_exit_bt.requestFocus();
}
private void addDataToSeries() {
for (int i = 0; i < MagnetDataJoiner.getInstance().getData().size(); i++) {
series.getData().get(i).setYValue(MagnetDataJoiner.getInstance().getData().get(i).getCurrent_diff());
}
}
}