如何调整xyimageannotation中的图像位置

发布于 2025-01-19 18:59:03 字数 817 浏览 4 评论 0原文

我在 JFreeChart 中有一个图标:

BufferedImage torreIcon = null;

try {
    torreIcon = ImageIO.read(getClass().getClassLoader().getResource(
        "Resources/Imagenes/torre_control_2.png"));
    } catch (IOException e) {
        e.printStackTrace();
    }
}

我已将此图标添加到 XYPlot 中:

XYAnnotation xyannotation = new XYImageAnnotation(0, 1900, torreIcon);
this.xyplot.addAnnotation(xyannotation);

图表中的结果是:

在此处输入图像描述

问题是当我放大时:

<一个href="https://i.sstatic.net/kQO0S.png" rel="nofollow noreferrer">在此处输入图像描述

我希望图标始终位于 x 轴旁边,没有空格,并且大小当然相同。是否可以?

I have a icon in JFreeChart:

BufferedImage torreIcon = null;

try {
    torreIcon = ImageIO.read(getClass().getClassLoader().getResource(
        "Resources/Imagenes/torre_control_2.png"));
    } catch (IOException e) {
        e.printStackTrace();
    }
}

And I have added this icon to the XYPlot:

XYAnnotation xyannotation = new XYImageAnnotation(0, 1900, torreIcon);
this.xyplot.addAnnotation(xyannotation);

And the result in the chart is:

enter image description here

The problem is when I make zoom in:

enter image description here

I would like that the icon always be next to the x axis, without space, and with the same size of course. Is it possible?

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

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

发布评论

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

