pastebin - collaborative debugging tool
blankb.kpaste.net RSS


SmolGraph2PHP
Posted by Anonymous on Wed 1st Aug 2018 01:27
raw | new post

  1. <?
  2. class smolGraph
  3. {
  4.         //Used for making a small graph on a bigger one.
  5.         public $physicalWidth;
  6.         public $physicalHeight;
  7.  
  8.         //what is the incoming data going to look like
  9.         public $minValueX;
  10.         public $maxValueX;
  11.         public $minValueY;
  12.         public $maxValueY;
  13.  
  14.         //Where does it live physicall on the screen
  15.         public $startX;
  16.         public $widthX;
  17.         public $startY;
  18.         public $heightY;
  19.  
  20.         //cartesian center relative to the X Y of physical screen
  21.         public $cartCenterX;
  22.         public $cartCenterY;
  23.  
  24.         function svgHeader()
  25.         {
  26.                 $SVGDOCUMENT="";
  27.                 $GRAPHWIDTH=$this->physicalWidth;
  28.                 $GRAPHHEIGHT=$this->physicalHeight;
  29.                 $SVGDOCUMENT.="<svg xmlns=\"http://www.w3.org/2000/svg\" version=\"1.1\" width=\"".$GRAPHWIDTH."\"  height=\"".$GRAPHHEIGHT."\"  >\n";
  30.                 $SVGDOCUMENT.="<rect x=\"0\" y=\"0\" width=\"".$GRAPHWIDTH."\" height=\"".$GRAPHHEIGHT."\"  style=\"fill:white; stroke-width:3;stroke:rgb(0,0,0)\"/>\n";
  31.                 //echo "[$SVGDOCUMENT]\n";
  32.                 return $SVGDOCUMENT;
  33.         }
  34.         function svgFooter()
  35.         {
  36.                 return "</svg>\n";
  37.         }
  38.         function graphLine($x,$y,$h,$v,$width,$color)
  39.         {
  40.                 $x1=$this->map($x,$this->minValueX,$this->maxValueX,$this->startX,$this->startX+$this->widthX)-($this->cartCenterX);
  41.                 $x2=$this->map($h,$this->minValueX,$this->maxValueX,$this->startX,$this->startX+$this->widthX)-($this->cartCenterX);
  42.                 $y1=$this->map($y,$this->minValueY,$this->maxValueY,$this->startY+$this->heightY,$this->startY)+$this->cartCenterY;
  43.                 $y2=$this->map($v,$this->minValueY,$this->maxValueY,$this->startY+$this->heightY,$this->startY)+$this->cartCenterY;
  44.                 $SVGDOCUMENT="<line x1=\"$x1\" y1=\"$y1\" x2=\"$x2\" y2=\"$y2\" style=\"stroke:$color;stroke-width:$width;\" />\n";
  45.                 return $SVGDOCUMENT;
  46.         }
  47.         function graphRectangle($x,$y,$h,$v,$color)
  48.         {
  49.                 $SVGDOCUMENT="";
  50.  
  51.                 $x1=$this->map($x,$this->minValueX,$this->maxValueX,$this->startX,$this->startX+$this->widthX)-($this->cartCenterX);
  52.                 $x2=$this->map($h,$this->minValueX,$this->maxValueX,$this->startX,$this->startX+$this->widthX)-($this->cartCenterX);
  53.                 $y1=$this->map($y,$this->minValueY,$this->maxValueY,$this->startY+$this->heightY,$this->startY)+$this->cartCenterY;
  54.                 $y2=$this->map($v,$this->minValueY,$this->maxValueY,$this->startY+$this->heightY,$this->startY)+$this->cartCenterY;
  55.                 $SVGDOCUMENT.="<polygon points=\"$x1,$y1 $x2,$y1 $x2,$y2 $x1,$y2 \" style=\"fill:$color;\" />\n";
  56.                 return $SVGDOCUMENT;
  57.         }
  58.         function graphCircle($x,$y,$radius,$color)
  59.         {
  60.                 $SVGDOCUMENT="";
  61.  
  62.                 $x1=$this->map($x,$this->minValueX,$this->maxValueX,$this->startX,$this->startX+$this->widthX)-($this->cartCenterX);
  63.                 $y1=$this->map($y,$this->minValueY,$this->maxValueY,$this->startY+$this->heightY,$this->startY)+$this->cartCenterY;
  64.                 $SVGDOCUMENT.="<circle cx=\"$x1\" cy=\"$y1\" r=\"$radius\" fill=\"$color\" />\n";
  65.                 return $SVGDOCUMENT;
  66.         }
  67.         function graphText($textValue,$x,$y,$color,$size)
  68.         {
  69.                 $SVGDOCUMENT="";
  70.                 $x1=$this->map($x,$this->minValueX,$this->maxValueX,$this->startX,$this->startX+$this->widthX)-($this->cartCenterX);
  71.                 $y1=$this->map($y,$this->minValueY,$this->maxValueY,$this->startY+$this->heightY,$this->startY)+$this->cartCenterY;
  72.                 $y1=$y1+($size/3);
  73.                 $SVGDOCUMENT.="<text x=\"$x1\" y=\"$y1\" fill=\"$color\" font-face=\"sans\" font-size=\"$size\">$textValue</text>\n";
  74.                 return $SVGDOCUMENT;
  75.         }
  76.         function map($value, $fromLow, $fromHigh, $toLow, $toHigh)
  77.         {
  78.                 $fromRange = $fromHigh - $fromLow;
  79.                 $toRange = $toHigh - $toLow;
  80.                 $scaleFactor = $toRange / $fromRange;
  81.                 $tmpValue = $value - $fromLow;
  82.                 $tmpValue *= $scaleFactor;
  83.                 return $tmpValue + $toLow;
  84.         }
  85.         function degreesToRadians($degrees)
  86.         {
  87.                 $radians=(pi()/180)*$degrees;
  88.                 return $radians;
  89.         }
  90.         function radiansToDegrees($radians)
  91.         {
  92.                 $degrees=$radians*(180/pi());
  93.                 return $degrees;
  94.         }
  95. }
  96.  
  97.         $AGraph=new smolGraph();
  98.  
  99.         $AGraph->physicalWidth=1000;
  100.         $AGraph->physicalHeight=1000;
  101.  
  102.         $AGraph->minValueX=-100;
  103.         $AGraph->maxValueX=100;
  104.         $AGraph->minValueY=-100;
  105.         $AGraph->maxValueY=100;
  106.  
  107.         $AGraph->startX=100;
  108.         $AGraph->startY=100;
  109.         $AGraph->widthX=200;
  110.         $AGraph->heightY=200;
  111.  
  112.         $AGraph->cartCenterX=0;
  113.         $AGraph->cartCenterY=0;
  114.  
  115.         $retV="";
  116.         $retV.=$AGraph->svgHeader();
  117.    //grid X
  118.     for ($x=$AGraph->minValueX; $x<$AGraph->maxValueX+1; $x+=10)
  119.     {
  120.                 $retV.=$AGraph->graphLine($x,$AGraph->minValueY,$x,$AGraph->maxValueY,1.0,"#888888");
  121.         }
  122.  
  123.    //grid Y
  124.     for ($y=$AGraph->minValueY; $y<$AGraph->maxValueY+1; $y+=10)
  125.     {
  126.                 $retV.=$AGraph->graphLine($AGraph->minValueX,$y,$AGraph->maxValueX,$y,1.0,"#888888");
  127.         }
  128.  
  129.         $retV.=$AGraph->graphLine(-100,0,100,0,2,"#444444");
  130.         $retV.=$AGraph->graphLine(0,-100,0,100,2,"#444444");
  131.  
  132.         $retV.=$AGraph->graphLine(0,0,100,100,2,"#882222");
  133.         $retV.=$AGraph->graphLine(0,0,100,-100,2,"#228822");
  134.  
  135.         $retV.=$AGraph->graphRectangle(-50,-50,50,50,"#222288");
  136.         $retV.=$AGraph->graphCircle(50,50,10,"#440000");
  137.  
  138.         $retV.=$AGraph->graphText("Hello Graph",0,0,"#000000",12);
  139.         $retV.=$AGraph->graphText("Hello Graph",-100,-110,"#000000",15);
  140.  
  141.         $retV.=$AGraph->svgFooter();
  142.         file_put_contents("/tmp/misc/test.svg", $retV);
  143.  
  144.  
  145. //generates a small graph within a larger screen.
  146. //the graph is auto scaled
  147. ?>

Submit a correction or amendment below (click here to make a fresh posting)
After submitting an amendment, you'll be able to view the differences between the old and new posts easily.

Syntax highlighting:

To highlight particular lines, prefix each line with {%HIGHLIGHT}





All content is user-submitted.
The administrators of this site (kpaste.net) are not responsible for their content.
Abuse reports should be emailed to us at