首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >JavaFX NumberAxis显示双倍

JavaFX NumberAxis显示双倍
EN

Stack Overflow用户
提问于 2022-05-04 14:03:03
回答 1查看 34关注 0票数 0

我已经创建了一个小应用程序,它使用一个活的动画线图来显示在某个文件夹中的文件数量每2秒。所有操作都很好,但是图表/yAxis在每次更新后都会显示双值。文件夹可以包含0到30.000个文件.

代码语言:javascript
运行
复制
@FXML
public LineChart<String, Integer> myChart;

@FXML
public CategoryAxis xAxis;

@FXML
public NumberAxis yAxis;

...

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

如何确保y轴只包含/使用整数值.?不应使用x,5值/行。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2022-05-04 14:33:04

如果启用了自动定位(默认情况下是这样的),则自动设置滴答单元。见文档

因此,一种方法是关闭自动测距,并手动设置范围。这可能需要您在数据更改时更新范围,但以下说明了此想法:

代码语言:javascript
运行
复制
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();
    }
}

如果要保留自动执行,可以使用滴答标签格式化程序返回非整数值的空字符串:

代码语言:javascript
运行
复制
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();
    }
}

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/72114316

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档