开发者

Graph and zoom function

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 开发者_JAVA百科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.


JFreeChart has this already built in.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