JFreechart:以特定单位后的值显示X轴 [英] JFreechart: Displaying X axis with values after specific units
问题描述
我正在使用jfreechart显示折线图,现在在X轴上显示图表上每个(x,y)对的值,结果X轴上有大量重叠的值,我只想显示几个值,例如每5个单位之后或类似的值.使用Jfreechart怎么可能.
I am using jfreechart for displaying line graph.Now, on X axis it shows value for every (x,y) pair on chart.As a result the X axis has huge amount of values getting overlapped.I want to display few values eg after every 5 units or something like that.How is this possible using Jfreechart.
推荐答案
在绘制图表的NumberAxis
之前,将刷新其刻度线.结果是List
,其中每个轴的刻度线都包含一个NumberTick
对象.
Before the NumberAxis
of the chart's plot is drawn, its tick marks are refreshed. The result is a List
that includes a NumberTick
object for each tick mark of the axis.
通过覆盖功能NumberAxis.refreshTicks
,您可以控制标记的显示方式和显示方式.
By overriding the function NumberAxis.refreshTicks
you can control how and if the marks will be shown.
例如,在下面的代码中,我获得了所有刻度线并对其进行迭代以查找TickType.MAJOR
.如果不能将主要刻度线的值除以5,则将其替换为次要刻度线.
For example, in the following code I get all tick marks and iterate through them looking for TickType.MAJOR
. If the value of a major tick mark is not dividable by 5, it gets replaced by a minor tick mark.
结果是,只有可除以5的值会显示在其文本标签中.
As a result, only values dividable by 5 will be shown with their text label.
XYPlot plot = (XYPlot) chart.getPlot();
NumberAxis myAxis = new NumberAxis(plot.getDomainAxis().getLabel()) {
@Override
public List refreshTicks(Graphics2D g2, AxisState state,
Rectangle2D dataArea, RectangleEdge edge) {
List allTicks = super.refreshTicks(g2, state, dataArea, edge);
List myTicks = new ArrayList();
for (Object tick : allTicks) {
NumberTick numberTick = (NumberTick) tick;
if (TickType.MAJOR.equals(numberTick.getTickType()) &&
(numberTick.getValue() % 5 != 0)) {
myTicks.add(new NumberTick(TickType.MINOR, numberTick.getValue(), "",
numberTick.getTextAnchor(), numberTick.getRotationAnchor(),
numberTick.getAngle()));
continue;
}
myTicks.add(tick);
}
return myTicks;
}
};
plot.setDomainAxis(myAxis);
这篇关于JFreechart:以特定单位后的值显示X轴的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!