Jump to content

php graph


redarrow

Recommended Posts

advance thank you.

Can you please advise me i have been reading on creating a graph and i am stuck.

i need to design a grapth that will have the number of months on and show the number of hits on that month.

example.
ect date till 31st
4
3
2
1
0
  jan feb march april may jun july aug sep oct nov dec

can it be done were do i go to get a proper tutoral to use to get this together in php cheers.
Link to comment
Share on other sites

thank you andy

andy all i get is a line but why please help cheers.

this is an example from the url provided from andy.

[code]
<?php

$bar_w = 100;
$max_y = 300;
$margin_top = 20;
$margin_bottom = 20;
$margin_left = 20;
$margin_right = 20;
$y_div = 10;
$rects = array();

$last_x2 = $margin_left;
$i = 1;
$pt = "pt".$i;
while (isset($_GET[$pt])) {
$x1 = $last_x2 + 1;
$x2 = $x1 + $bar_w;
$y1 = $margin_top + $max_y - $_GET[$pt];
$y2 = $margin_top + $max_y;
$ar = array($x1, $y1, $x2, $y2);
array_push($rects, $ar);
$i++;
$last_x2 = $x2;
$pt = "pt".$i;
}
$img_w = $last_x2 + $margin_right;
$img_h = $margin_top + $max_y + $margin_bottom;

$ih = imagecreate($img_w, $img_h);

$black = imagecolorallocate($ih, 0, 0, 0);
$white = imagecolorallocate($ih, 255, 255, 255);

imagefill($ih, 0, 0, $white);
for ($r = 0; $r < count($rects); $r++) {
$red = rand(0, 255);
$green = rand(0, 255);
$blue = rand(0, 255);
$hist_color = imagecolorallocate($ih, $red, $green, $blue);
imagefilledrectangle($ih, $rects[$r][0], $rects[$r][1], $rects[$r][2], $rects[$r][3], $hist_color);
imageline($ih, $rects[$r][2], $margin_top + $max_y, $rects[$r][2], $margin_top + $max_y + 3, $black);
$ttfbox = imagettfbbox(8, 0, 'c:\winnt\fonts\arial.ttf', "pt" . ($r + 1));
$half_pt = ($bar_w/2) - ceil(($ttfbox[4] - $ttfbox[6])/2);
imagettftext($ih, 8, 0, $rects[$r][0] + $half_pt, $rects[$r][3] + 10, $black, 'c:\winnt\fonts\arial.ttf', "pt" . ($r

+ 1));
}

imageline($ih, $margin_left, $margin_top, $margin_left, $margin_top + $max_y + 3, $black);
imageline($ih, $margin_left, $margin_top + $max_y, $last_x2, $margin_top + $max_y, $black);
$tick = 0;
while ($tick < $max_y) {
$tick += $y_div;
$tick_y = $margin_top + $max_y - $tick;
imageline($ih, $margin_left - 3, $tick_y, $margin_left, $tick_y, $black);
}

imagepng($ih);
imagedestroy($ih);

?>
[/code]
Link to comment
Share on other sites

what i am going to do is this if it's possable and thank you.

setup a database feild called chart_data in chart data have the user id then jan to dec and every time a user looks at a profile update the correct current month to increment by one and then show the information via a chart.

is that possable with your link and thank you.
Link to comment
Share on other sites

like this example m8 from your link but how does it get the results from the database.?

include('baaChart.php');
$mygraph = new baaChart(600);
$mygraph->setTitle('Regional Sales','Jan - Jun 2002');
$mygraph->setXLabels("Jan,Feb,Mar,Apr,May,Jun");
$mygraph->addDataSeries('C',COLS_STACKED,"25,30,35,40,30,35","South");
$mygraph->addDataSeries('C',0,"65,70,80,90,75,48","North");
$mygraph->addDataSeries('C',0,"12,18,25,20,22,30","West");
$mygraph->addDataSeries('C',0,"50,60,75,80,60,75","East");
$mygraph->addDataSeries('L',3,"30,45,50,55,52,60","Europe");
$mygraph->setBgColor(0,0,0,1);  //transparent background
$mygraph->setChartBgColor(0,0,0,1);  //as background
$mygraph->setXAxis("Month",1);
$mygraph->setYAxis("Sales (£000)",0,250,50,1);
$mygraph->drawGraph();
Link to comment
Share on other sites

That example was for a stacked column chart. You would only need a simpler single series chart.

Easiest way is to read the data into an array , value for each month.

[code]<?php
include('baaChart.php');

