图形和缩放功能

发布于 2024-10-31 12:20:21 字数 2135 浏览 0 评论 0原文

我想在我的图表中添加缩放功能。 例如,如果我的图表的 maxY 是 5000,并且我围绕中心缩放 200 米的距离,则我的轴应代表从 2400 到 2600 的值,中心为 2500。

private class ZoomHandler implements MouseWheelListener {

    public void mouseWheelMoved(MouseWheelEvent e) {
        double distanceY = maxY;

        double cursorY = maxY / 2.0;

        int rotation = e.getWheelRotation();
        if (rotation < 0) {
            distanceY = 200 / 2;
        } else {
            distanceY = 200 * 2;
        }
        maxY = cursorY + distanceY;

        repaint();
    }
}

有人可以帮助进行数学运算吗? 谢谢。

图形是这样绘制的:

private void drawAxis(Graphics2D g2) {
    FontMetrics metrics = g2.getFontMetrics();
    double axisH = yPositionToPixel(originY);
    double axisV = xPositionToPixel(originX);

    g2.drawLine(0 + V_BORDER, (int) axisH, getWidth(), (int) axisH);

    g2.drawLine((int) axisV, (int) axisH, (int) axisV, getHeight() - H_BORDER);
}

protected double yPositionToPixel(double position) {
    double height = (double) getHeight();

    return pixelOriginY + (position) / (maxY) * (height - (pixelOriginY + H_BORDER));
}

protected double xPositionToPixel(double position) {
    double width = getWidth();
        return (width) - pixelOriginX - (position - minX) / (maxX - minX) * (width - (pixelOriginX + V_BORDER));
}

private void drawHorizontalLabels(Graphics2D g2) {
    double axisV = xPositionToPixel(originX);
    FontMetrics metrics = g2.getFontMetrics();
    for (double y = originY + majorY; y < maxY + majorY; y += majorY) {
        int position = (int) yPositionToPixel(y);
        if (rightToLeft) {
            g2.drawString(formatter.format(y), (int) axisV + 5, position);
    }}

构造函数如下所示:

public Graph(double originX, double originY, double pixelOriginX, double pixelOriginY,
        double minX, double maxX,
        double minY, double maxY,
        double majorX, double majorY, String labelXaxis, String labelYaxis) {}

这就是我初始化它的方式:

new GraphDisplay(0.0, 0.0, 40, 100, -0.1, 120, -0.1, 5000, 20, 1000);

GraphDisplay 扩展了 Graph。

I would like to add a zooming function in my graph.
For exemple, if the maxY of my graph is 5000, and i zoom a distance of 200 meters around the center, my axis should represent values from 2400 to 2600, the center at being 2500.

private class ZoomHandler implements MouseWheelListener {

    public void mouseWheelMoved(MouseWheelEvent e) {
        double distanceY = maxY;

        double cursorY = maxY / 2.0;

        int rotation = e.getWheelRotation();
        if (rotation < 0) {
            distanceY = 200 / 2;
        } else {
            distanceY = 200 * 2;
        }
        maxY = cursorY + distanceY;

        repaint();
    }
}

Can somebody help with math operation ?
Thank you.

The graph is draw this way:

private void drawAxis(Graphics2D g2) {
    FontMetrics metrics = g2.getFontMetrics();
    double axisH = yPositionToPixel(originY);
    double axisV = xPositionToPixel(originX);

    g2.drawLine(0 + V_BORDER, (int) axisH, getWidth(), (int) axisH);

    g2.drawLine((int) axisV, (int) axisH, (int) axisV, getHeight() - H_BORDER);
}

protected double yPositionToPixel(double position) {
    double height = (double) getHeight();

    return pixelOriginY + (position) / (maxY) * (height - (pixelOriginY + H_BORDER));
}

protected double xPositionToPixel(double position) {
    double width = getWidth();
        return (width) - pixelOriginX - (position - minX) / (maxX - minX) * (width - (pixelOriginX + V_BORDER));
}

private void drawHorizontalLabels(Graphics2D g2) {
    double axisV = xPositionToPixel(originX);
    FontMetrics metrics = g2.getFontMetrics();
    for (double y = originY + majorY; y < maxY + majorY; y += majorY) {
        int position = (int) yPositionToPixel(y);
        if (rightToLeft) {
            g2.drawString(formatter.format(y), (int) axisV + 5, position);
    }}

The constructor look like this :

public Graph(double originX, double originY, double pixelOriginX, double pixelOriginY,
        double minX, double maxX,
        double minY, double maxY,
        double majorX, double majorY, String labelXaxis, String labelYaxis) {}

And this is how i initialise it :

new GraphDisplay(0.0, 0.0, 40, 100, -0.1, 120, -0.1, 5000, 20, 1000);

GraphDisplay extends Graph.

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

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

发布评论

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

评论(1

一生独一 2024-11-07 12:20:21

JFreeChart 已内置此功能。

JFreeChart has this already built in.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文