0

How can I show the statistics of data stored in a MySQL table dynamically with PHP image functions? I am able to show the counts in form of bar charts using count query on static tables but as shown in Image given. I have database that stores the logs of server dynamically.

The image is generated from 3 tables and image & draw functions of php

0

3 Answers 3

1

I dont know how you are trying. But there is a nice Jquery plugin called Highcharts. You can use this to create various types of graphs. Please refer the below link if you are interested

http://www.highcharts.com/

1

Rgraph is also a very nice HTML5/Javascript Graph library, easy to use and with lots of options.

http://www.rgraph.net/

var data = ['<?php echo implode("','", $average); ?>']; 

function drawGraph(){
    RGraph.Clear(document.getElementById('myRadar'));
    var radar = new RGraph.Radar('myRadar', data);

This is how you can add a PHP array, $average is the array with the numbers, it's a radar chart in this example. The implode function puts a comma between the values of the array.

0

I have used the following code create a simple bar chart with php. You have to get the bar value from database and put it in $data array. Next have to set the $height and $width for image size. Based on your image size, you have to position the chat.

<?php

$data = array('400', '2570', '245', '473', '1000', '3456', '780', '5000', '30', '420');
$sum = array_sum($data);

$height = 480;
$width  = 640;

$im         = imagecreate($width, $height);
$background = imagecolorallocate($im, 255, 255, 255);

$white  = imagecolorallocate($im, 255, 255, 255);
$black  = imagecolorallocate($im, 0, 0, 0);
$red    = imagecolorallocate($im, 255, 0, 0);
$green  = imagecolorallocate($im, 51, 153, 0);
$yellow = imagecolorallocate($im, 255, 255, 0);

imageline($im, 10, 5, 10, $height - 20, $black);
imageline($im, 10, $height - 20, 620, $height - 20, $black);

header("Content-type: image/png");

$x       = 11;
$y       = 459;
$x_width = 20;
$y_ht    = 0;

$max_i = count($data);
for ($i     = 0; $i < $max_i; $i++) {
    $y_ht = ($data[$i] / $sum) * $height;
    if ($data[$i] > 1000) {
        imagefilledrectangle($im, $x, $y, $x + $x_width, ($y - $y_ht), $green);
    } else if ($data[$i] > 500) {
        imagefilledrectangle($im, $x, $y, $x + $x_width, ($y - $y_ht), $yellow);
    } else {
        imagefilledrectangle($im, $x, $y, $x + $x_width, ($y - $y_ht), $red);
    }
    imagestring($im, 2, $x - 1, ($y - $y_ht - 15), $data[$i], $black);
    $x += ($x_width + 2);
}
imagepng($im);
imagedestroy($im);
?>

ref: http://www.talkphp.com/advanced-php-programming/1629-bar-chart-php.html

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.