dolgraph.class.php 39 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298
  1. <?php
  2. /* Copyright (c) 2003-2006 Rodolphe Quiedeville <rodolphe@quiedeville.org>
  3. * Copyright (c) 2004-2015 Laurent Destailleur <eldy@users.sourceforge.net>
  4. *
  5. * This program is free software; you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation; either version 3 of the License, or
  8. * (at your option) any later version.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with this program. If not, see <https://www.gnu.org/licenses/>.
  17. */
  18. /**
  19. * \file htdocs/core/class/dolgraph.class.php
  20. * \ingroup core
  21. * \brief File for class to generate graph
  22. */
  23. /**
  24. * Class to build graphs.
  25. * Usage is:
  26. * $dolgraph=new DolGraph();
  27. * $dolgraph->SetTitle($langs->transnoentities('MyTitle').'<br>'.$langs->transnoentities('MyTitlePercent').'%');
  28. * $dolgraph->SetMaxValue(50);
  29. * $dolgraph->SetData($data);
  30. * $dolgraph->setShowLegend(2);
  31. * $dolgraph->setShowPercent(1);
  32. * $dolgraph->SetType(array('pie'));
  33. * $dolgraph->setHeight('200');
  34. * $dolgraph->draw('idofgraph');
  35. * print $dolgraph->show($total?0:1);
  36. */
  37. class DolGraph
  38. {
  39. public $type = array(); // Array with type of each series. Example: array('bars', 'lines', ...)
  40. public $mode = 'side'; // Mode bars graph: side, depth
  41. private $_library = 'jflot'; // Graphic library to use (jflot, artichow)
  42. //! Array of data
  43. public $data; // Data of graph: array(array('abs1',valA1,valB1), array('abs2',valA2,valB2), ...)
  44. public $title; // Title of graph
  45. public $cssprefix = ''; // To add into css styles
  46. /**
  47. * @var int|string Width of graph. It can be a numeric for pixels or a string like '100%'
  48. */
  49. public $width = 380;
  50. /**
  51. * @var int Height of graph
  52. */
  53. public $height = 200;
  54. public $MaxValue = 0;
  55. public $MinValue = 0;
  56. public $SetShading = 0;
  57. public $horizTickIncrement = -1;
  58. public $SetNumXTicks = -1;
  59. public $labelInterval = -1;
  60. public $hideXGrid = false;
  61. public $hideYGrid = false;
  62. public $Legend = array();
  63. public $LegendWidthMin = 0;
  64. public $showlegend = 1;
  65. public $showpointvalue = 1;
  66. public $showpercent = 0;
  67. public $combine = 0; // 0.05 if you want to combine records < 5% into "other"
  68. public $graph; // Objet Graph (Artichow, Phplot...)
  69. /**
  70. * @var string Error code (or message)
  71. */
  72. public $error = '';
  73. public $bordercolor; // array(R,G,B)
  74. public $bgcolor; // array(R,G,B)
  75. public $bgcolorgrid = array(255, 255, 255); // array(R,G,B)
  76. public $datacolor; // array(array(R,G,B),...)
  77. private $stringtoshow; // To store string to output graph into HTML page
  78. /**
  79. * Constructor
  80. *
  81. * @param string $library 'auto' (default)
  82. */
  83. public function __construct($library = 'auto')
  84. {
  85. global $conf;
  86. global $theme_bordercolor, $theme_datacolor, $theme_bgcolor;
  87. $this->bordercolor = array(235, 235, 224);
  88. $this->datacolor = array(array(120, 130, 150), array(160, 160, 180), array(190, 190, 220));
  89. $this->bgcolor = array(235, 235, 224);
  90. $color_file = DOL_DOCUMENT_ROOT.'/theme/'.$conf->theme.'/theme_vars.inc.php';
  91. if (is_readable($color_file))
  92. {
  93. include_once $color_file;
  94. if (isset($theme_bordercolor)) $this->bordercolor = $theme_bordercolor;
  95. if (isset($theme_datacolor)) $this->datacolor = $theme_datacolor;
  96. if (isset($theme_bgcolor)) $this->bgcolor = $theme_bgcolor;
  97. }
  98. //print 'bgcolor: '.join(',',$this->bgcolor).'<br>';
  99. $this->_library = $library;
  100. if ($this->_library == 'auto') {
  101. $this->_library = (empty($conf->global->MAIN_JS_GRAPH) ? 'jflot': $conf->global->MAIN_JS_GRAPH);
  102. }
  103. }
  104. // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
  105. /**
  106. * Utiliser SetNumTicks ou SetHorizTickIncrement mais pas les 2
  107. *
  108. * @param float $xi Xi
  109. * @return boolean True
  110. */
  111. public function SetHorizTickIncrement($xi)
  112. {
  113. // phpcs:enable
  114. $this->horizTickIncrement = $xi;
  115. return true;
  116. }
  117. // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
  118. /**
  119. * Utiliser SetNumTicks ou SetHorizTickIncrement mais pas les 2
  120. *
  121. * @param float $xt Xt
  122. * @return boolean True
  123. */
  124. public function SetNumXTicks($xt)
  125. {
  126. // phpcs:enable
  127. $this->SetNumXTicks = $xt;
  128. return true;
  129. }
  130. // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
  131. /**
  132. * Set label interval to reduce number of labels
  133. *
  134. * @param float $x Label interval
  135. * @return boolean True
  136. */
  137. public function SetLabelInterval($x)
  138. {
  139. // phpcs:enable
  140. $this->labelInterval = $x;
  141. return true;
  142. }
  143. // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
  144. /**
  145. * Hide X grid
  146. *
  147. * @param boolean $bool XGrid or not
  148. * @return boolean true
  149. */
  150. public function SetHideXGrid($bool)
  151. {
  152. // phpcs:enable
  153. $this->hideXGrid = $bool;
  154. return true;
  155. }
  156. // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
  157. /**
  158. * Hide Y grid
  159. *
  160. * @param boolean $bool YGrid or not
  161. * @return boolean true
  162. */
  163. public function SetHideYGrid($bool)
  164. {
  165. // phpcs:enable
  166. $this->hideYGrid = $bool;
  167. return true;
  168. }
  169. // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
  170. /**
  171. * Set y label
  172. *
  173. * @param string $label Y label
  174. * @return boolean|null True
  175. */
  176. public function SetYLabel($label)
  177. {
  178. // phpcs:enable
  179. $this->YLabel = $label;
  180. }
  181. // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
  182. /**
  183. * Set width
  184. *
  185. * @param int|string $w Width (Example: 320 or '100%')
  186. * @return boolean|null True
  187. */
  188. public function SetWidth($w)
  189. {
  190. // phpcs:enable
  191. $this->width = $w;
  192. }
  193. // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
  194. /**
  195. * Set title
  196. *
  197. * @param string $title Title
  198. * @return void
  199. */
  200. public function SetTitle($title)
  201. {
  202. // phpcs:enable
  203. $this->title = $title;
  204. }
  205. // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
  206. /**
  207. * Set data
  208. *
  209. * @param array $data Data
  210. * @return void
  211. * @see draw_jflot() for syntax of data array
  212. */
  213. public function SetData($data)
  214. {
  215. // phpcs:enable
  216. $this->data = $data;
  217. }
  218. // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
  219. /**
  220. * Set data
  221. *
  222. * @param array $datacolor Data color array(array(R,G,B),array(R,G,B)...)
  223. * @return void
  224. */
  225. public function SetDataColor($datacolor)
  226. {
  227. // phpcs:enable
  228. $this->datacolor = $datacolor;
  229. }
  230. // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
  231. /**
  232. * Set type
  233. *
  234. * @param array $type Array with type for each serie. Example: array('type1', 'type2', ...) where type can be:
  235. * 'pie', 'piesemicircle', 'polar', 'lines', 'linesnopoint', 'bars', ...
  236. * @return void
  237. */
  238. public function SetType($type)
  239. {
  240. // phpcs:enable
  241. $this->type = $type;
  242. }
  243. // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
  244. /**
  245. * Set legend
  246. *
  247. * @param array $legend Legend. Example: array('seriename1','seriname2',...)
  248. * @return void
  249. */
  250. public function SetLegend($legend)
  251. {
  252. // phpcs:enable
  253. $this->Legend = $legend;
  254. }
  255. // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
  256. /**
  257. * Set min width
  258. *
  259. * @param int $legendwidthmin Min width
  260. * @return void
  261. */
  262. public function SetLegendWidthMin($legendwidthmin)
  263. {
  264. // phpcs:enable
  265. $this->LegendWidthMin = $legendwidthmin;
  266. }
  267. // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
  268. /**
  269. * Set max value
  270. *
  271. * @param int $max Max value
  272. * @return void
  273. */
  274. public function SetMaxValue($max)
  275. {
  276. // phpcs:enable
  277. $this->MaxValue = $max;
  278. }
  279. // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
  280. /**
  281. * Get max value
  282. *
  283. * @return int Max value
  284. */
  285. public function GetMaxValue()
  286. {
  287. // phpcs:enable
  288. return $this->MaxValue;
  289. }
  290. // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
  291. /**
  292. * Set min value
  293. *
  294. * @param int $min Min value
  295. * @return void
  296. */
  297. public function SetMinValue($min)
  298. {
  299. // phpcs:enable
  300. $this->MinValue = $min;
  301. }
  302. // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
  303. /**
  304. * Get min value
  305. *
  306. * @return int Max value
  307. */
  308. public function GetMinValue()
  309. {
  310. // phpcs:enable
  311. return $this->MinValue;
  312. }
  313. // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
  314. /**
  315. * Set height
  316. *
  317. * @param int $h Height
  318. * @return void
  319. */
  320. public function SetHeight($h)
  321. {
  322. // phpcs:enable
  323. $this->height = $h;
  324. }
  325. // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
  326. /**
  327. * Set shading
  328. *
  329. * @param string $s Shading
  330. * @return void
  331. */
  332. public function SetShading($s)
  333. {
  334. // phpcs:enable
  335. $this->SetShading = $s;
  336. }
  337. // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
  338. /**
  339. * Set shading
  340. *
  341. * @param string $s Shading
  342. * @return void
  343. */
  344. public function SetCssPrefix($s)
  345. {
  346. // phpcs:enable
  347. $this->cssprefix = $s;
  348. }
  349. // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
  350. /**
  351. * Reset bg color
  352. *
  353. * @return void
  354. */
  355. public function ResetBgColor()
  356. {
  357. // phpcs:enable
  358. unset($this->bgcolor);
  359. }
  360. // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
  361. /**
  362. * Reset bgcolorgrid
  363. *
  364. * @return void
  365. */
  366. public function ResetBgColorGrid()
  367. {
  368. // phpcs:enable
  369. unset($this->bgcolorgrid);
  370. }
  371. /**
  372. * Is graph ko
  373. *
  374. * @return string Error
  375. */
  376. public function isGraphKo()
  377. {
  378. return $this->error;
  379. }
  380. /**
  381. * Show legend or not
  382. *
  383. * @param int $showlegend 1=Show legend (default), 0=Hide legend, 2=Show legend on right
  384. * @return void
  385. */
  386. public function setShowLegend($showlegend)
  387. {
  388. $this->showlegend = $showlegend;
  389. }
  390. /**
  391. * Show pointvalue or not
  392. *
  393. * @param int $showpointvalue 1=Show value for each point, as tooltip or inline (default), 0=Hide value
  394. * @return void
  395. */
  396. public function setShowPointValue($showpointvalue)
  397. {
  398. $this->showpointvalue = $showpointvalue;
  399. }
  400. /**
  401. * Show percent or not
  402. *
  403. * @param int $showpercent 1=Show percent for each point, as tooltip or inline, 0=Hide percent (default)
  404. * @return void
  405. */
  406. public function setShowPercent($showpercent)
  407. {
  408. $this->showpercent = $showpercent;
  409. }
  410. // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
  411. /**
  412. * Define background color of complete image
  413. *
  414. * @param array $bg_color array(R,G,B) ou 'onglet' ou 'default'
  415. * @return void
  416. */
  417. public function SetBgColor($bg_color = array(255, 255, 255))
  418. {
  419. // phpcs:enable
  420. global $theme_bgcolor, $theme_bgcoloronglet;
  421. if (!is_array($bg_color))
  422. {
  423. if ($bg_color == 'onglet')
  424. {
  425. //print 'ee'.join(',',$theme_bgcoloronglet);
  426. $this->bgcolor = $theme_bgcoloronglet;
  427. }
  428. else
  429. {
  430. $this->bgcolor = $theme_bgcolor;
  431. }
  432. }
  433. else
  434. {
  435. $this->bgcolor = $bg_color;
  436. }
  437. }
  438. // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
  439. /**
  440. * Define background color of grid
  441. *
  442. * @param array $bg_colorgrid array(R,G,B) ou 'onglet' ou 'default'
  443. * @return void
  444. */
  445. public function SetBgColorGrid($bg_colorgrid = array(255, 255, 255))
  446. {
  447. // phpcs:enable
  448. global $theme_bgcolor, $theme_bgcoloronglet;
  449. if (!is_array($bg_colorgrid))
  450. {
  451. if ($bg_colorgrid == 'onglet')
  452. {
  453. //print 'ee'.join(',',$theme_bgcoloronglet);
  454. $this->bgcolorgrid = $theme_bgcoloronglet;
  455. }
  456. else
  457. {
  458. $this->bgcolorgrid = $theme_bgcolor;
  459. }
  460. }
  461. else
  462. {
  463. $this->bgcolorgrid = $bg_colorgrid;
  464. }
  465. }
  466. // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
  467. /**
  468. * Reset data color
  469. *
  470. * @return void
  471. */
  472. public function ResetDataColor()
  473. {
  474. // phpcs:enable
  475. unset($this->datacolor);
  476. }
  477. // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
  478. /**
  479. * Get max value
  480. *
  481. * @return int Max value
  482. */
  483. public function GetMaxValueInData()
  484. {
  485. // phpcs:enable
  486. $k = 0;
  487. $vals = array();
  488. $nblines = count($this->data);
  489. $nbvalues = count($this->data[0]) - 1;
  490. for ($j = 0; $j < $nblines; $j++)
  491. {
  492. for ($i = 0; $i < $nbvalues; $i++)
  493. {
  494. $vals[$k] = $this->data[$j][$i + 1];
  495. $k++;
  496. }
  497. }
  498. rsort($vals);
  499. return $vals[0];
  500. }
  501. // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
  502. /**
  503. * Return min value of all data
  504. *
  505. * @return int Min value of all data
  506. */
  507. public function GetMinValueInData()
  508. {
  509. // phpcs:enable
  510. $k = 0;
  511. $vals = array();
  512. $nblines = count($this->data);
  513. $nbvalues = count($this->data[0]) - 1;
  514. for ($j = 0; $j < $nblines; $j++)
  515. {
  516. for ($i = 0; $i < $nbvalues; $i++)
  517. {
  518. $vals[$k] = $this->data[$j][$i + 1];
  519. $k++;
  520. }
  521. }
  522. sort($vals);
  523. return $vals[0];
  524. }
  525. // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
  526. /**
  527. * Return max value of all data
  528. *
  529. * @return int Max value of all data
  530. */
  531. public function GetCeilMaxValue()
  532. {
  533. // phpcs:enable
  534. $max = $this->GetMaxValueInData();
  535. if ($max != 0) $max++;
  536. $size = dol_strlen(abs(ceil($max)));
  537. $factor = 1;
  538. for ($i = 0; $i < ($size - 1); $i++)
  539. {
  540. $factor *= 10;
  541. }
  542. $res = 0;
  543. if (is_numeric($max)) $res = ceil($max / $factor) * $factor;
  544. //print "max=".$max." res=".$res;
  545. return $res;
  546. }
  547. // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
  548. /**
  549. * Return min value of all data
  550. *
  551. * @return double Max value of all data
  552. */
  553. public function GetFloorMinValue()
  554. {
  555. // phpcs:enable
  556. $min = $this->GetMinValueInData();
  557. if ($min == '') $min = 0;
  558. if ($min != 0) $min--;
  559. $size = dol_strlen(abs(floor($min)));
  560. $factor = 1;
  561. for ($i = 0; $i < ($size - 1); $i++)
  562. {
  563. $factor *= 10;
  564. }
  565. $res = floor($min / $factor) * $factor;
  566. //print "min=".$min." res=".$res;
  567. return $res;
  568. }
  569. /**
  570. * Build a graph into memory using correct library (may also be wrote on disk, depending on library used)
  571. *
  572. * @param string $file Image file name to use to save onto disk (also used as javascript unique id)
  573. * @param string $fileurl Url path to show image if saved onto disk
  574. * @return integer|null
  575. */
  576. public function draw($file, $fileurl = '')
  577. {
  578. if (empty($file))
  579. {
  580. $this->error = "Call to draw method was made with empty value for parameter file.";
  581. dol_syslog(get_class($this)."::draw ".$this->error, LOG_ERR);
  582. return -2;
  583. }
  584. if (!is_array($this->data))
  585. {
  586. $this->error = "Call to draw method was made but SetData was not called or called with an empty dataset for parameters";
  587. dol_syslog(get_class($this)."::draw ".$this->error, LOG_ERR);
  588. return -1;
  589. }
  590. if (count($this->data) < 1)
  591. {
  592. $this->error = "Call to draw method was made but SetData was is an empty dataset";
  593. dol_syslog(get_class($this)."::draw ".$this->error, LOG_WARNING);
  594. }
  595. $call = "draw_".$this->_library;
  596. call_user_func_array(array($this, $call), array($file, $fileurl));
  597. }
  598. // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
  599. /**
  600. * Build a graph using JFlot library. Input when calling this method should be:
  601. * $this->data = array(array(0=>'labelxA',1=>yA), array('labelxB',yB));
  602. * $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
  603. * $this->data = array(array('label'=>'labelxA','data'=>yA), array('labelxB',yB)); // Syntax deprecated
  604. * $this->legend= array("Val1",...,"Valn"); // list of n series name
  605. * $this->type = array('bars',...'lines','linesnopoint'); or array('pie') or array('polar')
  606. * $this->mode = 'depth' ???
  607. * $this->bgcolorgrid
  608. * $this->datacolor
  609. * $this->shownodatagraph
  610. *
  611. * @param string $file Image file name to use to save onto disk (also used as javascript unique id)
  612. * @param string $fileurl Url path to show image if saved onto disk. Never used here.
  613. * @return void
  614. */
  615. private function draw_jflot($file, $fileurl)
  616. {
  617. // phpcs:enable
  618. global $conf, $langs;
  619. dol_syslog(get_class($this)."::draw_jflot this->type=".join(',', $this->type)." this->MaxValue=".$this->MaxValue);
  620. if (empty($this->width) && empty($this->height))
  621. {
  622. print 'Error width or height not set';
  623. return;
  624. }
  625. $legends = array();
  626. $nblot = 0;
  627. if (is_array($this->data) && is_array($this->data[0])) {
  628. $nblot = count($this->data[0]) - 1; // -1 to remove legend
  629. }
  630. if ($nblot < 0) dol_syslog('Bad value for property ->data. Must be set by mydolgraph->SetData before calling mydolgrapgh->draw', LOG_WARNING);
  631. $firstlot = 0;
  632. // Works with line but not with bars
  633. //if ($nblot > 2) $firstlot = ($nblot - 2); // We limit nblot to 2 because jflot can't manage more than 2 bars on same x
  634. $i = $firstlot;
  635. $serie = array();
  636. while ($i < $nblot) // Loop on each serie
  637. {
  638. $values = array(); // Array with horizontal y values (specific values of a serie) for each abscisse x
  639. $serie[$i] = "var d".$i." = [];\n";
  640. // Fill array $values
  641. $x = 0;
  642. foreach ($this->data as $valarray) // Loop on each x
  643. {
  644. $legends[$x] = $valarray[0];
  645. $values[$x] = (is_numeric($valarray[$i + 1]) ? $valarray[$i + 1] : null);
  646. $x++;
  647. }
  648. if (isset($this->type[$firstlot]) && in_array($this->type[$firstlot], array('pie', 'piesemicircle', 'polar')))
  649. {
  650. foreach ($values as $x => $y) {
  651. if (isset($y)) $serie[$i] .= 'd'.$i.'.push({"label":"'.dol_escape_js($legends[$x]).'", "data":'.$y.'});'."\n";
  652. }
  653. }
  654. else
  655. {
  656. foreach ($values as $x => $y) {
  657. if (isset($y)) $serie[$i] .= 'd'.$i.'.push(['.$x.', '.$y.']);'."\n";
  658. }
  659. }
  660. unset($values);
  661. $i++;
  662. }
  663. $tag = dol_escape_htmltag(dol_string_unaccent(dol_string_nospecial(basename($file), '_', array('-', '.'))));
  664. $this->stringtoshow = '<!-- Build using jflot -->'."\n";
  665. if (!empty($this->title)) $this->stringtoshow .= '<div class="center dolgraphtitle'.(empty($this->cssprefix) ? '' : ' dolgraphtitle'.$this->cssprefix).'">'.$this->title.'</div>';
  666. if (!empty($this->shownographyet))
  667. {
  668. $this->stringtoshow .= '<div style="width:'.$this->width.'px;height:'.$this->height.'px;" class="nographyet"></div>';
  669. $this->stringtoshow .= '<div class="nographyettext">'.$langs->trans("NotEnoughDataYet").'</div>';
  670. return;
  671. }
  672. // Start the div that will contains all the graph
  673. $dolxaxisvertical='';
  674. if (count($this->data) > 20) $dolxaxisvertical='dol-xaxis-vertical';
  675. $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";
  676. $this->stringtoshow .= '<script id="'.$tag.'">'."\n";
  677. $this->stringtoshow .= '$(function () {'."\n";
  678. $i = $firstlot;
  679. if ($nblot < 0)
  680. {
  681. $this->stringtoshow .= '<!-- No series of data -->'."\n";
  682. }
  683. else
  684. {
  685. while ($i < $nblot)
  686. {
  687. $this->stringtoshow .= '<!-- Serie '.$i.' -->'."\n";
  688. $this->stringtoshow .= $serie[$i]."\n";
  689. $i++;
  690. }
  691. }
  692. $this->stringtoshow .= "\n";
  693. // Special case for Graph of type 'pie'
  694. if (isset($this->type[$firstlot]) && in_array($this->type[$firstlot], array('pie', 'piesemicircle', 'polar')))
  695. {
  696. $datacolor = array();
  697. foreach ($this->datacolor as $val) {
  698. if (is_array($val)) $datacolor[] = "#".sprintf("%02x%02x%02x", $val[0], $val[1], $val[2]); // If datacolor is array(R, G, B)
  699. else $datacolor[] = "#".str_replace(array('#', '-'), '', $val); // If $val is '124' or '#124'
  700. }
  701. $urltemp = ''; // TODO Add support for url link into labels
  702. $showlegend = $this->showlegend;
  703. $showpointvalue = $this->showpointvalue;
  704. $showpercent = $this->showpercent;
  705. $this->stringtoshow .= '
  706. function plotWithOptions_'.$tag.'() {
  707. $.plot($("#placeholder_'.$tag.'"), d0,
  708. {
  709. series: {
  710. pie: {
  711. show: true,
  712. radius: 0.8,
  713. '.($this->combine ? '
  714. combine: {
  715. threshold: '.$this->combine.'
  716. },' : '').'
  717. label: {
  718. show: true,
  719. radius: 0.9,
  720. formatter: function(label, series) {
  721. var percent=Math.round(series.percent);
  722. var number=series.data[0][1];
  723. return \'';
  724. $this->stringtoshow .= '<span style="font-size:8pt;text-align:center;padding:2px;color:black;">';
  725. if ($urltemp) $this->stringtoshow .= '<a style="color: #FFFFFF;" border="0" href="'.$urltemp.'">';
  726. $this->stringtoshow .= '\'+';
  727. $this->stringtoshow .= ($showlegend ? '' : 'label+\' \'+'); // Hide label if already shown in legend
  728. $this->stringtoshow .= ($showpointvalue ? 'number+' : '');
  729. $this->stringtoshow .= ($showpercent ? '\'<br/>\'+percent+\'%\'+' : '');
  730. $this->stringtoshow .= '\'';
  731. if ($urltemp) $this->stringtoshow .= '</a>';
  732. $this->stringtoshow .= '</span>\';
  733. },
  734. background: {
  735. opacity: 0.0,
  736. color: \'#000000\'
  737. }
  738. }
  739. }
  740. },
  741. zoom: {
  742. interactive: true
  743. },
  744. pan: {
  745. interactive: true
  746. },';
  747. if (count($datacolor))
  748. {
  749. $this->stringtoshow .= 'colors: '.(!empty($data['seriescolor']) ? json_encode($data['seriescolor']) : json_encode($datacolor)).',';
  750. }
  751. $this->stringtoshow .= 'legend: {show: '.($showlegend ? 'true' : 'false').', position: \'ne\' }
  752. });
  753. }'."\n";
  754. }
  755. // Other cases, graph of type 'bars', 'lines'
  756. else
  757. {
  758. // Add code to support tooltips
  759. // TODO: remove js css and use graph-tooltip-inner class instead by adding css in each themes
  760. $this->stringtoshow .= '
  761. function showTooltip_'.$tag.'(x, y, contents) {
  762. $(\'<div class="graph-tooltip-inner" id="tooltip_'.$tag.'">\' + contents + \'</div>\').css({
  763. position: \'absolute\',
  764. display: \'none\',
  765. top: y + 10,
  766. left: x + 15,
  767. border: \'1px solid #000\',
  768. padding: \'5px\',
  769. \'background-color\': \'#000\',
  770. \'color\': \'#fff\',
  771. \'font-weight\': \'bold\',
  772. width: 200,
  773. opacity: 0.80
  774. }).appendTo("body").fadeIn(100);
  775. }
  776. var previousPoint = null;
  777. $("#placeholder_'.$tag.'").bind("plothover", function (event, pos, item) {
  778. $("#x").text(pos.x.toFixed(2));
  779. $("#y").text(pos.y.toFixed(2));
  780. if (item) {
  781. if (previousPoint != item.dataIndex) {
  782. previousPoint = item.dataIndex;
  783. $("#tooltip").remove();
  784. /* console.log(item); */
  785. var x = item.datapoint[0].toFixed(2);
  786. var y = item.datapoint[1].toFixed(2);
  787. var z = item.series.xaxis.ticks[item.dataIndex].label;
  788. ';
  789. if ($this->showpointvalue > 0) $this->stringtoshow .= '
  790. showTooltip_'.$tag.'(item.pageX, item.pageY, item.series.label + "<br>" + z + " => " + y);
  791. ';
  792. $this->stringtoshow .= '
  793. }
  794. }
  795. else {
  796. $("#tooltip_'.$tag.'").remove();
  797. previousPoint = null;
  798. }
  799. });
  800. ';
  801. $this->stringtoshow .= 'var stack = null, steps = false;'."\n";
  802. $this->stringtoshow .= 'function plotWithOptions_'.$tag.'() {'."\n";
  803. $this->stringtoshow .= '$.plot($("#placeholder_'.$tag.'"), [ '."\n";
  804. $i = $firstlot;
  805. while ($i < $nblot)
  806. {
  807. if ($i > $firstlot) $this->stringtoshow .= ', '."\n";
  808. $color = sprintf("%02x%02x%02x", $this->datacolor[$i][0], $this->datacolor[$i][1], $this->datacolor[$i][2]);
  809. $this->stringtoshow .= '{ ';
  810. if (!isset($this->type[$i]) || $this->type[$i] == 'bars') $this->stringtoshow .= 'bars: { lineWidth: 1, show: true, align: "'.($i == $firstlot ? 'center' : 'left').'", barWidth: 0.5 }, ';
  811. 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').' }, ';
  812. $this->stringtoshow .= 'color: "#'.$color.'", label: "'.(isset($this->Legend[$i]) ? dol_escape_js($this->Legend[$i]) : '').'", data: d'.$i.' }';
  813. $i++;
  814. }
  815. // shadowSize: 0 -> Drawing is faster without shadows
  816. $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";
  817. // Xaxis
  818. $this->stringtoshow .= ', xaxis: { ticks: ['."\n";
  819. $x = 0;
  820. foreach ($this->data as $key => $valarray)
  821. {
  822. if ($x > 0) $this->stringtoshow .= ', '."\n";
  823. $this->stringtoshow .= ' ['.$x.', "'.$valarray[0].'"]';
  824. $x++;
  825. }
  826. $this->stringtoshow .= '] }'."\n";
  827. // Yaxis
  828. $this->stringtoshow .= ', yaxis: { min: '.$this->MinValue.', max: '.($this->MaxValue).' }'."\n";
  829. // Background color
  830. $color1 = sprintf("%02x%02x%02x", $this->bgcolorgrid[0], $this->bgcolorgrid[0], $this->bgcolorgrid[2]);
  831. $color2 = sprintf("%02x%02x%02x", $this->bgcolorgrid[0], $this->bgcolorgrid[1], $this->bgcolorgrid[2]);
  832. $this->stringtoshow .= ', grid: { hoverable: true, backgroundColor: { colors: ["#'.$color1.'", "#'.$color2.'"] }, borderWidth: 1, borderColor: \'#e6e6e6\', tickColor : \'#e6e6e6\' }'."\n";
  833. //$this->stringtoshow.=', shadowSize: 20'."\n"; TODO Uncommet this
  834. $this->stringtoshow .= '});'."\n";
  835. $this->stringtoshow .= '}'."\n";
  836. }
  837. $this->stringtoshow .= 'plotWithOptions_'.$tag.'();'."\n";
  838. $this->stringtoshow .= '});'."\n";
  839. $this->stringtoshow .= '</script>'."\n";
  840. }
  841. // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
  842. /**
  843. * Build a graph using Chart library. Input when calling this method should be:
  844. * $this->data = array(array(0=>'labelxA',1=>yA), array('labelxB',yB));
  845. * $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
  846. * $this->data = array(array('label'=>'labelxA','data'=>yA), array('labelxB',yB)); // Syntax deprecated
  847. * $this->legend= array("Val1",...,"Valn"); // list of n series name
  848. * $this->type = array('bars',...'lines', 'linesnopoint'); or array('pie') or array('polar') or array('piesemicircle');
  849. * $this->mode = 'depth' ???
  850. * $this->bgcolorgrid
  851. * $this->datacolor
  852. * $this->shownodatagraph
  853. *
  854. * @param string $file Image file name to use to save onto disk (also used as javascript unique id)
  855. * @param string $fileurl Url path to show image if saved onto disk. Never used here.
  856. * @return void
  857. */
  858. private function draw_chart($file, $fileurl)
  859. {
  860. // phpcs:enable
  861. global $conf, $langs;
  862. dol_syslog(get_class($this)."::draw_chart this->type=".join(',', $this->type)." this->MaxValue=".$this->MaxValue);
  863. if (empty($this->width) && empty($this->height))
  864. {
  865. print 'Error width or height not set';
  866. return;
  867. }
  868. $showlegend = $this->showlegend;
  869. $legends = array();
  870. $nblot = 0;
  871. if (is_array($this->data) && is_array($this->data[0])) {
  872. $nblot = count($this->data[0]) - 1; // -1 to remove legend
  873. }
  874. if ($nblot < 0) dol_syslog('Bad value for property ->data. Must be set by mydolgraph->SetData before calling mydolgrapgh->draw', LOG_WARNING);
  875. $firstlot = 0;
  876. // Works with line but not with bars
  877. //if ($nblot > 2) $firstlot = ($nblot - 2); // We limit nblot to 2 because jflot can't manage more than 2 bars on same x
  878. $i = $firstlot;
  879. $serie = array(); $arrayofgroupslegend = array();
  880. while ($i < $nblot) // Loop on each serie
  881. {
  882. $values = array(); // Array with horizontal y values (specific values of a serie) for each abscisse x (with x=0,1,2,...)
  883. $serie[$i] = "";
  884. //var_dump($this->data);exit;
  885. // Fill array $values
  886. $x = 0;
  887. foreach ($this->data as $valarray) // Loop on each x
  888. {
  889. $legends[$x] = (array_key_exists('label', $valarray) ? $valarray['label'] : $valarray[0]);
  890. $array_of_ykeys = array_keys($valarray);
  891. $alabelexists = 1;
  892. $tmpykey = explode('_', ($array_of_ykeys[$i+($alabelexists ? 1 : 0)]), 3);
  893. if (! empty($tmpykey[2])) { // This is a group by array
  894. $tmpvalue = (array_key_exists('y_'.$tmpykey[1].'_'.$tmpykey[2], $valarray) ? $valarray['y_'.$tmpykey[1].'_'.$tmpykey[2]] : $valarray[$i + 1]);
  895. $values[$x] = (is_numeric($tmpvalue) ? $tmpvalue : null);
  896. $arrayofgroupslegend[$i] = array(
  897. 'stacknum'=> $tmpykey[1],
  898. 'legend' => $this->Legend[$tmpykey[1]],
  899. 'legendwithgroup' => $this->Legend[$tmpykey[1]].' - '.$tmpykey[2]
  900. );
  901. } else {
  902. $tmpvalue = (array_key_exists('y_'.$i, $valarray) ? $valarray['y_'.$i] : $valarray[$i + 1]);
  903. $values[$x] = (is_numeric($tmpvalue) ? $tmpvalue : null);
  904. }
  905. $x++;
  906. }
  907. $j = 0;
  908. foreach ($values as $x => $y) {
  909. if (isset($y)) {
  910. $serie[$i] .= ($j > 0 ? ", " : "").$y;
  911. $j++;
  912. }
  913. }
  914. $values = null; // Free mem
  915. $i++;
  916. }
  917. $tag = dol_escape_htmltag(dol_string_unaccent(dol_string_nospecial(basename($file), '_', array('-', '.'))));
  918. $this->stringtoshow = '<!-- Build using chart -->'."\n";
  919. if (!empty($this->title)) $this->stringtoshow .= '<div class="center dolgraphtitle'.(empty($this->cssprefix) ? '' : ' dolgraphtitle'.$this->cssprefix).'">'.$this->title.'</div>';
  920. if (!empty($this->shownographyet))
  921. {
  922. $this->stringtoshow .= '<div style="width:'.$this->width.(strpos($this->width, '%') > 0 ? '': 'px').'; height:'.$this->height.'px;" class="nographyet"></div>';
  923. $this->stringtoshow .= '<div class="nographyettext">'.$langs->trans("NotEnoughDataYet").'</div>';
  924. return;
  925. }
  926. // Start the div that will contains all the graph
  927. $dolxaxisvertical='';
  928. if (count($this->data) > 20) $dolxaxisvertical='dol-xaxis-vertical';
  929. // No height for the pie grah
  930. $cssfordiv = 'dolgraphchart';
  931. if (isset($this->type[$firstlot])) $cssfordiv .= ' dolgraphchar'.$this->type[$firstlot];
  932. $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";
  933. $this->stringtoshow .= '<script id="'.$tag.'">'."\n";
  934. $i = $firstlot;
  935. if ($nblot < 0)
  936. {
  937. $this->stringtoshow .= '<!-- No series of data -->';
  938. }
  939. else
  940. {
  941. while ($i < $nblot)
  942. {
  943. //$this->stringtoshow .= '<!-- Series '.$i.' -->'."\n";
  944. //$this->stringtoshow .= $serie[$i]."\n";
  945. $i++;
  946. }
  947. }
  948. $this->stringtoshow .= "\n";
  949. // Special case for Graph of type 'pie', 'piesemicircle', or 'polar'
  950. if (isset($this->type[$firstlot]) && (in_array($this->type[$firstlot], array('pie', 'polar', 'piesemicircle'))))
  951. {
  952. $type = $this->type[$firstlot]; // pie or polar
  953. $this->stringtoshow .= 'var options = {'."\n";
  954. if ($this->type[$firstlot] == 'piesemicircle') {
  955. $this->stringtoshow .= 'circumference: Math.PI,'."\n";
  956. $this->stringtoshow .= 'rotation: -Math.PI,'."\n";
  957. }
  958. if ($showlegend) $this->stringtoshow .= 'legend: { position: \''.($showlegend == 2 ? 'right' : 'top').'\' },'."\n";
  959. $this->stringtoshow .= 'elements: { arc: {'."\n";
  960. // Color of earch arc
  961. $this->stringtoshow .= 'backgroundColor: [';
  962. $i = 0; $foundnegativecolor = 0;
  963. foreach($legends as $val) // Loop on each serie
  964. {
  965. if ($i > 0) $this->stringtoshow .= ', '."\n";
  966. 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)
  967. else {
  968. $tmp = str_replace('#', '', $this->datacolor[$i]);
  969. if (strpos($tmp, '-') !== false) {
  970. $foundnegativecolor++;
  971. $color = '#FFFFFF'; // If $val is '-123'
  972. }
  973. else $color = "#".$tmp; // If $val is '123' or '#123'
  974. }
  975. $this->stringtoshow .= "'".$color."'";
  976. $i++;
  977. }
  978. $this->stringtoshow .= '], '."\n";
  979. // Border color
  980. if ($foundnegativecolor) {
  981. $this->stringtoshow .= 'borderColor: [';
  982. $i = 0;
  983. foreach($legends as $val) // Loop on each serie
  984. {
  985. if ($i > 0) $this->stringtoshow .= ', '."\n";
  986. if (is_array($this->datacolor[$i])) $color = 'null'; // If datacolor is array(R, G, B)
  987. else {
  988. $tmp = str_replace('#', '', $this->datacolor[$i]);
  989. if (strpos($tmp, '-') !== false) $color = '#'.str_replace('-', '', $tmp); // If $val is '-123'
  990. else $color = 'null'; // If $val is '123' or '#123'
  991. }
  992. $this->stringtoshow .= ($color == 'null' ? "'rgba(0,0,0,0.2)'" : "'".$color."'");
  993. $i++;
  994. }
  995. $this->stringtoshow .= ']';
  996. }
  997. $this->stringtoshow .= '} } };'."\n";
  998. $this->stringtoshow .= '
  999. var ctx = document.getElementById("canvas_'.$tag.'").getContext("2d");
  1000. var chart = new Chart(ctx, {
  1001. // The type of chart we want to create
  1002. type: \''.(in_array($type, array('pie', 'piesemicircle')) ? 'doughnut' : 'polarArea').'\',
  1003. // Configuration options go here
  1004. options: options,
  1005. data: {
  1006. labels: [';
  1007. $i = 0;
  1008. foreach($legends as $val) // Loop on each serie
  1009. {
  1010. if ($i > 0) $this->stringtoshow .= ', ';
  1011. $this->stringtoshow .= "'".$val."'";
  1012. $i++;
  1013. }
  1014. $this->stringtoshow .= '],
  1015. datasets: [';
  1016. $i = 0;
  1017. $i = 0;
  1018. while ($i < $nblot) // Loop on each serie
  1019. {
  1020. $color = 'rgb('.$this->datacolor[$i][0].', '.$this->datacolor[$i][1].', '.$this->datacolor[$i][2].')';
  1021. //$color = (!empty($data['seriescolor']) ? json_encode($data['seriescolor']) : json_encode($datacolor));
  1022. if ($i > 0) $this->stringtoshow .= ', '."\n";
  1023. $this->stringtoshow .= '{'."\n";
  1024. //$this->stringtoshow .= 'borderColor: \''.$color.'\', ';
  1025. //$this->stringtoshow .= 'backgroundColor: \''.$color.'\', ';
  1026. $this->stringtoshow .= ' data: ['.$serie[$i].']';
  1027. $this->stringtoshow .= '}'."\n";
  1028. $i++;
  1029. }
  1030. $this->stringtoshow .= ']'."\n";
  1031. $this->stringtoshow .= '}'."\n";
  1032. $this->stringtoshow .= '});'."\n";
  1033. }
  1034. // Other cases, graph of type 'bars', 'lines', 'linesnopoint'
  1035. else
  1036. {
  1037. $type = 'bar';
  1038. if (!isset($this->type[$firstlot]) || $this->type[$firstlot] == 'bars') $type = 'bar';
  1039. if (isset($this->type[$firstlot]) && ($this->type[$firstlot] == 'lines' || $this->type[$firstlot] == 'linesnopoint')) $type = 'line';
  1040. $this->stringtoshow .= 'var options = { maintainAspectRatio: false, aspectRatio: 2.5';
  1041. if (count($arrayofgroupslegend) > 0) {
  1042. $this->stringtoshow .= ', scales: {xAxes: [{ stacked: true, }], yAxes: [{ stacked: true }] }';
  1043. }
  1044. $this->stringtoshow .= '};';
  1045. $this->stringtoshow .= '
  1046. var ctx = document.getElementById("canvas_'.$tag.'").getContext("2d");
  1047. var chart = new Chart(ctx, {
  1048. // The type of chart we want to create
  1049. type: \''.$type.'\',
  1050. // Configuration options go here
  1051. options: options,
  1052. data: {
  1053. labels: [';
  1054. $i = 0;
  1055. foreach($legends as $val) // Loop on each serie
  1056. {
  1057. if ($i > 0) $this->stringtoshow .= ', ';
  1058. $this->stringtoshow .= "'".$val."'";
  1059. $i++;
  1060. }
  1061. //var_dump($arrayofgroupslegend);
  1062. $this->stringtoshow .= '],
  1063. datasets: [';
  1064. $i = 0; $iinstack = 0;
  1065. $oldstacknum = -1;
  1066. while ($i < $nblot) // Loop on each serie
  1067. {
  1068. if (count($arrayofgroupslegend[$i]) > 0) { // We used a 'group by'
  1069. // If we change the stack
  1070. $newcolor = $this->datacolor[$arrayofgroupslegend[$i]['stacknum']];
  1071. if ($oldstacknum == -1 || $arrayofgroupslegend[$i]['stacknum'] != $oldstacknum) {
  1072. $iinstack = 0;
  1073. //var_dump('iinstack='.$iinstack.' - '.colorArrayToHex($newcolor));
  1074. } else {
  1075. // Change color with offset of $$iinstack
  1076. //var_dump($newcolor);
  1077. $ratio = max(-90, -10 * $iinstack);
  1078. $brightnessratio = min(90, 20 * $iinstack);
  1079. $newcolor = array_values(colorHexToRgb(colorAgressiveness(colorArrayToHex($newcolor), $ratio, $brightnessratio), false, true));
  1080. //var_dump($ratio.' '.$brightnessratio);
  1081. //var_dump($newcolor);
  1082. }
  1083. $oldstacknum = $arrayofgroupslegend[$i]['stacknum'];
  1084. $color = 'rgb('.$newcolor[0].', '.$newcolor[1].', '.$newcolor[2].', 0.9)';
  1085. $bordercolor = 'rgb('.$newcolor[0].', '.$newcolor[1].', '.$newcolor[2].')';
  1086. $textoflegend = $arrayofgroupslegend[$i]['legendwithgroup'];
  1087. } else {
  1088. $color = 'rgb('.$this->datacolor[$i][0].', '.$this->datacolor[$i][1].', '.$this->datacolor[$i][2].', 0.9)';
  1089. $bordercolor = $color;
  1090. //$color = (!empty($data['seriescolor']) ? json_encode($data['seriescolor']) : json_encode($datacolor));
  1091. $textoflegend = $this->Legend[$i];
  1092. }
  1093. if ($i > 0) $this->stringtoshow .= ', ';
  1094. $this->stringtoshow .= "\n";
  1095. $this->stringtoshow .= '{';
  1096. $this->stringtoshow .= 'dolibarrinfo: \'y_'.$i.'\', ';
  1097. $this->stringtoshow .= 'label: \''.dol_escape_js(dol_string_nohtmltag($textoflegend)).'\', ';
  1098. $this->stringtoshow .= 'pointStyle: \''.($this->type[$i] == 'linesnopoint' ? 'line' : 'circle').'\', ';
  1099. $this->stringtoshow .= 'fill: '.($type == 'bar' ? 'true' : 'false').', ';
  1100. $this->stringtoshow .= 'borderWidth: \'1\', ';
  1101. $this->stringtoshow .= 'borderColor: \''.$bordercolor.'\', ';
  1102. $this->stringtoshow .= 'backgroundColor: \''.$color.'\', ';
  1103. if ($arrayofgroupslegend[$i]) $this->stringtoshow .= 'stack: \''.$arrayofgroupslegend[$i]['stacknum'].'\', ';
  1104. $this->stringtoshow .= 'data: ['.$serie[$i].']';
  1105. $this->stringtoshow .= '}'."\n";
  1106. $i++;
  1107. $iinstack++;
  1108. }
  1109. $this->stringtoshow .= ']'."\n";
  1110. $this->stringtoshow .= '}'."\n";
  1111. $this->stringtoshow .= '});'."\n";
  1112. }
  1113. $this->stringtoshow .= '</script>'."\n";
  1114. }
  1115. /**
  1116. * Output HTML string to total value
  1117. *
  1118. * @return string HTML string to total value
  1119. */
  1120. public function total()
  1121. {
  1122. $value = 0;
  1123. foreach ($this->data as $valarray) // Loop on each x
  1124. {
  1125. $value += $valarray[1];
  1126. }
  1127. return $value;
  1128. }
  1129. /**
  1130. * Output HTML string to show graph
  1131. *
  1132. * @param int|string $shownographyet Show graph to say there is not enough data or the message in $shownographyet if it is a string.
  1133. * @return string HTML string to show graph
  1134. */
  1135. public function show($shownographyet = 0)
  1136. {
  1137. global $langs;
  1138. if ($shownographyet)
  1139. {
  1140. $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>';
  1141. $s .= '<div class="nographyettext">';
  1142. if (is_numeric($shownographyet)) {
  1143. $s .= $langs->trans("NotEnoughDataYet");
  1144. } else {
  1145. $s .= $shownographyet;
  1146. }
  1147. $s .= '</div>';
  1148. return $s;
  1149. }
  1150. return $this->stringtoshow;
  1151. }
  1152. /**
  1153. * getDefaultGraphSizeForStats
  1154. *
  1155. * @param string $direction 'width' or 'height'
  1156. * @param string $defaultsize Value we want as default size
  1157. * @return int Value of width or height to use by default
  1158. */
  1159. public static function getDefaultGraphSizeForStats($direction, $defaultsize = '')
  1160. {
  1161. global $conf;
  1162. if ($direction == 'width')
  1163. {
  1164. if (empty($conf->dol_optimize_smallscreen)) return ($defaultsize ? $defaultsize : '500');
  1165. else return (empty($_SESSION['dol_screen_width']) ? '280' : ($_SESSION['dol_screen_width'] - 40));
  1166. }
  1167. if ($direction == 'height')
  1168. {
  1169. return (empty($conf->dol_optimize_smallscreen) ? ($defaultsize ? $defaultsize : '200') : '160');
  1170. }
  1171. return 0;
  1172. }
  1173. }