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.
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.
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