# Graphing with PHP and GD Page 3

By Allan Kent
on August 30, 2000

## Line Graphs

To start off we can create a blank 200×200 pixel canvas and fill it with a gray. Onto this we are going to plot our lines. We then allocate a color for each office, red for London, green for Atlanta.
Now we can start getting our data and drawing it. The important thing to keep in mind when drawing lines, is that the `ImageLine` requires us to tell it the x and y co-ordinates of the start and of the end of the line. Now if we are grabbing our data out of a database we will only have one set of co-ordinates at a time. So we could either store our values into an array, or have a temporary variable that we store the previous set of co-ordinates in. I’ve opted to use an array.
Besides the actual co-ordinates that we will be plotting, we will need to know the maximum value that we can attain (the ceiling of our graph) as well as the number of columns of information (the number of plots along our x axis). So after connecting to the database, we select the MAX(g_num) from our database and store that away for later use. We then select all of the records where the g_team is ‘London’ and grab the number of rows that were returned, so that we will know how many columns of data we are working with. From our table above, you can see that we will be working with 6 months or columns of data, and that the maximum value we ever have is 410
``` <?php     \$columns = 6;     \$max = 410; ```

```?> ```

The x value is always going to be the easiest – that will range from 0 to the width of our image, in this case 200. We’ll start our line at 0 and want to end our line at 200, so after starting with \$x=0 we’ll need to increment \$x by an amount (\$columns-1) times (in this case 5) to get to 200 – which will give us 6 plots, with the last being on 200.
With this in mind we can create a variable called `\$xincrement` and give that a value of 200 divided by (\$columns-1).
We’ll then give `\$x` a starting value of 0 and at the bottom of each loop while we’re grabbing the rows of our result set we can increment `\$x` by `\$xincrement`.
``` <?php       \$xincrement = bcdiv(200,\$columns-1,0); ```

```?> ```

bcdiv divides the first operand by the second and returns a value with third operand decimal spaces. To use the `BCMath` functions you will need to compile it into PHP under Unix, but the Windows version has it already compiled in. `README.BCMATH` in the root directory of the PHP source will explain the where and how.
The y value is going to be a proportionate amount of `\$max`, so what we can do is divide the `g_num` amount by `\$max` to get the proportional fraction, and then multiply that by 200 – the height of our image.
``` <?php     \$y = bcmul(bcdiv(\$salesRow,\$max,2),200,2); ```

```?> ```

`\$salesRow` is `g_num` for the current row, dividing that by `\$max` and getting a result with 2 decimal places. We then use bcmul to multiply the result by 200.
Then what we can do is loop around through the array, using the current element as the start of the line, and current element +1 as the end co-ordinates of the line. Because the last element will never be the start of a line, we must cut the loop short by one, so instead of looping around while less than or equal to `\$columns-1`, we loop around while less than `\$columns-1`.

﻿