$data = array (
        300, 500, 600, 450, 400, 200, 750, 670, 420, 375, 250, 400
    );

  $mygraph = new baaChart(600);
  $mygraph->setTitle('Hits per Month');
  $mygraph->setXLabels("Jan,Feb,Mar,Apr,May,Jun,Jul,Aug,Sep,Oct,Nov,Dec");
  $mygraph->addDataSeries('C',0,$data,"Hits");
  $mygraph->setXAxis("Month",1);
  $mygraph->setYAxis("Hits",0,250,50,1);
  $mygraph->drawGraph(); 
?>[/code]
Link to comment
Share on other sites

thank you as you can see from my code below each month will get a hit depending on the month it is, so how can i set your code up to also output the correct result.

thank you so much for your help.


[code]
<?php

$month=date("m");

$stat="15";

if($month=="01"){

$jan=$jan+1;

}elseif($month=="02"){

$feb=$feb+1;

}elseif($month=="03"){

$mar=$mar+1;

}elseif($month=="04"){

$apr=$apr+1;

}elseif($month=="05"){

$may=$may+1;

}elseif($month=="06"){

$jun=$jun+1;

}elseif($month=="07"){

$jul=$jul+1;

}elseif($month=="08"){

$aug=$aug+1;

}elseif($month=="09"){

$sep=$sep+1;

}elseif($month=="10"){

$oct=$oct+1;

}elseif($month=="11"){

$nov=$nov+1;

}elseif($month=="12"){

$dec=$dec+1;
}

?>
[/code]
Link to comment
Share on other sites

Life is so much easier with arrays

[code]
<?php
include('baaChart.php');

$data = array (
        300, 500, 600, 450, 400, 200, 750, 670, 420, 375, 250, 400
    );
   
$month = date('n')-1;      // get current month number
$data[$month]++;        // increment that month's count by 1

  $mygraph = new baaChart(600);
  $mygraph->setTitle('Hits per Month');
  $mygraph->setXLabels("Jan,Feb,Mar,Apr,May,Jun,Jul,Aug,Sep,Oct,Nov,Dec");
  $mygraph->addDataSeries('C',0,$data,"Hits");
  $mygraph->setXAxis("Month",1);
  $mygraph->setYAxis("Hits",0,1000,100,1);
  $mygraph->drawGraph(); 
?>[/code]
Link to comment
Share on other sites

thank you last question

is this the correct code that will update the correct database field
this would be for the update page for the stats but does it look ok cheers.

[code]
<?php

database connection

$update_array=array("jan","feb","mar","apr","may","jun","jul"
,"aug","sep","oct","nov","dec");

$date=date("m");

if($date==$update_array){

$update_array=$update_array+1;

$query="UPDATE hits set jan='$jan', feb='$feb', mar='$mar',
apr='$apr',may='$may',jun='$jun',jul='$jul',aug='$aug',sep='$sep'
oct='$oct',nov='$nov',dec='$dec'";

$result=mysql_query($query);

}

?>
[/code]
Link to comment
Share on other sites

Thank you so much your such a good php mysql programmer, what you show me tonight as i am in the uk is about 1 book but you show me in a few sentences thank you.

as your such a good guru can you kindly if you got time point me in the correct direction to learn joins as i find them so confuessing.

once agin thank you and all the best redarrow.

Link to comment
Share on other sites

If you want a quick and easy way to produce a graph, save this code as "bar.php" in same folder as the script below it
[code]
<?php
// set dimensions
    $w = 300;
    $h = 12;
// create image
    $im = imagecreate($w, $h);
// set colours to be used
    $bg = imagecolorallocate($im, 0xE0, 0xE0, 0xE0);
    $black = imagecolorallocate($im, 0x00, 0x00, 0x00);
    $barcolor  = imagecolorallocate($im, 0xFF, 0xFF, 0x00);
// draw border
    imagerectangle($im, 0,0,$w-1,$h-1,$black);
// get value and max value from query string
    $val = $_GET['val'];
    $max = $_GET['max'];
// calculate dimensions of inner bar
    $barw = $max ? floor(($w-2) * $val / $max) : 0;
    $barh = $h - 2;
// draw inner bar
if ($barw)
    imagefilledrectangle($im, 1, 1, $barw, $barh, $barcolor);
// send image header
    header("content-type: image/png");
// send png image
    imagepng($im);
    imagedestroy($im);
?>
[/code]

Now try this script

[code]<?php
$data = array (
        300, 500, 600, 450, 400, 200, 750, 670, 420, 375, 250, 400
    );
$months = array (
        'Jan', 'Feb', 'Mar',
        'Apr', 'May', 'Jun',
        'Jul', 'Aug', 'Sep',
        'Oct', 'Nov', 'Dec',
    );
   
