gpt4 book ai didi

java - 如何在Java中更改图表比例

转载 作者:行者123 更新时间:2023-11-30 03:04:06 25 4
gpt4 key购买 nike

enter image description here

我有一个图表,其中 x 轴显示时间,y 轴显示我的数据。我想通过为 x 轴选择不同的指标来更改比例。例如秒、分钟和小时刻度。默认是第二个。因此,如果我选择分钟,图表应该更小并且更弯曲。任何想法? UI 尚未完成,但您认为会有 x 轴和 y 轴。参数 Degree 决定了它应该缩放到秒( Degree=1 ),分钟(度=60)或小时(度=3600)

private void drawLines(Graphics g, ArrayList<Point> points,int degree) throws Exception {

if (points == null || points.isEmpty()) {
throw new Exception("No points found!!!");
}

for (int i = 0; i < points.size() - 1; i++) {

Point firstPoint = points.get(i);
Point secondPoint = points.get(i + 1);

g.drawLine((int) firstPoint.getX(), (int) firstPoint.getY(), (int) secondPoint.getX(),
(int) secondPoint.getY());
}

}

最佳答案

考虑使用 ,缩放图形以填充封闭容器。在示例中看到 here ,封闭容器是一个 ChartPanel,它添加到框架默认 BorderLayoutCENTER 中。这将允许图形随着封闭框架大小的调整而增大和缩小。

image

一般方案使用linear interpolation映射模型和 View 坐标。 。给定以下比例,您可以叉乘并求解缺失的坐标,如完整的example所示将鼠标坐标映射到图像中的像素坐标。

view.x : panelWidthInPixels :: model.x : modelXRange
view.y : panelHeightInPixels :: model.y : modelYRange

I do not want to use the JFreeChart. Is there any other way?

是的,如@MadProgrammer comments ,你可以

  • 使用上面显示的比例将数据缩放到封闭容器。 example引用孤立的基本方法; JFreeChart是一个功能齐全的示例。

  • 使用 BufferedImage 将渲染的图像缩放到封闭容器。这个example's ComponentHandler 绘制到 BufferedImage 中,该图像的大小可填充封闭的 JPanel背景图像在paintComponent()的实现中渲染。调整框架大小以查看效果。

  • 使用应用于图形上下文的变换将渲染图像缩放到封闭容器。典型示例如下 herehere .

关于java - 如何在Java中更改图表比例,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35234941/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com