答案 0 :(得分:1)
在评论中关注@kleopatra建议后,我实现了自己的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是最终的,所以我不能将其子类化。
我不想复制/粘贴NumberAxis的内容,但是我不能使用委托作为受保护的方法,所以除了复制/粘贴之外我没有找到任何其他方法......
但它有效,使用:
FixedTicksAxis yAxis = new FixedTicksAxis(30, 50, 70);
我们得到: