如何在Java中更改图表Scale [英] How to change diagram Scale in Java
问题描述
我有一个图表,其中x轴显示时间,y轴显示我的数据。
我想通过为x轴选择不同的指标来更改比例。例如秒,分钟和小时刻度。默认值为秒。因此,如果我选择分钟,图表应该更小并且更加弯曲。任何的想法? UI没有完成,但是你想假设有x和y轴。参数度确定它应该缩放到秒(度= 1),
分钟(度= 60)或小时(度= 3600)
private void drawLines(Graphics g,ArrayList< Point> points,int degree)抛出异常{
if(points == null || points.isEmpty()){
抛出新的例外(找不到积分!!!);
}
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( ));
}
}
考虑使用。给定以下比例,您可以交叉乘法并求解缺失的坐标,如映射鼠标的完整示例所示坐标到图像中的像素坐标。
view.x:panelWidthInPixels :: model.x: modelXRange
view.y:panelHeightInPixels :: model.y:modelYRange
我不想使用
JFreeChart
。还有其他方法吗?
是的,因为@MadProgrammer 评论,你可以
-
使用上面显示的比例将数据缩放到封闭容器中。引用的示例隔离了基本方法;
JFreeChart
是一个功能齐全的示例。 -
使用
BufferedImage
将渲染图像缩放到封闭容器。这个示例ComponentHandler
绘制成BufferedImage
,其大小可以填充封闭的JPanel
。后台
图像在paintComponent()
的实现中呈现。调整框架大小以查看效果。
I have a diagram in which x axis shows the time and the y axis shows my data. I want to change the Scale by choosing different metrics for x axis. e.g second, minute and hour scale. the default is second. therefore if I choose the minute the diagram should be smaller and be more curved. Any idea? The UI is not completed however you suppose that the there would be x and y axis. The parameter degree determines that It should be scaled to second(degree=1), minute (degree=60) or hour(degree=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());
}
}
Consider using jfreechart, which scales the graph to fill the enclosing container. In the example seen here, the enclosing container is a ChartPanel
that is added to the CENTER
of frame's default BorderLayout
. This will allow the graph to grow and shrink as the enclosing frame is resized.
The general scheme maps model and view coordinates using linear interpolation. Given the following proportions, you can cross-multiply and solve for the missing coordinate, as shown in this complete example that maps mouse coordinates to pixel coordinates in an image.
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?
Yes, as @MadProgrammer comments, you can
Scale the data to the enclosing container using the proportions shown above. The example cited isolates the basic approach;
JFreeChart
is a full featured example.Scale the rendered image to the enclosing container using a
BufferedImage
. This example'sComponentHandler
draws into aBufferedImage
that is sized to fill the enclosingJPanel
. Thebackground
image is rendered in the implementation ofpaintComponent()
. Resize the frame to see the effect.Scale the rendered image to the enclosing container using a transform applied to the graphics context. Typical examples are shown here and here.
这篇关于如何在Java中更改图表Scale的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!