Adding a static gridline to a JFreeChart time seri

2019-02-25 01:49发布

I am trying to implement a timeseries chart with a peculiar requirement in JFreeChart. I can draw the chart, but I don't know how to implement the vertical red line at the last value in the chart. It should always be in the same spot and should always intersect with the last value.

I am absolutely out of ideas on how this would be done. I was thinking that it might be possible to implement it as a static gridline, but I don't know how to specify one.

Also, the size of the charts will be static, so some roundabout way of doing this is acceptable, hopefully without introducing any 3rd party libraries.

An image of what I am trying to achieve can be found here.

Thanks.

2条回答
孤傲高冷的网名
2楼-- · 2019-02-25 02:22

Well, I solved it using a marker. Here's the code that does it:

JFreeChart chart = ChartFactory.createTimeSeriesChart(...);
XYPlot plot = chart.getXYPlot();
Long timestampToMark = new Date().getTime();
Marker m = new ValueMarker(timestampToMark);
m.setStroke(new BasicStroke(2));
m.setPaint(Color.RED);
plot.addDomainMarker(m);

Maybe someone else will find this useful.

查看更多
手持菜刀,她持情操
3楼-- · 2019-02-25 02:29

I'd just set a custom cross-hair on the last domain value:

XYPlot plot = chart.getXYPlot();
plot.setDomainCrosshairVisible(true);
plot.setDomainCrosshairPaint(Color.red);
plot.setDomainCrosshairStroke(new BasicStroke(3f));
plot.setDomainCrosshairValue(dataset.getXValue(0, dataset.getItemCount(0) - 1));
查看更多
登录 后发表回答