PHP 8 deprecated parameter in imagefilledpolygon. More float to int problems. Only...
[fa-stable.git] / reporting / includes / class.graphic.inc
index 4d1e5bb9be2b8e4e44fe3e33f7efe7e4353e0c3a..0a3591afa575d97923d8d4796669d70aa76c16a4 100644 (file)
@@ -4,14 +4,13 @@
 $pg = new Chart('bar'); // Width and Height is set automatically4
 $pg->setTitle("Testing the Chart");
 $pg->setLabels(array('Jan', 'Feb', 'Mar'));
-$pg->addSerie(array(10345, 15666, 12222), 'Sales', false, true); 
-$pg->addSerie(array(20767, 10456, 2888), 'Costs', false, false);
-$pg->addSerie(array(15657, 7567, 12890), 'Result', 'spline', false);
+$pg->addSerie('Sales', array(10345, 15666, 12222), false, true); 
+$pg->addSerie('Costs', array(20767, 10456, 2888), false, false);
+$pg->addSerie('Result', array(15657, 7567, 12890), 'spline', false);
 $pg->setXTitle("Names");
 $pg->setYTitle("Amount");
-$pg->setDValues(False);
 $pg->setDTitle(number_format("15666")); // only printed on donut or half donut
-$filename = "test.png";
+//$filename = "test.png";
 $pg->display(); // with filename to file
 
 In your html file you set it up as:
@@ -186,6 +185,7 @@ class Chart
        {
                if ($label == false)
                        $label = "Serie $this->num_series";
+               $type = $type == 'line' ? 4 : ($type == 'spline' ? 8 : $type);
                $this->series[$this->num_series]['label'] = $label;
                $this->series[$this->num_series]['data'] = $data;
                $this->series[$this->num_series]['type'] = $type;
@@ -330,9 +330,9 @@ class Chart
                        for ($i = 1; $i < 10; $i++)
                        {
                                $dec_y = $i * ($this->higher_value_size / 10);
-                               $x1 = $this->graphic_area_x1;
-                               $y1 = $this->graphic_area_y2 - $dec_y;
-                               $x2 = $this->graphic_area_x2;
+                               $x1 = round($this->graphic_area_x1);
+                               $y1 = round($this->graphic_area_y2 - $dec_y);
+                               $x2 = round($this->graphic_area_x2);
                                $y2 = $y1;
                                if ($this->stream == 'svg')
                                        $this->svg->line($x1, $y1, $x2, $y2, $this->color['bg_lines']);
@@ -835,13 +835,13 @@ class Chart
                                        $j = $n - 1;
                                        while ($i + 1 < $j) {
                                                $k = $i + ($j - $i) / 2;
-                                               if ($x <= $splines[$k]['x']) {
+                                               if ($x <= $splines[round($k)]['x']) {
                                                        $j = $k;
                                                } else {
                                                        $i = $k;
                                                }
                                        }
-                                       $s = $splines[$j];
+                                       $s = $splines[round($j)];
                                }
                        }
                        $dx = ($x - $s['x']);
@@ -872,6 +872,7 @@ class Chart
                        $ang1=0;          // start angle
                        foreach ($this->labels as $i => $num)
                        {
+                               $n = count(self::$palette);
                                $pct[$i] = round($this->series[0]['data'][$i] / $this->sum_total[0] * 100, 1);
                                $dang = $this->series[0]['data'][$i] * $area;           // delta angle
                                $laf  = $dang > 180 ? 1 : 0; // Large Arc Flag
@@ -886,7 +887,7 @@ class Chart
                                $p4 = sprintf('%0.2f,%0.2f', $cx + $rc * cos($a), $cy + $rc * sin($a));
                                $a = ($ang1 - $start) * $rad + asin($gap / $rc); 
                                $p5 = sprintf('%0.2f,%0.2f', $cx + $rc * cos($a), $cy + $rc * sin($a));
-                               $this->svg->path("M$p1 L$p2 A$r,$r 0 $laf,1 $p3 L$p4 A$rc,$rc 0 $laf,0 $p5", $this->color[$i], 0, $this->color[$i], "class=\"tips\"", "$num: ".number_format($pct[$i], 1)."&#x25;");
+                               $this->svg->path("M$p1 L$p2 A$r,$r 0 $laf,1 $p3 L$p4 A$rc,$rc 0 $laf,0 $p5", $this->color[$i % $n], 0, $this->color[$i % $n], "class=\"tips\"", "$num: ".number_format($pct[$i], 1)."&#x25;");
                                $ang1=$ang2;
                        }
                        if (!empty($this->donut_title) && $this->type != 5)
@@ -1217,7 +1218,9 @@ class Chart
 
        function string_width($string, $size) 
        {
-               $p = imagettfbbox($size, 0, $this->fontfile, $string);
+               if ($string == NULL)
+                       return $size;
+               $p = imageftbbox($size, 0, $this->fontfile, $string);
                return $p[4] - $p[0];
        }
 
@@ -1225,7 +1228,7 @@ class Chart
        {
                $digits   = strlen(round($this->max_value));
                $interval = pow(10, ($digits-1));
-               $this->higher_value = round(($this->max_value - ($this->max_value % $interval) + $interval), 1);
+               $this->higher_value = round(($this->max_value - (round($this->max_value) % $interval) + $interval), 1);
                $this->higher_value_str = $this->number_formated($this->higher_value, $this->dec1);
        }
 
@@ -1342,8 +1345,14 @@ class Chart
                        round($x2 + (1+$k)*$a), round($y2 - (1-$k)*$a),
                        round($x2 + (1-$k)*$a), round($y2 + (1+$k)*$a),
                );
-               imagefilledpolygon($image, $points, 4, $color);
-               return imagepolygon($image, $points, 4, $color);
+               if (PHP_MAJOR_VERSION >= 8) { // num_points is deprecated
+                       imagefilledpolygon($image, $points, $color);
+                       return imagepolygon($image, $points, $color);
+               }
+               else {
+                       imagefilledpolygon($image, $points, 4, $color);
+                       return imagepolygon($image, $points, 4, $color);
+               }
        }
 
        function _imagestring($img, $size, $x, $y, $string, $col, $alt=0, $angle=0, $align=false)
@@ -1385,7 +1394,7 @@ class Chart
                        $size += 2;
                        $string = str_replace(" ", "  ", $string);
                }       
-               imagettftext($img, $size, $angle, $x, $y  + $alt, $col, $this->fontfile, $string);
+               imagettftext($img, $size, $angle, round($x), $y  + $alt, $col, $this->fontfile, $string);
        }
 
        function hex2rgb($hex)