我的问题:使用jfreechart,我需要在y轴上显示不均匀间隔的百分比阈值。例如,我y轴上的唯一标签应该是阈值,如下所示;所有其他纵坐标应为空白:
-
93%
85%
78%
72%
66%
-
50%
-
-
-
-
-
我目前正在使用此代码段显示百分比,但这只会创建一个均匀间隔的百分比轴:
CategoryPlot plot = (CategoryPlot) chart.getPlot();
NumberAxis rangeAxis = (NumberAxis) plot.getRangeAxis();
DecimalFormat pctFormat = new DecimalFormat("#.0%");
rangeAxis.setNumberFormatOverride(pctFormat);
任何帮助将不胜感激!
答案 0 :(得分:2)
这是一个有点骇人听闻的答案,我会赞成任何能以一种好方式获得相同结果的答案。但它有效 - 这就是我所做的。
首先,我创建了一个我想要使用的双值列表:
public static List<Double> m_lPercentageValuesForY = Arrays.asList(0.1,0.2,0.3,0.4,0.5,0.58,0.66,0.70,0.74,0.78,0.82,0.95,1.0);
然后我覆盖NumberAxis中的refreshTicks方法,首先将所有默认刻度设置为Ticktype.MINOR,然后使用Ticktype.MAJOR添加并创建我的自定义刻度:
NumberAxis axisLeft = new NumberAxis(plot.getRangeAxis().getLabel()) {
@Override
public List refreshTicks(
Graphics2D g2,
AxisState state,
Rectangle2D dataArea,
RectangleEdge edge) {
List defaultTicks = super.refreshTicks(g2, state, dataArea, edge);
List customTicks = new ArrayList();
for (Object aTick : defaultTicks) {
NumberTick currenttick = (NumberTick) aTick;
customTicks.add(new NumberTick(
TickType.MINOR,
currenttick.getValue(),
"", //empty
currenttick.getTextAnchor(),
currenttick.getRotationAnchor(),
currenttick.getAngle()));
}
NumberTick aTick = (NumberTick) defaultTicks.get(0);
for (double dTest : m_lPercentageValuesForY) {
customTicks.add(new NumberTick(
TickType.MAJOR,
dTest,
String.format("%.0f%%", dTest * 100), //only wanted values are set to major
aTick.getTextAnchor(),
aTick.getRotationAnchor(),
aTick.getAngle()));
}
return customTicks;
}
};
plot.setRangeAxis(axisLeft);