echo "<table>\n";
foreach ($data as $m => $hits) {
    echo "<tr>
            <td>{$months[$m]}</td>
            <td><img src='bar.php?val=$hits&max=1000'></td>
            <td>$hits</td>
            </tr>\n";
}
echo "</table>\n";
?>
[/code]
Link to comment
Share on other sites

thank you i get what you mean know and yes i will use you code as provided and try to learn the other page you made but i must say looks grate thank you.

you see the first array is it possable to get from the database all the information in that array

so whats in my database will be the lines total cheers.
example only.

[code]

<?php

database connection

while($record=mysql_fetch_assoc($result)){

$jan=$record['jan'];

$data = array ($jan, 500, 600, 450, 400, 200, 750, 670, 420, 375, 250, 400
    );
$months = array (
        'Jan', 'Feb', 'Mar',
        'Apr', 'May', 'Jun',
        'Jul', 'Aug', 'Sep',
        'Oct', 'Nov', 'Dec',
    );
   
echo "<table>\n";
foreach ($data as $m => $hits) {
    echo "<tr>
            <td>{$months[$m]}</td>
            <td><img src='bar.php?val=$hits&max=1000'></td>
            <td>$hits</td>
            </tr>\n";
}
echo "</table>\n";
}
?>
[/code]
Link to comment
Share on other sites

To pull data from your table and use the bar.php graph method
[code]<?php
$query = "SELECT jan, feb, mar, apr, may, jun, jul, aug, sep, oct, nov, dec
      FROM hits";
$result = mysql_query($query);
$data = mysql_fetch_assoc($result);

echo "<table>\n";
foreach ($data as $month => $hits) {
    echo "<tr>
            <td>$month</td>
            <td><img src='bar.php?val=$hits&max=1000'></td>
            <td>$hits</td>
            </tr>\n";
}
echo "</table>\n";
?>
[/code]
Link to comment
Share on other sites

done cheers.

i love it your the best thank you.
barand can you kindly deatail this even more as i got it on a white board at home and trying to study it but it's really hard i also have you instuctions but that goes far but not as far as i need cheers.

ps only if you got time thank you.
[code]
<?php
// set dimensions
    $w = 300;
    $h = 12;
// create image
    $im = imagecreate($w, $h);
// set colours to be used
    $bg = imagecolorallocate($im, 0xE0, 0xE0, 0xE0);
    $black = imagecolorallocate($im, 0x00, 0x00, 0x00);
    $barcolor  = imagecolorallocate($im, 0xFF, 0xFF, 0x00);
// draw border
    imagerectangle($im, 0,0,$w-1,$h-1,$black);
// get value and max value from query string
    $val = $_GET['val'];
    $max = $_GET['max'];
// calculate dimensions of inner bar
    $barw = $max ? floor(($w-2) * $val / $max) : 0;
    $barh = $h - 2;
// draw inner bar
if ($barw)
    imagefilledrectangle($im, 1, 1, $barw, $barh, $barcolor);
// send image header
    header("content-type: image/png");
// send png image
    imagepng($im);
    imagedestroy($im);
?>
[/code]
Link to comment
Share on other sites

I added a few more explantory comments. Hope it helps.

[code]
<?php
// set dimensions
    $w = 300;
    $h = 12;
// create image
    $im = imagecreate($w, $h);
// set colours to be used
    $bg = imagecolorallocate($im, 0xE0, 0xE0, 0xE0);
    $black = imagecolorallocate($im, 0x00, 0x00, 0x00);
    $barcolor  = imagecolorallocate($im, 0xFF, 0xFF, 0x00);
// draw border
    imagerectangle($im, 0,0,$w-1,$h-1,$black);
// get value and max value from query string
        // Called by, for example : <img src='bar.php?val=25&max=100'>
        // This gets the val and max sent in the query string
    $val = $_GET['val'];
    $max = $_GET['max'];
// calculate dimensions of inner bar
        // Check that $max is not 0 as we need to divide by it
        // Calculate the width of bar to be plotted
        // As the full width of the bar represents a value of $max we
        //  need total width * ($val / $max)
        // Since there is a 1 pixel wide border all round, our total width
        //  for the value bar is the image width - 2
        // Floor() rounds it down to nearest whole number
        // Similarly, out bar height is image height - 2
    $barw = $max ? floor(($w-2) * $val / $max) : 0;
    $barh = $h - 2;
// draw inner bar
        // Now draw the bar as a filled rectangle which fits inside the border
    if ($barw)
    imagefilledrectangle($im, 1, 1, $barw, $barh, $barcolor);
// send image header
    header("content-type: image/png");
// send png image
    imagepng($im);
    imagedestroy($im);
?>
[/code]
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.