-
Notifications
You must be signed in to change notification settings - Fork 89
/
Histogram2DimSample.java
154 lines (131 loc) · 6.17 KB
/
Histogram2DimSample.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
package io.fair_acc.sample.chart;
import static io.fair_acc.dataset.spi.AbstractHistogram.HistogramOuterBounds.BINS_ALIGNED_WITH_BOUNDARY;
import java.util.Random;
import java.util.Timer;
import java.util.TimerTask;
import javafx.application.Application;
import javafx.scene.Node;
import javafx.scene.layout.StackPane;
import javafx.stage.Stage;
import io.fair_acc.chartfx.XYChart;
import io.fair_acc.chartfx.axes.spi.DefaultNumericAxis;
import io.fair_acc.chartfx.plugins.EditAxis;
import io.fair_acc.chartfx.plugins.ParameterMeasurements;
import io.fair_acc.chartfx.plugins.Zoomer;
import io.fair_acc.chartfx.renderer.LineStyle;
import io.fair_acc.chartfx.renderer.spi.ContourDataSetRenderer;
import io.fair_acc.chartfx.renderer.spi.ErrorDataSetRenderer;
import io.fair_acc.chartfx.renderer.spi.MetaDataRenderer;
import io.fair_acc.chartfx.ui.geometry.Side;
import io.fair_acc.chartfx.utils.FXUtils;
import io.fair_acc.dataset.spi.Histogram2;
public class Histogram2DimSample extends ChartSample {
private static final int UPDATE_DELAY = 1000; // [ms]
private static final int UPDATE_PERIOD = 100; // [ms]
private static final int UPDATE_N_SAMPLES = 10;
private static final int N_BINS_X = 120;
private static final int N_BINS_Y = 120;
private final Random rnd = new Random();
private final Histogram2 histogram1 = new Histogram2("hist1", N_BINS_X, 0.0, 20.0, N_BINS_Y, 0.0, 30.0, BINS_ALIGNED_WITH_BOUNDARY);
private final Histogram2 histogram2 = new Histogram2("hist2", N_BINS_X, 0.0, 20.0, N_BINS_Y, 0.0, 30.0, BINS_ALIGNED_WITH_BOUNDARY);
private int counter;
private void fillData() {
counter++;
final double angle = Math.PI / 4;
for (int i = 0; i < Histogram2DimSample.UPDATE_N_SAMPLES; i++) {
final double x0 = rnd.nextGaussian() * 0.5 + 5.0;
final double y0 = rnd.nextGaussian() * 0.5 + 5.0;
final double x = rnd.nextGaussian() * 1.5;
final double y = rnd.nextGaussian() * 0.5;
final double x1 = x * Math.sin(angle) + y * Math.cos(angle);
final double x2 = x * Math.cos(angle) - y * Math.sin(angle);
histogram1.fill(x0, y0);
histogram2.fill(x1 + 14.0, x2 + 20.0);
}
if (counter % 500 == 0) {
// reset distribution every now and then
counter = 0;
histogram1.reset();
histogram2.reset();
}
}
@Override
public Node getChartPanel(final Stage primaryStage) {
final StackPane root = new StackPane();
final DefaultNumericAxis xAxis = new DefaultNumericAxis("x-Axis");
xAxis.setAutoRanging(true);
xAxis.setAutoRangeRounding(false);
xAxis.setSide(Side.BOTTOM);
final DefaultNumericAxis yAxis = new DefaultNumericAxis("y-Axis");
yAxis.setAutoRanging(true);
yAxis.setAutoRangeRounding(false);
yAxis.setSide(Side.LEFT);
final DefaultNumericAxis zAxis = new DefaultNumericAxis("z Amplitude");
zAxis.setAnimated(false);
zAxis.setAutoRangeRounding(false);
zAxis.setAutoRanging(true);
zAxis.setSide(Side.RIGHT);
final DefaultNumericAxis yAxis1 = new DefaultNumericAxis("y-Axis x-Projection");
yAxis1.setLogAxis(false);
yAxis1.setAnimated(false);
yAxis1.setAutoRangeRounding(true);
yAxis1.setAutoRangePadding(2.0);
yAxis1.setAutoRanging(true);
yAxis1.setSide(Side.RIGHT);
final DefaultNumericAxis xAxis1 = new DefaultNumericAxis("x-Axis y-Projection");
xAxis1.setLogAxis(false);
xAxis1.setAnimated(false);
xAxis1.setAutoRangeRounding(true);
xAxis1.setAutoRangePadding(2.0);
xAxis1.setAutoRanging(true);
xAxis1.setSide(Side.TOP);
final XYChart chart = new XYChart(xAxis, yAxis);
chart.setAnimated(false);
final ContourDataSetRenderer heatMap = new ContourDataSetRenderer();
heatMap.getAxes().addAll(xAxis, yAxis, zAxis);
heatMap.getDatasets().addAll(histogram1, histogram2);
chart.getRenderers().set(0, heatMap);
final ErrorDataSetRenderer projectionRendererX = new ErrorDataSetRenderer();
projectionRendererX.getAxes().setAll(xAxis, yAxis1);
projectionRendererX.getDatasets().setAll(histogram1.getProjectionX(), histogram2.getProjectionX());
histogram1.getProjectionX().setStyle("dsIndex=0");
histogram2.getProjectionX().setStyle("dsIndex=1");
projectionRendererX.setPolyLineStyle(LineStyle.HISTOGRAM);
projectionRendererX.setPointReduction(false);
chart.getRenderers().add(projectionRendererX);
final ErrorDataSetRenderer projectionRendererY = new ErrorDataSetRenderer();
projectionRendererY.getAxes().setAll(xAxis1, yAxis);
projectionRendererY.getDatasets().setAll(histogram1.getProjectionY(), histogram2.getProjectionY());
histogram1.getProjectionY().setStyle("dsIndex=0");
histogram2.getProjectionY().setStyle("dsIndex=1");
projectionRendererY.setPolyLineStyle(LineStyle.HISTOGRAM);
projectionRendererY.setPointReduction(false);
projectionRendererY.setAssumeSortedData(false);
chart.getRenderers().add(projectionRendererY);
final MetaDataRenderer metaRenderer = new MetaDataRenderer(chart);
metaRenderer.getDatasets().addAll(histogram2, histogram1);
chart.getRenderers().add(metaRenderer);
chart.legendVisibleProperty().set(true);
chart.getPlugins().add(new ParameterMeasurements());
chart.getPlugins().add(new EditAxis());
final Zoomer zoomer = new Zoomer();
zoomer.setSliderVisible(false);
chart.getPlugins().add(zoomer);
root.getChildren().add(chart);
final Timer timer = new Timer("sample-update-timer", true);
timer.scheduleAtFixedRate(new TimerTask() {
@Override
public void run() {
fillData();
chart.invalidate();
}
}, Histogram2DimSample.UPDATE_DELAY, Histogram2DimSample.UPDATE_PERIOD);
return root;
}
/**
* @param args the command line arguments
*/
public static void main(final String[] args) {
Application.launch(args);
}
}