如何更改/自定义JFreeChart的十字线叠图? [英] How to change/customize Crosshair Overlays for JFreeChart?
问题描述
我正在尝试将十字线叠加层用于XYPlot.尽管我想更改标签的绘制方式,但效果很好.这是我当前的代码段:
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) {}
});
这将产生以下标签:
首先,我想删除文本周围的框,并希望控制字体大小和字体.但是使用Crosshair#setLabelOutlineVisible(boolean)
也会删除文本,并且Crosshair#setLabelFont(Font)
完全不会更改字体.这仍在进行中还是我在这里做错了什么?
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:
通过使用@trashgod的建议扩展CrosshairOverlay
并将此代码用于单个十字准线:
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");
}
});
推荐答案
drawVerticalCrosshair()
呈现十字线标签,如下所示此处,但以前既未使用Crosshair::isLabelOutlineVisible
也未使用Crosshair::getLabelFont
.您可以尝试这样的事情:
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);
drawHorizontalCrosshair()
中的类似代码也应更新;保存和还原图形上下文的字体也可能是必要的.
Similar code in drawHorizontalCrosshair()
should also be updated; saving and restoring the graphics context's font may also be warranted.
这篇关于如何更改/自定义JFreeChart的十字线叠图?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!