Java Reference
In-Depth Information
Figure 8-12. Setting the gap between bars to one pixel
Clearly, this one line of code leads to a huge difference in readability.
Using the StackedBarChart
The StackedBarChart was added in JavaFX 2.1. The StackedBarChart displays data in bars just like the BarChart ,
but instead of rendering bars in the same category next to each other, the StackedBarChart shows the bars within the
same category on top of each other. This often makes it easier to inspect totals.
Typically, categories correspond with the common key values in the data series. As a consequence, in our
example the different years (2011, 2012, ... 2020) can be considered as categories. We can add these categories to the
xAxis , as follows:
IntStream.range(2011,2020).forEach(t -> xAxis.getCategories().add(String.valueOf(t)));
Apart from this, the only code change is replacing the BarChart with the StackedBarChart in code and in the
import statement. This leads to the code snippet in Listing 8-9.
Listing 8-9. Using a StackedBarChart Instead of a ScatterChart
public void start(Stage primaryStage) {
CategoryAxis xAxis = new CategoryAxis();
IntStream.range(2011,2020).forEach(t -> xAxis.getCategories().add(String.valueOf(t)));
NumberAxis yAxis = new NumberAxis();
StackedBarChart stackedBarChart = new StackedBarChart(xAxis, yAxis, getChartData());
stackedBarChart.setTitle("speculations");
primaryStage.setTitle("StackedBarChart example");
StackPane root = new StackPane();
root.getChildren().add(stackedBarChart);
Scene scene = new Scene(root, 400, 250);
primaryStage.setScene(scene);
primaryStage.show();
}
 
Search WWH ::




Custom Search