图形和缩放功能
我想在我的图表中添加缩放功能。 例如,如果我的图表的 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
JFreeChart
已内置此功能。JFreeChart
has this already built in.