Adding a static gridline to a JFreeChart time series chart

旧城冷巷雨未停 提交于 2019-11-27 08:04:45

问题


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.


回答1:


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.




回答2:


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));


来源:https://stackoverflow.com/questions/4407735/adding-a-static-gridline-to-a-jfreechart-time-series-chart

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!