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, minu
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.