JavaFX 折線圖

2021-05-22 15:13 更新

JavaFX教程 - JavaFX折線圖


折線圖,一種雙軸圖表,顯示了特定時間間隔內數(shù)據(jù)的動態(tài)。

對于雙軸圖表,我們可以定義一系列數(shù)據(jù)XYChart.Series類。

我們可以為每個系列分配一個名稱,以便在圖例中顯示。

import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.chart.LineChart;
import javafx.scene.chart.NumberAxis;
import javafx.scene.chart.XYChart;
import javafx.stage.Stage;

public class Main extends Application {

  @Override
  public void start(Stage stage) {
    final NumberAxis xAxis = new NumberAxis();
    final NumberAxis yAxis = new NumberAxis();
    xAxis.setLabel("Number of Month");
    final LineChart<Number, Number> lineChart = new LineChart<Number, Number>(
        xAxis, yAxis);

    lineChart.setTitle("Line Chart");
    XYChart.Series<Number, Number> series = new XYChart.Series<Number, Number>();
    series.setName("My Data");
    // populating the series with data
    series.getData().add(new XYChart.Data<Number, Number>(1, 23));
    series.getData().add(new XYChart.Data<Number, Number>(2, 114));
    series.getData().add(new XYChart.Data<Number, Number>(3, 15));
    series.getData().add(new XYChart.Data<Number, Number>(4, 124));

    Scene scene = new Scene(lineChart, 800, 600);
    lineChart.getData().add(series);

    stage.setScene(scene);
    stage.show();
  }

  public static void main(String[] args) {
    launch(args);
  }
}

上面的代碼生成以下結果。

null

折線圖創(chuàng)建

對于每個圖表,我們可以設置標題及其相對于圖表的位置。標題可以位于圖表的頂部,右側,左側或底部。

我們可以以相同的方式指定圖表圖例的位置。

import javafx.application.Application;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.scene.Scene;
import javafx.scene.chart.CategoryAxis;
import javafx.scene.chart.LineChart;
import javafx.scene.chart.NumberAxis;
import javafx.scene.chart.XYChart;
import javafx.scene.chart.XYChart.Series;
import javafx.scene.layout.StackPane;
import javafx.stage.Stage;

public class Main extends Application {
  public static void main(String[] args) {
    launch(args);
  }

  @Override
  public void start(Stage primaryStage) {
    CategoryAxis xAxis = new CategoryAxis();
    NumberAxis yAxis = new NumberAxis();
    LineChart lineChart = new LineChart(xAxis, yAxis);
    lineChart.setData(getChartData());
    lineChart.setTitle("Chart");

    StackPane root = new StackPane();
    root.getChildren().add(lineChart);
    primaryStage.setScene(new Scene(root, 400, 250));
    primaryStage.show();
  }

  private ObservableList<XYChart.Series<String, Double>> getChartData() {
    double aValue = 10;
    double cValue = 20;
    ObservableList<XYChart.Series<String, Double>> answer = FXCollections
        .observableArrayList();
    Series<String, Double> aSeries = new Series<String, Double>();
    Series<String, Double> cSeries = new Series<String, Double>();
    aSeries.setName("a");
    cSeries.setName("C");

    for (int i = 2001; i < 2021; i++) {
      aSeries.getData().add(new XYChart.Data(Integer.toString(i), aValue));
      aValue = aValue + Math.random()*100 -50;
      cSeries.getData().add(new XYChart.Data(Integer.toString(i), cValue));
      cValue = cValue + Math.random()*100 -50 ;
    }
    answer.addAll(aSeries, cSeries);
    return answer;
  }
}

上面的代碼生成以下結果。

null

折線圖的類別

下面的代碼顯示了如何使用CategoryAxis類而不是NumberAxis類以在線形圖中呈現(xiàn)非數(shù)字數(shù)據(jù)。

import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.chart.CategoryAxis;
import javafx.scene.chart.LineChart;
import javafx.scene.chart.NumberAxis;
import javafx.scene.chart.XYChart;
import javafx.stage.Stage;

public class Main extends Application {

  @Override
  public void start(Stage stage) {
    final CategoryAxis xAxis = new CategoryAxis();
    final NumberAxis yAxis = new NumberAxis();
    xAxis.setLabel("Month");

    final LineChart<String, Number> lineChart = new LineChart<String, Number>(
        xAxis, yAxis);

    lineChart.setTitle("My Chart");

    XYChart.Series<String, Number> series = new XYChart.Series<String, Number>();
    series.setName("My data");

    series.getData().add(new XYChart.Data<String, Number>("Jan", 23));
    series.getData().add(new XYChart.Data<String, Number>("Feb", 114));
    series.getData().add(new XYChart.Data<String, Number>("Mar", 15));
    series.getData().add(new XYChart.Data<String, Number>("Apr", 24));
    series.getData().add(new XYChart.Data<String, Number>("May", 134));

    Scene scene = new Scene(lineChart, 800, 600);
    lineChart.getData().add(series);

    stage.setScene(scene);
    stage.show();
  }

