nuxy Posted December 4, 2007 Share Posted December 4, 2007 I'm creating an graph, rather simple to display cpu usage of the server the script runs on. It is all going well, except for the fact that the graph appears to be more random that what I made it to be. What I want to accomplish is simply showing a graph that would display the last 50 known percentages that was logged to a text file, but for some reason it is reading either a null or just making percentages up. It will draw a line upwards when the percentage goes lower, and at random times. I already found a script that reads the cpu usage, and I know for a fact it work, just because the percentage isn't random on the text I made in the upper left corner of the graph. The script has been cut down to the part where the graph gets generated. <?php $load = round(get_server_load(true), 0); $fp = fopen('cpustats.txt', 'a'); fputs($fp, ':' . $load); fclose($fp); $data = explode(':', strrev(file_get_contents('cpustats.txt')), 50); $data[50] = explode(':', 2); $data[50] = $data[50][0]; $im = imagecreate(500, 200); $white = imagecolorallocate($im, 0xFF, 0xFF, 0xFF); $gray = imagecolorallocate($im, 0xDD, 0x00, 0x00); $black = imagecolorallocate($im, 0x00, 0x00, 0x00); $whay = imagecolorallocate($im, 0xEE, 0xEE, 0xEE); $dark = imagecolorallocate($im, 0xBB, 0x00, 0x00); imagefill($im, 0, 0, $white); imageline($im, 50, 0, 50, 200, $whay); imageline($im, 100, 0, 100, 200, $whay); imageline($im, 150, 0, 150, 200, $whay); imageline($im, 200, 0, 200, 200, $whay); imageline($im, 250, 0, 250, 200, $whay); imageline($im, 300, 0, 300, 200, $whay); imageline($im, 350, 0, 350, 200, $whay); imageline($im, 400, 0, 400, 200, $whay); imageline($im, 450, 0, 450, 200, $whay); imageline($im, 0, 50, 500, 50, $whay); imageline($im, 0, 100, 500, 100, $whay); imageline($im, 0, 150, 500, 150, $whay); $c = 0; $t = 0; for ($i = 0; $i <= 50; $i++) { $c += 10; $point = $data[$i] * 2; $l = tra($point); imageline($im, $c - 10, tra($t), $c, $l, $dark); $t = $point; } imagealphablending($im, $true); $trans = imagecolorallocatealpha($im, 0x33, 0x33, 0x33, 90); imagestring($im, 3, 5, 3, $load . '%', $trans); imagepng($im); exit; ?> Thanks. Quote Link to comment https://forums.phpfreaks.com/topic/80085-gd-graph/ Share on other sites More sharing options...
Barand Posted December 4, 2007 Share Posted December 4, 2007 On your graph, y=0 is at the bottom. In GD, y=0 is at the top. So you need to compensate and translate the y values. Quote Link to comment https://forums.phpfreaks.com/topic/80085-gd-graph/#findComment-405890 Share on other sites More sharing options...
nuxy Posted December 4, 2007 Author Share Posted December 4, 2007 I figured that, that is why I pass the values trough the tra function. function tra($num) { $result = 200 - $num; if ($result < 0) $result = 0; return $result; } Thanks! Quote Link to comment https://forums.phpfreaks.com/topic/80085-gd-graph/#findComment-405951 Share on other sites More sharing options...
nuxy Posted December 4, 2007 Author Share Posted December 4, 2007 *bump* Quote Link to comment https://forums.phpfreaks.com/topic/80085-gd-graph/#findComment-406058 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.