如何在 JavaFX XY 圖表中修改刻度的長度?


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

在 XY 圖中,給定的資料點繪製在 XY 平面圖中。沿著 x 和 y 軸,會有標記和標記標籤。標記表示具有統一間隔的各種值。

更改標記的長度

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

NumberAxis 類用於建立數值軸,而 CategoryAxis 類用於建立字串分類的軸。

此類具有一個屬性,名為刻度線長度(double),用於指定當前軸中所有刻度線的長度。可使用 setTickLength() 方法設定此屬性的值。

要更改 XY 圖表的刻度標記長度,請透過將所需長度作為引數來呼叫此方法。

示例

import javafx.application.Application;
import javafx.geometry.Insets;
import javafx.scene.Scene;
import javafx.scene.chart.BubbleChart;
import javafx.stage.Stage;
import javafx.scene.chart.NumberAxis;
import javafx.scene.chart.XYChart;
import javafx.scene.layout.StackPane;
public class TickMarkLength extends Application {
   public void start(Stage stage) {
      //Creating X and Y axes
      NumberAxis xAxis = new NumberAxis(0, 90, 10);
      NumberAxis yAxis = new NumberAxis(20, 90, 10);
      //Creating labels to the axes
      xAxis.setLabel("Age");
      yAxis.setLabel("Weight");
      //Creating the Bubble chart
      BubbleChart bubbleChart = new BubbleChart(xAxis, yAxis);
      //Preparing data for bubble chart
      XYChart.Series series = new XYChart.Series();
      series.getData().add(new XYChart.Data(10, 30, 4));
      series.getData().add(new XYChart.Data(25, 40, 5));
      series.getData().add(new XYChart.Data(40, 50, 6));
      series.getData().add(new XYChart.Data(55, 60, 8));
      series.getData().add(new XYChart.Data(70, 70, 9));
      //series.getData().add(new XYChart.Data(85, 80, 12));
      //Setting the data to bar chart
      bubbleChart.getData().add(series);
      //Setting name to the bubble chart
      series.setName("work");
      //Changing the length of the tickmark
      xAxis.setTickLength(25);
      yAxis.setTickLength(25);
      //Creating a stack pane to hold the chart
      StackPane pane = new StackPane(bubbleChart);
      pane.setPadding(new Insets(15, 15, 15, 15));
      pane.setStyle("-fx-background-color: BEIGE");
      //Setting the Scene
      Scene scene = new Scene(pane, 595, 350);
      stage.setTitle("JavaFX Example");
      stage.setScene(scene);
      stage.show();
   }
   public static void main(String args[]){
      launch(args);
   }
}

輸出

更新於:2020 年 5 月 20 日

375 次檢視

開啟您的 職業 生涯

完成課程以獲得認證

開始學習
廣告