我正在使用以下我从网络上获得的代码

TickUnits units = new TickUnits();
DecimalFormat df1 = new DecimalFormat("$0");
DecimalFormat df2 = new DecimalFormat("$0.00");

// we can add the units in any order, the TickUnits collection will
// sort them...
units.add(new NumberTickUnit(100, df1, 0));
units.add(new NumberTickUnit(50, df1, 0));
units.add(new NumberTickUnit(20, df1, 0));
units.add(new NumberTickUnit(10, df1, 0));
units.add(new NumberTickUnit(5, df1, 0));
units.add(new NumberTickUnit(2, df1, 0));
units.add(new NumberTickUnit(1, df1, 0));


由于我是个新手,所以我不理解这段代码的实际作用。我以为它在y轴上设置了值,但事实并非如此,因为在y轴上有超过100美元的值。这段代码究竟是做什么的。 (准确的是刻度单位。它是在轴上定义标签还是在这里我弄错了)。

最佳答案

是的,TickUnits定义了Axis标签的格式,但是完整的说明超出了Stackoverflow的范围。 developer guide†是确定性的,但检查针对特定TickUnitSource子类返回Axis的方法可能具有指导意义。例如,createIntegerTickUnits()NumberAxis的源代码以及NumberTickUnitSource

引用的示例存在缺陷,因为它忽略了用户的语言环境。

附录:如果要本地化的货币格式,请覆盖默认的Number格式程序。

XYPlot plot = chart.getXYPlot();
NumberAxis range = (NumberAxis) plot.getRangeAxis();
range.setNumberFormatOverride(NumberFormat.getCurrencyInstance());


†免责声明:不隶属于Object Refinery Limited;只是一个满意的客户,贡献很小。

10-07 19:46
查看更多