| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166 |
- <?php
- // Documentation
- // https://www.chartjs.org/docs/2.6.0/
- class chart
- {
- public static function printCanvas(array $_config)
- {
- $id = $_config["id"];
- $width = (isset($_config["width"])) ? $_config["width"] : 600;
- $height = (isset($_config["height"])) ? $_config["height"] : 400;
- echo '<canvas id="' . $id . '" width="' . $width . '" height="' . $height . '" style="display: block; width: ' . $width . 'px; height: ' . $height . 'px;" class="chartjs-render-monitor"></canvas>';
- switch ($_config["charts"]) {
- case "doughnut":
- echo self::javascriptDoughnut($_config);
- break;
- case "bar":
- echo self::javascriptBar($_config);
- break;
- case "line":
- echo self::javascriptLine($_config);
- break;
- default:
- break;
- }
- }
- private static function constructData(array $_data)
- {
- $label = $nb = $backgroundColor = $borderColor = "";
- $opacity = 0.5;
- $nbData = count($_data);
- $data["backgroundColor"] = array(
- "rgba(255, 99, 132, " . $opacity . ")", // Red
- "rgba(54, 162, 235, " . $opacity . ")", // Blue
- "rgba(255, 206, 86, " . $opacity . ")", // Yellow
- "rgba(75, 192, 192, " . $opacity . ")", // Green,
- "rgba(153, 102, 255, " . $opacity . ")", // Purple
- "rgba(255, 159, 64, " . $opacity . ")" // Orange
- );
- $data["borderColor"] = array(
- "rgba(255,99,132,1)", // Red
- "rgba(54, 162, 235, 1)", // Blue
- "rgba(255, 206, 86, 1)", // Yellow
- "rgba(75, 192, 192, 1)", // Green
- "rgba(153, 102, 255, 1)", // Purple
- "rgba(255, 159, 64, 1)" // Orange
- );
- for ($i = 0; $i < $nbData; $i++) {
- $backgroundColor .= '"' . $data["backgroundColor"][$i] . '"';
- if ($i != $nbData) {
- $backgroundColor .= ", ";
- }
- }
- for ($i = 0; $i < $nbData; $i++) {
- $borderColor .= '"' . $data["borderColor"][$i] . '"';
- if ($i != $nbData) {
- $borderColor .= ", ";
- }
- }
- for ($i = 0; $i < $nbData; $i++) {
- $label .= '"' . $_data[$i]["label"] . '"';
- if ($i != $nbData) {
- $label .= ", ";
- }
- }
- for ($i = 0; $i < $nbData; $i++) {
- $nb .= '"' . $_data[$i]["nb"] . '"';
- if ($i != $nbData) {
- $nb .= ", ";
- }
- }
- return array("label" => $label, "nb" => $nb, "backgroundColor" => $backgroundColor, "borderColor" => $borderColor);
- }
- private static function javascriptDoughnut(array $_config)
- {
- $sort = self::constructData($_config["data"]);
- return '<script>
- var ctx = document.getElementById("' . $_config["id"] . '");
- var myChart = new Chart(ctx, {
- type: "doughnut",
- data: {
- labels: [' . $sort["label"] . '],
- datasets: [{
- label: "' . $_config["label"] . '",
- data: [' . $sort["nb"] . '],
- backgroundColor: [' . $sort["backgroundColor"] . '],
- borderColor: [' . $sort["borderColor"] . '],
- borderWidth: 1
- }]
- }
- });
- </script>';
- }
- private static function javascriptBar(array $_config)
- {
- $sort = self::constructData($_config["data"]);
- return '<script>
- var ctx = document.getElementById("' . $_config["id"] . '");
- var myChart = new Chart(ctx, {
- type: "bar",
- data: {
- labels: [' . $sort["label"] . '],
- datasets: [{
- label: "' . $_config["label"] . '",
- data: [' . $sort["nb"] . '],
- backgroundColor: [' . $sort["backgroundColor"] . '],
- borderColor: [' . $sort["borderColor"] . '],
- borderWidth: 1
- }]
- }
- });
- </script>';
- }
- private static function constructDataLine(array $_data)
- {
- $return["labels"] = $return["data"] = NULL;
- foreach ($_data[0] as $key => $value) {
- $return["labels"] .= '"' . $key . '", ';
- $return["data"] .= $value . ', ';
- }
- return $return;
- }
- private static function javascriptLine(array $_config)
- {
- $sort = self::constructDataLine($_config["data"]);
- return '<script>
- var ctx = document.getElementById("' . $_config["id"] . '");
- var myChart = new Chart(ctx, {
- type: "line",
- data: {
- "labels":[' . $sort["labels"] . '],
- datasets: [{
- "label":"' . $_config["label"] . '",
- "data":[' . $sort["data"] . '],
- "fill":false,
- "borderColor":"rgb(75, 192, 192)",
- "lineTension":0.1
- }],
- "options":{}
- }
- });
- </script>';
- }
- }
|