如何更改/自定义JFreeChart的十字线重叠?

时间:2017-05-22 16:47:03

标签: java jfreechart

我正在尝试为XYPlot使用十字准线覆盖。虽然我想改变标签的绘制方式,但效果很好。这是我目前的片段:

// add crosshair
final CrosshairOverlay crosshairOverlay = new CrosshairOverlay();
final Crosshair xCrosshair = new Crosshair(Double.NaN, Color.DARK_GRAY, new BasicStroke(0f));
xCrosshair.setLabelBackgroundPaint(new Color(1f, 1f, 1f, 0f));
xCrosshair.setLabelOutlineVisible(false);
xCrosshair.setLabelVisible(true);
xCrosshair.setLabelGenerator(new CrosshairLabelGenerator() {
    @Override
    public String generateLabel(final Crosshair ch) {
        return UnitConverter.freq2Str(ch.getValue());
    }
});

final Crosshair yCrosshair = new Crosshair(Double.NaN, Color.DARK_GRAY, new BasicStroke(0f));
yCrosshair.setLabelBackgroundPaint(new Color(1f, 1f, 1f, 0f));
yCrosshair.setLabelVisible(true);
yCrosshair.setLabelOutlineVisible(false);
yCrosshair.setLabelGenerator(new CrosshairLabelGenerator() {
    @Override
    public String generateLabel(final Crosshair ch) {
        return UnitConverter.val2Str(ch.getValue(), 5, "dBc/Hz");
    }
});

crosshairOverlay.addDomainCrosshair(xCrosshair);
crosshairOverlay.addRangeCrosshair(yCrosshair);
this.addOverlay(crosshairOverlay);
this.addChartMouseListener(new ChartMouseListener() {
    @Override
    public void chartMouseMoved(final ChartMouseEvent event) {
        final Rectangle2D dataArea = APChartPanel.this.getScreenDataArea();
        final XYPlot plot = (XYPlot) event.getChart().getPlot();
        final double x = plot.getDomainAxis().java2DToValue(event.getTrigger().getX(), dataArea, RectangleEdge.BOTTOM);
        final double y = plot.getRangeAxis().java2DToValue(event.getTrigger().getY(), dataArea, RectangleEdge.LEFT);
        xCrosshair.setValue(x);
        yCrosshair.setValue(y);
    }

    @Override
    public void chartMouseClicked(final ChartMouseEvent arg0) {}
});

这会产生以下标签:

current labels

首先,我想删除文本周围的框,并希望控制字体大小和系列。但是使用Crosshair#setLabelOutlineVisible(boolean)也会删除文字,Crosshair#setLabelFont(Font)根本不会更改字体。这仍然在进行中,还是我在这里做错了什么?

我得到了以下解决方案:

final result

通过@trashgod的建议扩展CrosshairOverlay并将此代码用于单个十字准线:

final Crosshair yCrosshair = new Crosshair(Double.NaN, Color.GRAY, new BasicStroke(0f));
yCrosshair.setLabelBackgroundPaint(new Color(1f, 1f, 1f, 0.7f));
yCrosshair.setLabelPaint(Color.GRAY);
yCrosshair.setLabelVisible(true);
yCrosshair.setLabelOutlineVisible(false);
yCrosshair.setLabelFont(yCrosshair.getLabelFont().deriveFont(11f));
yCrosshair.setLabelGenerator(new CrosshairLabelGenerator() {
    @Override
    public String generateLabel(final Crosshair ch) {
        return UnitConverter.val2Str(ch.getValue(), 5, "dBc/Hz");
    }
});

1 个答案:

答案 0 :(得分:1)

CrosshairOverlay方法drawVerticalCrosshair()呈现十字准线标签,如here所示,但它既不使用Crosshair::isLabelOutlineVisible也不使用Crosshair::getLabelFont。您可以尝试这样的事情:

if (crosshair.isLabelOutlineVisible()) { g2.draw(hotspot); }
g2.setFont(crosshair.getLabelFont());
TextUtilities.drawAlignedString(label, g2, xx, yy, alignPt);

从这个example开始,我进行了以下更改以显示结果。

xCrosshair.setLabelFont(xCrosshair.getLabelFont().deriveFont(20f));
xCrosshair.setLabelBackgroundPaint(new Color(1f, 1f, 1f, 0f));
xCrosshair.setLabelOutlineVisible(false);

image

drawHorizontalCrosshair()中的类似代码也应该更新;保存和恢复图形上下文的字体也可能是有保证的。