JavaFX 折線圖中的符號是什麼?如何停用它們?


在內聯圖表中,資料值一系列由直線連線的點表示。在 JavaFX 中,你可以透過例項化 javafx.scene.chart.LineChart 類建立折線圖。

預設情況下,

  • JavaFX 折線圖包含指向沿著 X 軸繪製的序列中的值的符號。通常,這些符號是小圓圈。

  • X 軸在底部的繪圖中。

  • Y 軸在左側。

停用符號

LineChart 類有一個名為 createSymbols (布林值)的屬性,用於指定是否為圖表中的資料項建立符號。你可以使用 setCreateSymbols() 方法向此方法設定值。

若要從折線圖中刪除符號,你需要透過將布林值 false 作為引數傳遞給此方法來呼叫此方法。

示例

import javafx.application.Application;
import javafx.geometry.Insets;
import javafx.scene.Scene;
import javafx.stage.Stage;
import javafx.scene.chart.CategoryAxis;
import javafx.scene.chart.LineChart;
import javafx.scene.chart.NumberAxis;
import javafx.scene.chart.XYChart;
import javafx.scene.layout.StackPane;
public class LineChart_Symbols extends Application {
   public void start(Stage stage) {
      //Defining the x an y axes
      CategoryAxis xAxis = new CategoryAxis();
      NumberAxis yAxis = new NumberAxis(200, 600, 100);
      //Setting labels for the axes
      xAxis.setLabel("Model");
      yAxis.setLabel("Price (USD)");
      //Creating a line chart
      LineChart linechart = new LineChart(xAxis, yAxis);
      //Preparing the data points for the line
      XYChart.Series series = new XYChart.Series();
      series.getData().add(new XYChart.Data("OnePlus X", 249));
      series.getData().add(new XYChart.Data("OnePlus One", 299));
      series.getData().add(new XYChart.Data("OnePlus 2", 329));
      series.getData().add(new XYChart.Data("OnePlus 3", 399));
      series.getData().add(new XYChart.Data("OnePlus 3T", 439));
      series.getData().add(new XYChart.Data("OnePlus 5", 479));
      series.getData().add(new XYChart.Data("OnePlus 5T", 499));
      series.getData().add(new XYChart.Data("OnePlus 6", 559));
      //Setting the name to the line (series)
      series.setName("Price of mobiles");
      //Setting the data to Line chart
      linechart.getData().add(series);
      //Removing the symbols of the line chart
      linechart.setCreateSymbols(false);
      //Creating a stack pane to hold the chart
      StackPane pane = new StackPane(linechart);
      pane.setPadding(new Insets(15, 15, 15, 15));
      pane.setStyle("-fx-background-color: BEIGE");
      //Setting the Scene
      Scene scene = new Scene(pane, 595, 300);
      stage.setTitle("Line Chart");
      stage.setScene(scene);
      stage.show();
   }
   public static void main(String args[]){
      launch(args);
   }
}

輸出

更新於: 2020 年 5 月 20 日

703 次瀏覽

開啟你的 職業生涯

完成課程獲得認證

開始
廣告