JFreechart系列工具提示上方形状标注

发布于 2024-11-25 14:02:53 字数 111 浏览 2 评论 0原文

我有一个 XYPlot,其上有系列和几个动态添加的形状注释,没有填充(因此每个系列点都是可见的)。是否可以在注释上显示系列工具提示(显示鼠标指针当前指向的系列点的坐标)?或者如何重新排列元素以使工具提示可见。

I have an XYPlot on which are series and a couple of dynamically added shape annotations with no fill (hence each of the series points are visible). Is it possible to display the series tool tips(that show the coordinate of the series point over which the mouse pointer is currently pointing to) over the annotations? Or how can I re-arrange the elements in order to make the tooltip visible.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

因为看清所以看轻 2024-12-02 14:02:53

我怀疑您正在将形状注释添加到图中,它们是最后绘制的。相反,请将它们添加到 Layer.BACKGROUND 中的渲染器中。如下所示,圆圈没有遮挡 (20, 20) 处的工具提示。另请注意 (10, 10) 不会 受到线注释的影响,而 (30, 30) 会被圆弧遮挡。

image

import java.awt.BasicStroke;
import java.awt.Color;
import java.awt.geom.Arc2D;
import java.awt.geom.Ellipse2D;
import java.util.Random;
import org.jfree.chart.ChartFactory;
import org.jfree.chart.ChartFrame;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.annotations.XYLineAnnotation;
import org.jfree.chart.annotations.XYShapeAnnotation;
import org.jfree.chart.plot.PlotOrientation;
import org.jfree.chart.plot.XYPlot;
import org.jfree.chart.renderer.xy.XYLineAndShapeRenderer;
import org.jfree.data.xy.XYDataset;
import org.jfree.data.xy.XYSeries;
import org.jfree.data.xy.XYSeriesCollection;
import org.jfree.ui.Layer;

/**
 * @see http://stackoverflow.com/questions/6797012
 * @see http://stackoverflow.com/questions/6604211
 */
public class ArcTest {

    private static final Random r = new Random();
    private static final Color blue = Color.blue;
    private static final BasicStroke stroke = new BasicStroke(2.0f);
    private static final double PI = 180d;
    private static final int X = 8;
    private static final int Y = 0;
    private static final int W = 6 * X;
    private static final int H = 3 * X;

    public static void main(String[] args) {
        JFreeChart chart = ChartFactory.createXYLineChart(
            "ArcTest", "X", "Y", createDataset(),
            PlotOrientation.VERTICAL, true, true, false);
        XYPlot plot = chart.getXYPlot();

        XYLineAndShapeRenderer renderer =
            (XYLineAndShapeRenderer) plot.getRenderer();
        renderer.setBaseShapesVisible(true);
        Ellipse2D.Double circle = new Ellipse2D.Double(X, X, 20, 20);
        renderer.addAnnotation(new XYShapeAnnotation(
            circle, stroke, blue), Layer.BACKGROUND);

        XYLineAnnotation line = new XYLineAnnotation(X, Y, X, H, stroke, blue);
        plot.addAnnotation(line);
        Arc2D.Double arc = new Arc2D.Double(X, Y, W, 2 * H, PI, PI, Arc2D.OPEN);
        plot.addAnnotation(new XYShapeAnnotation(arc, stroke, blue));

        ChartFrame frame = new ChartFrame("Test", chart);
        frame.pack();
        frame.setVisible(true);
    }

    private static XYDataset createDataset() {
        XYSeriesCollection result = new XYSeriesCollection();
        XYSeries series = new XYSeries("ArcTest");
        series.add(0, 0);
        series.add(10, 10);
        series.add(20, 20);
        series.add(30, 30);
        series.add(W, W);
        result.addSeries(series);
        return result;
    }
}

I suspect you are adding the shape annotations to the plot, where they are drawn last. Instead, add them to the renderer in Layer.BACKGROUND. As shown below, the circle does not obscure the tool tip at (20, 20). Note also how (10, 10) is not affected by the line annotation, while (30, 30) is obscured by the arc.

image

import java.awt.BasicStroke;
import java.awt.Color;
import java.awt.geom.Arc2D;
import java.awt.geom.Ellipse2D;
import java.util.Random;
import org.jfree.chart.ChartFactory;
import org.jfree.chart.ChartFrame;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.annotations.XYLineAnnotation;
import org.jfree.chart.annotations.XYShapeAnnotation;
import org.jfree.chart.plot.PlotOrientation;
import org.jfree.chart.plot.XYPlot;
import org.jfree.chart.renderer.xy.XYLineAndShapeRenderer;
import org.jfree.data.xy.XYDataset;
import org.jfree.data.xy.XYSeries;
import org.jfree.data.xy.XYSeriesCollection;
import org.jfree.ui.Layer;

/**
 * @see http://stackoverflow.com/questions/6797012
 * @see http://stackoverflow.com/questions/6604211
 */
public class ArcTest {

    private static final Random r = new Random();
    private static final Color blue = Color.blue;
    private static final BasicStroke stroke = new BasicStroke(2.0f);
    private static final double PI = 180d;
    private static final int X = 8;
    private static final int Y = 0;
    private static final int W = 6 * X;
    private static final int H = 3 * X;

    public static void main(String[] args) {
        JFreeChart chart = ChartFactory.createXYLineChart(
            "ArcTest", "X", "Y", createDataset(),
            PlotOrientation.VERTICAL, true, true, false);
        XYPlot plot = chart.getXYPlot();

        XYLineAndShapeRenderer renderer =
            (XYLineAndShapeRenderer) plot.getRenderer();
        renderer.setBaseShapesVisible(true);
        Ellipse2D.Double circle = new Ellipse2D.Double(X, X, 20, 20);
        renderer.addAnnotation(new XYShapeAnnotation(
            circle, stroke, blue), Layer.BACKGROUND);

        XYLineAnnotation line = new XYLineAnnotation(X, Y, X, H, stroke, blue);
        plot.addAnnotation(line);
        Arc2D.Double arc = new Arc2D.Double(X, Y, W, 2 * H, PI, PI, Arc2D.OPEN);
        plot.addAnnotation(new XYShapeAnnotation(arc, stroke, blue));

        ChartFrame frame = new ChartFrame("Test", chart);
        frame.pack();
        frame.setVisible(true);
    }

    private static XYDataset createDataset() {
        XYSeriesCollection result = new XYSeriesCollection();
        XYSeries series = new XYSeries("ArcTest");
        series.add(0, 0);
        series.add(10, 10);
        series.add(20, 20);
        series.add(30, 30);
        series.add(W, W);
        result.addSeries(series);
        return result;
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文