Line graph with PHP

today i was hired to make a web app that is powered by php & mySQL. Its main purpose is to create graphs based on queries.
More specifically, I will be given a bunch of data on many athletes for an organisation.
Then The user has to be able to view the data in the form of a line graph that is filtered by either search queries entered through an input field or radio/check boxes being clicked/filled.

Examples of these would be by name, age, sport, and gender.

I was wondering if anyone here one SP forums or more specifically the php forum would be kind enough to help me on where to start with plotting the data from the mySQL db’sinto a graph.

Also any help with storing the data with mySQL, accessing it, or coding the filtering to dynamically create a graph would be greatly appreciated :slight_smile:
Oh btw the default graph(s) will be of sports. The x axis being age and The y being strength level. Then there will be 3 graphs of 3 sports (basketball, football, and hockey).

I apologise if anybody is offended by me seeming to ask for free help or code, but spf has always helped and steered me in the right direction in the past And I was hoping I good get some help, ideas, or code to start with :slight_smile:

Please reply I you have any Questions, Comments, or Concerns.

Thanks in Advance & Best Regards,
Team 1504

First off let me say: Woot okemos! My Birthplace!!

Secondly you’ll need to plot the points for the line(s) keeping track of the x,y coordinates of each point to connect then use imageline PHP: imageline - Manual to draw the line connecting them.

You have two main tasks. Get the appropriate data from your database, and display it as a chart.

How to set up your database will depend on your data, but if you have no idea about how to do that then you better start googling or looking at the basics from blogs, articles and tutorials you will find here on SP.

As for the second part I’d recommend you start off by investigating Google Chart tools - plenty of sample code to look at if that is not sophisticated enough for you then look into using a js library, [URL=“”]dojo charting looks particularly easy to use, ymmv.

Also, 20 Best JavaScript Charting and Plotting Libraries

I created a line chart using Imagemagick and assume it could also be done with GD. When looking into this I am sure I found a php class but it was to complicated for what I wanted as it also created bar charts, pie charts etc.

Writing your own gives you more options to add extra data but it would need a lot of work if you want scales etc.
I suppose this could be a project for me in OOP and no doubt I would get some help from Cups :wink:

An example of my charts below. From memory this data came from a text file and I created each line with a function:

Yeah, of course, you could generate server-generated chart images in GD as Rubble states. That had not occurred to me.

Just found my code.

This is the function for the lines:

/****** Function to create the data lines ******/
function chart( $data, $pitch, $colour, $title, $scale ){

// How many items in the array
$count = count( $data );

// Draw the background
$height = "1400";
$width = "1200";

// Find average value
$mean = round( array_sum( $data )/$count, 2 );

// loop through the data creating the points for the polyline
// Set initial value for $cmd
$cmd = "";

// Initian start point x axis
$next = "10";

// Draw the lines
foreach( $data as $value ){

$value1 = round(($value * $scale), 4);
$cmd .= " {$next},{$value1} ";

$next = $next+$pitch;

exec("convert -size {$width}x{$height} xc:none -fill none -stroke $colour -strokewidth 1 -draw \\"polyline $cmd \\" -flip $title.miff ");


This is how the function was called:

// Send chart data to function
chart( $imagemagick, "30", "Red", "ImageMagick", $scale );

// Send chart data to function
chart( $imagick, "30", "Blue", "Imagick", $scale );

// Send chart data to function
chart( $gd, "30", "Green", "gd", $scale );

The contents of $imagemagick etc. were actualy an array.

Its all a bit messy but I just did it for a one off.

P.S. I remember now it drew the lines upside down so I had to use -flip in my command to turn it over!