【问题标题】:Annotations on candlestick chart not working烛台图上的注释不起作用
【发布时间】:2019-06-13 11:03:45
【问题描述】:

我正在尝试向图表添加注释。似乎它被添加了,因为如果我再添加一个,情节的注释列表的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);

有什么想法吗?

【问题讨论】:

    标签: java jfreechart


    【解决方案1】:

    XYShapeAnnotation API 说:

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

    Rectangle2D 的坐标相对于您的实际数据可能不明显。相反,请使用您的 OHLCDataset 中的坐标来构建您的注释。关注 series1 在此 example 中的第二项,下图说明了从底层 OHLCSeries 检索数据以创建一个周期宽并跨越高/低值的注释。

    // 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 的其他实现有相应的访问器。

    【讨论】:

    • 好吧,我假设坐标 (0, 0) 会在图的左上角...我会试试你的代码!
    • 哦,我明白了.. Y 坐标是price,X 坐标是milliseconds。所以因为我使用日线图,我的 X 值很大,比如3600 * 1000 * 24
    • @BakedInhalf:完全正确;我已更新片段以使用目标项目的期间。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-08-14
    • 1970-01-01
    • 2018-05-13
    • 2021-02-07
    • 1970-01-01
    • 2013-09-20
    • 2020-10-09
    相关资源
    最近更新 更多