如何在JFreeChart图表上画线?

我有可更新的OHLCChart。 我需要在图表上划一条线。

怎么实现呢?

如果要在轴上的给定位置绘制垂直或水平线,可以使用ValueMarker :

ValueMarker marker = new ValueMarker(position); // position is the value on the axis marker.setPaint(Color.black); //marker.setLabel("here"); // see JavaDoc for labels, colors, strokes XYPlot plot = (XYPlot) chart.getPlot(); plot.addDomainMarker(marker); 

如果要绘制水平线,请使用plot.addRangeMarker()

如果你想绘制一个线指示器(例如移动平均线),这样的东西应该有效:

  XYDataset dataSet = // your line dataset CombinedDomainXYPlot plot = (CombinedDomainXYPlot) chart.getPlot(); XYPlot plot = (XYPlot) plot.getSubplots().get(0); int dataSetIndx = plot.getDatasetCount(); plot.setDataset(dataSetIndx, dataSet); XYLineAndShapeRenderer lineRenderer = new XYLineAndShapeRenderer(true, false); plot.setRenderer(dataSetIndx, lineRenderer);