Source for file SvgGrapher.php
Documentation is available at SvgGrapher.php
* $Source: /cvsroot/svggrapher/svggrapher/SvgGrapher.php,v $
* @version $Id: SvgGrapher.php,v 1.20 2007/10/23 09:22:51 geelweb Exp $
* @author Guillaume L. <guillaume@geelweb.org>
* @copyright Copyright © 2006, Guillaume Luchet.
* @license http://opensource.org/licenses/bsd-license.php BSD License
* Define the library base path.
* Be carefull if you redefine this!
if(!defined('SVGGRAPHER_CLASS_PATH')) {
define('SVGGRAPHER_CLASS_PATH', dirname(__FILE__
));
* Include the library config file
require_once SVGGRAPHER_CLASS_PATH .
'/SvgGrapher.config.php';
* Include the objects helper
require_once SVGGRAPHER_CLASS_PATH .
'/mains/PluginManager.php';
require_once SVGGRAPHER_CLASS_PATH .
'/mains/Box.php';
require_once SVGGRAPHER_CLASS_PATH .
'/mains/Curve.php';
require_once SVGGRAPHER_CLASS_PATH .
'/mains/SvgDocumentRenderer.php';
require_once SVGGRAPHER_CLASS_PATH .
'/XMLDocument.php';
* Include the mains svg elements objects
require_once SVGGRAPHER_ELEMENTS_PATH .
'/GElement.php';
require_once SVGGRAPHER_ELEMENTS_PATH .
'/DefsElement.php';
require_once SVGGRAPHER_ELEMENTS_PATH .
'/LineElement.php';
* The most of plugin functionality are plugin defined. The plugin types are :
* - <b>dataprovider</b> for get the datas from various sources.
* - <b>defs</b> for the various svg definition.
* - <b>filter</b> for the curves filters.
* - <b>internal</b> for the internal functions (must already be in the internal
* - <b>marker</b> for the curves makers
* - <b>script</b> for the svg scripts.
* - <b>visual</b> for the visual representation of the curves.
* For each plugin the file must be named <b>svgGrapher.type.keyword.php</b> and
* must contain a function named <b>svgGrapher_type_keyword</b>. eg: to the
* visual plugin who defined the line representation of curve the file name is
* <i>svgGrapher.visual.line.php</i> and the function is named
* <i>svgGrapher_visual_line</i>.
* All plugins are call using the PluginManager class, the plugins can
* access to the SvgGrapher class using the SvgGrapher::singleton() method.
* The plugins paths are define in the PluginManager class file.
* You can redefine most of constant using by the library, they are listed in
* the <i>SvgGrapher.constants.php</i>, you must redefined them before include
* @author Guillaume L. <guillaume@geelweb.org>
* @link http://www.geelweb.org
* @license http://opensource.org/licenses/bsd-license.php BSD License
* @copyright Copyright © 2006, Guillaume Luchet.
* @version CVS: $Id: SvgGrapher.php,v 1.20 2007/10/23 09:22:51 geelweb Exp $
* @tutorial SvgGrapher.pkg
* Instance (singleton) of the class
static $instance =
false;
* Titles of the svg document
* Defs elements of the svg document
* CDATA section of the svg document
* Graph section of the svg document
* Group element to curve tooltips
* Space between graph and abscisse tags.
* default: 5% of the width, limit to 15.
* Space between graph and ordinate tags.
* default: 5% of the width, limit to 15.
* Number of pixel between each line of the abscisse grid.
* Number of pixel between each line of the ordinate grid.
* Padding of the first point from the abscisse.
* Padding of the first point from the ordinate.
* Min value to calculate the scale.
* This value is not calculated, you must fixe
* it if the min value is not the "real" min value passed in the curve data.
* Max value to calculate the scale.
* This value is not calculated, you must fixe it if the max value is not
* the "real" max value passed in the curve data.
* Coefficient pixel/data to the ordinate axe.
public $title =
DEFAULT_TITLE;
* Rotation angle (degree) for the abscisse tags
* Number of lines to the abscisses grid
* Frequency of the abscisses tags
* Number of line to the grid of the Y axe
* You can't access directly this variable using
* <code>$object->display['box'] = false;</code>
* see SvgGrapher::setDisplay() for more details.
'low' =>
array('value' =>
0, 'color'=>
'#FF0000'),
'median' =>
array('value' =>
0, 'color'=>
'#00FF00'),
'high' =>
array('value' =>
0, 'color'=>
'#FF0000'),
* Limits of the data. (calculed with the internal findrange plugin)
public $range =
array('min' =>
0, 'max' =>
0, 'delta' =>
0);
* true if boxes position and scale need to be recalulated
* Boxes positions array, acceptables values are:
* - title : top or bottom
* - legend : left or right
* Call back functions used in SvgDocumentRenderer to do operations on tags.
// SvgGrapher::__construct() {{{
* @param float $width width width
* @param float $height document height
* @param bool $setEHOn true to set exception handler on
public function __construct($width=
400, $height=
300, $setEHOn=
false)
set_exception_handler(array('SvgGrapher', 'exceptionHandler'));
$this->boxDoc =
new Box(array('width'=>
$width, 'height'=>
$height));
$this->svgDom->createDocumentType(
'svg', '-//W3C//DTD SVG 1.1//EN',
'http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd');
$this->svgDocument->setAttribute('xmlns', 'http://www.w3.org/2000/svg');
$this->svgDocument->setAttribute('xmlns:xlink', 'http://www.w3.org/1999/xlink');
// SvgGrapher::singleton() {{{
* Get the current instance (singleton) of the class, or make a new
* @param float $width document width (only for new instance)
* @param float $height document height (only for new instance)
static function singleton($width=
0, $height=
0)
self::$instance =
new SvgGrapher($width, $height);
// svgGrapher::exceptionHandler() {{{
* @param object $exception objet Exception
echo
'SvgGrapher error : ' .
$exception->getMessage();
exit($exception->getCode());
// SvgGrapher::__set() {{{
public function __set($property, $value)
$method =
'set' .
$property;
if(method_exists($this, $method)) {
return $this->$method($value);
return $this->$property =
$value;
// SvgGrapher::__get() {{{
public function __get($property)
$method =
'get' .
$property;
// SvgGrapher::_importCoreFunctions() {{{
* Import the core functions founded in internal directory.
$coreName =
'svgGrapher.internal.';
$coreDir =
dir($corePath);
while(false !==
($f =
$coreDir->read()) ) {
include_once $corePath .
$f;
// SvgGrapher::_calculateBoxProperties() {{{
* _calculateBoxProperties
* - title on the document's top
* - legend on the document's right and on the title's bottom
* - graph pane under the title on the legend's left
* - X tags on the left of the graph pane
* - Y tags on the bottom of the graph pane
* - X label on the left of the X tags
* - Y label on the bottom of the X tags
// calculate the nicer look offsets
// Calculate the data scale to display in the Y axe.
foreach($this->curves as $curve) {
if ($this->range['delta'] ==
0) {
throw
new Exception('No range to plot. Check data');
// Number of lines on the X axe
// calculate the properties of the title box
// Calculate the properties of the legend box
foreach($this->curves as $curve) {
$legends[] =
$curve->legend;
$this->legendTextHeight =
$box['height'];
// calculate the properties of the label y box
// calculate the properties of the tag y box
// calculate the properties of the label x box
// calculate the properties of the tags x box
// Calculate the lines offset of the grid
// SvgGrapher::addCurve() {{{
* Add a curve to the graph.
* @param mixed $name Curve identifier.
* @param array $params params to Curve object
* @param string $color Curve color
public function addCurve($name, $params=
array())
$curve =
new Curve($params, $name);
$this->curves[$name] =
$curve;
$this->tagsX =
$params['labels'];
} elseif(isset
($params['labels']) &&
is_array($params['labels'])) {
// On rassemble les deux tableaux de label en un seul et on dedoublonne.
// Essaye de conserver l'ordre naturel des donnees.
foreach($this->tagsX as $idx =>
$tag) {
// Le label d'index courant est unique, on prend...
if(isset
($params['labels'][$idx]) &&
!in_array($params['labels'][$idx], $this->tagsX)) {
$tmpArr[] =
$params['labels'][$idx];
// La tag en cours est unique ou n'a pas deja etait pris...
// SvgGrapher::genGraph() {{{
// SvgGrapher::drawHLine() {{{
* Draw an horizontal line.
* @param integer $value ordinate of the line.
* @param string $color line color.
public function drawHLine($value, $color =
NULL, $id =
NULL)
'x1' =>
$left, 'y1' =>
$y,
'x2' =>
$right, 'y2' =>
$y,
// SvgGrapher::drawVLine() {{{
* Draw a vertical line (run checkChange before draw line).
* @param integer $value Tag name where draw the line.
* @param string $color line color.
public function drawVLine($value, $color =
NULL, $id =
NULL)
foreach($this->tagsX as $idx =>
$tag) {
'x1' =>
$x, 'y1' =>
$top,
'x2' =>
$x, 'y2' =>
$bottom), $style);
// SvgGrapher::dumpSVG() {{{
* Write the svg in plain-text (to debug)
* @param string href to a CSS file.
public function dumpSVG($specifyCSS=
NULL, $prettyXML=
false)
header("Cache-Control: private");
header("Content-type: text/plain");
print
($this->svgDocument->writeSVGString($specifyCSS, $prettyXML));
// SvgGrapher::drawSVG() {{{
* Draw the svg or save it in file.
* @param string $filename file name to save svg in file.
* @param string href to a CSS file.
* @param boolean $prettyXML true to render pretty xml (unused if filename
public function drawSVG($filename=
NULL, $specifyCSS=
NULL, $prettyXML=
false)
header("Cache-Control: private");
header("Content-type: image/svg+xml");
//print($this->svgDocument->WriteSVGString($specifyCSS, $prettyXML));
print
($this->svgDom->asPrettyXML());
$this->svgDocument->WriteSVGFile($filename, $specifyCSS, $prettyXML);
// SvgGrapher::setDisplay() {{{
* set the display property
* - boolean: all the elements of the $display array take the value
* - array of string: the elements listed in the array take the value true
* - array of keys / values: for each element of the array, the elements of
* the $display array with the key take the value
* - string: the elements of the $display$ array take the value true
* // all the elements in $display take the value false
* // $o->display['legend'] = true
* $o->display = array('stepX', 'stepY');
* // $o->display['stepX'] = true;
* // $o->display['stepY'] = true;
* $o->display = array('labelX'=>false, 'labelY'=>true, 'title'=>true);
* // $o->display['labelX'] = false;
* // $o->display['labelY'] = true;
* // $o->display['title'] = true;
foreach($value as $k=>
$v) {
} elseif(isset
($this->display[$v])) {
foreach($this->display as $k=>
$v) {
Documentation generated on Tue, 23 Oct 2007 11:32:14 +0200 by phpDocumentor 1.4.0