1

I have a LineChart that displays a market indicator such as this one:

legend

As you see, what is interesting when using this kind of indicator is when the indicator is above or below threshold lines.

I want to do exactly as shown on the picture above: only have ticks on the YAxis at given values (middle, upper limit, lower limit).

Is it possible?

Ben
  • 6,321
  • 9
  • 40
  • 76

1 Answers1

2

Following @kleopatra advice in the comments, I implemented my own NumberAxis:

public class FixedTicksAxis extends ValueAxis<Number> {

    // List of ticks
    private final List<Number> ticks;

    // Formatter
    private NumberAxis.DefaultFormatter defaultFormatter;

    public FixedTicksAxis(Number... ticks) {
        super();

        this.ticks = Arrays.asList(ticks);
        this.defaultFormatter = new NumberAxis.DefaultFormatter(new NumberAxis());
    }

    @Override
    protected List<Number> calculateMinorTickMarks() {
        return new ArrayList<>();
    }

    @Override
    protected void setRange(Object range, boolean animate) {
        final double[] rangeProps = (double[]) range;
        final double lowerBound = rangeProps[0];
        final double upperBound = rangeProps[1];
        final double scale = rangeProps[2];
        setLowerBound(lowerBound);
        setUpperBound(upperBound);

        currentLowerBound.set(lowerBound);
        setScale(scale);
    }

    @Override
    protected Object getRange() {
        return new double[]{
                getLowerBound(),
                getUpperBound(),
                getScale(),
        };
    }

    @Override
    protected List<Number> calculateTickValues(double length, Object range) {
        return ticks;
    }

    @Override
    protected String getTickMarkLabel(Number value) {
        StringConverter<Number> formatter = getTickLabelFormatter();
        if (formatter == null) formatter = defaultFormatter;
        return formatter.toString(value);
    }
}

NumberAxis is final, so I can't subclass it.

I didn't want to copy/paste the content of NumberAxis, but I cannot use a delegate for the protected methods, so I did not find any other way than copy/pasting...

But it works, using:

 FixedTicksAxis yAxis = new FixedTicksAxis(30, 50, 70);

We get:

enter image description here

Ben
  • 6,321
  • 9
  • 40
  • 76