  public static void main(String[] args) {
    launch(args);
  }
}

上面的代碼生成以下結果。

null

將系列添加到折線圖

import java.io.ByteArrayOutputStream;

import java.io.File;

import java.io.IOException;

 

import javax.imageio.ImageIO;

import javax.imageio.stream.ImageOutputStream;

 

import javafx.application.Application;

import javafx.embed.swing.SwingFXUtils;

import javafx.scene.Scene;

import javafx.scene.SnapshotParameters;

import javafx.scene.chart.CategoryAxis;

import javafx.scene.chart.LineChart;

import javafx.scene.chart.NumberAxis;

import javafx.scene.chart.XYChart;

import javafx.scene.image.WritableImage;

import javafx.stage.Stage;

 

 

public class LineChartSample extends Application {

 

    @Override 

    public void start(Stage stage) {

        stage.setTitle("Line Chart Sample");

        CategoryAxis xAxis = new CategoryAxis();

    NumberAxis yAxis = new NumberAxis();

        xAxis.setLabel("Month");

        final LineChart<String,Number> lineChart = 

                new LineChart<String,Number>(xAxis,yAxis);

       

        lineChart.setTitle("Stock Monitoring, 2010");

                          

        XYChart.Series<String,Number> series1 = new XYChart.Series<String,Number>();

        series1.setName("Portfolio 1");

        

        series1.getData().add(new XYChart.Data<String,Number>("Jan", 23));

        series1.getData().add(new XYChart.Data("Feb", 14));

        series1.getData().add(new XYChart.Data("Mar", 15));

        series1.getData().add(new XYChart.Data("Apr", 24));

        series1.getData().add(new XYChart.Data("May", 34));

        series1.getData().add(new XYChart.Data("Jun", 36));

        series1.getData().add(new XYChart.Data("Jul", 22));

        series1.getData().add(new XYChart.Data("Aug", 45));

        series1.getData().add(new XYChart.Data("Sep", 43));

        series1.getData().add(new XYChart.Data("Oct", 17));

        series1.getData().add(new XYChart.Data("Nov", 29));

        series1.getData().add(new XYChart.Data("Dec", 25));

        

        XYChart.Series<String,Number> series2 = new XYChart.Series<String,Number>();

        series2.setName("Portfolio 2");

        series2.getData().add(new XYChart.Data("Jan", 33));

        series2.getData().add(new XYChart.Data("Feb", 34));

        series2.getData().add(new XYChart.Data("Mar", 25));

        series2.getData().add(new XYChart.Data("Apr", 44));

        series2.getData().add(new XYChart.Data("May", 39));

        series2.getData().add(new XYChart.Data("Jun", 16));

        series2.getData().add(new XYChart.Data("Jul", 55));

        series2.getData().add(new XYChart.Data("Aug", 54));

        series2.getData().add(new XYChart.Data("Sep", 48));

        series2.getData().add(new XYChart.Data("Oct", 27));

        series2.getData().add(new XYChart.Data("Nov", 37));

        series2.getData().add(new XYChart.Data("Dec", 29));


        XYChart.Series<String,Number> series3 = new XYChart.Series<String,Number>();

        series3.setName("Portfolio 3");

        series3.getData().add(new XYChart.Data("Jan", 44));

        series3.getData().add(new XYChart.Data("Feb", 35));

        series3.getData().add(new XYChart.Data("Mar", 36));

        series3.getData().add(new XYChart.Data("Apr", 33));

        series3.getData().add(new XYChart.Data("May", 31));

        series3.getData().add(new XYChart.Data("Jun", 26));

        series3.getData().add(new XYChart.Data("Jul", 22));

        series3.getData().add(new XYChart.Data("Aug", 25));

        series3.getData().add(new XYChart.Data("Sep", 43));

        series3.getData().add(new XYChart.Data("Oct", 44));

        series3.getData().add(new XYChart.Data("Nov", 45));

        series3.getData().add(new XYChart.Data("Dec", 44));

        Scene scene  = new Scene(lineChart,800,600);       

        lineChart.getData().addAll(series1, series2, series3);

        stage.setScene(scene);

        stage.show();

    }

    public static void main(String[] args) {

        launch(args);

    }

}

上面的代碼生成以下結果。

20181206164603906



以上內容是否對您有幫助:
在線筆記
App下載
App下載

掃描二維碼

下載編程獅App

公眾號
微信公眾號

編程獅公眾號