Graph : Use Color in namespace

Graph : Allow to hide a serie available in the data
This commit is contained in:
2021-05-18 12:23:17 +02:00
parent adafd14564
commit e7c6833d2d

View File

@@ -66,6 +66,9 @@ class Graph
if (! function_exists ("imagecreatetruecolor"))
throw new \Exception (dgettext ("domframework",
"No GD support in PHP : can't create image"), 500);
if (! function_exists ("bccomp"))
throw new \Exception (dgettext ("domframework",
"No BCMath support in PHP : can't create image"), 500);
$this->title = new GraphTitle ();
$this->legend = new GraphLegend ();
$this->data = new GraphData ();
@@ -131,7 +134,7 @@ class Graph
if ($bgcolor === null)
return $this->bgcolor;
if (! is_string ($bgcolor) ||
! in_array ($bgcolor, \color::colorList ()))
! in_array ($bgcolor, Color::colorList ()))
throw new \Exception (dgettext ("domframework",
"Invalid bgcolor provided to graph"), 406);
$this->bgcolor = $bgcolor;
@@ -165,7 +168,7 @@ class Graph
! in_array ($style, array ("line", "points", "linePoints")))
throw new \Exception (dgettext ("domframework",
"Invalid style provided to graph"), 406);
$styleClass = "GraphStyle".$style;
$styleClass = __NAMESPACE__."\\GraphStyle".ucfirst($style);
if ($this->style === null || $this->style ()->name () !== $style)
{
$this->style = new $styleClass ();
@@ -310,7 +313,7 @@ class Graph
$gd = imagecreatetruecolor ($this->width, $this->height);
// Put the background color
imagefilledrectangle ($gd, 0, 0, $this->width - 1 , $this->height - 1 ,
\color::allocateFromText ($gd, $this->bgcolor));
Color::allocateFromText ($gd, $this->bgcolor));
// The coordinates of the free space. Will be modified each time something
// is drawing on the graph
// xtop, ytop, xbottom, ybottom
@@ -415,7 +418,14 @@ class GraphSeries
*/
public function getList ()
{
return array_keys ($this->series);
$series = [];
foreach ($this->series as $name => $serie)
{
if ($serie->hide() === true)
continue;
$series[] = $name;
}
return $series;
}
/** Remove an existing serie
@@ -472,6 +482,9 @@ class GraphSerie
/** The style object for the serie
*/
private $style;
/** If set, hide this serie and do not display it
*/
private $hide;
/** The axis used to draw the serie.
* If false for main Y axis
@@ -637,6 +650,20 @@ class GraphSerie
$this->style = new $styleClass ();
return $this->style;
}
/** Set/Get the hidden state of the serie
* If the parameter is not provided, return the actual state
* @param boolean|null $hide The hidden state
*/
public function hide ($hide = null)
{
if ($hide === null)
return $this->hide;
if (! is_bool ($hide))
throw new \Exception (dgettext ("domframework",
"Invalid hide mode provided to serie"), 406);
$this->hide = $hide;
return $this;
}
/** The serie is based on the secondary Y axis
* Set the value if the parameter is provided, get the value if the parameter
@@ -663,6 +690,8 @@ class GraphSerie
*/
public function draw ($gd, $free, $axisX, $axisY)
{
if ($this->hide === true)
return;
$this->style->draw ($gd, $free, $this->data, $axisX, $axisY);
}
}
@@ -1016,7 +1045,7 @@ class GraphTitle
if ($color === null)
return $this->color;
if (! is_string ($color) ||
! in_array ($color, \color::colorList ()))
! in_array ($color, Color::colorList ()))
throw new \Exception (dgettext ("domframework",
"Invalid color provided to graph title"), 406);
$this->color = $color;
@@ -1057,7 +1086,7 @@ class GraphTitle
$x += $free[0];
$y += $free[1];
imagettftext ($gd, $this->fontsize, 0, $x, $y,
\color::allocateFromText ($gd, $this->color),
Color::allocateFromText ($gd, $this->color),
$this->fontfile, $this->text);
return array (intval ($free[0]), intval ($free[1] + $y + $this->padding),
intval ($free[2]), intval ($free[3]));
@@ -1152,7 +1181,7 @@ class GraphLegend
if ($color === null)
return $this->color;
if (! is_string ($color) ||
! in_array ($color, \color::colorList ()))
! in_array ($color, Color::colorList ()))
throw new \Exception (dgettext ("domframework",
"Invalid color provided to graph legend"), 406);
$this->color = $color;
@@ -1168,7 +1197,7 @@ class GraphLegend
if ($bgcolor === null)
return $this->bgcolor;
if (! is_string ($bgcolor) ||
! in_array ($bgcolor, \color::colorList ()))
! in_array ($bgcolor, Color::colorList ()))
throw new \Exception (dgettext ("domframework",
"Invalid bgcolor provided to graph legend"), 406);
$this->bgcolor = $bgcolor;
@@ -1184,7 +1213,7 @@ class GraphLegend
if ($borderColor === null)
return $this->borderColor;
if (! is_string ($borderColor) ||
! in_array ($borderColor, \color::colorList ()))
! in_array ($borderColor, Color::colorList ()))
throw new \Exception (dgettext ("domframework",
"Invalid borderColor provided to graph legend"), 406);
$this->borderColor = $borderColor;
@@ -1244,10 +1273,10 @@ class GraphLegend
$border = 1;
// Draw the background rectangle
imagefilledrectangle ($gd, $x1, $y1, $x2, $y2,
\color::allocateFromText ($gd, $this->borderColor));
Color::allocateFromText ($gd, $this->borderColor));
imagefilledrectangle ($gd, $x1+$border, $y1+$border,
$x2-$border, $y2-$border,
\color::allocateFromText ($gd, $this->bgcolor));
Color::allocateFromText ($gd, $this->bgcolor));
// Display the serie names
$y = $y1;
@@ -1259,7 +1288,7 @@ class GraphLegend
$y += $height;
imagettftext ($gd, $this->fontsize, 0,
$x1 + 30, intval ($y + $height / 2),
\color::allocateFromText ($gd, $this->color),
Color::allocateFromText ($gd, $this->color),
$this->fontfile, $serie);
// Draw the sample
$series->serie ($serie)->style ()->sample ($gd, $x1 + 15, $y);
@@ -1509,7 +1538,7 @@ class GraphAxisGeneral
if ($axisColor === null)
return $this->axisColor;
if (! is_string ($axisColor) ||
! in_array ($axisColor, \color::colorList ()))
! in_array ($axisColor, Color::colorList ()))
throw new \Exception (dgettext ("domframework",
"Invalid axisColor provided to graph axis"), 406);
$this->axisColor = $axisColor;
@@ -1525,7 +1554,7 @@ class GraphAxisGeneral
if ($gridColor === null)
return $this->gridColor;
if (! is_string ($gridColor) ||
! in_array ($gridColor, \color::colorList ()))
! in_array ($gridColor, Color::colorList ()))
throw new \Exception (dgettext ("domframework",
"Invalid gridColor provided to graph grid"), 406);
$this->gridColor = $gridColor;
@@ -1581,7 +1610,7 @@ class GraphAxisGeneral
// Return the 0 label and the values arround it
$zeroAlreadyDraw = false;
for ($i = 0 ;
bccomp ($i, 1.2 * $max, abs ($base) + 1) < 1 ;
\bccomp ($i, 1.2 * $max, abs ($base) + 1) < 1 ;
$i += $scale)
{
if ($i === 0)
@@ -1590,7 +1619,7 @@ class GraphAxisGeneral
$labels[] = $i;
}
for ($i = 0 ;
bccomp ($i, 1.4 * $min, abs ($base) + 1) >= 0 ;
\bccomp ($i, 1.4 * $min, abs ($base) + 1) >= 0 ;
$i -= $scale)
{
if ($zeroAlreadyDraw === true && $i === 0)
@@ -1603,7 +1632,7 @@ class GraphAxisGeneral
{
// No 0 Axis : From $this->min to $this->max
for ($i = $min ;
bccomp ($i, $max, abs ($base) + 1) < 1 ;
\bccomp ($i, $max, abs ($base) + 1) < 1 ;
$i += $scale)
{
if ($deb) echo "Values no Zero : add val $i\n";
@@ -1619,14 +1648,14 @@ class GraphAxisGeneral
else
$label = sprintf ("%".intval ($base)."d", $label);
if ($this->labelMin === null ||
bccomp ($this->labelMin, $label, abs ($base) + 1) >= 0)
\bccomp ($this->labelMin, $label, abs ($base) + 1) >= 0)
$this->labelMin = $label;
if ($this->labelMax === null ||
bccomp ($this->labelMax, $label, abs ($base) + 1) <= 0)
\bccomp ($this->labelMax, $label, abs ($base) + 1) <= 0)
$this->labelMax = $label;
}
if (bccomp ($this->labelMin, $this->min, abs ($base) + 1) > 0)
if (\bccomp ($this->labelMin, $this->min, abs ($base) + 1) > 0)
{
// Add a label in the minimum
$this->labelMin = $this->labelMin - $scale;
@@ -1637,7 +1666,7 @@ class GraphAxisGeneral
else
$labels[] = sprintf ("%".intval ($base)."d", $this->labelMin);
}
if (bccomp ($this->labelMax, $this->max, abs ($base) + 1) < 0)
if (\bccomp ($this->labelMax, $this->max, abs ($base) + 1) < 0)
{
$this->labelMax = $this->labelMax + $scale;
if ($deb) echo "Force Add Max $this->labelMax\n";
@@ -1828,7 +1857,7 @@ class GraphAxisX extends GraphAxisHorizontal
*/
public function draw ($gd)
{
$axisColor = \color::allocateFromText ($gd, $this->axisColor);
$axisColor = Color::allocateFromText ($gd, $this->axisColor);
if ($this->numerical)
{
if ($this->angle === null)
@@ -1847,7 +1876,7 @@ class GraphAxisX extends GraphAxisHorizontal
$y = $this->bottom - $this->padding;
// The font color is forced to black
imagettftext ($gd, $this->fontsize, $this->angle, $x, $y,
\color::allocateFromText ($gd, "black"),
Color::allocateFromText ($gd, "black"),
$this->fontfile, $value);
// Draw the scale
@@ -1883,7 +1912,7 @@ class GraphAxisX extends GraphAxisHorizontal
$y = $this->bottom - $this->padding;
// The font color is forced to black
imagettftext ($gd, $this->fontsize, $this->angle, $x, $y,
\color::allocateFromText ($gd, "black"),
Color::allocateFromText ($gd, "black"),
$this->fontfile, $value);
// Draw the separators
@@ -1912,7 +1941,7 @@ class GraphAxisX extends GraphAxisHorizontal
{
if ($this->gridColor === null || $this->gridColor === "transparent")
return;
$gridColor = \color::allocateFromText ($gd, $this->gridColor);
$gridColor = Color::allocateFromText ($gd, $this->gridColor);
if ($position === null)
return;
$y = $this->bottom - $this->height;
@@ -2065,7 +2094,7 @@ class GraphAxisVertical extends GraphAxisGeneral
*/
public function draw ($gd)
{
$axisColor = \color::allocateFromText ($gd, $this->axisColor);
$axisColor = Color::allocateFromText ($gd, $this->axisColor);
if ($this->numerical)
{
// Look at the minimum distance between two labeled values
@@ -2112,7 +2141,7 @@ class GraphAxisY1 extends GraphAxisVertical
*/
protected function drawOne ($gd, $width, $val)
{
$axisColor = \color::allocateFromText ($gd, $this->axisColor);
$axisColor = Color::allocateFromText ($gd, $this->axisColor);
if ($this->numerical)
{
// Do not allow the label to be longer than $this->max
@@ -2131,7 +2160,7 @@ class GraphAxisY1 extends GraphAxisVertical
imagettftext ($gd, $this->fontsize, 0,
$width - $labelwidth,
$y + $labelheight / 2,
\color::allocateFromText ($gd, "black"),
Color::allocateFromText ($gd, "black"),
$this->fontfile, $itmp);
// Display the separators
@@ -2150,7 +2179,7 @@ die ("graphAxisY1:: drawOne NOT numerical line ".__LINE__."\n");
*/
protected function drawAxis ($gd, $width)
{
$axisColor = \color::allocateFromText ($gd, $this->axisColor);
$axisColor = Color::allocateFromText ($gd, $this->axisColor);
imageline ($gd, $width, $this->bottom, $width, $this->top, $axisColor);
}
@@ -2163,7 +2192,7 @@ die ("graphAxisY1:: drawOne NOT numerical line ".__LINE__."\n");
{
if ($this->gridColor === null || $this->gridColor === "transparent")
return;
$gridColor = \color::allocateFromText ($gd, $this->gridColor);
$gridColor = Color::allocateFromText ($gd, $this->gridColor);
if ($this->numerical)
{
$y = $this->position ($val);
@@ -2190,7 +2219,7 @@ class GraphAxisY2 extends GraphAxisVertical
*/
protected function drawOne ($gd, $width, $val)
{
$axisColor = \color::allocateFromText ($gd, $this->axisColor);
$axisColor = Color::allocateFromText ($gd, $this->axisColor);
if ($this->numerical)
{
// Do not allow the label to be longer than $this->max
@@ -2209,7 +2238,7 @@ class GraphAxisY2 extends GraphAxisVertical
imagettftext ($gd, $this->fontsize, 0,
$this->left + $this->padding + 3,
$y + $labelheight / 2,
\color::allocateFromText ($gd, "black"),
Color::allocateFromText ($gd, "black"),
$this->fontfile, $itmp);
// Display the separators
@@ -2228,7 +2257,7 @@ die ("graphAxisY2:: drawOne NOT numerical line ".__LINE__."\n");
*/
protected function drawAxis ($gd, $width)
{
$axisColor = \color::allocateFromText ($gd, $this->axisColor);
$axisColor = Color::allocateFromText ($gd, $this->axisColor);
imageline ($gd, $this->left, $this->bottom,
$this->left, $this->top, $axisColor);
}
@@ -2296,7 +2325,7 @@ class GraphStyleLinePoints
return $this->lineColor;
if (! is_string ($lineColor) ||
($lineColor !== "transparent" &&
! in_array ($lineColor, \color::colorList ())))
! in_array ($lineColor, Color::colorList ())))
throw new \Exception (dgettext ("domframework",
"Invalid lineColor provided to line style"), 406);
$this->lineColor = $lineColor;
@@ -2328,7 +2357,7 @@ class GraphStyleLinePoints
return $this->pointBgcolor;
if (! is_string ($pointBgcolor) ||
($pointBgcolor !== "transparent" &&
! in_array ($pointBgcolor, \color::colorList ())))
! in_array ($pointBgcolor, Color::colorList ())))
throw new \Exception (dgettext ("domframework",
"Invalid pointBgcolor provided to line style"), 406);
$this->pointBgcolor = $pointBgcolor;
@@ -2345,7 +2374,7 @@ class GraphStyleLinePoints
return $this->pointColor;
if (! is_string ($pointColor) ||
($pointColor !== "transparent" &&
! in_array ($pointColor, \color::colorList ())))
! in_array ($pointColor, Color::colorList ())))
throw new \Exception (dgettext ("domframework",
"Invalid pointColor provided to line style"), 406);
$this->pointColor = $pointColor;
@@ -2419,7 +2448,7 @@ class GraphStyleLinePoints
public function draw ($gd, $free, $data, $axisX, $axisY)
{
if ($this->lineColor !== "transparent")
$lineColor = \color::allocateFromText ($gd, $this->lineColor);
$lineColor = Color::allocateFromText ($gd, $this->lineColor);
$lastX = null;
$lastY = null;
foreach ($data as $key=>$value)
@@ -2465,9 +2494,9 @@ class GraphStyleLinePoints
private function drawPoint ($gd, $posX, $posY)
{
if ($this->pointColor !== "transparent")
$pointColor = \color::allocateFromText ($gd, $this->pointColor);
$pointColor = Color::allocateFromText ($gd, $this->pointColor);
if ($this->pointBgcolor !== "transparent")
$pointBgcolor = \color::allocateFromText ($gd, $this->pointBgcolor);
$pointBgcolor = Color::allocateFromText ($gd, $this->pointBgcolor);
$half = intval ($this->pointWidth / 2);
switch ($this->pointShape)
{
@@ -2530,7 +2559,7 @@ class GraphStyleLinePoints
{
if ($this->lineColor !== "transparent")
{
$lineColor = \color::allocateFromText ($gd, $this->lineColor);
$lineColor = Color::allocateFromText ($gd, $this->lineColor);
imageline ($gd, $x-10, $y, $x+10, $y, $lineColor);
}
if ($this->pointWidth === null)