Perf: Optimize memory usage when generating graph.

This commit is contained in:
Laurent Destailleur 2013-08-05 18:30:10 +02:00
parent 85572a4bd1
commit 928446596d

View File

@ -742,7 +742,7 @@ class DolGraph
* Build a graph onto disk using JFlot library. Input when calling this method should be: * 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( 0=>'labelxA', 1=>yA), array('labelxB',yB)); or
* $this->data = array(array('label'=>'labelxA','data'=>yA), array('labelxB',yB)); * $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->legend= array("Val1",...,"Valn"); // list of n series name
* $this->type = array('bars',...'lines'); or array('pie') * $this->type = array('bars',...'lines'); or array('pie')
* $this->mode = 'depth' ??? * $this->mode = 'depth' ???
@ -759,54 +759,41 @@ class DolGraph
dol_syslog(get_class($this)."::draw_jflot this->type=".join(',',$this->type)); dol_syslog(get_class($this)."::draw_jflot this->type=".join(',',$this->type));
// On boucle sur chaque lot de donnees
$legends=array(); $legends=array();
$nblot=count($this->data[0])-1; // -1 to remove legend $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'); if ($nblot < 0) dol_print_error('Bad value for property ->data. Must be set by mydolgraph->SetData before callinf mydolgrapgh->draw');
$firstlot=0; $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 //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; $i=$firstlot;
$serie=array(); $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; $x=0;
$values=array(); foreach($this->data as $valarray) // Loop on each x
foreach($this->data as $key => $valarray)
{ {
$legends[$x] = $valarray[0]; $legends[$x] = $valarray[0];
$values[$x] = $valarray[$i+1]; $values[$x] = (is_numeric($valarray[$i+1]) ? $valarray[$i+1] : null);
$x++; $x++;
} }
// We fix unknown values to null // TODO Avoid push by adding generating a long array...
$newvalues=array(); if (isset($this->type[$firstlot]) && $this->type[$firstlot] == 'pie')
foreach($values as $val) {
{ foreach($values as $x => $y) { if (isset($y)) $serie[$i].='d'.$i.'.push({"label":"'.dol_escape_js($legends[$x]).'", "data":'.$y.'});'."\n"; }
$newvalues[]=(is_numeric($val) ? $val : null); }
} else
{
//print "Lot de donnees $i<br>"; foreach($values as $x => $y) { if (isset($y)) $serie[$i].='d'.$i.'.push(['.$x.', '.$y.']);'."\n"; }
//print_r($values);
//print '<br>';
// 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++; $i++;
} }
$tag=dol_escape_htmltag(dol_string_unaccent(dol_string_nospecial(basename($file),'_',array('-','.')))); $tag=dol_escape_htmltag(dol_string_unaccent(dol_string_nospecial(basename($file),'_',array('-','.'))));