From: William Desportes <williamdes@wdes.fr>
Date: Thu, 26 Dec 2024 14:00:43 +0100
Subject: Replace c-pchart by a polyfill

Forwarded discussion to https://github.com/matomo-org/matomo/issues/20570

Origin: vendor
Forwarded: no
---
 libs/ChartLib/ChartLib.php                   | 317 +++++++++++++++++++++++++++
 plugins/ImageGraph/StaticGraph.php           |   4 +-
 plugins/ImageGraph/StaticGraph/Exception.php |   2 +-
 plugins/ImageGraph/StaticGraph/PieGraph.php  |   2 +-
 4 files changed, 321 insertions(+), 4 deletions(-)
 create mode 100644 libs/ChartLib/ChartLib.php

diff --git a/libs/ChartLib/ChartLib.php b/libs/ChartLib/ChartLib.php
new file mode 100644
index 0000000..b53552f
--- /dev/null
+++ b/libs/ChartLib/ChartLib.php
@@ -0,0 +1,317 @@
+<?php
+
+namespace ChartLib {
+    /**
+     * 			message: "#^Constant AXIS_FORMAT_CUSTOM not found\\.$#"
+     *			message: "#^Constant LEGEND_FAMILY_LINE not found\\.$#"
+     *			message: "#^Constant LEGEND_FAMILY_BOX not found\\.$#"
+     *			message: "#^Constant LEGEND_HORIZONTAL not found\\.$#"
+     *			message: "#^Constant LEGEND_NOBORDER not found\\.$#"
+     *			message: "#^Constant LEGEND_VERTICAL not found\\.$#"
+     *			message: "#^Constant SCALE_MODE_MANUAL not found\\.$#"
+     *			message: "#^Constant SCALE_POS_LEFTRIGHT not found\\.$#"
+     *			message: "#^Constant SCALE_POS_TOPBOTTOM not found\\.$#"
+     */
+
+    // Copied from https://github.com/szymach/c-pchart/blob/v3.0.17/constants.php
+
+    define("AXIS_FORMAT_CUSTOM", 680007);
+
+    define("SCALE_POS_LEFTRIGHT", 690101);
+    define("SCALE_POS_TOPBOTTOM", 690102);
+    define("SCALE_MODE_MANUAL", 690205);
+
+    define("LEGEND_FAMILY_BOX", 691051);
+    define("LEGEND_FAMILY_LINE", 691053);
+
+    define("LEGEND_NOBORDER", 690800);
+    define("LEGEND_VERTICAL", 690901);
+    define("LEGEND_HORIZONTAL", 690902);
+
+    abstract class Draw {
+
+        /**
+         * GD picture object
+         * @var resource
+         */
+        public $Picture;
+
+        /**
+         * Turn antialias on or off
+         * @var boolean
+         */
+        public $Antialias = true;
+
+        // -----
+
+        /**
+         * Draw a bar chart
+         * @param array $Format
+         */
+        public function drawBarChart(array $Format = [])
+        {
+        }
+
+        /**
+         * @param array $Format
+         * @throws Exception
+         */
+        public function drawScale(array $Format = [])
+        {
+        }
+
+        /**
+         * Write text
+         * @param int|float $X
+         * @param int|float $Y
+         * @param string $Text
+         * @param array $Format
+         * @return array
+         */
+        public function drawText($X, $Y, $Text, array $Format = [])
+        {
+        }
+
+
+        /**
+         * Draw a line chart
+         * @param array $Format
+         */
+        public function drawLineChart(array $Format = [])
+        {
+        }
+    }
+
+    class Pie {
+        public function __construct($pImage, $pData) {
+        }
+
+        /**
+         * Set the color of the specified slice
+         * @param mixed $SliceID
+         * @param array $Format
+         */
+        public function setSliceColor($SliceID, array $Format = [])
+        {
+        }
+
+        /**
+         * Draw the legend of pie chart
+         * @param int $X
+         * @param int $Y
+         * @param array $Format
+         * @return int
+         */
+        public function drawPieLegend($X, $Y, array $Format = [])
+        {
+        }
+
+        /**
+         * Draw a 3D pie chart
+         * @param int $X
+         * @param int $Y
+         * @param array $Format
+         * @return int
+         */
+        public function draw3DPie($X, $Y, array $Format = [])
+        {
+        }
+
+        /**
+         * Draw a pie chart
+         * @param int $X
+         * @param int $Y
+         * @param array $Format
+         * @return int
+         */
+        public function draw2DPie($X, $Y, array $Format = [])
+        {
+        }
+
+    }
+    class Data {
+        public function __construct() {
+        }
+        /**
+         * Return the data & configuration of the series
+         * @return array
+         */
+        public function getData()
+        {
+            return [];
+        }
+
+        /**
+         * Return the max value of a given serie
+         * @param string $Serie
+         * @return mixed
+         */
+        public function getMax($Serie)
+        {
+            return null;
+        }
+
+        /**
+         * Define if a serie should be draw with a special weight
+         * @param mixed $Series
+         * @param int $Weight
+         */
+        public function setSerieWeight($Series, $Weight = 0)
+        {
+        }
+
+        /**
+         * Set the color of one serie
+         * @param mixed $Series
+         * @param array $Format
+         */
+        public function setPalette($Series, array $Format = [])
+        {
+        }
+
+        /**
+         * Set the description of a given serie
+         * @param string|array $Series
+         * @param string $Description
+         */
+        public function setSerieDescription($Series, $Description = "My serie")
+        {
+        }
+        /**
+         * Set the serie that will be used as abscissa
+         * @param string $Serie
+         */
+        public function setAbscissa($Serie)
+        {
+        }
+
+        /**
+         * Add a single point or an array to the given serie
+         * @param mixed $Values
+         * @param string $SerieName
+         * @return int
+         */
+        public function addPoints($Values, $SerieName = "Serie1")
+        {
+        }
+
+        /**
+         * Set the display mode of an Axis
+         * @param int $AxisID
+         * @param int $Mode
+         * @param array $Format
+         */
+        public function setAxisDisplay($AxisID, $Mode = AXIS_FORMAT_DEFAULT, $Format = null)
+        {
+        }
+
+        /**
+         * Set the icon associated to a given serie
+         * @param mixed $Series
+         * @param mixed $Picture
+         */
+        public function setSeriePicture($Series, $Picture = null)
+        {
+        }
+    }
+
+    class Image extends Draw {
+        /**
+         * @param int $XSize
+         * @param int $YSize
+         * @param Data $DataSet
+         * @param boolean $TransparentBackground
+         */
+        public function __construct(
+            $XSize,
+            $YSize,
+            Data $DataSet = null,
+            $TransparentBackground = false
+        ) {
+            $this->Picture = imagecreate($XSize, $YSize)
+                or die("Cannot Initialize new GD image stream");
+            $background = imagecolorallocate($this->Picture, 224, 224, 209);
+            $textColor = imagecolorallocate($this->Picture, 0, 51, 102);
+
+            imagestring(
+                $this->Picture,
+                2,// Font
+                5,// X
+                5,// Y
+                "Graphs renders are disabled",
+                $textColor
+            );
+        }
+
+        /**
+         * Set the graph area position
+         * @param int $X1
+         * @param int $Y1
+         * @param int $X2
+         * @param int $Y2
+         * @return int|null
+         */
+        public function setGraphArea($X1, $Y1, $X2, $Y2)
+        {
+        }
+
+        /**
+         * @param array $Format
+         * @throws \Exception
+         */
+        public function drawScale(array $Format = [])
+        {
+        }
+
+        /**
+         * Draw a filled rectangle
+         * @param int $X1
+         * @param int $Y1
+         * @param int $X2
+         * @param int $Y2
+         * @param array $Format
+         */
+        public function drawFilledRectangle($X1, $Y1, $X2, $Y2, array $Format = [])
+        {
+        }
+
+        /**
+         * Draw the legend of the active series
+         * @param int $X
+         * @param int $Y
+         * @param array $Format
+         */
+        public function drawLegend($X, $Y, array $Format = [])
+        {
+        }
+
+        /**
+         * Set current font properties
+         * @param array $Format
+         */
+        public function setFontProperties($Format = [])
+        {
+        }
+
+        /**
+         * Render the picture to a file
+         * @param string $FileName
+         */
+        public function render($FileName)
+        {
+            imagepng($this->Picture, $FileName);
+            imagedestroy($this->Picture);
+        }
+
+        /**
+         * Render the picture to a web browser stream
+         * @param boolean $BrowserExpire
+         */
+        public function stroke($BrowserExpire = false)
+        {
+            header('Content-type: image/png');
+            imagepng($this->Picture);
+            imagedestroy($this->Picture);
+        }
+    }
+}
diff --git a/plugins/ImageGraph/StaticGraph.php b/plugins/ImageGraph/StaticGraph.php
index 03c37ba..6a82ba6 100644
--- a/plugins/ImageGraph/StaticGraph.php
+++ b/plugins/ImageGraph/StaticGraph.php
@@ -9,8 +9,8 @@
 
 namespace Piwik\Plugins\ImageGraph;
 
