Re: Drawing graphs in Java
You need a loop for each axis.
Calculate the maximum number of labels that will fit on the axis by dividing the length (or height) of the axis in pixels by the width (or height) of a label in pixels (including spacing to the next label).
max X labels = axis length / (label width + spacer width)
may Y labels = axis height / (label height + spacer height)
Decide the value increment between labels by dividing the data value range by the max number of labels (if the value range is less than the max number of labels, the increment can be one).
X value increment = X value range / max X labels
Y value increment = Y value range / may Y labels
For each axis, loop through the data range incrementing by the X or Y value increment calculated above.
For the X axis, draw the X value as text at the X value ordinate and a Y ordinate below the X axis line.
For the Y axis, draw the Y value as text at the Y value ordinate and an X ordinate left of the Y axis line.
You may want to adjust the X or Y label ordinates to centre the labels around the X or Y value, and for the Y axis, to right-justify the labels.
Alternatively, to make it look more professional, why not draw tics on the X and Y axes and arrange the labels every 5, or 10, or 20, etc., ticks ?
However you want to do it, I strongly recommend drawing it out on paper first and working out how you want the axis labels or ticks to look, and how you'll calculate the label positions.
But why not use a library like JChart or JGraphT that automatically gives you all that stuff?
If I had eight hours to chop down a tree, I would spend 6 hours sharpening an axe...
Anon.