Review of other XY charts

The rest of the charts are just variations of a line chart. You can use the same code to show most of them, like in the next screenshot:

There are these interesting points in the code for this example:

  • We are using CategoryAxis for months to show a new API and because bar charts don't work with a horizontal NumberAxis
  • You need to provide a separate instance of axes and series for each chart
  • Charts monitor their data through binding, so to add new data you just need to update the series lists
  • All new data will be animated

Refer to the following code snippet:

// chapter10/chart/XYChartsDemo.java

public void start(Stage stage) {
// we need a bit of lambdacraft to work with constructors
Supplier<CategoryAxis> supplierX = () -> {
return new CategoryAxis();
};
Supplier<NumberAxis> supplierY = () -> {
NumberAxis axisY = new NumberAxis();
axisY.setLabel("Average Temperature (C)");
return axisY;
};
TilePane root = new TilePane(2, 2);

Stream.of(
// API is the same, only class name changes:
new AreaChart<>(supplierX.get(), supplierY.get()),
new BarChart<>(supplierX.get(), supplierY.get()),
new LineChart<>(supplierX.get(), supplierY.get()),
new ScatterChart<>(supplierX.get(), supplierY.get()),
new StackedAreaChart<>(supplierX.get(), supplierY.get()),
new StackedBarChart<>(supplierX.get(), supplierY.get())
).forEach((chart)-> {
chart.setTitle(chart.getClass().getSimpleName());
chart.setLegendVisible(false);
chart.setPrefSize(350, 280);
XYChart.Series<String, Number> seriesMoscow = new XYChart.Series<>(
"Moscow",
FXCollections.observableArrayList(
new XYChart.Data<>("January", -7),
new XYChart.Data<>("February", -6),
new XYChart.Data<>("March", 0),
new XYChart.Data<>("April", 7),
new XYChart.Data<>("May", 15),
new XYChart.Data<>("June", 18),
new XYChart.Data<>("July", 21),
new XYChart.Data<>("August", 19),
new XYChart.Data<>("September", 13),
new XYChart.Data<>("October", 6),
new XYChart.Data<>("November", 1),
new XYChart.Data<>("December", -4)
));

XYChart.Series<String, Number> seriesLondon = new XYChart.Series<>(
"London",
FXCollections.observableArrayList(
new XYChart.Data<>("January", 7),
new XYChart.Data<>("February", 7),
new XYChart.Data<>("March", 9),
new XYChart.Data<>("April", 12),
new XYChart.Data<>("May", 15),
new XYChart.Data<>("June", 18),
new XYChart.Data<>("July", 20),
new XYChart.Data<>("August", 20),
new XYChart.Data<>("September", 17),
new XYChart.Data<>("October", 13),
new XYChart.Data<>("November", 10),
new XYChart.Data<>("December", 7)
));
chart.getData().addAll(seriesLondon, seriesMoscow);
chart.setOnMouseClicked((e) -> {
// animated update demo
seriesMoscow.getData().add(
new XYChart.Data<>("Nonexistembr", Math.random()*15));
});
root.getChildren().add(chart);
});
stage.setScene(new Scene(root, 720, 600));
stage.show();
}
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.191.147.77