区域图是另一种类型的双轴图表。

  1. import javafx.application.Application;
  2. import javafx.scene.Group;
  3. import javafx.scene.Scene;
  4. import javafx.scene.chart.NumberAxis;
  5. import javafx.scene.chart.StackedAreaChart;
  6. import javafx.scene.chart.XYChart;
  7. import javafx.stage.Stage;
  8. public class Main extends Application {
  9. public static void main(String[] args) {
  10. launch(args);
  11. }
  12. @Override
  13. public void start(Stage primaryStage) {
  14. Group root = new Group();
  15. final NumberAxis xAxis = new NumberAxis(1, 12, 1);
  16. final NumberAxis yAxis = new NumberAxis();
  17. final StackedAreaChart<Number,Number> stackedAreaChart = new StackedAreaChart<Number,Number>(xAxis,yAxis);
  18. final XYChart.Series<Number,Number> series1 = new XYChart.Series<Number,Number>();
  19. xAxis.setLabel("Month");
  20. yAxis.setLabel("Value");
  21. stackedAreaChart.setTitle("StackedAreaChart");
  22. series1.setName("XYChart.Series 1");
  23. series1.getData().add(new XYChart.Data(1, 100));
  24. series1.getData().add(new XYChart.Data(2, 200));
  25. series1.getData().add(new XYChart.Data(10, 150));
  26. XYChart.Series<Number,Number> series2 = new XYChart.Series();
  27. series2.setName("XYChart.Series 2");
  28. series2.getData().add(new XYChart.Data(1, 50));
  29. series2.getData().add(new XYChart.Data(2, 200));
  30. series2.getData().add(new XYChart.Data(10, 260));
  31. stackedAreaChart.getData().addAll(series1, series2);
  32. root.getChildren().addAll(stackedAreaChart);
  33. primaryStage.setScene(new Scene(root, 500, 400));
  34. primaryStage.show();
  35. }
  36. }

image.png

创建区域图

要创建区域图,请使用AreaChart对象和使用XYChart.Series类创建一个或多个数据系列,并将数据分配给图表。

  1. import javafx.application.Application;
  2. import javafx.scene.Group;
  3. import javafx.scene.Scene;
  4. import javafx.scene.chart.AreaChart;
  5. import javafx.scene.chart.CategoryAxis;
  6. import javafx.scene.chart.NumberAxis;
  7. import javafx.scene.chart.XYChart;
  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. primaryStage.setTitle("");
  16. Group root = new Group();
  17. final CategoryAxis xAxis = new CategoryAxis();
  18. final NumberAxis yAxis = new NumberAxis();
  19. xAxis.setLabel("Month");
  20. yAxis.setLabel("Value");
  21. final AreaChart<String,Number> areaChart = new AreaChart<String,Number>(xAxis,yAxis);
  22. areaChart.setTitle("AreaChart");
  23. XYChart.Series series = new XYChart.Series();
  24. series.setName("XYChart.Series");
  25. series.getData().add(new XYChart.Data("January", 100));
  26. series.getData().add(new XYChart.Data("February", 200));
  27. series.getData().add(new XYChart.Data("March", 50));
  28. areaChart.getData().add(series);
  29. root.getChildren().add(areaChart);
  30. primaryStage.setScene(new Scene(root, 500, 400));
  31. primaryStage.show();
  32. }
  33. }

image.png

向AreaChart添加更多系列

  1. import javafx.application.Application;
  2. import javafx.scene.Scene;
  3. import javafx.scene.chart.AreaChart;
  4. import javafx.scene.chart.NumberAxis;
  5. import javafx.scene.chart.XYChart;
  6. import javafx.stage.Stage;
  7. public class Main extends Application {
  8. @Override public void start(Stage stage) {
  9. stage.setTitle("Area Chart Sample");
  10. final NumberAxis xAxis = new NumberAxis(1, 30, 1);
  11. final NumberAxis yAxis = new NumberAxis(-5, 27, 5);
  12. final AreaChart<Number,Number> ac =
  13. new AreaChart<Number,Number>(xAxis,yAxis);
  14. xAxis.setForceZeroInRange(true);
  15. ac.setTitle("Temperature Monitoring (in Degrees C)");
  16. XYChart.Series series1 = new XYChart.Series();
  17. series1.setName("March");
  18. series1.getData().add(new XYChart.Data(0, -2));
  19. series1.getData().add(new XYChart.Data(3, -4));
  20. series1.getData().add(new XYChart.Data(6, 0));
  21. series1.getData().add(new XYChart.Data(9, 5));
  22. XYChart.Series series2 = new XYChart.Series();
  23. series2.setName("April");
  24. series2.getData().add(new XYChart.Data(0, 4));
  25. series2.getData().add(new XYChart.Data(3, 10));
  26. series2.getData().add(new XYChart.Data(6, 15));
  27. series2.getData().add(new XYChart.Data(9, 8));
  28. XYChart.Series series3 = new XYChart.Series();
  29. series3.setName("May");
  30. series3.getData().add(new XYChart.Data(0, 20));
  31. series3.getData().add(new XYChart.Data(3, 15));
  32. series3.getData().add(new XYChart.Data(6, 13));
  33. series3.getData().add(new XYChart.Data(9, 12));
  34. Scene scene = new Scene(ac,800,600);
  35. //scene.getStylesheets().add("areachartsample/Chart.css");
  36. ac.setHorizontalZeroLineVisible(true);
  37. ac.getData().addAll(series1, series2, series3);
  38. stage.setScene(scene);
  39. stage.show();
  40. }
  41. public static void main(String[] args) {
  42. launch(args);
  43. }
  44. }

image.png

创建堆叠区域图

  1. import javafx.application.Application;
  2. import javafx.scene.Scene;
  3. import javafx.scene.chart.NumberAxis;
  4. import javafx.scene.chart.StackedAreaChart;
  5. import javafx.scene.chart.XYChart;
  6. import javafx.stage.Stage;
  7. public class Main extends Application {
  8. final NumberAxis xAxis = new NumberAxis(1, 10, 1);
  9. final NumberAxis yAxis = new NumberAxis();
  10. final StackedAreaChart<Number, Number> sac =
  11. new StackedAreaChart<>(xAxis, yAxis);
  12. @Override
  13. public void start(Stage stage) {
  14. sac.setTitle("title");
  15. XYChart.Series<Number, Number> seriesApril =
  16. new XYChart.Series<>();
  17. seriesApril.setName("April");
  18. seriesApril.getData().add(new XYChart.Data(1, 4));
  19. seriesApril.getData().add(new XYChart.Data(3, 10));
  20. seriesApril.getData().add(new XYChart.Data(6, 15));
  21. XYChart.Series<Number, Number> seriesMay =
  22. new XYChart.Series<>();
  23. seriesMay.setName("May");
  24. seriesMay.getData().add(new XYChart.Data(1, 23));
  25. seriesMay.getData().add(new XYChart.Data(7, 26));
  26. seriesMay.getData().add(new XYChart.Data(10, 26));
  27. Scene scene = new Scene(sac, 800, 600);
  28. sac.getData().addAll(seriesApril, seriesMay);
  29. stage.setScene(scene);
  30. stage.show();
  31. }
  32. public static void main(String[] args) {
  33. launch(args);
  34. }
  35. }

image.png