brimborium brimborium - 1 year ago 219
Java Question

How to change/customize Crosshair Overlays for JFreeChart?

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:

current 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:

final result

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

Answer Source

The CrosshairOverlay method drawVerticalCrosshair() renders the crosshair label, as shown here, but it uses 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);

image

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

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download