散点图是一个双轴图表,将其数据作为一组点显示。每个点由X和Y值定义。

  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.CategoryAxis;
  6. import javafx.scene.chart.NumberAxis;
  7. import javafx.scene.chart.ScatterChart;
  8. import javafx.scene.chart.XYChart;
  9. import javafx.scene.chart.XYChart.Series;
  10. import javafx.scene.layout.StackPane;
  11. import javafx.stage.Stage;
  12. public class Main extends Application {
  13. public static void main(String[] args) {
  14. launch(args);
  15. }
  16. @Override
  17. public void start(Stage primaryStage) {
  18. CategoryAxis xAxis = new CategoryAxis();
  19. NumberAxis yAxis = new NumberAxis();
  20. ScatterChart scatterChart = new ScatterChart(xAxis, yAxis);
  21. scatterChart.setData(getChartData());
  22. scatterChart.setTitle("speculations");
  23. primaryStage.setTitle("ScatteredChart example");
  24. StackPane root = new StackPane();
  25. root.getChildren().add(scatterChart);
  26. primaryStage.setScene(new Scene(root, 400, 250));
  27. primaryStage.show();
  28. }
  29. private ObservableList<XYChart.Series<String, Double>> getChartData() {
  30. double aValue = 1.56;
  31. double cValue = 1.06;
  32. ObservableList<XYChart.Series<String, Double>> answer = FXCollections.observableArrayList();
  33. Series<String, Double> aSeries = new Series<String, Double>();
  34. Series<String, Double> cSeries = new Series<String, Double>();
  35. aSeries.setName("a");
  36. cSeries.setName("C");
  37. for (int i = 2011; i < 2021; i++) {
  38. aSeries.getData().add(new XYChart.Data(Integer.toString(i), aValue));
  39. aValue = aValue + Math.random() - .5;
  40. cSeries.getData().add(new XYChart.Data(Integer.toString(i), cValue));
  41. cValue = cValue + Math.random() - .5;
  42. }
  43. answer.addAll(aSeries, cSeries);
  44. return answer;
  45. }
  46. }

image.png

添加系列

  1. import javafx.application.Application;
  2. import javafx.collections.FXCollections;
  3. import javafx.event.ActionEvent;
  4. import javafx.event.EventHandler;
  5. import javafx.geometry.Insets;
  6. import javafx.scene.Group;
  7. import javafx.scene.Scene;
  8. import javafx.scene.chart.NumberAxis;
  9. import javafx.scene.chart.ScatterChart;
  10. import javafx.scene.chart.XYChart;
  11. import javafx.scene.control.Button;
  12. import javafx.scene.effect.DropShadow;
  13. import javafx.scene.layout.HBox;
  14. import javafx.scene.layout.VBox;
  15. import javafx.scene.paint.Color;
  16. import javafx.stage.Stage;
  17. public class Main extends Application {
  18. @Override public void start(Stage stage) {
  19. stage.setTitle("Scatter Chart Sample");
  20. final NumberAxis xAxis = new NumberAxis(0, 10, 1);
  21. final NumberAxis yAxis = new NumberAxis(-100, 500, 100);
  22. final ScatterChart<Number,Number> sc = new ScatterChart<Number,Number>(xAxis,yAxis);
  23. xAxis.setLabel("Age (years)");
  24. yAxis.setLabel("Returns to date");
  25. sc.setTitle("Investment Overview");
  26. XYChart.Series series1 = new XYChart.Series();
  27. series1.setName("Option 1");
  28. series1.getData().add(new XYChart.Data(4.2, 193.2));
  29. series1.getData().add(new XYChart.Data(2.8, 33.6));
  30. series1.getData().add(new XYChart.Data(6.2, 24.8));
  31. sc.setPrefSize(500, 400);
  32. sc.getData().addAll(series1);
  33. Scene scene = new Scene(new Group());
  34. final VBox vbox = new VBox();
  35. final HBox hbox = new HBox();
  36. final Button add = new Button("Add Series");
  37. add.setOnAction(new EventHandler<ActionEvent>() {
  38. @Override public void handle(ActionEvent e) {
  39. if (sc.getData() == null)
  40. sc.setData(FXCollections.<XYChart.Series<Number,
  41. Number>>observableArrayList());
  42. ScatterChart.Series<Number, Number> series =
  43. new ScatterChart.Series<Number, Number>();
  44. series.setName("Option "+(sc.getData().size()+1));
  45. for (int i=0; i<100; i++) series.getData().add(
  46. new ScatterChart.Data<Number,
  47. Number>(Math.random()*100, Math.random()*500));
  48. sc.getData().add(series);
  49. }
  50. });
  51. final Button remove = new Button("Remove Series");
  52. remove.setOnAction(new EventHandler<ActionEvent>() {
  53. @Override public void handle(ActionEvent e) {
  54. if (!sc.getData().isEmpty())
  55. sc.getData().remove((int)(Math.random()*(sc.getData().size()-1)));
  56. }
  57. });
  58. final DropShadow shadow = new DropShadow();
  59. shadow.setOffsetX(2);
  60. shadow.setColor(Color.GREY);
  61. sc.setEffect(shadow);
  62. hbox.setSpacing(10);
  63. hbox.getChildren().addAll(add, remove);
  64. vbox.getChildren().addAll(sc, hbox);
  65. hbox.setPadding(new Insets(10, 10, 10, 50));
  66. ((Group)scene.getRoot()).getChildren().add(vbox);
  67. stage.setScene(scene);
  68. stage.show();
  69. }
  70. public static void main(String[] args) {
  71. launch(args);
  72. }
  73. }

image.png