烛台图表上的注释不起作用

Annotations on candlestick chart not working

我正在尝试向图表添加注释。它似乎是添加的,因为如果我再添加一个,绘图注释列表的 size() 会增加。问题是没有显示。

OHLCDataset candles = createCandleDataset();

// Create chart
chart = ChartFactory.createCandlestickChart(
    "mychart", "", "", candles, true);

XYPlot plot = (XYPlot) chart.getPlot();        

XYShapeAnnotation a1 = new XYShapeAnnotation(
    new Rectangle2D.Double(10.0, 20.0, 20.0, 30.0),
    new BasicStroke(1.0f), Color.blue);
plot.addAnnotation(a1);

ChartPanel panel = new ChartPanel(chart);
setContentPane(panel);

有什么想法吗?

XYShapeAnnotation API 说:

The shape coordinates are specified in data space.

您的 Rectangle2D 坐标相对于您的实际数据可能不明显。相反,使用来自 OHLCDataset to construct your annotation. Focusing on the second item in series1 in this example 的坐标,下图说明了从基础 OHLCSeries 检索数据以创建一个周期宽且跨越 high/low 值的注释。

// series
addSeries1();
OHLCSeries series = seriesCollection.getSeries(0);
OHLCItem item = (OHLCItem) series.getDataItem(1);
RegularTimePeriod t = item.getPeriod();
long x = t.getFirstMillisecond();
long w = t.getLastMillisecond() - t.getFirstMillisecond(); 
double y = item.getLowValue();
double h = item.getHighValue() - y;
XYShapeAnnotation a1 = new XYShapeAnnotation(
    new Rectangle2D.Double(x, y, w, h),
    new BasicStroke(1f), Color.blue
);
chart.getXYPlot().addAnnotation(a1);

OHLCDataset 的其他实现有相应的访问器。