From bec38a5848d8e10be95dae198b2b3f7a52ed331a Mon Sep 17 00:00:00 2001 From: Yannick Warnier Date: Fri, 2 Nov 2018 18:12:03 -0500 Subject: [PATCH 1/2] Add getColorPalette() to ChamiloApi to return common array of colors --- main/inc/ajax/statistics.ajax.php | 2 ++ src/CoreBundle/Component/Utils/ChamiloApi.php | 29 +++++++++++++++++++ 2 files changed, 31 insertions(+) diff --git a/main/inc/ajax/statistics.ajax.php b/main/inc/ajax/statistics.ajax.php index eedec70394..b3299167be 100644 --- a/main/inc/ajax/statistics.ajax.php +++ b/main/inc/ajax/statistics.ajax.php @@ -8,6 +8,8 @@ use Chamilo\CoreBundle\Component\Utils\ChamiloApi; */ require_once __DIR__.'/../global.inc.php'; +use Chamilo\CoreBundle\Component\Utils\ChamiloApi; + api_protect_admin_script(); $action = isset($_REQUEST['a']) ? $_REQUEST['a'] : null; diff --git a/src/CoreBundle/Component/Utils/ChamiloApi.php b/src/CoreBundle/Component/Utils/ChamiloApi.php index 3f30a2737a..076379a37f 100644 --- a/src/CoreBundle/Component/Utils/ChamiloApi.php +++ b/src/CoreBundle/Component/Utils/ChamiloApi.php @@ -289,4 +289,33 @@ class ChamiloApi 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; + } } From 253f31198fa2a75cf84ac74a43dd4dda10c7976d Mon Sep 17 00:00:00 2001 From: Yannick Warnier Date: Fri, 2 Nov 2018 18:28:02 -0500 Subject: [PATCH 2/2] Add fillUpTo option to getColorPalette() to enable more colors - refs #2717 --- src/CoreBundle/Component/Utils/ChamiloApi.php | 15 +++++++++++++-- 1 file changed, 13 insertions(+), 2 deletions(-) diff --git a/src/CoreBundle/Component/Utils/ChamiloApi.php b/src/CoreBundle/Component/Utils/ChamiloApi.php index 076379a37f..df161994b2 100644 --- a/src/CoreBundle/Component/Utils/ChamiloApi.php +++ b/src/CoreBundle/Component/Utils/ChamiloApi.php @@ -294,10 +294,14 @@ class ChamiloApi * 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) + * @param int $fillUpTo If the number of colors is smaller than this number, generate more colors * @return array An array of string colors */ - public static function getColorPalette($decimalOpacity = false, $wrapInRGBA = false) - { + public static function getColorPalette( + $decimalOpacity = false, + $wrapInRGBA = false, + $fillUpTo = null + ) { // Get the common colors from the palette used for pchart $paletteFile = api_get_path(SYS_CODE_PATH).'palettes/pchart/default.color'; $palette = file($paletteFile); @@ -316,6 +320,13 @@ class ChamiloApi $palette[$index] = 'rgba('.$palette[$index].')'; } } + // If we want more colors, loop through existing colors + $count = count($palette); + if (isset($fillUpTo) && $fillUpTo > $count) { + for ($i = $count; $i < $fillUpTo; $i++) { + $palette[$i] = $palette[$i%$count]; + } + } return $palette; } }