tags:

views:

21

answers:

1

I'm making a line graph with PHP using imagecreate() and imageline(). Im' trying to figure out how to do the calculation to find the y-axis for each point on the graph.

Here are a few values that would go in the graph:

$values[jan] = .84215;
$values[feb] = 1.57294;
$values[mar] = 3.75429;

Here is an example of the line graph. The x-axis labels are positioned at the vertical middle of the x-axis lines. The gap between the x-axis lines is 25px.

How would you do the calculation to find the y-axis for the values in the array above?

5.00
4.75
4.50
4.25
4.00
3.75
3.50
3.25
3.00
2.75
2.50
2.25
2.00
1.75
1.50
1.25
1.00
0.75
0.50
0.25
0.00
     Jan  Feb  Mar  apr  may  jun  jul  aug  sep  oct  nov  dec
A: 

You need a way to map any floating point number between [0.00 5.00] to your Y-axis points.

The granularity of your Y-axis is 0.25. So you can divide the input by 0.25 to get the exact point on the Y-axis. But this value could be between two point for example input is 0.3 and 0.3/0.25 is 1.2 and there is not 1.2 on the Y-axis.

To solve this we associate a range +|-0.125 with each Y-axis number. So 1.0 will have the range 0.75 to 1.25, this any input with input/0.25 falling in [0.75 1.25] will have 1.00 as its Y-axis point.

In PHP you can do this as:

$Y_cord = ceil ( $input / 0.25 - 0.125) * 0.25;

You can see the mapping of a random dataset here.

codaddict