如何使JavaFX图表NumberAxis只显示整数值,而不是双精度值 [英] How to make JavaFX Chart NumberAxis only show Integer value,not double

查看:1004
本文介绍了如何使JavaFX图表NumberAxis只显示整数值,而不是双精度值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图创建一个图表,谁的yAxis是设计为显示员工人数,所以它必须只显示整数。

但我发现这不是那么容易,因为我已经试图 yAxis.setTickUnit(1)但它不会工作,当值很小(等。最大值为3,它仍然会显示0.5,1.5 ...,我只想要刻度值像1,2,3,4 ..)

如何实现这一点?

I'm trying to create a chart who's yAxis is designed to show number of employee number, so it must only show whole numbers.
But I found it's not that easy as I already tried to yAxis.setTickUnit(1) but it won't work when the values are small(etc. the max value is 3, it'll still show 0.5,1.5..., I only want tick value like 1,2,3,4..)
How Could I to achieve this?

根据@jewelsea的回答,我试过这个(在javafx 2.2 jdk7)

According to @jewelsea 's answer, I tried this(In javafx 2.2 jdk7)

class IntegerStringConverter extends StringConverter<Number>{

    public IntegerStringConverter() {
    }

    @Override
    public String toString(Number object) {
        if(object.intValue()!=object.doubleValue())
            return "";
        return ""+(object.intValue());
    }

    @Override
    public Number fromString(String string) {
        Number val = Double.parseDouble(string);
        return val.intValue();
    }
}  

这是结果是可以接受的。双价值已经消失了,但是还有那里的蜱仍然存在。

It's result is kind of acceptable. Double value's are gone, but there ticks are still there.

推荐答案

设置 tickLabelFormatter

这篇关于如何使JavaFX图表NumberAxis只显示整数值,而不是双精度值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