Jelajahi Sumber

Chart & debug & core

stany.ferer 3 bulan lalu
induk
melakukan
a404c0fe45

+ 90 - 52
core/class/chart.class.php

@@ -12,6 +12,12 @@ class chart
         $width = (isset($_config["width"])) ? $_config["width"] : 600;
         $height = (isset($_config["height"])) ? $_config["height"] : 400;
 
+        if(!empty($_config["config"])){
+            $_config["config"] = core::extractArrayInArray($_config["config"]);
+        } else {
+            $_config["config"] = NULL;
+        }
+
         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"]) {
@@ -24,64 +30,72 @@ class chart
             case "line":
                 echo self::javascriptLine($_config);
                 break;
+            case "pie":
+                echo self::javascriptPie($_config);
+                break;
             default:
                 break;
         }
     }
 
-    private static function constructData(array $_data)
+    private static function constructData(array $_data, ?array $_config = NULL)
     {
         $label = $nb = $backgroundColor = $borderColor = "";
-        $opacity = 0.5;
         $nbData = count($_data);
 
-        
-        $data["backgroundColor"] = array(
-            "rgba(242, 84, 84, 0.6)",
-            "rgba(242, 132, 84, 0.6)",
-            "rgba(242, 179, 84, 0.6)",
-            "rgba(242, 226, 84, 0.6)",
-            "rgba(210, 242, 84, 0.6)",
-            "rgba(163, 242, 84, 0.6)",
-            "rgba(116, 242, 84, 0.6)",
-            "rgba(84, 242, 100, 0.6)",
-            "rgba(84, 242, 147, 0.6)",
-            "rgba(84, 242, 195, 0.6)",
-            "rgba(84, 242, 242, 0.6)",
-            "rgba(84, 195, 242, 0.6)",
-            "rgba(84, 147, 242, 0.6)",
-            "rgba(84, 100, 242, 0.6)",
-            "rgba(116, 84, 242, 0.6)",
-            "rgba(163, 84, 242, 0.6)",
-            "rgba(210, 84, 242, 0.6)",
-            "rgba(242, 84, 226, 0.6)",
-            "rgba(242, 84, 179, 0.6)",
-            "rgba(242, 84, 132, 0.6)"
-        );
-
-
-        $data["borderColor"] = array(
-            "rgba(242, 84, 84, 1.0)",
-            "rgba(242, 132, 84, 1.0)",
-            "rgba(242, 179, 84, 1.0)",
-            "rgba(242, 226, 84, 1.0)",
-            "rgba(210, 242, 84, 1.0)",
-            "rgba(163, 242, 84, 1.0)",
-            "rgba(116, 242, 84, 1.0)",
-            "rgba(84, 242, 100, 1.0)",
-            "rgba(84, 242, 147, 1.0)",
-            "rgba(84, 242, 195, 1.0)",
-            "rgba(84, 242, 242, 1.0)",
-            "rgba(84, 195, 242, 1.0)",
-            "rgba(84, 147, 242, 1.0)",
-            "rgba(84, 100, 242, 1.0)",
-            "rgba(116, 84, 242, 1.0)",
-            "rgba(163, 84, 242, 1.0)",
-            "rgba(210, 84, 242, 1.0)",
-            "rgba(242, 84, 226, 1.0)",
-            "rgba(242, 84, 179, 1.0)",
-            "rgba(242, 84, 132, 1.0)"
-        );
+        if(empty($_config["backgroundColor"])){
+            $data["backgroundColor"] = array(
+                "rgba(242, 84, 84, 0.6)",
+                "rgba(242, 132, 84, 0.6)",
+                "rgba(242, 179, 84, 0.6)",
+                "rgba(242, 226, 84, 0.6)",
+                "rgba(210, 242, 84, 0.6)",
+                "rgba(163, 242, 84, 0.6)",
+                "rgba(116, 242, 84, 0.6)",
+                "rgba(84, 242, 100, 0.6)",
+                "rgba(84, 242, 147, 0.6)",
+                "rgba(84, 242, 195, 0.6)",
+                "rgba(84, 242, 242, 0.6)",
+                "rgba(84, 195, 242, 0.6)",
+                "rgba(84, 147, 242, 0.6)",
+                "rgba(84, 100, 242, 0.6)",
+                "rgba(116, 84, 242, 0.6)",
+                "rgba(163, 84, 242, 0.6)",
+                "rgba(210, 84, 242, 0.6)",
+                "rgba(242, 84, 226, 0.6)",
+                "rgba(242, 84, 179, 0.6)",
+                "rgba(242, 84, 132, 0.6)"
+            );
+        } else {
+            $data["backgroundColor"] = $_config["backgroundColor"];
+        }
+
+        if(empty($_config["borderColor"])){
+            $data["borderColor"] = array(
+                "rgba(242, 84, 84, 1.0)",
+                "rgba(242, 132, 84, 1.0)",
+                "rgba(242, 179, 84, 1.0)",
+                "rgba(242, 226, 84, 1.0)",
+                "rgba(210, 242, 84, 1.0)",
+                "rgba(163, 242, 84, 1.0)",
+                "rgba(116, 242, 84, 1.0)",
+                "rgba(84, 242, 100, 1.0)",
+                "rgba(84, 242, 147, 1.0)",
+                "rgba(84, 242, 195, 1.0)",
+                "rgba(84, 242, 242, 1.0)",
+                "rgba(84, 195, 242, 1.0)",
+                "rgba(84, 147, 242, 1.0)",
+                "rgba(84, 100, 242, 1.0)",
+                "rgba(116, 84, 242, 1.0)",
+                "rgba(163, 84, 242, 1.0)",
+                "rgba(210, 84, 242, 1.0)",
+                "rgba(242, 84, 226, 1.0)",
+                "rgba(242, 84, 179, 1.0)",
+                "rgba(242, 84, 132, 1.0)"
+            );
+        } else {
+            $data["borderColor"] = $_config["borderColor"];
+        }
 
         for ($i = 0; $i < $nbData; $i++) {
             $backgroundColor .= '"' . $data["backgroundColor"][$i] . '"';
@@ -98,7 +112,7 @@ class chart
         }
 
         for ($i = 0; $i < $nbData; $i++) {
-            $label .= '"' . $_data[$i]["label"] . '"';
+            $label .= ($_data[$i]["nb"] > 0) ? '"' . $_data[$i]["label"] . '"' : '"Aucun(e)s ' . $_data[$i]["label"] . '"';
             if ($i != $nbData) {
                 $label .= ", ";
             }
@@ -114,10 +128,33 @@ class chart
         return array("label" => $label, "nb" => $nb, "backgroundColor" => $backgroundColor, "borderColor" => $borderColor);
     }
 
+    private static function javascriptPie(array $_config)
+    {
+
+        $sort = self::constructData($_config["data"], $_config["config"]);
+
+        return '<script>
+                var ctx = document.getElementById("' . $_config["id"] . '");
+                var myChart = new Chart(ctx, {
+                    type: "pie",
+                    data: {
+                        labels: [' . $sort["label"] . '],
+                        datasets: [{
+                            label: "' . $_config["label"] . '",
+                            data: [' . $sort["nb"] . '],
+                            backgroundColor: [' . $sort["backgroundColor"] . '],
+                            borderColor: [' . $sort["borderColor"] . '],
+                            borderWidth: 1
+                        }]
+                    }
+                });
+            </script>';
+    }
+
     private static function javascriptDoughnut(array $_config)
     {
 
-        $sort = self::constructData($_config["data"]);
+        $sort = self::constructData($_config["data"], $_config["config"]);
 
         return '<script>
                 var ctx = document.getElementById("' . $_config["id"] . '");
@@ -140,7 +177,7 @@ class chart
     private static function javascriptBar(array $_config)
     {
 
-        $sort = self::constructData($_config["data"]);
+        $sort = self::constructData($_config["data"], $_config["config"]);
 
         return '<script>
                 var ctx = document.getElementById("' . $_config["id"] . '");
@@ -193,4 +230,5 @@ class chart
                 });
             </script>';
     }
