How to change diagram Scale in Java

大兔子大兔子 提交于 2019-12-19 11:28:11

问题


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());
    }

}

回答1:


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's ComponentHandler draws into a BufferedImage that is sized to fill the enclosing JPanel. The background image is rendered in the implementation of paintComponent(). 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.



来源:https://stackoverflow.com/questions/35234941/how-to-change-diagram-scale-in-java

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!