Jump to content

GD Graph


nuxy

Recommended Posts

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.

Link to comment
https://forums.phpfreaks.com/topic/80085-gd-graph/
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.