+
 }

+ 14 - 0
core/class/core.class.php

@@ -509,4 +509,18 @@ class core
     
         return $ip;
     }
+
+    static public function extractArrayInArray(?array $_array = NULL){
+        if($_array != NULL){
+            foreach ($_array as $item) {
+                foreach ($item as $key => $value) {
+                    $flattened[$key] = $value;
+                }
+            }
+        } else {
+            $flattened = NULL;
+        }
+        
+        return $flattened;
+    }
 }

+ 9 - 4
core/class/debug.class.php

@@ -156,12 +156,17 @@ class debug
         return $return;
     }
 
-    public static function print_r(array $_array, ?int $_exit = NULL)
+    public static function print_r(?array $_array = NULL, ?int $_exit = NULL)
     {
         echo "<div>".debug::getTraces() . "</div>";
-        echo "<pre>";
-        print_r($_array);
-        echo "</pre>";
+
+        if(empty($_array)){
+            echo "<pre>EMPTY</pre>";
+        } else {
+            echo "<pre>";
+            print_r($_array);
+            echo "</pre>";
+        }
         ($_exit != NULL) ? exit() : NULL;
     }
 

+ 5 - 3
core/views/pages/cms.stats-an-entree.php

@@ -1,6 +1,6 @@
-<div class="row" style="margin-top: 20x;">
+<div class="row" style="margin-top: 20px;">
     
