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