瀏覽代碼

Add getColorPalette() to ChamiloApi to return common array of colors

Yannick Warnier 6 年之前
父節點
當前提交
b378a73da6
共有 2 個文件被更改,包括 33 次插入12 次删除
  1. 4 12
      main/inc/ajax/statistics.ajax.php
  2. 29 0
      src/Chamilo/CoreBundle/Component/Utils/ChamiloApi.php

+ 4 - 12
main/inc/ajax/statistics.ajax.php

@@ -6,6 +6,8 @@
  */
  */
 require_once __DIR__.'/../global.inc.php';
 require_once __DIR__.'/../global.inc.php';
 
 
+use Chamilo\CoreBundle\Component\Utils\ChamiloApi;
+
 api_protect_admin_script();
 api_protect_admin_script();
 
 
 $action = isset($_REQUEST['a']) ? $_REQUEST['a'] : null;
 $action = isset($_REQUEST['a']) ? $_REQUEST['a'] : null;
@@ -154,17 +156,7 @@ switch ($action) {
             $list['labels'][] = $tick;
             $list['labels'][] = $tick;
         }
         }
 
 
-        // Get the common colors from the palette used for pchart
-        $paletteFile = api_get_path(SYS_CODE_PATH).'palettes/pchart/default.color';
-        $palette = file($paletteFile);
-        // Because the pchart palette has transparency as integer values
-        // (0..100) and chartjs uses percentage (0.0..1.0), we need to divide
-        // the last value by 100, which is a bit overboard for just one chart
-        foreach ($palette as $index => $color) {
-            $components = preg_split('/,/', trim($color));
-            $components[3] = round($components[3] / 100, 1);
-            $palette[$index] = join(',', $components);
-        }
+        $palette = ChamiloApi::getColorPalette(true, true);
 
 
         $list['datasets'][0]['label'] = get_lang('Tools');
         $list['datasets'][0]['label'] = get_lang('Tools');
         $list['datasets'][0]['borderColor'] = "rgba(255,255,255,1)";
         $list['datasets'][0]['borderColor'] = "rgba(255,255,255,1)";
@@ -173,7 +165,7 @@ switch ($action) {
         foreach ($all as $tick => $tock) {
         foreach ($all as $tick => $tock) {
             $j = $i % count($palette);
             $j = $i % count($palette);
             $list['datasets'][0]['data'][] = $tock;
             $list['datasets'][0]['data'][] = $tock;
-            $list['datasets'][0]['backgroundColor'][] = 'rgba('.$palette[$j].')';
+            $list['datasets'][0]['backgroundColor'][] = $palette[$j];
             $i++;
             $i++;
         }
         }
 
 

+ 29 - 0
src/Chamilo/CoreBundle/Component/Utils/ChamiloApi.php

@@ -289,4 +289,33 @@ class ChamiloApi
 
 
         return api_get_path(WEB_CSS_PATH).'editor_content.css';
         return api_get_path(WEB_CSS_PATH).'editor_content.css';
     }
     }
+    /**
+     * Get a list of colors from the palette at main/palette/pchart/default.color
+     * and return it as an array of strings
+     * @param bool $decimalOpacity Whether to return the opacity as 0..100 or 0..1
+     * @param bool $wrapInRGBA Whether to return it as 1,1,1,100 or rgba(1,1,1,100)
+     * @return array An array of string colors
+     */
+    public static function getColorPalette($decimalOpacity = false, $wrapInRGBA = false)
+    {
+        // Get the common colors from the palette used for pchart
+        $paletteFile = api_get_path(SYS_CODE_PATH).'palettes/pchart/default.color';
+        $palette = file($paletteFile);
+        if ($decimalOpacity) {
+            // Because the pchart palette has transparency as integer values
+            // (0..100) and chartjs uses percentage (0.0..1.0), we need to divide
+            // the last value by 100, which is a bit overboard for just one chart
+            foreach ($palette as $index => $color) {
+                $components = preg_split('/,/', trim($color));
+                $components[3] = round($components[3] / 100, 1);
+                $palette[$index] = join(',', $components);
+            }
+        }
+        if ($wrapInRGBA) {
+            foreach ($palette as $index => $color) {
+                $palette[$index] = 'rgba('.$palette[$index].')';
+            }
+        }
+        return $palette;
+    }
 }
 }