The style class, below, defines the colors in the PieChart (for the four slices), rather than using the default colors.
The name of the file is pieColors.css and is in the same directory as the main java file, folder ex81 of src.
.default-color0.chart-pie { -fx-pie-color: blue; }
.default-color1.chart-pie { -fx-pie-color: red; }
.default-color2.chart-pie { -fx-pie-color: peru; }
.default-color3.chart-pie { -fx-pie-color: green; }
The data for the PieChart is an ObjervableList of type PieChart.Data. It is four numbers from a recent poll
The title is printed using setTitle and put at bottom using setTitleSide. Also it is rotated from original ordering. You can comment out the setStartAngle line to see the original ordering.
package ex81;
import javafx.application.Application;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.geometry.Side;
import javafx.scene.Scene;
import javafx.scene.chart.PieChart;
import javafx.scene.layout.StackPane;
import javafx.stage.Stage;
public class Ex81 extends Application {
ObservableList obList =
FXCollections.observableArrayList();
public static void main(String[] args) {
launch(args);
}
@Override
public void start(Stage primaryStage) {
initData();
PieChart pieChart = new PieChart(obList);
pieChart.setTitle("\nABC News/Washington Post\n\t6/20-6/23");
pieChart.setTitleSide(Side.BOTTOM);
pieChart.setStartAngle(180);
StackPane root = new StackPane();
root.getChildren().add(pieChart);
primaryStage.setTitle("Example 81. PieChart");
Scene scene = new Scene(root, 300, 400);
scene.getStylesheets().add("ex81/pieColors.css");
primaryStage.setScene(scene);
primaryStage.show();
}
private void initData() {
obList.addAll(
new PieChart.Data("CLINTON", 47),
new PieChart.Data("trump", 37),
new PieChart.Data("Johnson", 7),
new PieChart.Data("Stein", 3));
}
}
This is the output:
No comments:
Post a Comment