Error: Could not fetch http://thedevnet.com/wp-content/plugins/gd-star-rating/css/gdsr.css.php?t=0&s=a10i10m20k20c05r05%23121620243046%23121620243240%23s1pchristmas%23s1pcrystal%23s1pdarkness%23s1poxygen%23s1goxygen_gif%23s1pplain%23s1ppumpkin%23s1psoft%23s1pstarrating%23s1pstarscape%23t1pclassical%23t1pstarrating%23t1gstarrating_gif&ver=1.7.2 for caching. You might need to exclude this file in WP Minify options. The Developer Network – 2D Plotter

2D Plotter

When doing math functions or showing a two dimensional line graph, this class will come in real handy. Plotting in two dimensions is a real asset, but not only that, but with a little bit of tweaking, you could rename the axis, and develop your own custom line graph.

Use this class to plot a 2D Graph:

  1. <?php
  2. class plot2D {
  3.  var $img, $imgWidth, $imgHeight, $fontSize, $fontWidth, $fontHeight, $cBack, $acPlot, $maxCatLen, $aItems, $maxVal, $numItems, $maxNumItems, $maxLinesDesc, $numberY, $cBlack, $cGrid;
  4.  var $graphX, $graphY, $graphWidth, $graphHeight;
  5.  var $titleString, $titleFontSize, $cTitle, $titleWidth, $titleHeight;
  6.  var $xDesc, $yDesc, $xDescLength, $yDescLength;
  7. http://thedevnet.com/php/classes/2d-plotter/
  8.  function plot2D($width=640, $height=480, $fontSize=2, $red=0xFF, $green=0xFF, $blue=0xFF) {
  9.   $this->img = imageCreate($width, $height);
  10.   $this->imgWidth = $width-10;
  11.   $this->imgHeight = $height;
  12.   $this->cBack = imageColorAllocate($this->img, $red, $green, $blue);
  13.   $this->cBlack = imageColorAllocate($this->img, 0, 0, 0);
  14.   $this->cNavy = imageColorAllocate($this->img, 0×00, 0×00, 0×80);
  15.   $this->fontSize = $fontSize;
  16.   $this->fontWidth = imageFontWidth($fontSize);
  17.   $this->fontHeight = imageFontHeight($fontSize);
  18.   $this->graphX = 0;
  19.   $this->graphY = 0;
  20.   $this->graphWidth = $this->imgWidth;
  21.   $this->graphHeight = $this->imgHeight;
  22.   $this->setGrid();
  23.  }
  24.  
  25.  function setTitle($title, $fontSize=3, $red=0×00, $green=0×00, $blue=0×00) {
  26.   $this->titleString = $title;
  27.   $this->titleFontSize = $fontSize;
  28.   $this->cTitle = imageColorAllocate($this->img, $red, $green, $blue);
  29.   $this->titleWidth = imageFontWidth($fontSize) * strLen($title);
  30.   $this->titleHeight = imageFontHeight($fontSize);
  31.   $this->graphY = $this->graphY + ($this->titleHeight + 2);
  32.   $this->graphHeight = $this->graphHeight - ($this->titleHeight + 2);
  33.  }
  34.  
  35.  function setDescription($x, $y="") {
  36.   if ($x) {
  37.    $this->xDesc = $x;
  38.    $this->xDescLength = $this->fontWidth * strLen($x);
  39.    $this->graphHeight = $this->graphHeight - ($this->fontHeight + 2);
  40.   }
  41.  
  42.   if ($y) {
  43.    $this->yDesc = $y;
  44.    $this->yDescLength = $this->fontWidth * strLen($y);
  45.    $this->graphX = $this->graphX + ($this->fontHeight + 2);
  46.    $this->graphWidth = $this->graphWidth - ($this->fontHeight + 2);
  47.   }
  48.  }
  49.  
  50.  function setGrid($number=5, $red=0xCC, $green=0xCC, $blue=0xCC) { //Horizontal grid
  51.   $this->cGrid = imageColorAllocate($this->img, $red, $green, $blue);
  52.   $this->numberY = $number;
  53.  }
  54.  
  55.  function addCategory($description, $red, $green, $blue) {
  56.   $this->acPlot[$description] = imageColorAllocate($this->img, $red, $green, $blue);
  57.   if (strLen($description) > $this->maxCatLen)
  58.    $this->maxCatLen = strLen($description);
  59.  }
  60.  
  61.  function categoryExists($description) {
  62.   if (isSet($this->acPlot[$description])) {
  63.    return true;
  64.   } else {
  65.    return false;
  66.   }
  67.  }
  68.  
  69.  function addItem($category, $description, $value) {
  70.   $this->aItems[$category][$description] = $value;
  71.   $this->checkItem($category, $description, $this->aItems[$category][$description]);
  72.  }
  73.  
  74.  function incrementItem($category, $description, $value) {
  75.   if (!isSet($this->aItems[$category][$description]))
  76.    $this->addItem($category, $description, $value);
  77.   $this->aItems[$category][$description] = $this->aItems[$category][$description] + $value;
  78.   $this->checkItem($category, $description, $this->aItems[$category][$description]);
  79.  }
  80.  
  81.  function checkItem($category, $description, $value) {
  82.   if ($value > $this->maxVal)
  83.    $this->maxVal = $value;
  84.   $this->numItems[$category]++;
  85.   if ($this->numItems[$category] > $this->maxNumItems)
  86.    $this->maxNumItems = $this->numItems[$category];
  87.   if (sizeOf(explode("\n", $description)) > $this->maxLinesDesc)
  88.    $this->maxLinesDesc = sizeOf(explode("\n", $description));
  89.  }
  90.  
  91.  function destroy() {
  92.   imageDestroy($this->img);
  93.  }
  94.  
  95.  function printGraph($filename = 0) {
  96.   $this->graphHeight = $this->graphHeight - (($this->maxLinesDesc + 1) * ($this->fontHeight + 2)); //Adjust for bottom values
  97.   $this->graphX = $this->graphX + ($this->fontWidth * strLen($this->maxVal) + 2); //Adjust left margin for values
  98.   $this->graphWidth = $this->graphWidth - ($this->fontWidth * strLen($this->maxVal) + 2) - (($this->maxCatLen * $this->fontWidth) + 17) - (strLen($this->maxVal) * $this->fontWidth); //+17 = Adjust right margin for category descriptions
  99.   imageRectangle($this->img, $this->graphX, $this->graphY, $this->graphX + $this->graphWidth, $this->graphY + $this->graphHeight, $this->cBlack); //Box
  100.   if ($this->titleString)
  101.    imageString($this->img, $this->titleFontSize, $this->graphX + ($this->graphWidth / 2) - ($this->titleWidth / 2), 0, $this->titleString, $this->cTitle);
  102.   if ($this->xDesc)
  103.    imageString($this->img, $this->fontSize, $this->graphX + ($this->graphWidth / 2) - ($this->xDescLength / 2), $this->graphY + $this->graphHeight + (($this->maxLinesDesc + 1) * ($this->fontHeight + 2)), $this->xDesc, $this->cBlack);
  104.   if ($this->yDesc)
  105.    imageStringUp($this->img, $this->fontSize, 0, ($this->graphY + ($this->graphHeight / 2)) + ($this->xDescLength / 2), $this->yDesc, $this->cBlack);
  106.  
  107.   for ($i = 0; $i <= $this->numberY; $i++) { //Grid
  108.     $yPos = $this->graphY + $i * ($this->graphHeight / ($this->numberY + 1));
  109.    if ($i)
  110.     imageLine($this->img, $this->graphX+1, $yPos, $this->graphX + $this->graphWidth - 1, $yPos, $this->cGrid);
  111.    $yVal = floor($this->maxVal - (($this->maxVal / ($this->numberY + 1)) * $i) + .5);
  112.    imageString($this->img, $this->fontSize, $this->graphX - 3 - ($this->fontWidth * strLen($yVal)), $yPos - ($this->fontHeight / 2), $yVal, $this->cNavy);
  113.   }
  114.  
  115.   imageString($this->img, $this->fontSize, $this->graphX - 3 - $this->fontWidth, $this->graphY + $this->graphHeight - ($this->fontHeight / 2), "0", $this->cNavy);
  116.  
  117.   $c = 0;
  118.   while (list($cDesc, $cColor) = each($this->acPlot)) { //Loop through categories
  119.    $s=0;
  120.    while (list($sKey, $sVal) = each($this->aItems[$cDesc])) { //Loop through items
  121.     $sum[$cDesc] += $sVal; //Sum up values for each category
  122.     $xPos = ($this->graphX + 4) + ((($this->graphWidth - 10) / ($this->maxNumItems - 1)) * $s);
  123.     $yPos = $this->graphY + ($this->graphHeight - floor((($sVal / $this->maxVal) * $this->graphHeight) + .5));
  124.     imageRectangle($this->img, $xPos - 2, $yPos - 2, $xPos + 2, $yPos + 2, $cColor);
  125.     if ($s)
  126.      imageLine($this->img, $prevX, $prevY, $xPos, $yPos, $cColor);
  127.  
  128.     $aValues = explode("\n", $sKey);
  129.     for ($i = 0; $i < sizeOf($aValues); $i++) {
  130.      imageString($this->img, $this->fontSize, $xPos - ((strLen($aValues[$i]) * $this->fontWidth) / 2), $this->graphY + $this->graphHeight + 2 + ($i * ($this->fontHeight + 2)), $aValues[$i], $this->cNavy); //Bottom values
  131.     }
  132.     $prevX = $xPos;
  133.     $prevY = $yPos;
  134.     $s++;
  135.    }
  136.    $boxX = $this->graphX + $this->graphWidth + 5;
  137.    $boxY = $this->graphY + 10 + ($c * 15);
  138.    imageFilledRectangle($this->img, $boxX, $boxY, $boxX + 10, $boxY + 10, $cColor);
  139.    imageRectangle($this->img, $boxX, $boxY, $boxX + 10, $boxY + 10, $this->cBlack);
  140.    imageString($this->img, $this->fontSize, $boxX + 14, ($boxY + 5) - ($this->fontHeight / 2), $cDesc."=".$sum[$cDesc], $cColor);
  141.    $c++;
  142.   }
  143.   if ($filename) {
  144.    imagePNG($this->img, "$filename.png");
  145.   } else {
  146.    imagePNG($this->img);
  147.   }
  148.  }
  149. }
  150. ?>

