- <?
- class smolGraph
- {
- //Used for making a small graph on a bigger one.
- public $physicalWidth;
- public $physicalHeight;
- //what is the incoming data going to look like
- public $minValueX;
- public $maxValueX;
- public $minValueY;
- public $maxValueY;
- //Where does it live physicall on the screen
- public $startX;
- public $widthX;
- public $startY;
- public $heightY;
- //cartesian center relative to the X Y of physical screen
- public $cartCenterX;
- public $cartCenterY;
- function svgHeader()
- {
- $SVGDOCUMENT="";
- $GRAPHWIDTH=$this->physicalWidth;
- $GRAPHHEIGHT=$this->physicalHeight;
- $SVGDOCUMENT.="<svg xmlns=\"http://www.w3.org/2000/svg\" version=\"1.1\" width=\"".$GRAPHWIDTH."\" height=\"".$GRAPHHEIGHT."\" >\n";
- $SVGDOCUMENT.="<rect x=\"0\" y=\"0\" width=\"".$GRAPHWIDTH."\" height=\"".$GRAPHHEIGHT."\" style=\"fill:white; stroke-width:3;stroke:rgb(0,0,0)\"/>\n";
- //echo "[$SVGDOCUMENT]\n";
- return $SVGDOCUMENT;
- }
- function svgFooter()
- {
- return "</svg>\n";
- }
- function graphLine($x,$y,$h,$v,$width,$color)
- {
- $x1=$this->map($x,$this->minValueX,$this->maxValueX,$this->startX,$this->startX+$this->widthX)-($this->cartCenterX);
- $x2=$this->map($h,$this->minValueX,$this->maxValueX,$this->startX,$this->startX+$this->widthX)-($this->cartCenterX);
- $y1=$this->map($y,$this->minValueY,$this->maxValueY,$this->startY+$this->heightY,$this->startY)+$this->cartCenterY;
- $y2=$this->map($v,$this->minValueY,$this->maxValueY,$this->startY+$this->heightY,$this->startY)+$this->cartCenterY;
- $SVGDOCUMENT="<line x1=\"$x1\" y1=\"$y1\" x2=\"$x2\" y2=\"$y2\" style=\"stroke:$color;stroke-width:$width;\" />\n";
- return $SVGDOCUMENT;
- }
- function graphRectangle($x,$y,$h,$v,$color)
- {
- $SVGDOCUMENT="";
- $x1=$this->map($x,$this->minValueX,$this->maxValueX,$this->startX,$this->startX+$this->widthX)-($this->cartCenterX);
- $x2=$this->map($h,$this->minValueX,$this->maxValueX,$this->startX,$this->startX+$this->widthX)-($this->cartCenterX);
- $y1=$this->map($y,$this->minValueY,$this->maxValueY,$this->startY+$this->heightY,$this->startY)+$this->cartCenterY;
- $y2=$this->map($v,$this->minValueY,$this->maxValueY,$this->startY+$this->heightY,$this->startY)+$this->cartCenterY;
- $SVGDOCUMENT.="<polygon points=\"$x1,$y1 $x2,$y1 $x2,$y2 $x1,$y2 \" style=\"fill:$color;\" />\n";
- return $SVGDOCUMENT;
- }
- function graphCircle($x,$y,$radius,$color)
- {
- $SVGDOCUMENT="";
- $x1=$this->map($x,$this->minValueX,$this->maxValueX,$this->startX,$this->startX+$this->widthX)-($this->cartCenterX);
- $y1=$this->map($y,$this->minValueY,$this->maxValueY,$this->startY+$this->heightY,$this->startY)+$this->cartCenterY;
- $SVGDOCUMENT.="<circle cx=\"$x1\" cy=\"$y1\" r=\"$radius\" fill=\"$color\" />\n";
- return $SVGDOCUMENT;
- }
- function graphText($textValue,$x,$y,$color,$size)
- {
- $SVGDOCUMENT="";
- $x1=$this->map($x,$this->minValueX,$this->maxValueX,$this->startX,$this->startX+$this->widthX)-($this->cartCenterX);
- $y1=$this->map($y,$this->minValueY,$this->maxValueY,$this->startY+$this->heightY,$this->startY)+$this->cartCenterY;
- $y1=$y1+($size/3);
- $SVGDOCUMENT.="<text x=\"$x1\" y=\"$y1\" fill=\"$color\" font-face=\"sans\" font-size=\"$size\">$textValue</text>\n";
- return $SVGDOCUMENT;
- }
- function map($value, $fromLow, $fromHigh, $toLow, $toHigh)
- {
- $fromRange = $fromHigh - $fromLow;
- $toRange = $toHigh - $toLow;
- $scaleFactor = $toRange / $fromRange;
- $tmpValue = $value - $fromLow;
- $tmpValue *= $scaleFactor;
- return $tmpValue + $toLow;
- }
- function degreesToRadians($degrees)
- {
- return $radians;
- }
- function radiansToDegrees($radians)
- {
- return $degrees;
- }
- }
- $AGraph=new smolGraph();
- $AGraph->physicalWidth=1000;
- $AGraph->physicalHeight=1000;
- $AGraph->minValueX=-100;
- $AGraph->maxValueX=100;
- $AGraph->minValueY=-100;
- $AGraph->maxValueY=100;
- $AGraph->startX=100;
- $AGraph->startY=100;
- $AGraph->widthX=200;
- $AGraph->heightY=200;
- $AGraph->cartCenterX=0;
- $AGraph->cartCenterY=0;
- $retV="";
- $retV.=$AGraph->svgHeader();
- //grid X
- for ($x=$AGraph->minValueX; $x<$AGraph->maxValueX+1; $x+=10)
- {
- $retV.=$AGraph->graphLine($x,$AGraph->minValueY,$x,$AGraph->maxValueY,1.0,"#888888");
- }
- //grid Y
- for ($y=$AGraph->minValueY; $y<$AGraph->maxValueY+1; $y+=10)
- {
- $retV.=$AGraph->graphLine($AGraph->minValueX,$y,$AGraph->maxValueX,$y,1.0,"#888888");
- }
- $retV.=$AGraph->graphLine(-100,0,100,0,2,"#444444");
- $retV.=$AGraph->graphLine(0,-100,0,100,2,"#444444");
- $retV.=$AGraph->graphLine(0,0,100,100,2,"#882222");
- $retV.=$AGraph->graphLine(0,0,100,-100,2,"#228822");
- $retV.=$AGraph->graphRectangle(-50,-50,50,50,"#222288");
- $retV.=$AGraph->graphCircle(50,50,10,"#440000");
- $retV.=$AGraph->graphText("Hello Graph",0,0,"#000000",12);
- $retV.=$AGraph->graphText("Hello Graph",-100,-110,"#000000",15);
- $retV.=$AGraph->svgFooter();
- //generates a small graph within a larger screen.
- //the graph is auto scaled
- ?>
SmolGraph2PHP
Posted by Anonymous on Wed 1st Aug 2018 01:27
raw | new post
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.