chart.class.php 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287
  1. <?php
  2. /**
  3. * Classe `chart`
  4. *
  5. * Cette classe fournit des outils pour générer des graphiques interactifs en utilisant la bibliothèque Chart.js.
  6. * Documentation officielle : https://www.chartjs.org/
  7. *
  8. * Fonctionnalités principales :
  9. * - Génération de différents types de graphiques (barres, lignes, secteurs, etc.).
  10. * - Gestion des données et des configurations pour les graphiques.
  11. * - Génération de scripts JavaScript pour intégrer les graphiques dans une page web.
  12. *
  13. * @package Core\Class
  14. */
  15. class chart
  16. {
  17. /**
  18. * Affiche un élément canvas HTML pour un graphique et génère le script JavaScript correspondant.
  19. *
  20. * @param array $_config Configuration du graphique (id, dimensions, type, données, etc.).
  21. * @return void
  22. */
  23. public static function printCanvas(array $_config)
  24. {
  25. $id = $_config["id"];
  26. $width = (isset($_config["width"])) ? $_config["width"] : 600;
  27. $height = (isset($_config["height"])) ? $_config["height"] : 400;
  28. if(!empty($_config["config"])){
  29. $_config["config"] = core::extractArrayInArray($_config["config"]);
  30. } else {
  31. $_config["config"] = NULL;
  32. }
  33. echo '<canvas id="' . $id . '" width="' . $width . '" height="' . $height . '" style="display: block; width: ' . $width . 'px; height: ' . $height . 'px;" class="chartjs-render-monitor"></canvas>';
  34. switch ($_config["charts"]) {
  35. case "doughnut":
  36. echo self::javascriptDoughnut($_config);
  37. break;
  38. case "bar":
  39. echo self::javascriptBar($_config);
  40. break;
  41. case "line":
  42. echo self::javascriptLine($_config);
  43. break;
  44. case "pie":
  45. echo self::javascriptPie($_config);
  46. break;
  47. default:
  48. break;
  49. }
  50. }
  51. /**
  52. * Construit les données nécessaires pour un graphique à partir des données fournies.
  53. *
  54. * @param array $_data Données brutes pour le graphique.
  55. * @param array|null $_config Configuration optionnelle des couleurs et styles.
  56. * @return array Données formatées pour le graphique.
  57. */
  58. private static function constructData(array $_data, ?array $_config = NULL)
  59. {
  60. $label = $nb = $backgroundColor = $borderColor = "";
  61. $nbData = count($_data);
  62. if(empty($_config["backgroundColor"])){
  63. $data["backgroundColor"] = array(
  64. "rgba(242, 84, 84, 0.6)",
  65. "rgba(242, 132, 84, 0.6)",
  66. "rgba(242, 179, 84, 0.6)",
  67. "rgba(242, 226, 84, 0.6)",
  68. "rgba(210, 242, 84, 0.6)",
  69. "rgba(163, 242, 84, 0.6)",
  70. "rgba(116, 242, 84, 0.6)",
  71. "rgba(84, 242, 100, 0.6)",
  72. "rgba(84, 242, 147, 0.6)",
  73. "rgba(84, 242, 195, 0.6)",
  74. "rgba(84, 242, 242, 0.6)",
  75. "rgba(84, 195, 242, 0.6)",
  76. "rgba(84, 147, 242, 0.6)",
  77. "rgba(84, 100, 242, 0.6)",
  78. "rgba(116, 84, 242, 0.6)",
  79. "rgba(163, 84, 242, 0.6)",
  80. "rgba(210, 84, 242, 0.6)",
  81. "rgba(242, 84, 226, 0.6)",
  82. "rgba(242, 84, 179, 0.6)",
  83. "rgba(242, 84, 132, 0.6)"
  84. );
  85. } else {
  86. $data["backgroundColor"] = $_config["backgroundColor"];
  87. }
  88. if(empty($_config["borderColor"])){
  89. $data["borderColor"] = array(
  90. "rgba(242, 84, 84, 1.0)",
  91. "rgba(242, 132, 84, 1.0)",
  92. "rgba(242, 179, 84, 1.0)",
  93. "rgba(242, 226, 84, 1.0)",
  94. "rgba(210, 242, 84, 1.0)",
  95. "rgba(163, 242, 84, 1.0)",
  96. "rgba(116, 242, 84, 1.0)",
  97. "rgba(84, 242, 100, 1.0)",
  98. "rgba(84, 242, 147, 1.0)",
  99. "rgba(84, 242, 195, 1.0)",
  100. "rgba(84, 242, 242, 1.0)",
  101. "rgba(84, 195, 242, 1.0)",
  102. "rgba(84, 147, 242, 1.0)",
  103. "rgba(84, 100, 242, 1.0)",
  104. "rgba(116, 84, 242, 1.0)",
  105. "rgba(163, 84, 242, 1.0)",
  106. "rgba(210, 84, 242, 1.0)",
  107. "rgba(242, 84, 226, 1.0)",
  108. "rgba(242, 84, 179, 1.0)",
  109. "rgba(242, 84, 132, 1.0)"
  110. );
  111. } else {
  112. $data["borderColor"] = $_config["borderColor"];
  113. }
  114. for ($i = 0; $i < $nbData; $i++) {
  115. $backgroundColor .= '"' . $data["backgroundColor"][$i] . '"';
  116. if ($i != $nbData) {
  117. $backgroundColor .= ", ";
  118. }
  119. }
  120. for ($i = 0; $i < $nbData; $i++) {
  121. $borderColor .= '"' . $data["borderColor"][$i] . '"';
  122. if ($i != $nbData) {
  123. $borderColor .= ", ";
  124. }
  125. }
  126. for ($i = 0; $i < $nbData; $i++) {
  127. $label .= ($_data[$i]["nb"] > 0) ? '"' . $_data[$i]["label"] . '"' : '"Aucun(e)s ' . $_data[$i]["label"] . '"';
  128. if ($i != $nbData) {
  129. $label .= ", ";
  130. }
  131. }
  132. for ($i = 0; $i < $nbData; $i++) {
  133. $nb .= '"' . $_data[$i]["nb"] . '"';
  134. if ($i != $nbData) {
  135. $nb .= ", ";
  136. }
  137. }
  138. return array("label" => $label, "nb" => $nb, "backgroundColor" => $backgroundColor, "borderColor" => $borderColor);
  139. }
  140. /**
  141. * Génère le script JavaScript pour un graphique de type "pie".
  142. *
  143. * @param array $_config Configuration du graphique.
  144. * @return string Script JavaScript pour le graphique.
  145. */
  146. private static function javascriptPie(array $_config)
  147. {
  148. $sort = self::constructData($_config["data"], $_config["config"]);
  149. return '<script>
  150. var ctx = document.getElementById("' . $_config["id"] . '");
  151. var myChart = new Chart(ctx, {
  152. type: "pie",
  153. data: {
  154. labels: [' . $sort["label"] . '],
  155. datasets: [{
  156. label: "' . $_config["label"] . '",
  157. data: [' . $sort["nb"] . '],
  158. backgroundColor: [' . $sort["backgroundColor"] . '],
  159. borderColor: [' . $sort["borderColor"] . '],
  160. borderWidth: 1
  161. }]
  162. }
  163. });
  164. </script>';
  165. }
  166. /**
  167. * Génère le script JavaScript pour un graphique de type "doughnut".
  168. *
  169. * @param array $_config Configuration du graphique.
  170. * @return string Script JavaScript pour le graphique.
  171. */
  172. private static function javascriptDoughnut(array $_config)
  173. {
  174. $sort = self::constructData($_config["data"], $_config["config"]);
  175. return '<script>
  176. var ctx = document.getElementById("' . $_config["id"] . '");
  177. var myChart = new Chart(ctx, {
  178. type: "doughnut",
  179. data: {
  180. labels: [' . $sort["label"] . '],
  181. datasets: [{
  182. label: "' . $_config["label"] . '",
  183. data: [' . $sort["nb"] . '],
  184. backgroundColor: [' . $sort["backgroundColor"] . '],
  185. borderColor: [' . $sort["borderColor"] . '],
  186. borderWidth: 1
  187. }]
  188. }
  189. });
  190. </script>';
  191. }
  192. /**
  193. * Génère le script JavaScript pour un graphique de type "bar".
  194. *
  195. * @param array $_config Configuration du graphique.
  196. * @return string Script JavaScript pour le graphique.
  197. */
  198. private static function javascriptBar(array $_config)
  199. {
  200. $sort = self::constructData($_config["data"], $_config["config"]);
  201. return '<script>
  202. var ctx = document.getElementById("' . $_config["id"] . '");
  203. var myChart = new Chart(ctx, {
  204. type: "bar",
  205. data: {
  206. labels: [' . $sort["label"] . '],
  207. datasets: [{
  208. label: "' . $_config["label"] . '",
  209. data: [' . $sort["nb"] . '],
  210. backgroundColor: [' . $sort["backgroundColor"] . '],
  211. borderColor: [' . $sort["borderColor"] . '],
  212. borderWidth: 1
  213. }]
  214. }
  215. });
  216. </script>';
  217. }
  218. /**
  219. * Construit les données nécessaires pour un graphique de type "line".
  220. *
  221. * @param array $_data Données brutes pour le graphique.
  222. * @return array Données formatées pour le graphique.
  223. */
  224. private static function constructDataLine(array $_data)
  225. {
  226. $return["labels"] = $return["data"] = NULL;
  227. foreach ($_data[0] as $key => $value) {
  228. $return["labels"] .= '"' . $key . '", ';
  229. $return["data"] .= $value . ', ';
  230. }
  231. return $return;
  232. }
  233. /**
  234. * Génère le script JavaScript pour un graphique de type "line".
  235. *
  236. * @param array $_config Configuration du graphique.
  237. * @return string Script JavaScript pour le graphique.
  238. */
  239. private static function javascriptLine(array $_config)
  240. {
  241. $sort = self::constructDataLine($_config["data"]);
  242. return '<script>
  243. var ctx = document.getElementById("' . $_config["id"] . '");
  244. var myChart = new Chart(ctx, {
  245. type: "line",
  246. data: {
  247. "labels":[' . $sort["labels"] . '],
  248. datasets: [{
  249. "label":"' . $_config["label"] . '",
  250. "data":[' . $sort["data"] . '],
  251. "fill":false,
  252. "borderColor":"rgb(75, 192, 192)",
  253. "lineTension":0.1
  254. }],
  255. "options":{}
  256. }
  257. });
  258. </script>';
  259. }
  260. }