Java Reference
In-Depth Information
Some Modifications
Although the result of the simple example already looks good, we can tweak both the code and the rendering. First of
all, the example uses two lines of code for creating the
PieChart
and populating it with data:
PieChart pieChart = new PieChart();
pieChart.setData(getChartData());
Because
PieChart
has a single argument constructor as well, the preceding code snippets can be replaced
as follows.
PieChart pieChart = new PieChart(getChartData());
Apart from the properties defined on the abstract
Chart
class, a
PieChart
has the following properties.
BooleanProperty clockwise
ObjectProperty<ObservableList<PieChart.Data>>data
DoubleProperty labelLineLength
BooleanProperty labelsVisible
DoubleProperty startAngle
We covered the data property in the previous section. Some of the other properties are demonstrated in the next
code snippet. Listing 8-2 contains a modified version of the
start()
method.
Listing 8-2.
Modified Version of the PieChart Example
public void start(Stage primaryStage) {
PieChart pieChart = new PieChart();
pieChart.setData(getChartData());
pieChart.setTitle("Tiobe index");
pieChart.setLegendSide(Side.LEFT);
pieChart.setClockwise(false);
pieChart.setLabelsVisible(false);
primaryStage.setTitle("PieChart");
StackPane root = new StackPane();
root.getChildren().add(pieChart);
primaryStage.setScene(new Scene(root, 400, 250));
primaryStage.show();
}
Because we used the
Side.LEFT
field in the new code, we have to import the
Side
class in our application as well.
This is done by adding the line
import javafx.geometry.Side
in the import block of the code.
Running this modified version results in the modified output shown in Figure
8-4
.
Search WWH ::
Custom Search