JFreeChart和Y轴单位

3

我有一个StackedXYAreaChart,看起来像这样:

enter image description here

如何格式化Y轴单位,使它们间距均匀并在特定间隔内显示? 例如,不要按1的增量(例如0, 1, 2, 3, ... 100)显示单位,而是按10或25的增量显示单位(例如0, 25, 50, 75, 100)。 谢谢!

3个回答

4
我找到了解决我的问题的办法。我使用了一个CustomTickUnit,它可以格式化数字并在后缀中添加单位,例如1000000变成了1 GB
我使用以下代码设置了我的刻度单位,使它们均匀分布,并适当地进行了格式化,以便它们非常易读:
public void setupRangeAxis(NumberAxis rangeAxis) {
  final TickUnits standardUnits = new TickUnits();
  standardUnits.add(new CustomTickUnit(1));
  standardUnits.add(new CustomTickUnit(10));
  standardUnits.add(new CustomTickUnit(100));
  standardUnits.add(new CustomTickUnit(1000)); // Kilo
  standardUnits.add(new CustomTickUnit(10000));
  standardUnits.add(new CustomTickUnit(100000));
  standardUnits.add(new CustomTickUnit(1000000)); // Mega
  standardUnits.add(new CustomTickUnit(10000000));
  standardUnits.add(new CustomTickUnit(100000000));
  standardUnits.add(new CustomTickUnit(1000000000)); // Giga
  standardUnits.add(new CustomTickUnit(10000000000L));
  standardUnits.add(new CustomTickUnit(100000000000L));
  standardUnits.add(new CustomTickUnit(1000000000000L)); // Tera
  standardUnits.add(new CustomTickUnit(10000000000000L));
  standardUnits.add(new CustomTickUnit(100000000000000L));
  standardUnits.add(new CustomTickUnit(1000000000000000L)); // Peta
  standardUnits.add(new CustomTickUnit(10000000000000000L));
  standardUnits.add(new CustomTickUnit(100000000000000000L));
  standardUnits.add(new CustomTickUnit(1000000000000000000L)); // Exa
  rangeAxis.setStandardTickUnits(standardUnits);
}

2
CategoryPlot plot = chart.getCategoryPlot();

NumberAxis rangeAxis = (NumberAxis) plot.getRangeAxis();

rangeAxis.setTickUnit(new NumberTickUnit(300));

你能提供一点关于你回答的解释吗?谢谢! - Rick Smith

1
假设有一个 NumberAxis,将刻度单位设置为25。这里有一个相关的示例here
axis.setTickUnit(new NumberTickUnit(25));

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接