* Copyright (c) 2004-2015 Laurent Destailleur * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation; either version 3 of the License, or * (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program. If not, see . */ /** * \file htdocs/core/class/dolgraph.class.php * \ingroup core * \brief File for class to generate graph */ /** * Class to build graphs. * Usage is: * $dolgraph=new DolGraph(); * $dolgraph->SetTitle($langs->transnoentities('MyTitle').'
'.$langs->transnoentities('MyTitlePercent').'%'); * $dolgraph->SetMaxValue(50); * $dolgraph->SetData($data); * $dolgraph->setShowLegend(2); * $dolgraph->setShowPercent(1); * $dolgraph->SetType(array('pie')); * $dolgraph->setHeight('200'); * $dolgraph->draw('idofgraph'); * print $dolgraph->show($total?0:1); */ class DolGraph { public $type = array(); // Array with type of each series. Example: array('bars', 'horizontalbars', 'lines', 'pies', 'piesemicircle', 'polar'...) public $mode = 'side'; // Mode bars graph: side, depth private $_library = 'chart'; // Graphic library to use (jflot, chart, artichow) //! Array of data public $data; // Data of graph: array(array('abs1',valA1,valB1), array('abs2',valA2,valB2), ...) public $title; // Title of graph public $cssprefix = ''; // To add into css styles /** * @var int|string Width of graph. It can be a numeric for pixels or a string like '100%' */ public $width = 380; /** * @var int Height of graph */ public $height = 200; public $MaxValue = 0; public $MinValue = 0; public $SetShading = 0; public $horizTickIncrement = -1; public $SetNumXTicks = -1; public $labelInterval = -1; public $hideXGrid = false; public $hideYGrid = false; public $Legend = array(); public $LegendWidthMin = 0; public $showlegend = 1; public $showpointvalue = 1; public $showpercent = 0; public $combine = 0; // 0.05 if you want to combine records < 5% into "other" public $graph; // Objet Graph (Artichow, Phplot...) /** * @var string Error code (or message) */ public $error = ''; public $bordercolor; // array(R,G,B) public $bgcolor; // array(R,G,B) public $bgcolorgrid = array(255, 255, 255); // array(R,G,B) public $datacolor; // array(array(R,G,B),...) private $stringtoshow; // To store string to output graph into HTML page /** * Constructor * * @param string $library 'auto' (default) */ public function __construct($library = 'auto') { global $conf; global $theme_bordercolor, $theme_datacolor, $theme_bgcolor; $this->bordercolor = array(235, 235, 224); $this->datacolor = array(array(120, 130, 150), array(160, 160, 180), array(190, 190, 220)); $this->bgcolor = array(235, 235, 224); $color_file = DOL_DOCUMENT_ROOT . '/theme/' . $conf->theme . '/theme_vars.inc.php'; if (is_readable($color_file)) { include_once $color_file; if (isset($theme_bordercolor)) $this->bordercolor = $theme_bordercolor; if (isset($theme_datacolor)) $this->datacolor = $theme_datacolor; if (isset($theme_bgcolor)) $this->bgcolor = $theme_bgcolor; } //print 'bgcolor: '.join(',',$this->bgcolor).'
'; $this->_library = $library; if ($this->_library == 'auto') { $this->_library = (empty($conf->global->MAIN_JS_GRAPH) ? 'chart' : $conf->global->MAIN_JS_GRAPH); } } // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps /** * Utiliser SetNumTicks ou SetHorizTickIncrement mais pas les 2 * * @param float $xi Xi * @return boolean True */ public function SetHorizTickIncrement($xi) { // phpcs:enable $this->horizTickIncrement = $xi; return true; } // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps /** * Utiliser SetNumTicks ou SetHorizTickIncrement mais pas les 2 * * @param float $xt Xt * @return boolean True */ public function SetNumXTicks($xt) { // phpcs:enable $this->SetNumXTicks = $xt; return true; } // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps /** * Set label interval to reduce number of labels * * @param float $x Label interval * @return boolean True */ public function SetLabelInterval($x) { // phpcs:enable $this->labelInterval = $x; return true; } // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps /** * Hide X grid * * @param boolean $bool XGrid or not * @return boolean true */ public function SetHideXGrid($bool) { // phpcs:enable $this->hideXGrid = $bool; return true; } // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps /** * Hide Y grid * * @param boolean $bool YGrid or not * @return boolean true */ public function SetHideYGrid($bool) { // phpcs:enable $this->hideYGrid = $bool; return true; } // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps /** * Set y label * * @param string $label Y label * @return boolean|null True */ public function SetYLabel($label) { // phpcs:enable $this->YLabel = $label; } // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps /** * Set width * * @param int|string $w Width (Example: 320 or '100%') * @return boolean|null True */ public function SetWidth($w) { // phpcs:enable $this->width = $w; } // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps /** * Set title * * @param string $title Title * @return void */ public function SetTitle($title) { // phpcs:enable $this->title = $title; } // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps /** * Set data * * @param array $data Data * @return void * @see draw_jflot() for syntax of data array */ public function SetData($data) { // phpcs:enable $this->data = $data; } // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps /** * Set data * * @param array $datacolor Data color array(array(R,G,B),array(R,G,B)...) * @return void */ public function SetDataColor($datacolor) { // phpcs:enable $this->datacolor = $datacolor; } // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps /** * Set type * * @param array $type Array with type for each serie. Example: array('type1', 'type2', ...) where type can be: * 'pie', 'piesemicircle', 'polar', 'lines', 'linesnopoint', 'bars', 'horizontalbars'... * @return void */ public function SetType($type) { // phpcs:enable $this->type = $type; } // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps /** * Set legend * * @param array $legend Legend. Example: array('seriename1','seriname2',...) * @return void */ public function SetLegend($legend) { // phpcs:enable $this->Legend = $legend; } // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps /** * Set min width * * @param int $legendwidthmin Min width * @return void */ public function SetLegendWidthMin($legendwidthmin) { // phpcs:enable $this->LegendWidthMin = $legendwidthmin; } // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps /** * Set max value * * @param int $max Max value * @return void */ public function SetMaxValue($max) { // phpcs:enable $this->MaxValue = $max; } // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps /** * Get max value * * @return int Max value */ public function GetMaxValue() { // phpcs:enable return $this->MaxValue; } // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps /** * Set min value * * @param int $min Min value * @return void */ public function SetMinValue($min) { // phpcs:enable $this->MinValue = $min; } // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps /** * Get min value * * @return int Max value */ public function GetMinValue() { // phpcs:enable return $this->MinValue; } // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps /** * Set height * * @param int $h Height * @return void */ public function SetHeight($h) { // phpcs:enable $this->height = $h; } // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps /** * Set shading * * @param string $s Shading * @return void */ public function SetShading($s) { // phpcs:enable $this->SetShading = $s; } // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps /** * Set shading * * @param string $s Shading * @return void */ public function SetCssPrefix($s) { // phpcs:enable $this->cssprefix = $s; } // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps /** * Reset bg color * * @return void */ public function ResetBgColor() { // phpcs:enable unset($this->bgcolor); } // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps /** * Reset bgcolorgrid * * @return void */ public function ResetBgColorGrid() { // phpcs:enable unset($this->bgcolorgrid); } /** * Is graph ko * * @return string Error */ public function isGraphKo() { return $this->error; } /** * Show legend or not * * @param int $showlegend 1=Show legend (default), 0=Hide legend, 2=Show legend on right * @return void */ public function setShowLegend($showlegend) { $this->showlegend = $showlegend; } /** * Show pointvalue or not * * @param int $showpointvalue 1=Show value for each point, as tooltip or inline (default), 0=Hide value * @return void */ public function setShowPointValue($showpointvalue) { $this->showpointvalue = $showpointvalue; } /** * Show percent or not * * @param int $showpercent 1=Show percent for each point, as tooltip or inline, 0=Hide percent (default) * @return void */ public function setShowPercent($showpercent) { $this->showpercent = $showpercent; } // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps /** * Define background color of complete image * * @param array $bg_color array(R,G,B) ou 'onglet' ou 'default' * @return void */ public function SetBgColor($bg_color = array(255, 255, 255)) { // phpcs:enable global $theme_bgcolor, $theme_bgcoloronglet; if (!is_array($bg_color)) { if ($bg_color == 'onglet') { //print 'ee'.join(',',$theme_bgcoloronglet); $this->bgcolor = $theme_bgcoloronglet; } else { $this->bgcolor = $theme_bgcolor; } } else { $this->bgcolor = $bg_color; } } // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps /** * Define background color of grid * * @param array $bg_colorgrid array(R,G,B) ou 'onglet' ou 'default' * @return void */ public function SetBgColorGrid($bg_colorgrid = array(255, 255, 255)) { // phpcs:enable global $theme_bgcolor, $theme_bgcoloronglet; if (!is_array($bg_colorgrid)) { if ($bg_colorgrid == 'onglet') { //print 'ee'.join(',',$theme_bgcoloronglet); $this->bgcolorgrid = $theme_bgcoloronglet; } else { $this->bgcolorgrid = $theme_bgcolor; } } else { $this->bgcolorgrid = $bg_colorgrid; } } // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps /** * Reset data color * * @return void */ public function ResetDataColor() { // phpcs:enable unset($this->datacolor); } // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps /** * Get max value * * @return int Max value */ public function GetMaxValueInData() { // phpcs:enable if (!is_array($this->data)) return 0; $k = 0; $vals = array(); $nblines = count($this->data); $nbvalues = (empty($this->data[0]) ? 0 : count($this->data[0]) - 1); for ($j = 0; $j < $nblines; $j++) { for ($i = 0; $i < $nbvalues; $i++) { $vals[$k] = $this->data[$j][$i + 1]; $k++; } } rsort($vals); return $vals[0]; } // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps /** * Return min value of all data * * @return int Min value of all data */ public function GetMinValueInData() { // phpcs:enable if (!is_array($this->data)) return 0; $k = 0; $vals = array(); $nblines = count($this->data); $nbvalues = (empty($this->data[0]) ? 0 : count($this->data[0]) - 1); for ($j = 0; $j < $nblines; $j++) { for ($i = 0; $i < $nbvalues; $i++) { $vals[$k] = $this->data[$j][$i + 1]; $k++; } } sort($vals); return $vals[0]; } // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps /** * Return max value of all data * * @return int Max value of all data */ public function GetCeilMaxValue() { // phpcs:enable $max = $this->GetMaxValueInData(); if ($max != 0) $max++; $size = dol_strlen(abs(ceil($max))); $factor = 1; for ($i = 0; $i < ($size - 1); $i++) { $factor *= 10; } $res = 0; if (is_numeric($max)) $res = ceil($max / $factor) * $factor; //print "max=".$max." res=".$res; return $res; } // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps /** * Return min value of all data * * @return double Max value of all data */ public function GetFloorMinValue() { // phpcs:enable $min = $this->GetMinValueInData(); if ($min == '') $min = 0; if ($min != 0) $min--; $size = dol_strlen(abs(floor($min))); $factor = 1; for ($i = 0; $i < ($size - 1); $i++) { $factor *= 10; } $res = floor($min / $factor) * $factor; //print "min=".$min." res=".$res; return $res; } /** * Build a graph into memory using correct library (may also be wrote on disk, depending on library used) * * @param string $file Image file name to use to save onto disk (also used as javascript unique id) * @param string $fileurl Url path to show image if saved onto disk * @return integer|null */ public function draw($file, $fileurl = '') { if (empty($file)) { $this->error = "Call to draw method was made with empty value for parameter file."; dol_syslog(get_class($this) . "::draw " . $this->error, LOG_ERR); return -2; } if (!is_array($this->data)) { $this->error = "Call to draw method was made but SetData was not called or called with an empty dataset for parameters"; dol_syslog(get_class($this) . "::draw " . $this->error, LOG_ERR); return -1; } if (count($this->data) < 1) { $this->error = "Call to draw method was made but SetData was is an empty dataset"; dol_syslog(get_class($this) . "::draw " . $this->error, LOG_WARNING); } $call = "draw_" . $this->_library; call_user_func_array(array($this, $call), array($file, $fileurl)); } // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps /** * Build a graph using JFlot library. Input when calling this method should be: * $this->data = array(array(0=>'labelxA',1=>yA), array('labelxB',yB)); * $this->data = array(array(0=>'labelxA',1=>yA1,...,n=>yAn), array('labelxB',yB1,...yBn)); // or when there is n series to show for each x * $this->data = array(array('label'=>'labelxA','data'=>yA), array('labelxB',yB)); // Syntax deprecated * $this->legend= array("Val1",...,"Valn"); // list of n series name * $this->type = array('bars',...'lines','linesnopoint'); or array('pie') or array('polar') * $this->mode = 'depth' ??? * $this->bgcolorgrid * $this->datacolor * $this->shownodatagraph * * @param string $file Image file name to use to save onto disk (also used as javascript unique id) * @param string $fileurl Url path to show image if saved onto disk. Never used here. * @return void */ private function draw_jflot($file, $fileurl) { // phpcs:enable global $conf, $langs; dol_syslog(get_class($this) . "::draw_jflot this->type=" . join(',', $this->type) . " this->MaxValue=" . $this->MaxValue); if (empty($this->width) && empty($this->height)) { print 'Error width or height not set'; return; } $legends = array(); $nblot = 0; if (is_array($this->data) && is_array($this->data[0])) { $nblot = count($this->data[0]) - 1; // -1 to remove legend } if ($nblot < 0) dol_syslog('Bad value for property ->data. Must be set by mydolgraph->SetData before calling mydolgrapgh->draw', LOG_WARNING); $firstlot = 0; // Works with line but not with bars //if ($nblot > 2) $firstlot = ($nblot - 2); // We limit nblot to 2 because jflot can't manage more than 2 bars on same x $i = $firstlot; $serie = array(); while ($i < $nblot) // Loop on each serie { $values = array(); // Array with horizontal y values (specific values of a serie) for each abscisse x $serie[$i] = "var d" . $i . " = [];\n"; // Fill array $values $x = 0; foreach ($this->data as $valarray) // Loop on each x { $legends[$x] = $valarray[0]; $values[$x] = (is_numeric($valarray[$i + 1]) ? $valarray[$i + 1] : null); $x++; } if (isset($this->type[$firstlot]) && in_array($this->type[$firstlot], array('pie', 'piesemicircle', 'polar'))) { foreach ($values as $x => $y) { if (isset($y)) $serie[$i] .= 'd' . $i . '.push({"label":"' . dol_escape_js($legends[$x]) . '", "data":' . $y . '});' . "\n"; } } else { foreach ($values as $x => $y) { if (isset($y)) $serie[$i] .= 'd' . $i . '.push([' . $x . ', ' . $y . ']);' . "\n"; } } unset($values); $i++; } $tag = dol_escape_htmltag(dol_string_unaccent(dol_string_nospecial(basename($file), '_', array('-', '.')))); $this->stringtoshow = '' . "\n"; if (!empty($this->title)) $this->stringtoshow .= '
' . $this->title . '
'; if (!empty($this->shownographyet)) { $this->stringtoshow .= '
'; $this->stringtoshow .= '
' . $langs->trans("NotEnoughDataYet") . '...
'; return; } // Start the div that will contains all the graph $dolxaxisvertical = ''; if (count($this->data) > 20) $dolxaxisvertical = 'dol-xaxis-vertical'; $this->stringtoshow .= '
' . "\n"; $this->stringtoshow .= '' . "\n"; } // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps /** * Build a graph using Chart library. Input when calling this method should be: * $this->data = array(array(0=>'labelxA',1=>yA), array('labelxB',yB)); * $this->data = array(array(0=>'labelxA',1=>yA1,...,n=>yAn), array('labelxB',yB1,...yBn)); // or when there is n series to show for each x * $this->data = array(array('label'=>'labelxA','data'=>yA), array('labelxB',yB)); // Syntax deprecated * $this->legend= array("Val1",...,"Valn"); // list of n series name * $this->type = array('bars',...'lines', 'linesnopoint'); or array('pie') or array('polar') or array('piesemicircle'); * $this->mode = 'depth' ??? * $this->bgcolorgrid * $this->datacolor * $this->shownodatagraph * * @param string $file Image file name to use to save onto disk (also used as javascript unique id) * @param string $fileurl Url path to show image if saved onto disk. Never used here. * @return void */ private function draw_chart($file, $fileurl) { // phpcs:enable global $conf, $langs; dol_syslog(get_class($this) . "::draw_chart this->type=" . join(',', $this->type) . " this->MaxValue=" . $this->MaxValue); if (empty($this->width) && empty($this->height)) { print 'Error width or height not set'; return; } $showlegend = $this->showlegend; $legends = array(); $nblot = 0; if (is_array($this->data)) { foreach ($this->data as $valarray) // Loop on each x { $nblot = max($nblot, count($valarray) - 1); // -1 to remove legend } } //var_dump($nblot); if ($nblot < 0) dol_syslog('Bad value for property ->data. Must be set by mydolgraph->SetData before calling mydolgrapgh->draw', LOG_WARNING); $firstlot = 0; // Works with line but not with bars //if ($nblot > 2) $firstlot = ($nblot - 2); // We limit nblot to 2 because jflot can't manage more than 2 bars on same x $serie = array(); $arrayofgroupslegend = array(); //var_dump($this->data); $i = $firstlot; while ($i < $nblot) // Loop on each serie { $values = array(); // Array with horizontal y values (specific values of a serie) for each abscisse x (with x=0,1,2,...) $serie[$i] = ""; // Fill array $values $x = 0; foreach ($this->data as $valarray) // Loop on each x { $legends[$x] = (array_key_exists('label', $valarray) ? $valarray['label'] : $valarray[0]); $array_of_ykeys = array_keys($valarray); $alabelexists = 1; $tmpykey = explode('_', ($array_of_ykeys[$i + ($alabelexists ? 1 : 0)]), 3); if (!empty($tmpykey[2]) || $tmpykey[2] == '0') { // This is a 'Group by' array $tmpvalue = (array_key_exists('y_' . $tmpykey[1] . '_' . $tmpykey[2], $valarray) ? $valarray['y_' . $tmpykey[1] . '_' . $tmpykey[2]] : $valarray[$i + 1]); $values[$x] = (is_numeric($tmpvalue) ? $tmpvalue : null); $arrayofgroupslegend[$i] = array( 'stacknum' => $tmpykey[1], 'legend' => $this->Legend[$tmpykey[1]], 'legendwithgroup' => $this->Legend[$tmpykey[1]] . ' - ' . $tmpykey[2] ); } else { $tmpvalue = (array_key_exists('y_' . $i, $valarray) ? $valarray['y_' . $i] : $valarray[$i + 1]); //var_dump($i.'_'.$x.'_'.$tmpvalue); $values[$x] = (is_numeric($tmpvalue) ? $tmpvalue : null); } $x++; } //var_dump($values); $j = 0; foreach ($values as $x => $y) { if (isset($y)) { $serie[$i] .= ($j > 0 ? ", " : "") . $y; } else { $serie[$i] .= ($j > 0 ? ", " : "") . 'null'; } $j++; } $values = null; // Free mem $i++; } //var_dump($serie); //var_dump($arrayofgroupslegend); $tag = dol_escape_htmltag(dol_string_unaccent(dol_string_nospecial(basename($file), '_', array('-', '.')))); $this->stringtoshow = '' . "\n"; if (!empty($this->title)) $this->stringtoshow .= '
' . $this->title . '
'; if (!empty($this->shownographyet)) { $this->stringtoshow .= '
'; $this->stringtoshow .= '
' . $langs->trans("NotEnoughDataYet") . '...
'; return; } // Start the div that will contains all the graph $dolxaxisvertical = ''; if (count($this->data) > 20) $dolxaxisvertical = 'dol-xaxis-vertical'; // No height for the pie grah $cssfordiv = 'dolgraphchart'; if (isset($this->type[$firstlot])) $cssfordiv .= ' dolgraphchar' . $this->type[$firstlot]; $this->stringtoshow .= '
' . "\n"; $this->stringtoshow .= '' . "\n"; } /** * Output HTML string to total value * * @return string HTML string to total value */ public function total() { $value = 0; foreach ($this->data as $valarray) // Loop on each x { $value += $valarray[1]; } return $value; } /** * Output HTML string to show graph * * @param int|string $shownographyet Show graph to say there is not enough data or the message in $shownographyet if it is a string. * @return string HTML string to show graph */ public function show($shownographyet = 0) { global $langs; if ($shownographyet) { $s = '
'; $s .= '
'; if (is_numeric($shownographyet)) { $s .= $langs->trans("NotEnoughDataYet") . '...'; } else { $s .= $shownographyet . '...'; } $s .= '
'; return $s; } return $this->stringtoshow; } /** * getDefaultGraphSizeForStats * * @param string $direction 'width' or 'height' * @param string $defaultsize Value we want as default size * @return int Value of width or height to use by default */ public static function getDefaultGraphSizeForStats($direction, $defaultsize = '') { global $conf; if ($direction == 'width') { if (empty($conf->dol_optimize_smallscreen)) return ($defaultsize ? $defaultsize : '500'); else return (empty($_SESSION['dol_screen_width']) ? '280' : ($_SESSION['dol_screen_width'] - 40)); } if ($direction == 'height') { return (empty($conf->dol_optimize_smallscreen) ? ($defaultsize ? $defaultsize : '200') : '160'); } return 0; } }