折线图,一种双轴图表,显示了特定时间间隔内数据的动态。
对于双轴图表,我们可以定义一系列数据XYChart.Series类。
我们可以为每个系列分配一个名称,以便在图例中显示。

  1. import javafx.application.Application;
  2. import javafx.scene.Scene;
  3. import javafx.scene.chart.LineChart;
  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
  9. public void start(Stage stage) {
  10. final NumberAxis xAxis = new NumberAxis();
  11. final NumberAxis yAxis = new NumberAxis();
  12. xAxis.setLabel("Number of Month");
  13. final LineChart<Number, Number> lineChart = new LineChart<Number, Number>(
  14. xAxis, yAxis);
  15. lineChart.setTitle("Line Chart");
  16. XYChart.Series<Number, Number> series = new XYChart.Series<Number, Number>();
  17. series.setName("My Data");
  18. // populating the series with data
  19. series.getData().add(new XYChart.Data<Number, Number>(1, 23));
  20. series.getData().add(new XYChart.Data<Number, Number>(2, 114));
  21. series.getData().add(new XYChart.Data<Number, Number>(3, 15));
  22. series.getData().add(new XYChart.Data<Number, Number>(4, 124));
  23. Scene scene = new Scene(lineChart, 800, 600);
  24. lineChart.getData().add(series);
  25. stage.setScene(scene);
  26. stage.show();
  27. }
  28. public static void main(String[] args) {
  29. launch(args);
  30. }
  31. }

image.png

折线图创建

对于每个图表,我们可以设置标题及其相对于图表的位置。标题可以位于图表的顶部,右侧,左侧或底部。
我们可以以相同的方式指定图表图例的位置。

  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.LineChart;
  7. import javafx.scene.chart.NumberAxis;
  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. LineChart lineChart = new LineChart(xAxis, yAxis);
  21. lineChart.setData(getChartData());
  22. lineChart.setTitle("Chart");
  23. StackPane root = new StackPane();
  24. root.getChildren().add(lineChart);
  25. primaryStage.setScene(new Scene(root, 400, 250));
  26. primaryStage.show();
  27. }
  28. private ObservableList<XYChart.Series<String, Double>> getChartData() {
  29. double aValue = 10;
  30. double cValue = 20;
  31. ObservableList<XYChart.Series<String, Double>> answer = FXCollections
  32. .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 = 2001; i < 2021; i++) {
  38. aSeries.getData().add(new XYChart.Data(Integer.toString(i), aValue));
  39. aValue = aValue + Math.random()*100 -50;
  40. cSeries.getData().add(new XYChart.Data(Integer.toString(i), cValue));
  41. cValue = cValue + Math.random()*100 -50 ;
  42. }
  43. answer.addAll(aSeries, cSeries);
  44. return answer;
  45. }
  46. }

image.png

折线图的类别

下面的代码显示了如何使用CategoryAxis类而不是NumberAxis类以在线形图中呈现非数字数据。

  1. import javafx.application.Application;
  2. import javafx.scene.Scene;
  3. import javafx.scene.chart.CategoryAxis;
  4. import javafx.scene.chart.LineChart;
  5. import javafx.scene.chart.NumberAxis;
  6. import javafx.scene.chart.XYChart;
  7. import javafx.stage.Stage;
  8. public class Main extends Application {
  9. @Override
  10. public void start(Stage stage) {
  11. final CategoryAxis xAxis = new CategoryAxis();
  12. final NumberAxis yAxis = new NumberAxis();
  13. xAxis.setLabel("Month");
  14. final LineChart<String, Number> lineChart = new LineChart<String, Number>(
  15. xAxis, yAxis);
  16. lineChart.setTitle("My Chart");
  17. XYChart.Series<String, Number> series = new XYChart.Series<String, Number>();
  18. series.setName("My data");
  19. series.getData().add(new XYChart.Data<String, Number>("Jan", 23));
  20. series.getData().add(new XYChart.Data<String, Number>("Feb", 114));
  21. series.getData().add(new XYChart.Data<String, Number>("Mar", 15));
  22. series.getData().add(new XYChart.Data<String, Number>("Apr", 24));
  23. series.getData().add(new XYChart.Data<String, Number>("May", 134));
  24. Scene scene = new Scene(lineChart, 800, 600);
  25. lineChart.getData().add(series);
  26. stage.setScene(scene);
  27. stage.show();
  28. }
  29. public static void main(String[] args) {
  30. launch(args);
  31. }
  32. }

