12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298 |
- <?php
- /* Copyright (c) 2003-2006 Rodolphe Quiedeville <rodolphe@quiedeville.org>
- * Copyright (c) 2004-2015 Laurent Destailleur <eldy@users.sourceforge.net>
- *
- * 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 <https://www.gnu.org/licenses/>.
- */
- /**
- * \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').'<br>'.$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', 'lines', ...)
- public $mode = 'side'; // Mode bars graph: side, depth
- private $_library = 'jflot'; // Graphic library to use (jflot, 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).'<br>';
- $this->_library = $library;
- if ($this->_library == 'auto') {
- $this->_library = (empty($conf->global->MAIN_JS_GRAPH) ? 'jflot': $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', ...
- * @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
- $k = 0;
- $vals = array();
- $nblines = count($this->data);
- $nbvalues = 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
- $k = 0;
- $vals = array();
- $nblines = count($this->data);
- $nbvalues = 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 = '<!-- Build using jflot -->'."\n";
- if (!empty($this->title)) $this->stringtoshow .= '<div class="center dolgraphtitle'.(empty($this->cssprefix) ? '' : ' dolgraphtitle'.$this->cssprefix).'">'.$this->title.'</div>';
- if (!empty($this->shownographyet))
- {
- $this->stringtoshow .= '<div style="width:'.$this->width.'px;height:'.$this->height.'px;" class="nographyet"></div>';
- $this->stringtoshow .= '<div class="nographyettext">'.$langs->trans("NotEnoughDataYet").'</div>';
- return;
- }
- // Start the div that will contains all the graph
- $dolxaxisvertical='';
- if (count($this->data) > 20) $dolxaxisvertical='dol-xaxis-vertical';
- $this->stringtoshow .= '<div id="placeholder_'.$tag.'" style="width:'.$this->width.'px;height:'.$this->height.'px;" class="dolgraph'.(empty($dolxaxisvertical)?'':' '.$dolxaxisvertical).(empty($this->cssprefix) ? '' : ' dolgraph'.$this->cssprefix).' center"></div>'."\n";
- $this->stringtoshow .= '<script id="'.$tag.'">'."\n";
- $this->stringtoshow .= '$(function () {'."\n";
- $i = $firstlot;
- if ($nblot < 0)
- {
- $this->stringtoshow .= '<!-- No series of data -->'."\n";
- }
- else
- {
- while ($i < $nblot)
- {
- $this->stringtoshow .= '<!-- Serie '.$i.' -->'."\n";
- $this->stringtoshow .= $serie[$i]."\n";
- $i++;
- }
- }
- $this->stringtoshow .= "\n";
- // Special case for Graph of type 'pie'
- if (isset($this->type[$firstlot]) && in_array($this->type[$firstlot], array('pie', 'piesemicircle', 'polar')))
- {
- $datacolor = array();
- foreach ($this->datacolor as $val) {
- if (is_array($val)) $datacolor[] = "#".sprintf("%02x%02x%02x", $val[0], $val[1], $val[2]); // If datacolor is array(R, G, B)
- else $datacolor[] = "#".str_replace(array('#', '-'), '', $val); // If $val is '124' or '#124'
- }
- $urltemp = ''; // TODO Add support for url link into labels
- $showlegend = $this->showlegend;
- $showpointvalue = $this->showpointvalue;
- $showpercent = $this->showpercent;
- $this->stringtoshow .= '
- function plotWithOptions_'.$tag.'() {
- $.plot($("#placeholder_'.$tag.'"), d0,
- {
- series: {
- pie: {
- show: true,
- radius: 0.8,
- '.($this->combine ? '
- combine: {
- threshold: '.$this->combine.'
- },' : '').'
- label: {
- show: true,
- radius: 0.9,
- formatter: function(label, series) {
- var percent=Math.round(series.percent);
- var number=series.data[0][1];
- return \'';
- $this->stringtoshow .= '<span style="font-size:8pt;text-align:center;padding:2px;color:black;">';
- if ($urltemp) $this->stringtoshow .= '<a style="color: #FFFFFF;" border="0" href="'.$urltemp.'">';
- $this->stringtoshow .= '\'+';
- $this->stringtoshow .= ($showlegend ? '' : 'label+\' \'+'); // Hide label if already shown in legend
- $this->stringtoshow .= ($showpointvalue ? 'number+' : '');
- $this->stringtoshow .= ($showpercent ? '\'<br/>\'+percent+\'%\'+' : '');
- $this->stringtoshow .= '\'';
- if ($urltemp) $this->stringtoshow .= '</a>';
- $this->stringtoshow .= '</span>\';
- },
- background: {
- opacity: 0.0,
- color: \'#000000\'
- }
- }
- }
- },
- zoom: {
- interactive: true
- },
- pan: {
- interactive: true
- },';
- if (count($datacolor))
- {
- $this->stringtoshow .= 'colors: '.(!empty($data['seriescolor']) ? json_encode($data['seriescolor']) : json_encode($datacolor)).',';
- }
- $this->stringtoshow .= 'legend: {show: '.($showlegend ? 'true' : 'false').', position: \'ne\' }
- });
- }'."\n";
- }
- // Other cases, graph of type 'bars', 'lines'
- else
- {
- // Add code to support tooltips
- // TODO: remove js css and use graph-tooltip-inner class instead by adding css in each themes
- $this->stringtoshow .= '
- function showTooltip_'.$tag.'(x, y, contents) {
- $(\'<div class="graph-tooltip-inner" id="tooltip_'.$tag.'">\' + contents + \'</div>\').css({
- position: \'absolute\',
- display: \'none\',
- top: y + 10,
- left: x + 15,
- border: \'1px solid #000\',
- padding: \'5px\',
- \'background-color\': \'#000\',
- \'color\': \'#fff\',
- \'font-weight\': \'bold\',
- width: 200,
- opacity: 0.80
- }).appendTo("body").fadeIn(100);
- }
- var previousPoint = null;
- $("#placeholder_'.$tag.'").bind("plothover", function (event, pos, item) {
- $("#x").text(pos.x.toFixed(2));
- $("#y").text(pos.y.toFixed(2));
- if (item) {
- if (previousPoint != item.dataIndex) {
- previousPoint = item.dataIndex;
- $("#tooltip").remove();
- /* console.log(item); */
- var x = item.datapoint[0].toFixed(2);
- var y = item.datapoint[1].toFixed(2);
- var z = item.series.xaxis.ticks[item.dataIndex].label;
- ';
- if ($this->showpointvalue > 0) $this->stringtoshow .= '
- showTooltip_'.$tag.'(item.pageX, item.pageY, item.series.label + "<br>" + z + " => " + y);
- ';
- $this->stringtoshow .= '
- }
- }
- else {
- $("#tooltip_'.$tag.'").remove();
- previousPoint = null;
- }
- });
- ';
- $this->stringtoshow .= 'var stack = null, steps = false;'."\n";
- $this->stringtoshow .= 'function plotWithOptions_'.$tag.'() {'."\n";
- $this->stringtoshow .= '$.plot($("#placeholder_'.$tag.'"), [ '."\n";
- $i = $firstlot;
- while ($i < $nblot)
- {
- if ($i > $firstlot) $this->stringtoshow .= ', '."\n";
- $color = sprintf("%02x%02x%02x", $this->datacolor[$i][0], $this->datacolor[$i][1], $this->datacolor[$i][2]);
- $this->stringtoshow .= '{ ';
- if (!isset($this->type[$i]) || $this->type[$i] == 'bars') $this->stringtoshow .= 'bars: { lineWidth: 1, show: true, align: "'.($i == $firstlot ? 'center' : 'left').'", barWidth: 0.5 }, ';
- if (isset($this->type[$i]) && ($this->type[$i] == 'lines' || $this->type[$i] == 'linesnopoint')) $this->stringtoshow .= 'lines: { show: true, fill: false }, points: { show: '.($this->type[$i] == 'linesnopoint' ? 'false' : 'true').' }, ';
- $this->stringtoshow .= 'color: "#'.$color.'", label: "'.(isset($this->Legend[$i]) ? dol_escape_js($this->Legend[$i]) : '').'", data: d'.$i.' }';
- $i++;
- }
- // shadowSize: 0 -> Drawing is faster without shadows
- $this->stringtoshow .= "\n".' ], { series: { shadowSize: 0, stack: stack, lines: { fill: false, steps: steps }, bars: { barWidth: 0.6, fillColor: { colors: [{opacity: 0.9 }, {opacity: 0.85}] }} }'."\n";
- // Xaxis
- $this->stringtoshow .= ', xaxis: { ticks: ['."\n";
- $x = 0;
- foreach ($this->data as $key => $valarray)
- {
- if ($x > 0) $this->stringtoshow .= ', '."\n";
- $this->stringtoshow .= ' ['.$x.', "'.$valarray[0].'"]';
- $x++;
- }
- $this->stringtoshow .= '] }'."\n";
- // Yaxis
- $this->stringtoshow .= ', yaxis: { min: '.$this->MinValue.', max: '.($this->MaxValue).' }'."\n";
- // Background color
- $color1 = sprintf("%02x%02x%02x", $this->bgcolorgrid[0], $this->bgcolorgrid[0], $this->bgcolorgrid[2]);
- $color2 = sprintf("%02x%02x%02x", $this->bgcolorgrid[0], $this->bgcolorgrid[1], $this->bgcolorgrid[2]);
- $this->stringtoshow .= ', grid: { hoverable: true, backgroundColor: { colors: ["#'.$color1.'", "#'.$color2.'"] }, borderWidth: 1, borderColor: \'#e6e6e6\', tickColor : \'#e6e6e6\' }'."\n";
- //$this->stringtoshow.=', shadowSize: 20'."\n"; TODO Uncommet this
- $this->stringtoshow .= '});'."\n";
- $this->stringtoshow .= '}'."\n";
- }
- $this->stringtoshow .= 'plotWithOptions_'.$tag.'();'."\n";
- $this->stringtoshow .= '});'."\n";
- $this->stringtoshow .= '</script>'."\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) && 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(); $arrayofgroupslegend = array();
- 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] = "";
- //var_dump($this->data);exit;
- // 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])) { // 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]);
- $values[$x] = (is_numeric($tmpvalue) ? $tmpvalue : null);
- }
- $x++;
- }
- $j = 0;
- foreach ($values as $x => $y) {
- if (isset($y)) {
- $serie[$i] .= ($j > 0 ? ", " : "").$y;
- $j++;
- }
- }
- $values = null; // Free mem
- $i++;
- }
- $tag = dol_escape_htmltag(dol_string_unaccent(dol_string_nospecial(basename($file), '_', array('-', '.'))));
- $this->stringtoshow = '<!-- Build using chart -->'."\n";
- if (!empty($this->title)) $this->stringtoshow .= '<div class="center dolgraphtitle'.(empty($this->cssprefix) ? '' : ' dolgraphtitle'.$this->cssprefix).'">'.$this->title.'</div>';
- if (!empty($this->shownographyet))
- {
- $this->stringtoshow .= '<div style="width:'.$this->width.(strpos($this->width, '%') > 0 ? '': 'px').'; height:'.$this->height.'px;" class="nographyet"></div>';
- $this->stringtoshow .= '<div class="nographyettext">'.$langs->trans("NotEnoughDataYet").'</div>';
- 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 .= '<div id="placeholder_'.$tag.'" style="min-height: '.$this->height.(strpos($this->height, '%') > 0 ? '': 'px').'; width:'.$this->width.(strpos($this->width, '%') > 0 ? '': 'px').';" class="'.$cssfordiv.' dolgraph'.(empty($dolxaxisvertical)?'':' '.$dolxaxisvertical).(empty($this->cssprefix) ? '' : ' dolgraph'.$this->cssprefix).' center"><canvas id="canvas_'.$tag.'"></canvas></div>'."\n";
- $this->stringtoshow .= '<script id="'.$tag.'">'."\n";
- $i = $firstlot;
- if ($nblot < 0)
- {
- $this->stringtoshow .= '<!-- No series of data -->';
- }
- else
- {
- while ($i < $nblot)
- {
- //$this->stringtoshow .= '<!-- Series '.$i.' -->'."\n";
- //$this->stringtoshow .= $serie[$i]."\n";
- $i++;
- }
- }
- $this->stringtoshow .= "\n";
- // Special case for Graph of type 'pie', 'piesemicircle', or 'polar'
- if (isset($this->type[$firstlot]) && (in_array($this->type[$firstlot], array('pie', 'polar', 'piesemicircle'))))
- {
- $type = $this->type[$firstlot]; // pie or polar
- $this->stringtoshow .= 'var options = {'."\n";
- if ($this->type[$firstlot] == 'piesemicircle') {
- $this->stringtoshow .= 'circumference: Math.PI,'."\n";
- $this->stringtoshow .= 'rotation: -Math.PI,'."\n";
- }
- if ($showlegend) $this->stringtoshow .= 'legend: { position: \''.($showlegend == 2 ? 'right' : 'top').'\' },'."\n";
- $this->stringtoshow .= 'elements: { arc: {'."\n";
- // Color of earch arc
- $this->stringtoshow .= 'backgroundColor: [';
- $i = 0; $foundnegativecolor = 0;
- foreach($legends as $val) // Loop on each serie
- {
- if ($i > 0) $this->stringtoshow .= ', '."\n";
- if (is_array($this->datacolor[$i])) $color = 'rgb('.$this->datacolor[$i][0].', '.$this->datacolor[$i][1].', '.$this->datacolor[$i][2].')'; // If datacolor is array(R, G, B)
- else {
- $tmp = str_replace('#', '', $this->datacolor[$i]);
- if (strpos($tmp, '-') !== false) {
- $foundnegativecolor++;
- $color = '#FFFFFF'; // If $val is '-123'
- }
- else $color = "#".$tmp; // If $val is '123' or '#123'
- }
- $this->stringtoshow .= "'".$color."'";
- $i++;
- }
- $this->stringtoshow .= '], '."\n";
- // Border color
- if ($foundnegativecolor) {
- $this->stringtoshow .= 'borderColor: [';
- $i = 0;
- foreach($legends as $val) // Loop on each serie
- {
- if ($i > 0) $this->stringtoshow .= ', '."\n";
- if (is_array($this->datacolor[$i])) $color = 'null'; // If datacolor is array(R, G, B)
- else {
- $tmp = str_replace('#', '', $this->datacolor[$i]);
- if (strpos($tmp, '-') !== false) $color = '#'.str_replace('-', '', $tmp); // If $val is '-123'
- else $color = 'null'; // If $val is '123' or '#123'
- }
- $this->stringtoshow .= ($color == 'null' ? "'rgba(0,0,0,0.2)'" : "'".$color."'");
- $i++;
- }
- $this->stringtoshow .= ']';
- }
- $this->stringtoshow .= '} } };'."\n";
- $this->stringtoshow .= '
- var ctx = document.getElementById("canvas_'.$tag.'").getContext("2d");
- var chart = new Chart(ctx, {
- // The type of chart we want to create
- type: \''.(in_array($type, array('pie', 'piesemicircle')) ? 'doughnut' : 'polarArea').'\',
- // Configuration options go here
- options: options,
- data: {
- labels: [';
- $i = 0;
- foreach($legends as $val) // Loop on each serie
- {
- if ($i > 0) $this->stringtoshow .= ', ';
- $this->stringtoshow .= "'".$val."'";
- $i++;
- }
- $this->stringtoshow .= '],
- datasets: [';
- $i = 0;
- $i = 0;
- while ($i < $nblot) // Loop on each serie
- {
- $color = 'rgb('.$this->datacolor[$i][0].', '.$this->datacolor[$i][1].', '.$this->datacolor[$i][2].')';
- //$color = (!empty($data['seriescolor']) ? json_encode($data['seriescolor']) : json_encode($datacolor));
- if ($i > 0) $this->stringtoshow .= ', '."\n";
- $this->stringtoshow .= '{'."\n";
- //$this->stringtoshow .= 'borderColor: \''.$color.'\', ';
- //$this->stringtoshow .= 'backgroundColor: \''.$color.'\', ';
- $this->stringtoshow .= ' data: ['.$serie[$i].']';
- $this->stringtoshow .= '}'."\n";
- $i++;
- }
- $this->stringtoshow .= ']'."\n";
- $this->stringtoshow .= '}'."\n";
- $this->stringtoshow .= '});'."\n";
- }
- // Other cases, graph of type 'bars', 'lines', 'linesnopoint'
- else
- {
- $type = 'bar';
- if (!isset($this->type[$firstlot]) || $this->type[$firstlot] == 'bars') $type = 'bar';
- if (isset($this->type[$firstlot]) && ($this->type[$firstlot] == 'lines' || $this->type[$firstlot] == 'linesnopoint')) $type = 'line';
- $this->stringtoshow .= 'var options = { maintainAspectRatio: false, aspectRatio: 2.5';
- if (count($arrayofgroupslegend) > 0) {
- $this->stringtoshow .= ', scales: {xAxes: [{ stacked: true, }], yAxes: [{ stacked: true }] }';
- }
- $this->stringtoshow .= '};';
- $this->stringtoshow .= '
- var ctx = document.getElementById("canvas_'.$tag.'").getContext("2d");
- var chart = new Chart(ctx, {
- // The type of chart we want to create
- type: \''.$type.'\',
- // Configuration options go here
- options: options,
- data: {
- labels: [';
- $i = 0;
- foreach($legends as $val) // Loop on each serie
- {
- if ($i > 0) $this->stringtoshow .= ', ';
- $this->stringtoshow .= "'".$val."'";
- $i++;
- }
- //var_dump($arrayofgroupslegend);
- $this->stringtoshow .= '],
- datasets: [';
- $i = 0; $iinstack = 0;
- $oldstacknum = -1;
- while ($i < $nblot) // Loop on each serie
- {
- if (count($arrayofgroupslegend[$i]) > 0) { // We used a 'group by'
- // If we change the stack
- $newcolor = $this->datacolor[$arrayofgroupslegend[$i]['stacknum']];
- if ($oldstacknum == -1 || $arrayofgroupslegend[$i]['stacknum'] != $oldstacknum) {
- $iinstack = 0;
- //var_dump('iinstack='.$iinstack.' - '.colorArrayToHex($newcolor));
- } else {
- // Change color with offset of $$iinstack
- //var_dump($newcolor);
- $ratio = max(-90, -10 * $iinstack);
- $brightnessratio = min(90, 20 * $iinstack);
- $newcolor = array_values(colorHexToRgb(colorAgressiveness(colorArrayToHex($newcolor), $ratio, $brightnessratio), false, true));
- //var_dump($ratio.' '.$brightnessratio);
- //var_dump($newcolor);
- }
- $oldstacknum = $arrayofgroupslegend[$i]['stacknum'];
- $color = 'rgb('.$newcolor[0].', '.$newcolor[1].', '.$newcolor[2].', 0.9)';
- $bordercolor = 'rgb('.$newcolor[0].', '.$newcolor[1].', '.$newcolor[2].')';
- $textoflegend = $arrayofgroupslegend[$i]['legendwithgroup'];
- } else {
- $color = 'rgb('.$this->datacolor[$i][0].', '.$this->datacolor[$i][1].', '.$this->datacolor[$i][2].', 0.9)';
- $bordercolor = $color;
- //$color = (!empty($data['seriescolor']) ? json_encode($data['seriescolor']) : json_encode($datacolor));
- $textoflegend = $this->Legend[$i];
- }
- if ($i > 0) $this->stringtoshow .= ', ';
- $this->stringtoshow .= "\n";
- $this->stringtoshow .= '{';
- $this->stringtoshow .= 'dolibarrinfo: \'y_'.$i.'\', ';
- $this->stringtoshow .= 'label: \''.dol_escape_js(dol_string_nohtmltag($textoflegend)).'\', ';
- $this->stringtoshow .= 'pointStyle: \''.($this->type[$i] == 'linesnopoint' ? 'line' : 'circle').'\', ';
- $this->stringtoshow .= 'fill: '.($type == 'bar' ? 'true' : 'false').', ';
- $this->stringtoshow .= 'borderWidth: \'1\', ';
- $this->stringtoshow .= 'borderColor: \''.$bordercolor.'\', ';
- $this->stringtoshow .= 'backgroundColor: \''.$color.'\', ';
- if ($arrayofgroupslegend[$i]) $this->stringtoshow .= 'stack: \''.$arrayofgroupslegend[$i]['stacknum'].'\', ';
- $this->stringtoshow .= 'data: ['.$serie[$i].']';
- $this->stringtoshow .= '}'."\n";
- $i++;
- $iinstack++;
- }
- $this->stringtoshow .= ']'."\n";
- $this->stringtoshow .= '}'."\n";
- $this->stringtoshow .= '});'."\n";
- }
- $this->stringtoshow .= '</script>'."\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 = '<div class="nographyet" style="width:'.(preg_match('/%/', $this->width) ? $this->width : $this->width.'px').'; height:'.(preg_match('/%/', $this->height) ? $this->height : $this->height.'px').';"></div>';
- $s .= '<div class="nographyettext">';
- if (is_numeric($shownographyet)) {
- $s .= $langs->trans("NotEnoughDataYet");
- } else {
- $s .= $shownographyet;
- }
- $s .= '</div>';
- 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;
- }
- }
|