JavaFX: how to keep the XYChart ticks constant

99封情书 提交于 2019-12-25 16:42:31

问题


I have a XYChart whose ticks unit is set to 25 for X axis. The chart has a sliding functionality. Each time I slide left or right, I want the ticks to stay constant. So far, I have 10 or 11 ticks, set at 25 distance one to another, and they change their values -- I want that constant.

For example: lowerBound = 480 upperBound = 600 Ticks: 480, 505, 530, 555, 580, 600.

I slide right: I still want to see 480, 505, 530, 555, 580, 600 at different positions though, and possible get rid of 480 and see 605, 630 if I went enough to the right...

Kindly Advise.


回答1:


Fixed it: wrote a new class similar to NumberAxis, except calculateTickValues is implemented using my own logic:

if (lowerBound + tickUnit < upperBound) {
    double start = lowerBound;
    while ((int)start % (int)tickUnit != 0) {
        start++;
    }
    int count = (int)Math.ceil((upperBound - start)/tickUnit);
    for (int i = 0; start < upperBound && i < count; start += tickUnit, i++) {
         if (!tickValues.contains(start)) {
             tickValues.add((int) start);
         }
    }
}

Here tickUnit=25, as set up on a Chart class I am using, which extends AbstractBaseChart. It is not very elegant but it works.



来源:https://stackoverflow.com/questions/43324028/javafx-how-to-keep-the-xychart-ticks-constant

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!