From 928446596dc33658662e429e79a1f40bb4588df6 Mon Sep 17 00:00:00 2001 From: Laurent Destailleur Date: Mon, 5 Aug 2013 18:30:10 +0200 Subject: [PATCH] Perf: Optimize memory usage when generating graph. --- htdocs/core/class/dolgraph.class.php | 51 +++++++++++----------------- 1 file changed, 19 insertions(+), 32 deletions(-) diff --git a/htdocs/core/class/dolgraph.class.php b/htdocs/core/class/dolgraph.class.php index f0598a00a82..9c6cc5b424e 100644 --- a/htdocs/core/class/dolgraph.class.php +++ b/htdocs/core/class/dolgraph.class.php @@ -742,7 +742,7 @@ class DolGraph * Build a graph onto disk using JFlot library. Input when calling this method should be: * $this->data = array(array( 0=>'labelxA', 1=>yA), array('labelxB',yB)); or * $this->data = array(array('label'=>'labelxA','data'=>yA), array('labelxB',yB)); - * $this->data = array(array(0=>'labelxA',1=>yA1,...,n=>yAn), array('labelxB',yB1,...yBn)); // when there is n value to show for each x + * $this->data = array(array(0=>'labelxA',1=>yA1,...,n=>yAn), array('labelxB',yB1,...yBn)); // when there is n series to show for each x * $this->legend= array("Val1",...,"Valn"); // list of n series name * $this->type = array('bars',...'lines'); or array('pie') * $this->mode = 'depth' ??? @@ -759,54 +759,41 @@ class DolGraph dol_syslog(get_class($this)."::draw_jflot this->type=".join(',',$this->type)); - // On boucle sur chaque lot de donnees $legends=array(); $nblot=count($this->data[0])-1; // -1 to remove legend if ($nblot < 0) dol_print_error('Bad value for property ->data. Must be set by mydolgraph->SetData before callinf mydolgrapgh->draw'); $firstlot=0; - // work with line but not with bars + // 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) + 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; - $values=array(); - foreach($this->data as $key => $valarray) + foreach($this->data as $valarray) // Loop on each x { $legends[$x] = $valarray[0]; - $values[$x] = $valarray[$i+1]; + $values[$x] = (is_numeric($valarray[$i+1]) ? $valarray[$i+1] : null); $x++; } - // We fix unknown values to null - $newvalues=array(); - foreach($values as $val) - { - $newvalues[]=(is_numeric($val) ? $val : null); + // TODO Avoid push by adding generating a long array... + if (isset($this->type[$firstlot]) && $this->type[$firstlot] == 'pie') + { + 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"; } } - - //print "Lot de donnees $i
"; - //print_r($values); - //print '
'; - // TODO Replace with json_encode - $serie[$i]="var d".$i." = [];\n"; - $x=0; - foreach($newvalues as $key => $val) - { - if (isset($this->type[$firstlot]) && $this->type[$firstlot] == 'pie') - { - if (isset($val)) $serie[$i].='d'.$i.'.push({"label":"'.dol_escape_js($legends[$x]).'", "data":'.$val.'});'."\n"; - } - else - { - if (isset($val)) $serie[$i].='d'.$i.'.push(['.$x.', '.$val.']);'."\n"; - } - $x++; - } - + + unset($values); $i++; } $tag=dol_escape_htmltag(dol_string_unaccent(dol_string_nospecial(basename($file),'_',array('-','.'))));