首页 > 解决方案 > JFreeChart:十字准线标签自定义位置

问题描述

是否可以将十字准线标签放置在自定义位置?我有 x 和 y 十字准线。我希望 y 十字准线标签位于数据点附近(更改标签偏移 X 坐标)。

问题是 RectangleAnchor 没有这样的选项

Crosshair yCrosshair = new Crosshair(Double.NaN, Color.DARK_GRAY, new BasicStroke(0f));
yCrosshair.setLabelAnchor(RectangleAnchor.CENTER);

而且似乎 JFreeChart 完全忽略了标签偏移设置:

Crosshair yCrosshair = new Crosshair(Double.NaN, Color.DARK_GRAY, new BasicStroke(0f));
yCrosshair.setLabelXOffset(5);

我在鼠标侦听器中有标签所需的绘图坐标,但我找不到如何将它应用到标签位置。

标签: jfreechart

解决方案


好的,我已经通过使用 XYPointerAnnotation 解决了我的问题。

XYPointerAnnotation pointer = new XYPointerAnnotation( "", 0, 0, 7.0 * Math.PI / 4.0 ); 
pointer.setTipRadius(3.0); 
pointer.setBaseRadius(15.0); 
pointer.setPaint(Color.blue); 
pointer.setTextAnchor(TextAnchor.HALF_ASCENT_LEFT); 
pointer.setBackgroundPaint(new Color(180, 180, 180, 180));

在鼠标移动事件中,我将注释定位到所需的点

mainPlot.removeAnnotation(pointer);
if ( !sY.isNaN() ) {
    pointer.setX(x);
    pointer.setY(sY);
    pointer.setText("POWER: "+ sY);
    mainPlot.addAnnotation(pointer);
}

推荐阅读