簡體   English   中英

JavaFX NumberAxis 顯示雙打

[英]JavaFX NumberAxis displays doubles

我創建了一個小應用程序,它使用實時動畫折線圖每 2 秒顯示某個文件夾中的文件數。 一切正常,但圖表/yAxis 在每次更新后都會顯示雙精度值。 該文件夾可以包含 0 到 30.000 個文件...

在此處輸入圖像描述

@FXML
public LineChart<String, Integer> myChart;

@FXML
public CategoryAxis xAxis;

@FXML
public NumberAxis yAxis;

...

yAxis.setMinorTickVisible(false);
yAxis.setTickMarkVisible(false);
yAxis.setTickUnit(1);

如何確保 y 軸僅包含/使用 integer 值...? 永遠不要使用 x,5 值/行。

如果啟用自動量程(默認情況下),則會自動設置刻度單位。 請參閱文檔

因此,一種方法是關閉自動量程,然后手動設置量程。 這可能需要您在數據更改時更新范圍,但下面演示了這個想法:

public class ChartTest extends Application {
    @Override
    public void start(Stage stage) throws IOException {
        CategoryAxis xAxis = new CategoryAxis();
        NumberAxis yAxis = new NumberAxis();
        yAxis.setTickUnit(1);
        yAxis.setMinorTickVisible(false);
        yAxis.setTickMarkVisible(false);
        yAxis.setAutoRanging(false);

        Random rng = new Random();
        XYChart.Series<String, Integer> data = new XYChart.Series<>();
        int max = Integer.MIN_VALUE;
        for (String x : "ABCDEFGHIJKLMNOPQRSTUVWXYZ".split("")) {
            int y = rng.nextInt(10);
            if (y > max) max = y ;
            data.getData().add(new XYChart.Data<>(x, y));
        }

        yAxis.setUpperBound(max + 1);

        data.setName("Data");
        LineChart<String, Integer> chart = new LineChart(xAxis, yAxis);
        chart.getData().add(data);
        Scene scene = new Scene(new BorderPane(chart), 600, 800);
        stage.setTitle("Chart Test");
        stage.setScene(scene);
        stage.show();
    }

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

在此處輸入圖像描述

如果你想保留自動量程,你可以使用一個 tick label 格式化程序,它為非整數值返回一個空字符串:

public class ChartTest extends Application {
    @Override
    public void start(Stage stage) throws IOException {
        CategoryAxis xAxis = new CategoryAxis();
        NumberAxis yAxis = new NumberAxis();

        yAxis.setMinorTickVisible(false);
        yAxis.setTickMarkVisible(false);
        yAxis.setTickLabelFormatter(new StringConverter<Number>() {
            @Override
            public String toString(Number number) {
                double d = number.doubleValue();
                if (d == Math.rint(d)) {
                    return Integer.toString(number.intValue());
                }
                return "" ;
            }

            // Not used
            @Override
            public Number fromString(String s) {
                return null;
            }
        });

        Random rng = new Random();
        XYChart.Series<String, Integer> data = new XYChart.Series<>();
        for (String x : "ABCDEFGHIJKLMNOPQRSTUVWXYZ".split("")) {
            int y = rng.nextInt(10);
            data.getData().add(new XYChart.Data<>(x, y));
        }

        data.setName("Data");
        LineChart<String, Integer> chart = new LineChart(xAxis, yAxis);
        chart.getData().add(data);
        Scene scene = new Scene(new BorderPane(chart), 600, 800);
        stage.setTitle("Chart Test");
        stage.setScene(scene);
        stage.show();
    }

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

在此處輸入圖像描述

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM