1

enter image description here

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

}
mKorbel
  • 109,525
  • 20
  • 134
  • 319
SSD
  • 359
  • 1
  • 3
  • 15

1 Answers1

2

Consider using , 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.

image

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.

Community
  • 1
  • 1
trashgod
  • 203,806
  • 29
  • 246
  • 1,045