forked from fair-acc/chart-fx
-
Notifications
You must be signed in to change notification settings - Fork 0
/
MultipleAxesSample.java
177 lines (152 loc) · 7.27 KB
/
MultipleAxesSample.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
package de.gsi.chart.samples;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.ScheduledFuture;
import java.util.concurrent.TimeUnit;
import javafx.application.Application;
import javafx.application.ConditionalFeature;
import javafx.application.Platform;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.CheckBox;
import javafx.scene.layout.BorderPane;
import javafx.scene.layout.HBox;
import javafx.stage.Stage;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import de.gsi.chart.Chart;
import de.gsi.chart.XYChart;
import de.gsi.chart.axes.Axis;
import de.gsi.chart.axes.spi.DefaultNumericAxis;
import de.gsi.chart.plugins.EditAxis;
import de.gsi.chart.plugins.Zoomer;
import de.gsi.chart.renderer.Renderer;
import de.gsi.chart.renderer.spi.ErrorDataSetRenderer;
import de.gsi.chart.ui.geometry.Side;
import de.gsi.dataset.testdata.spi.CosineFunction;
import de.gsi.dataset.testdata.spi.GaussFunction;
import de.gsi.dataset.testdata.spi.RandomWalkFunction;
import de.gsi.dataset.testdata.spi.SineFunction;
import de.gsi.dataset.utils.ProcessingProfiler;
public class MultipleAxesSample extends Application {
private static final Logger LOGGER = LoggerFactory.getLogger(MultipleAxesSample.class);
private static final int N_SAMPLES = 10_000; // default: 10000
private static final long UPDATE_DELAY = 1000; // [ms]
private static final long UPDATE_PERIOD = 1000; // [ms]
private final ScheduledExecutorService timer = Executors.newScheduledThreadPool(1);
private ScheduledFuture<?> scheduledFuture;
@Override
public void start(final Stage primaryStage) {
if (Platform.isSupported(ConditionalFeature.TRANSPARENT_WINDOW)) {
Application.setUserAgentStylesheet(Chart.class.getResource("solid-pick.css").toExternalForm());
}
ProcessingProfiler.setVerboseOutputState(true);
ProcessingProfiler.setLoggerOutputState(true);
ProcessingProfiler.setDebugState(false);
final BorderPane root = new BorderPane();
final Scene scene = new Scene(root, 800, 600);
final DefaultNumericAxis xAxis1 = new DefaultNumericAxis("x axis");
xAxis1.setAnimated(false);
final DefaultNumericAxis yAxis1 = new DefaultNumericAxis("y axis (random)");
yAxis1.setAnimated(false);
final DefaultNumericAxis yAxis2 = new DefaultNumericAxis("y axis (sine/cosine)");
// yAxis2.setSide(Side.LEFT); // unusual but possible case
yAxis2.setSide(Side.RIGHT);
yAxis2.setAnimated(false);
final DefaultNumericAxis yAxis3 = new DefaultNumericAxis("y axis (gauss)");
yAxis3.setSide(Side.RIGHT);
yAxis3.invertAxis(true);
yAxis3.setAnimated(false);
final XYChart chart = new XYChart(xAxis1, yAxis1);
// N.B. it's important to set secondary axis on the 2nd renderer before
// adding the renderer to the chart
final ErrorDataSetRenderer errorRenderer2 = new ErrorDataSetRenderer();
errorRenderer2.getAxes().add(yAxis2);
final ErrorDataSetRenderer errorRenderer3 = new ErrorDataSetRenderer();
errorRenderer3.getAxes().add(yAxis3);
chart.getRenderers().addAll(errorRenderer2, errorRenderer3);
final Zoomer zoom = new Zoomer();
// add axes that shall be excluded from the zoom action
zoom.omitAxisZoomList().add(yAxis3);
// alternatively (uncomment):
// Zoomer.setOmitZoom(yAxis3, true);
chart.getPlugins().add(zoom);
chart.getToolBar().getChildren().add(new MyZoomCheckBox(zoom, yAxis3));
chart.getPlugins().add(new EditAxis());
final Button newDataSet = new Button("new DataSet");
newDataSet.setOnAction(
evt -> Platform.runLater(getTask(chart.getRenderers().get(0), errorRenderer2, errorRenderer3)));
final Button startTimer = new Button("timer");
startTimer.setOnAction(evt -> {
if (scheduledFuture == null || scheduledFuture.isCancelled()) {
scheduledFuture = timer.scheduleAtFixedRate(
getTask(chart.getRenderers().get(0), errorRenderer2, errorRenderer3),
MultipleAxesSample.UPDATE_DELAY, MultipleAxesSample.UPDATE_PERIOD, TimeUnit.MILLISECONDS);
} else {
scheduledFuture.cancel(false);
}
});
root.setTop(new HBox(newDataSet, startTimer));
// generate the first set of data
getTask(chart.getRenderers().get(0), errorRenderer2, errorRenderer3).run();
long startTime = ProcessingProfiler.getTimeStamp();
ProcessingProfiler.getTimeDiff(startTime, "adding data to chart");
startTime = ProcessingProfiler.getTimeStamp();
root.setCenter(chart);
ProcessingProfiler.getTimeDiff(startTime, "adding chart into StackPane");
startTime = ProcessingProfiler.getTimeStamp();
primaryStage.setTitle(this.getClass().getSimpleName());
primaryStage.setScene(scene);
primaryStage.setOnCloseRequest(evt -> Platform.exit());
primaryStage.show();
ProcessingProfiler.getTimeDiff(startTime, "for showing");
}
public static Runnable getTask(final Renderer renderer1, final Renderer renderer2, final Renderer renderer3) {
return new Runnable() {
private int updateCount;
@Override
public void run() {
Platform.runLater(() -> {
// setAll in order to implicitly clear previous list of
// 'old' data sets
renderer1.getDatasets().setAll(new RandomWalkFunction("random walk", MultipleAxesSample.N_SAMPLES));
renderer2.getDatasets().setAll(new CosineFunction("cosy", MultipleAxesSample.N_SAMPLES, true),
new SineFunction("siny", MultipleAxesSample.N_SAMPLES, true));
renderer3.getDatasets().setAll(new GaussFunction("gaussy", MultipleAxesSample.N_SAMPLES));
if (updateCount % 10 == 0) {
LOGGER.atInfo().log("update iteration #" + updateCount);
}
updateCount++;
});
}
};
}
/**
* @param args the command line arguments
*/
public static void main(final String[] args) {
Application.launch(args);
}
private class MyZoomCheckBox extends CheckBox {
/**
* @param zoom the zoom interactor
* @param axis to be synchronised
*/
public MyZoomCheckBox(Zoomer zoom, Axis axis) {
super("enable zoom for axis '" + axis.getName() + "'");
this.setSelected(!zoom.omitAxisZoomList().contains(axis) || Zoomer.isOmitZoom(axis));
this.selectedProperty().addListener((obj, o, n) -> {
if (n.equals(o)) {
return;
}
if (n.booleanValue()) {
zoom.omitAxisZoomList().remove(axis);
Zoomer.setOmitZoom(axis, false); // alternative implementation
} else {
zoom.omitAxisZoomList().add(axis);
Zoomer.setOmitZoom(axis, true); // alternative implementation
}
});
}
}
}