如何只显示javafx图表轴上可以被tick单位整除的数字

时间:2016-02-04 11:32:43

标签: java javafx charts axis

这是我现在所拥有的

enter image description here

如您所见,左侧的数字不能被刻度标记整除(在这种情况下为50)。我想只看到2950,3000,3050等数字。顺便提一下顶级标记(漂浮在那里困扰着我)

1 个答案:

答案 0 :(得分:2)

如果自动量程功能未提供刻度线所需的值,您可以手动配置刻度线范围。

yAxis.setAutoRanging(false);
yAxis.setLowerBound(1000);
yAxis.setUpperBound(5000);
yAxis.setTickUnit(500);
yAxis.setMinorTickVisible(false);

示例应用

Y轴上的数字只能被为轴指定的刻度单位(500)整除。此外,Y轴的上限和下限是明确设置的,可以精确控制这些值。

sample app

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 LineChartRanging 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<>(xAxis, yAxis);

        XYChart.Series series = new XYChart.Series();
        series.setName("My portfolio");
        series.getData().add(new XYChart.Data(1, 2317));
        series.getData().add(new XYChart.Data(2, 1427));
        series.getData().add(new XYChart.Data(3, 1573));
        series.getData().add(new XYChart.Data(4, 2452));
        series.getData().add(new XYChart.Data(5, 3495));
        series.getData().add(new XYChart.Data(6, 3663));
        series.getData().add(new XYChart.Data(7, 2215));
        series.getData().add(new XYChart.Data(8, 4541));
        series.getData().add(new XYChart.Data(9, 4393));
        series.getData().add(new XYChart.Data(10, 1772));
        series.getData().add(new XYChart.Data(11, 2994));
        series.getData().add(new XYChart.Data(12, 2508));

        Scene scene  = new Scene(lineChart);
        lineChart.getData().add(series);
        lineChart.setPrefSize(500, 340);

        yAxis.setAutoRanging(false);
        yAxis.setLowerBound(1000);
        yAxis.setUpperBound(5000);
        yAxis.setTickUnit(500);
        yAxis.setMinorTickVisible(false);

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

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