评论(1

嗳卜坏 2025-01-26 18:59:03

如果我放置 (0, 0) 坐标,则图标中心将放置在 0, 0 处,并且图像不会位于 x 轴上方。

正确的;除非另有说明,否则 XYImageAnnotation 使用 RectangleAnchor.CENTER 在指定数据空间坐标处居中

我手动放置了使图标最初放置在 x 轴上方的坐标,正如我们所看到的 y 坐标是 1900。

相反,指定所需的 <构造 XYImageAnnotation 时的 code>RectangleAnchor:

new XYImageAnnotation(0, 0, image, RectangleAnchor.BOTTOM)

下面的示例将蓝色框架添加到方便的图像中,以便查看图像相对于锚点的绘制方式。原点处的 RectangleAnchor.BOTTOM 如下图所示。当您平移和缩放时,图像保持锚定在原点。尝试其他 RectangleAnchor 实例以查看效果。

image

import java.awt.Color;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.image.BufferedImage;
import javax.swing.Icon;
import javax.swing.JFrame;
import javax.swing.UIManager;
import org.jfree.chart.ChartFactory;
import org.jfree.chart.ChartPanel;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.annotations.XYImageAnnotation;
import org.jfree.chart.plot.XYPlot;
import org.jfree.chart.ui.RectangleAnchor;
import org.jfree.data.xy.XYDataset;
import org.jfree.data.xy.XYSeries;
import org.jfree.data.xy.XYSeriesCollection;

/**
 * @see https://stackoverflow.com/q/71780485/230513
 */
public class XYImageTest {

    private static final int N = 1;

    private XYDataset createDataset() {
        XYSeries series = new XYSeries("Series");
        for (int i = -N; i <= N; i++) {
            series.add(i, i);
        }
        return new XYSeriesCollection(series);
    }

    private JFreeChart createChart(final XYDataset dataset, Image image) {
        JFreeChart chart = ChartFactory.createXYLineChart("Test", "X", "Y", dataset);
        XYPlot plot = (XYPlot) chart.getPlot();
        plot.setDomainPannable(true);
        plot.setRangePannable(true);
        plot.addAnnotation(new XYImageAnnotation(0, 0, image, RectangleAnchor.BOTTOM));
        return chart;
    }

    private void display() {
        Icon icon = UIManager.getIcon("OptionPane.informationIcon");
        BufferedImage image = new BufferedImage(icon.getIconWidth(),
            icon.getIconHeight(), BufferedImage.TYPE_INT_ARGB);
        Graphics g = image.getGraphics();
        g.setColor(Color.blue);
        g.drawRect(0, 0, image.getWidth() - 1, image.getHeight() - 1);
        icon.paintIcon(null, g, 0, 0);
        g.dispose();
        JFrame f = new JFrame("XYImageTest");
        f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        f.add(new ChartPanel(createChart(createDataset(), image)) {
            @Override
            public Dimension getPreferredSize() {
                return new Dimension(400, 300);
            }
        });
        f.pack();
        f.setLocationRelativeTo(null);
        f.setVisible(true);
    }

    public static void main(String[] args) {
        EventQueue.invokeLater(new XYImageTest()::display);
    }
}

If I put (0, 0) coordinates, the icon center is put in 0, 0 and the image is not above x axis.

Correct; unless otherwise specified, an XYImageAnnotation is centered at the specified data space coordinates using RectangleAnchor.CENTER.

I put manually the coordinates that makes the icon put above the x axis initially, and as we can see the y coordinate that make this is 1900.

Instead, specify the desired RectangleAnchor when you construct the XYImageAnnotation:

new XYImageAnnotation(0, 0, image, RectangleAnchor.BOTTOM)

The example below adds a blue frame to a convenient image in order to see how the image is drawn relative to the anchor. RectangleAnchor.BOTTOM at the origin is illustrated below. The image stays anchored to the origin as you pan and zoom. Try the other RectangleAnchor instances to see the effect.

image

import java.awt.Color;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.image.BufferedImage;
import javax.swing.Icon;
import javax.swing.JFrame;
import javax.swing.UIManager;
import org.jfree.chart.ChartFactory;
import org.jfree.chart.ChartPanel;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.annotations.XYImageAnnotation;
import org.jfree.chart.plot.XYPlot;
import org.jfree.chart.ui.RectangleAnchor;
import org.jfree.data.xy.XYDataset;
import org.jfree.data.xy.XYSeries;
import org.jfree.data.xy.XYSeriesCollection;

/**
 * @see https://stackoverflow.com/q/71780485/230513
 */
public class XYImageTest {

    private static final int N = 1;

    private XYDataset createDataset() {
        XYSeries series = new XYSeries("Series");
        for (int i = -N; i <= N; i++) {
            series.add(i, i);
        }
        return new XYSeriesCollection(series);
    }

    private JFreeChart createChart(final XYDataset dataset, Image image) {
        JFreeChart chart = ChartFactory.createXYLineChart("Test", "X", "Y", dataset);
        XYPlot plot = (XYPlot) chart.getPlot();
        plot.setDomainPannable(true);
        plot.setRangePannable(true);
        plot.addAnnotation(new XYImageAnnotation(0, 0, image, RectangleAnchor.BOTTOM));
        return chart;
    }

    private void display() {
        Icon icon = UIManager.getIcon("OptionPane.informationIcon");
        BufferedImage image = new BufferedImage(icon.getIconWidth(),
            icon.getIconHeight(), BufferedImage.TYPE_INT_ARGB);
        Graphics g = image.getGraphics();
        g.setColor(Color.blue);
        g.drawRect(0, 0, image.getWidth() - 1, image.getHeight() - 1);
        icon.paintIcon(null, g, 0, 0);
        g.dispose();
        JFrame f = new JFrame("XYImageTest");
        f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        f.add(new ChartPanel(createChart(createDataset(), image)) {
            @Override
            public Dimension getPreferredSize() {
                return new Dimension(400, 300);
            }
        });
        f.pack();
        f.setLocationRelativeTo(null);
        f.setVisible(true);
    }

    public static void main(String[] args) {
        EventQueue.invokeLater(new XYImageTest()::display);
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文