How to change/customize Crosshair Overlays for JFreeChart?

ε祈祈猫儿з 提交于 2021-01-28 01:50:52

问题


I am trying to use a crosshair overlay for an XYPlot. This works quite well, though I would like to change the way the label is drawn. This is my current snippet:

// 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) {}
});

This results in the following labels:

As a start, I would like to remove the box around the text and would like to control the font size and family. But using Crosshair#setLabelOutlineVisible(boolean) will also remove the text and Crosshair#setLabelFont(Font) doesn't change the font at all. Is this still work in progress or am I doing something wrong here?


I got to the following solution:

by extending CrosshairOverlay with the suggestions of @trashgod and using this code for an individual crosshair:

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:


The CrosshairOverlay method drawVerticalCrosshair() renders the crosshair label, as shown here, but it previously used neither Crosshair::isLabelOutlineVisible nor Crosshair::getLabelFont. You might try something like this:

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

Starting from this example, I made the following changes to get the result shown.

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

Similar code in drawHorizontalCrosshair() should also be updated; saving and restoring the graphics context's font may also be warranted.



来源:https://stackoverflow.com/questions/44118184/how-to-change-customize-crosshair-overlays-for-jfreechart

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!