Java Reference
In-Depth Information
All layout changes so far are done programmatically. It is also possible, and often recommended, to style
applications in general and charts in particular using a CSS style sheet.
We remove the layout changes from the Java code and add a style sheet containing some layout instructions.
Listing 8-3 shows the modified code of the start() method and Listing 8-4 contains the style sheet we added.
Listing 8-3. Remove Programmatic Layout Instructions
public void start(Stage primaryStage) {
PieChart pieChart = new PieChart();
pieChart.setData(getChartData());
pieChart.titleProperty().set("Tiobe index");
primaryStage.setTitle("PieChart");
StackPane root = new StackPane();
root.getChildren().add(pieChart);
Scene scene = new Scene (root, 400, 250);
scene.getStylesheets().add("/chartappstyle.css");
primaryStage.setScene(scene);
primaryStage.show();
}
Listing 8-4. Style Sheet for PieChart Example
.chart {
-fx-clockwise: false;
-fx-pie-label-visible: true;
-fx-label-line-length: 5;
-fx-start-angle: 90;
-fx-legend-side: right;
}
.chart-pie-label {
-fx-font-size:9px;
}
.chart-content {
-fx-padding:1;
}
.default-color0.chart-pie {
-fx-pie-color:blue;
}
.chart-legend {
-fx-background-color: #f0e68c;
-fx-border-color: #696969;
-fx-border-width:1;
}
Running this code results in the output shown in Figure 8-5 .
 
Search WWH ::




Custom Search