-
Notifications
You must be signed in to change notification settings - Fork 89
/
CategoryAxisSample.java
98 lines (83 loc) · 3.62 KB
/
CategoryAxisSample.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
package io.fair_acc.sample.chart;
import java.text.DateFormatSymbols;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Locale;
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.AxisLabelOverlapPolicy;
import io.fair_acc.chartfx.axes.spi.CategoryAxis;
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.ErrorDataSetRenderer;
import io.fair_acc.dataset.spi.DefaultErrorDataSet;
import io.fair_acc.dataset.testdata.spi.RandomDataGenerator;
/**
* @author rstein
*/
public class CategoryAxisSample extends ChartSample {
private static final int N_SAMPLES = 30;
@Override
public Node getChartPanel(final Stage primaryStage) {
final StackPane root = new StackPane();
final CategoryAxis xAxis = new CategoryAxis("months");
// xAxis.setTickLabelRotation(90);
// alt:
xAxis.setOverlapPolicy(AxisLabelOverlapPolicy.SHIFT_ALT);
xAxis.setMaxMajorTickLabelCount(N_SAMPLES + 1);
final DefaultNumericAxis yAxis = new DefaultNumericAxis("yAxis");
final XYChart lineChartPlot = new XYChart(xAxis, yAxis);
// set them false to make the plot faster
lineChartPlot.setAnimated(false);
lineChartPlot.getRenderers().clear();
// lineChartPlot.getRenderers().add(new ReducingLineRenderer());
final ErrorDataSetRenderer renderer = new ErrorDataSetRenderer();
renderer.setPolyLineStyle(LineStyle.NORMAL);
renderer.setPolyLineStyle(LineStyle.HISTOGRAM);
lineChartPlot.getRenderers().add(renderer);
lineChartPlot.legendVisibleProperty().set(true);
lineChartPlot.getPlugins().add(new ParameterMeasurements());
lineChartPlot.getPlugins().add(new EditAxis());
final Zoomer zoomer = new Zoomer();
// zoomer.setSliderVisible(false);
// zoomer.setAddButtonsToToolBar(false);
lineChartPlot.getPlugins().add(zoomer);
final DefaultErrorDataSet dataSet = new DefaultErrorDataSet("myData");
final DateFormatSymbols dfs = new DateFormatSymbols(Locale.ENGLISH);
final List<String> categories = new ArrayList<>(Arrays.asList(Arrays.copyOf(dfs.getShortMonths(), 12)));
for (int i = categories.size(); i < CategoryAxisSample.N_SAMPLES; i++) {
categories.add("Month" + (i + 1));
}
// setting the category via axis forces the axis' category
// N.B. disable this if you want to use the data set's categories
xAxis.setCategories(categories);
double y = 0;
for (int n = 0; n < CategoryAxisSample.N_SAMPLES; n++) {
y += RandomDataGenerator.random() - 0.5;
final double ex = 0.0;
final double ey = 0.1;
dataSet.add(n, y, ex, ey);
dataSet.addDataLabel(n, "SpecialCategory#" + n);
}
// setting the axis categories to null forces the first data set's
// category
// enable this if you want to use the data set's categories
// xAxis.setCategories(null);
lineChartPlot.getDatasets().add(dataSet);
root.getChildren().add(lineChartPlot);
return root;
}
/**
* @param args the command line arguments
*/
public static void main(final String[] args) {
Application.launch(args);
}
}