Example Usage:

  1. <?php
  2. $plot = new plot2D();
  3. $plot->setTitle("John's spending habits");
  4. $plot->setDescription("Days of the week", "Dollars spent");
  5. $plot->setGrid();
  6. $plot->addCategory("Food", 0xCC, 0×00, 0×00);
  7.  $plot->addItem("Food", "sun\n12/1", 152);
  8.  $plot->addItem("Food", "mon\n12/2", 76);
  9.  $plot->addItem("Food", "tues\n12/3", 81);
  10. $plot->addCategory("Candy", 0×00, 0xCC, 0×00);
  11.  $plot->addItem("Candy", "sun\n12/1", 51);
  12.  $plot->addItem("Candy", "mon\n12/2", 127);
  13.  $plot->addItem("Candy", "tues\n12/3", 45);
  14. $plot->printGraph();
  15. $plot->destroy();
  16. ?>
GD Star Rating
loading...

Originally posted 2009-11-24 10:07:15.

Popularity: 4%

Posted by natster   @   16 May 2010
Tags : , , ,

Like this post? Share it!

RSS Digg Twitter StumbleUpon Delicious Technorati Facebook

0 Comments

No comments yet. Be the first to leave a comment !
Leave a Comment

You must be logged in to post a comment.

Previous Post
Next Post
Display an image or URL on a specific date »
Powered by Wordpress   |   Lunated designed by ZenVerse