如何在 JavaFX XY 圖表中移除刻度標記?


javafx.scene.XYChart 類是所有在 x-y 平面中繪製的圖表的基本類。透過例項化此類的子類,您可以建立各種 XY 圖表,例如 - 線形圖、面積圖、柱狀圖、餅圖、氣泡圖、散點圖等。

在 XY 圖表中,給定的資料點繪製在 XY 平面上。在 x 和 y 軸上,您將看到刻度標記和刻度標籤。刻度標記代表具有統一間隔的各種值。

移除刻度標記

javafx.scene.chart.Axis 類(抽象類)是 XY 圖表中所有軸的基本類。要建立 X 和 Y 軸,您需要例項化這些類的子類。

NumberAxis 類用於為數值建立軸,而 CategoryAxis 類用於為字串類別建立軸。

此類具有一個名為 tickMarkVisible(布林型)的屬性,它指定是否顯示刻度標記。您可以使用 setTickMarkVisible() 方法設定此屬性的值。

要移除 XY 圖表的刻度標記,請透過傳遞布林值 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 RemovingTickMarks 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 tick marks
      xAxis.setTickMarkVisible(false);
      yAxis.setTickMarkVisible(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("JavaFX Example");
      stage.setScene(scene);
      stage.show();
   }
   public static void main(String args[]){
      launch(args);
   }
}

輸出

更新於: 2020年5月20日

246 次瀏覽

開啟您的 職業生涯

透過完成課程獲得認證

立即開始
廣告