烛台图上的注释不起作用

问题描述:

我正在尝试向图表添加注释.好像已经添加了,因为如果再添加一个,则图的注释列表的size()会增加.问题是它没有被显示.

I'm trying to add annotations to a chart. It seems like it's added, since the size() of the plot's annotations list increases if I add one more. The problem is that it's not being displayed.

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

有什么想法吗?

The XYShapeAnnotation API says:

形状坐标在数据空间中指定.

The shape coordinates are specified in data space.

Rectangle2D的坐标相对于实际数据可能不明显.相反,请使用 OHLCDataset 来构造您的注释.下面的图表着重于本示例series1中的第二项,下图说明了从基础OHLCSeries中检索数据到创建一个周期为一个周期的注释,该注释跨越高/低值.

The coordinates of your Rectangle2D may be inapparent relative to your actual data. Instead, use coordinates from your OHLCDataset to construct your annotation. Focusing on the second item in series1 in this example, the chart below illustrates retrieving data from the underlying OHLCSeries to create an annotation one period wide and spanning the high/low value.

// 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 具有相应的访问器.

Other implementations of OHLCDataset have corresponding accessors.