source-function-F_getSVGGraphCode

It appears that you are using AdBlocking software. The cost of running this website is covered by advertisements. If you like it please feel free to a small amount of money to secure the future of this website.
Overview

Classes

Interfaces

Exceptions

Functions

  1: <?php
  2: //============================================================+
  3: // File name   : tce_functions_svg_graph.php
  4: // Begin       : 2012-04-15
  5: // Last Update : 2013-07-14
  6: //
  7: // Description : Function to create an SVG graph for user results.
  8: //
  9: // Author: Nicola Asuni
 10: //
 11: // (c) Copyright:
 12: //               Nicola Asuni
 13: //               Tecnick.com LTD
 14: //               www.tecnick.com
 15: //               info@tecnick.com
 16: //
 17: // License:
 18: //    Copyright (C) 2004-2013  Nicola Asuni - Tecnick.com LTD
 19: //    See LICENSE.TXT file for more information.
 20: //============================================================+
 21: 
 22: /**
 23:  * @file
 24:  * Function to create an SVG graph for user results.
 25:  * @package com.tecnick.tcexam.admin
 26:  * @author Nicola Asuni
 27:  * @since 2013-07-14
 28:  */
 29: 
 30: /**
 31:  */
 32: 
 33: /**
 34:  * Replace angular parenthesis with html equivalents (html entities).
 35:  * @param $p (string) String containing point data.
 36:  * @param $w (int) Graph width.
 37:  * @param $h (int) Graph height.
 38:  * @return converted string
 39:  */
 40: function F_getSVGGraphCode($p, $w = '', $h = '')
 41: {
 42: 
 43:     // points to graph (values between 0 and 100)
 44:     $points = explode('x', $p);
 45: 
 46:     // count the of points
 47:     $numpoints = count($points);
 48:     // vertical and horizontal space to leave for labels
 49:     $label_space = 35;
 50:     // graph width
 51:     $width = ($label_space + ($numpoints * 2));
 52:     if (!empty($w)) {
 53:         $width = max($width, intval($w));
 54:     }
 55:     // graph height
 56:     $height = 200 + $label_space;
 57:     if (!empty($h)) {
 58:         $height = max($height, intval($h));
 59:     }
 60:     // graph colors
 61:     $color = array('ff0000', '0000ff');
 62: 
 63:     // font size for labels
 64:     $fontsize = sprintf('%.3F', ($label_space / 3));
 65: 
 66:     // create SVG graph
 67:     $svg = '<'.'?'.'xml version="1.0" standalone="no"'.'?'.'>'."\n";
 68:     $svg .= '<!DOCTYPE svg PUBLIC "-//W3C//DTD SVG 1.1//EN" "http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd">'."\n";
 69:     $svg .= '<svg width="'.$width.'" height="'.$height.'" version="1.1" xmlns="http://www.w3.org/2000/svg">'."\n";
 70: 
 71:     // draw horizontal grids
 72:     $vstep = floor(($height - $label_space) / 11);
 73:     $pstep = ($vstep / 10);
 74:     $hw = ($width - 4);
 75:     $textpos = ($label_space * 0.8);
 76:     $svg .= '<g stroke="#cccccc" fill="#666666" stroke-width="1" text-anchor="end" font-family="Arial,Verdana" font-size="'.$fontsize.'">'."\n";
 77:     for ($i = 0; $i <= 10; ++$i) {
 78:         $y = (($i + 1) * $vstep);
 79:         // text
 80:         $svg .= "\t".'<text x="'.$textpos.'" y="'.$y.'" stroke-width="0">'.(100 - ($i * 10)).'%</text>'."\n";
 81:         // line
 82:         $svg .= "\t".'<line x1="'.$label_space.'" y1="'.$y.'" x2="'.$hw.'" y2="'.$y.'" />'."\n";
 83:     }
 84:     $svg .= '</g>'."\n";
 85: 
 86:     // draw vertical grids and points
 87:     $hstep = floor(($hw - $label_space) / ($numpoints - 1));
 88:     $vh = ($height - $label_space);
 89:     $textpos = $vh + ($label_space * 0.5);
 90:     $svg .= '<g stroke="#cccccc" fill="#666666" stroke-width="1" text-anchor="end" font-family="Arial,Verdana" font-size="'.$fontsize.'">'."\n";
 91:     $graph = array('', '');
 92:     $step = 1;
 93:     if ($numpoints > 30) {
 94:         $step = 5;
 95:     } elseif ($numpoints > 100) {
 96:         $step = 10;
 97:     }
 98: 
 99:     for ($i = 0; $i < $numpoints; ++$i) {
100:         $point = explode('v', $points[$i]);
101:         $x = (($i * $hstep) + $label_space);
102:         // line
103:         $svg .= "\t".'<line x1="'.$x.'" y1="'.$vstep.'" x2="'.$x.'" y2="'.$vh.'" />'."\n";
104:         $xi = ($i + 1);
105:         if (($xi == 1) or (($xi % $step) == 0)) {
106:             // text
107:             $svg .= "\t".'<text x="'.$x.'" y="'.$textpos.'" stroke-width="0">'.($xi).'</text>'."\n";
108:         }
109:         for ($k = 0; $k <= 1; ++$k) {
110:             // graph path
111:             $y = sprintf('%.3F', (11 * $vstep) - (intval($point[$k]) * $pstep));
112:             $graph[$k] .= ' '.$x.','.$y;
113:             // point
114:             $svg .= "\t".'<circle cx="'.$x.'" cy="'.$y.'" r="4" stroke-width="0" fill="#'.$color[$k].'" />'."\n";
115:         }
116:     }
117:     $svg .= '</g>'."\n";
118: 
119:     // draw graph
120:     for ($k = 0; $k <= 1; ++$k) {
121:         $svg .= '<path fill-opacity="0.2" fill="#'.$color[$k].'" stroke-width="0" d="M '.$label_space.' '.(11 * $vstep).' L '.$graph[$k].' '.((($numpoints - 1) * $hstep) + $label_space).' '.(11 * $vstep).' Z" />'."\n";
122:         $svg .= '<polyline fill="none" stroke="#'.$color[$k].'" stroke-width="2" points="'.$graph[$k].'" />'."\n";
123:     }
124: 
125:     // close SVG graph
126:     $svg .= '</svg>'."\n";
127: 
128:     return $svg;
129: }
130: 
131: /**
132:  * Replace angular parenthesis with html equivalents (html entities).
133:  * @param $p (string) String containing point data.
134:  * @param $w (int) Graph width.
135:  * @param $h (int) Graph height.
136:  * @return converted string
137:  */
138: function F_getSVGGraph($p, $w = '', $h = '')
139: {
140:     // send headers
141:     header('Content-Description: SVG Data');
142:     header('Cache-Control: public, must-revalidate, max-age=0'); // HTTP/1.1
143:     header('Pragma: public');
144:     header('Expires: Sat, 26 Jul 1997 05:00:00 GMT'); // Date in the past
145:     header('Last-Modified: '.gmdate('D, d M Y H:i:s').' GMT');
146:     header('Content-Type: image/svg+xml');
147:     header('Content-Disposition: inline; filename="tce_svg_graph_'.md5($p.$w.$h).'.svg";');
148:     // Turn on output buffering with the gzhandler
149:     //ob_start('ob_gzhandler');
150:     // output SVG code
151:     echo F_getSVGGraphCode($p, $w, $h);
152: }
153: 
154: //============================================================+
155: // END OF FILE
156: //============================================================+
157: 
 

© 2004-2018 – Nicola Asuni - Tecnick.com - All rights reserved.
about - disclaimer - privacy