-    <div class="col-md-10" style="margin-top: 30px;">
+    <div class="col-md-5" style="margin-top: 30px;">
         <h6 class="bg-light p-2">Général</h6>
         <?php
         $config = array(
@@ -13,7 +13,9 @@
         chart::printCanvas($config);
         ?>
     </div>
-    
+
+<div class="w-100"></div>
+
 <?php
     foreach ($statGeneral["Lieux"] as $keyLieu => $valueLieu) {
         $premiereValeur = reset($valueLieu["EntreesParAnnee"]);

+ 17 - 7
core/views/pages/cms.stats-contrats.php

@@ -1,21 +1,27 @@
-<div class="row" style="margin-top: 20x;">
+<div class="row" style="margin-top: 20px;">
     
-    <div class="col-md-10" style="margin-top: 30px;">
+    <div class="col-md-5" style="margin-top: 30px;">
         <h6 class="bg-light p-2">Général</h6>
         <?php
         $config = array(
             "id" => "ContratActif",
             "charts" => "doughnut",
-            "label" => "Général",
+            "label" => "doughnut",
             "data" => array(
                 array("label" => "Contrats actifs", "nb" => $statGeneral["ContratOn"]),
-                array("label" => "Contrats en arrêt", "nb" => $statGeneral["ContratOff"]))
-        );
+                array("label" => "Contrats en arrêt", "nb" => $statGeneral["ContratOff"])
+            ),
+            "config" => array(
+                    array("backgroundColor" => array( "rgba(84, 242, 147, 0.6)", "rgba(242, 84, 84, 0.6)")),
+                    array("borderColor" => array( "rgba(84, 242, 147, 1.0)", "rgba(242, 84, 84, 1.0)"))
+            )
+            );
 
         chart::printCanvas($config);
         ?>  
     </div>
 
+<div class="w-100"></div>
 
 <?php
     foreach ($statGeneral["Lieux"] as $keyLieu => $valueLieu) {
@@ -32,8 +38,12 @@
             "label" => $keyLieu,
             "data" => array(
                 array("label" => "Contrats actifs", "nb" => $valueLieu["ContratOn"]),
-                array("label" => "Contrats en arrêt", "nb" => $valueLieu["ContratOff"]))
-        );
+                array("label" => "Contrats en arrêt", "nb" => $valueLieu["ContratOff"])),
+            "config" => array(
+                    array("backgroundColor" => array( "rgba(84, 242, 147, 0.6)", "rgba(242, 84, 84, 0.6)")),
+                    array("borderColor" => array( "rgba(84, 242, 147, 1.0)", "rgba(242, 84, 84, 1.0)"))
+            )
+            );
 
         chart::printCanvas($config);
         ?>  

+ 12 - 2
core/views/pages/cms.stats-femmes-hommes.php

