表示圆圈中的数据的图表,每个切片表示百分比。

  1. import javafx.application.Application;
  2. import javafx.collections.FXCollections;
  3. import javafx.collections.ObservableList;
  4. import javafx.scene.Scene;
  5. import javafx.scene.chart.PieChart;
  6. import javafx.scene.chart.PieChart.Data;
  7. import javafx.scene.layout.StackPane;
  8. import javafx.stage.Stage;
  9. public class Main extends Application {
  10. public static void main(String[] args) {
  11. launch(args);
  12. }
  13. @Override
  14. public void start(Stage primaryStage) {
  15. PieChart pieChart = new PieChart();
  16. pieChart.setData(getChartData());
  17. primaryStage.setTitle("PieChart");
  18. StackPane root = new StackPane();
  19. root.getChildren().add(pieChart);
  20. primaryStage.setScene(new Scene(root, 400, 250));
  21. primaryStage.show();
  22. }
  23. private ObservableList<Data> getChartData() {
  24. ObservableList<Data> answer = FXCollections.observableArrayList();
  25. answer.addAll(new PieChart.Data("java", 17.56),
  26. new PieChart.Data("JavaFx", 31.37));
  27. return answer;
  28. }
  29. }

image.png

创建饼图

要创建饼图,我们需要PieChart类。
饼图数据包裹在PieChart.Data对象中。
每个PieChart.Data对象有两个字段:饼图扇区的名称及其对应的值。

  1. import javafx.application.Application;
  2. import javafx.collections.FXCollections;
  3. import javafx.collections.ObservableList;
  4. import javafx.geometry.Side;
  5. import javafx.scene.Scene;
  6. import javafx.scene.chart.PieChart;
  7. import javafx.scene.chart.PieChart.Data;
  8. import javafx.scene.layout.StackPane;
  9. import javafx.stage.Stage;
  10. public class Main extends Application {
  11. public static void main(String[] args) {
  12. launch(args);
  13. }
  14. @Override
  15. public void start(Stage primaryStage) {
  16. PieChart pieChart = new PieChart();
  17. pieChart.setData(getChartData());
  18. pieChart.setTitle("Title");
  19. pieChart.setLegendSide(Side.LEFT);
  20. pieChart.setClockwise(false);
  21. pieChart.setLabelsVisible(false);
  22. StackPane root = new StackPane();
  23. root.getChildren().add(pieChart);
  24. primaryStage.setScene(new Scene(root, 300, 250));
  25. primaryStage.show();
  26. }
  27. private ObservableList<Data> getChartData() {
  28. ObservableList<Data> answer = FXCollections.observableArrayList();
  29. answer.addAll(new PieChart.Data("java", 17),
  30. new PieChart.Data("JavaFx",31),
  31. new PieChart.Data("Swing",10),
  32. new PieChart.Data("IO",20),
  33. new PieChart.Data("NIO",21)
  34. );
  35. return answer;
  36. }
  37. }

image.png

处理饼图的事件

以下代码创建一个EventHandler对象来处理落入特定图表切片的MOUSE_PRESSED事件。

  1. import javafx.application.Application;
  2. import javafx.collections.FXCollections;
  3. import javafx.collections.ObservableList;
  4. import javafx.event.EventHandler;
  5. import javafx.scene.Scene;
  6. import javafx.scene.paint.Color;
  7. import javafx.stage.Stage;
  8. import javafx.scene.chart.*;
  9. import javafx.scene.Group;
  10. import javafx.scene.control.Label;
  11. import javafx.scene.input.MouseEvent;
  12. public class Main extends Application {
  13. @Override
  14. public void start(Stage stage) {
  15. Scene scene = new Scene(new Group());
  16. stage.setTitle("Imported Fruits");
  17. stage.setWidth(500);
  18. stage.setHeight(500);
  19. ObservableList<PieChart.Data> pieChartData =
  20. FXCollections.observableArrayList(
  21. new PieChart.Data("Grapefruit", 13),
  22. new PieChart.Data("Oranges", 25),
  23. new PieChart.Data("Plums", 10),
  24. new PieChart.Data("Pears", 22),
  25. new PieChart.Data("Apples", 30));
  26. final PieChart chart = new PieChart(pieChartData);
  27. chart.setTitle("Imported Fruits");
  28. final Label caption = new Label("");
  29. caption.setTextFill(Color.DARKORANGE);
  30. caption.setStyle("-fx-font: 24 arial;");
  31. for (final PieChart.Data data : chart.getData()) {
  32. data.getNode().addEventHandler(MouseEvent.MOUSE_PRESSED,
  33. new EventHandler<MouseEvent>() {
  34. @Override public void handle(MouseEvent e) {
  35. caption.setTranslateX(e.getSceneX());
  36. caption.setTranslateY(e.getSceneY());
  37. caption.setText(String.valueOf(data.getPieValue())
  38. + "%");
  39. }
  40. });
  41. }
  42. ((Group) scene.getRoot()).getChildren().addAll(chart, caption);
  43. stage.setScene(scene);
  44. //scene.getStylesheets().add("piechartsample/Chart.css");
  45. stage.show();
  46. }
  47. public static void main(String[] args) {
  48. launch(args);
  49. }
  50. }

image.png