Madrugada
Madrugada

Reputation: 1289

JavaFX: how to keep the XYChart ticks constant

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.

Upvotes: 1

Views: 431

Answers (1)

Madrugada
Madrugada

Reputation: 1289

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.

Upvotes: 1

Related Questions