@@ -1,6 +1,6 @@
-<div class="row" style="margin-top: 20x;">
+<div class="row" style="margin-top: 20px;">
     
-    <div class="col-md-10" style="margin-top: 30px;">
+    <div class="col-md-5" style="margin-top: 30px;">
         <h6 class="bg-light p-2">Général</h6>
         <?php
         $config = array(
@@ -10,6 +10,10 @@
             "data" => array(
                 array("label" => "Femme", "nb" => $statGeneral["Femme"]),
                 array("label" => "Homme", "nb" => $statGeneral["Homme"])
+            ),
+            "config" => array(
+                    array("backgroundColor" => array( "rgba(163, 84, 242, 0.6)", "rgba(84, 195, 242, 0.6)")),
+                    array("borderColor" => array( "rgba(163, 84, 242, 1.0)", "rgba(84, 195, 242, 1.0)"))
             )
         );
 
@@ -17,6 +21,8 @@
         ?>  
     </div>
 
+    <div class="w-100"></div>
+
     <?php
     foreach ($statGeneral["Lieux"] as $keyLieu => $valueLieu) {
         if($valueLieu["Femme"] > 0 OR $valueLieu["Homme"] > 0){
@@ -32,6 +38,10 @@
             "data" => array(
                 array("label" => "Femme", "nb" => $valueLieu["Femme"]),
                 array("label" => "Homme", "nb" => $valueLieu["Homme"])
+            ),
+            "config" => array(
+                    array("backgroundColor" => array( "rgba(163, 84, 242, 0.6)", "rgba(84, 195, 242, 0.6)")),
+                    array("borderColor" => array( "rgba(163, 84, 242, 1.0)", "rgba(84, 195, 242, 1.0)"))
             )
         );
 

+ 1 - 1
core/views/pages/cms.stats-general.php

@@ -1,4 +1,4 @@
-<div class="row" style="margin-top: 20x;">
+<div class="row" style="margin-top: 20px;">
     <div class="col-md-10" style="margin-top: 30px;">
         <h6 class="bg-light p-2">Général</h6>
         <table  id="table" 

+ 12 - 2
core/views/pages/cms.stats-tous-sites.php

@@ -1,6 +1,6 @@
 <div class="row" style="margin-top: 20px;">
 
-    <div class="col-md-10" style="margin-top: 10px;">
+    <div class="col-md-5" style="margin-top: 10px;">
         <h6 class="bg-light p-2">Nb. salariés par site</h6>
         <?php
 
@@ -25,7 +25,7 @@
         ?>
     </div>
 
-    <div class="col-md-10" style="margin-top: 30px;">
+    <div class="col-md-5" style="margin-top: 30px;">
         <h6 class="bg-light p-2">Années d'entrée</h6>
         <?php
         $config = array(
@@ -49,12 +49,17 @@
             "data" => array(
                 array("label" => "Femme", "nb" => $statGeneral["Femme"]),
                 array("label" => "Homme", "nb" => $statGeneral["Homme"])
+            ),
+            "config" => array(
+                    array("backgroundColor" => array( "rgba(163, 84, 242, 0.6)", "rgba(84, 195, 242, 0.6)")),
+                    array("borderColor" => array( "rgba(163, 84, 242, 1.0)", "rgba(84, 195, 242, 1.0)"))
             )
         );
 
         chart::printCanvas($config);
         ?>
     </div>
+
     <div class="col-md-5" style="margin-top: 30px;">
         <h6 class="bg-light p-2">Contrats actifs et non actifs</h6>
         <?php
@@ -65,10 +70,15 @@
             "data" => array(
                 array("label" => "Contrats actifs", "nb" => $statGeneral["ContratOn"]),
                 array("label" => "Contrats en arrêt", "nb" => $statGeneral["ContratOff"])
+            ),
+            "config" => array(
+                    array("backgroundColor" => array( "rgba(84, 242, 147, 0.6)", "rgba(242, 84, 84, 0.6)")),
+                    array("borderColor" => array( "rgba(84, 242, 147, 1.0)", "rgba(242, 84, 84, 1.0)"))
             )
         );
 
         chart::printCanvas($config);
         ?>
     </div>
+
 </div>