image.png

将系列添加到折线图

  1. import java.io.ByteArrayOutputStream;
  2. import java.io.File;
  3. import java.io.IOException;
  4. import javax.imageio.ImageIO;
  5. import javax.imageio.stream.ImageOutputStream;
  6. import javafx.application.Application;
  7. import javafx.embed.swing.SwingFXUtils;
  8. import javafx.scene.Scene;
  9. import javafx.scene.SnapshotParameters;
  10. import javafx.scene.chart.CategoryAxis;
  11. import javafx.scene.chart.LineChart;
  12. import javafx.scene.chart.NumberAxis;
  13. import javafx.scene.chart.XYChart;
  14. import javafx.scene.image.WritableImage;
  15. import javafx.stage.Stage;
  16. public class LineChartSample extends Application {
  17. @Override
  18. public void start(Stage stage) {
  19. stage.setTitle("Line Chart Sample");
  20. CategoryAxis xAxis = new CategoryAxis();
  21. NumberAxis yAxis = new NumberAxis();
  22. xAxis.setLabel("Month");
  23. final LineChart<String,Number> lineChart =
  24. new LineChart<String,Number>(xAxis,yAxis);
  25. lineChart.setTitle("Stock Monitoring, 2010");
  26. XYChart.Series<String,Number> series1 = new XYChart.Series<String,Number>();
  27. series1.setName("Portfolio 1");
  28. series1.getData().add(new XYChart.Data<String,Number>("Jan", 23));
  29. series1.getData().add(new XYChart.Data("Feb", 14));
  30. series1.getData().add(new XYChart.Data("Mar", 15));
  31. series1.getData().add(new XYChart.Data("Apr", 24));
  32. series1.getData().add(new XYChart.Data("May", 34));
  33. series1.getData().add(new XYChart.Data("Jun", 36));
  34. series1.getData().add(new XYChart.Data("Jul", 22));
  35. series1.getData().add(new XYChart.Data("Aug", 45));
  36. series1.getData().add(new XYChart.Data("Sep", 43));
  37. series1.getData().add(new XYChart.Data("Oct", 17));
  38. series1.getData().add(new XYChart.Data("Nov", 29));
  39. series1.getData().add(new XYChart.Data("Dec", 25));
  40. XYChart.Series<String,Number> series2 = new XYChart.Series<String,Number>();
  41. series2.setName("Portfolio 2");
  42. series2.getData().add(new XYChart.Data("Jan", 33));
  43. series2.getData().add(new XYChart.Data("Feb", 34));
  44. series2.getData().add(new XYChart.Data("Mar", 25));
  45. series2.getData().add(new XYChart.Data("Apr", 44));
  46. series2.getData().add(new XYChart.Data("May", 39));
  47. series2.getData().add(new XYChart.Data("Jun", 16));
  48. series2.getData().add(new XYChart.Data("Jul", 55));
  49. series2.getData().add(new XYChart.Data("Aug", 54));
  50. series2.getData().add(new XYChart.Data("Sep", 48));
  51. series2.getData().add(new XYChart.Data("Oct", 27));
  52. series2.getData().add(new XYChart.Data("Nov", 37));
  53. series2.getData().add(new XYChart.Data("Dec", 29));
  54. XYChart.Series<String,Number> series3 = new XYChart.Series<String,Number>();
  55. series3.setName("Portfolio 3");
  56. series3.getData().add(new XYChart.Data("Jan", 44));
  57. series3.getData().add(new XYChart.Data("Feb", 35));
  58. series3.getData().add(new XYChart.Data("Mar", 36));
  59. series3.getData().add(new XYChart.Data("Apr", 33));
  60. series3.getData().add(new XYChart.Data("May", 31));
  61. series3.getData().add(new XYChart.Data("Jun", 26));
  62. series3.getData().add(new XYChart.Data("Jul", 22));
  63. series3.getData().add(new XYChart.Data("Aug", 25));
  64. series3.getData().add(new XYChart.Data("Sep", 43));
  65. series3.getData().add(new XYChart.Data("Oct", 44));
  66. series3.getData().add(new XYChart.Data("Nov", 45));
  67. series3.getData().add(new XYChart.Data("Dec", 44));
  68. Scene scene = new Scene(lineChart,800,600);
  69. lineChart.getData().addAll(series1, series2, series3);
  70. stage.setScene(scene);
  71. stage.show();
  72. }
  73. public static void main(String[] args) {
  74. launch(args);
  75. }
  76. }

image.png