-use CpChart\Data;
-use CpChart\Image;
+use ChartLib\Data;
+use ChartLib\Image;
 use Piwik\Container\StaticContainer;
 use Piwik\NumberFormatter;
 use Piwik\Piwik;
diff --git a/plugins/ImageGraph/StaticGraph/Exception.php b/plugins/ImageGraph/StaticGraph/Exception.php
index bb019d5..e0cab89 100644
--- a/plugins/ImageGraph/StaticGraph/Exception.php
+++ b/plugins/ImageGraph/StaticGraph/Exception.php
@@ -9,7 +9,7 @@
 
 namespace Piwik\Plugins\ImageGraph\StaticGraph;
 
-use CpChart\Data;
+use ChartLib\Data;
 use Piwik\Plugins\ImageGraph\StaticGraph;
 
 /**
diff --git a/plugins/ImageGraph/StaticGraph/PieGraph.php b/plugins/ImageGraph/StaticGraph/PieGraph.php
index da1026c..613ffe2 100644
--- a/plugins/ImageGraph/StaticGraph/PieGraph.php
+++ b/plugins/ImageGraph/StaticGraph/PieGraph.php
@@ -9,7 +9,7 @@
 
 namespace Piwik\Plugins\ImageGraph\StaticGraph;
 
-use CpChart\Chart\Pie;
+use ChartLib\Chart\Pie;
 use Piwik\Plugins\ImageGraph\StaticGraph;
 
 /**
