Chartjs-遍历数据集并分配颜色 [英] Chartjs - Loop thru dataset and assign colors

查看:247
本文介绍了Chartjs-遍历数据集并分配颜色的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想根据数据值在折线图中分配pointBackgroundColor. 我有这个数组(示例):

I want to assign pointBackgroundColor in a line-chart depending on the value of the data. I have this array (example) :

Array ( [0] => 7 [1] => 2 [2] => 5 [3] => 6 [4] => 5 [5] => 6 [6] => 7 [7] => 5 [8] => 6 [9] => 4 [10] => 1 [11] => 2 [12] => 7 [13] => 1 [14] => 2 [15] => 1 [16] => 2 )

我将intColors初始化为颜色数组.
我在$ arrDataSets中放入了用于构建图表的参数.
我现在需要知道如何根据条件定义颜色
例如,如果值> 2,则pointBackgroundColor = green?

I initialized intColors as an array of colors.
I have put into $arrDataSets the parameters to build the chart.
I now need to know how to define the colors depending on conditions
For example if value > 2 then pointBackgroundColor = green ?

$intColors = array("#82f827", "#ff4040", "#31698A", "#6666FF","#ff7F50","#fe6b60","#6c1ba1","#97bdd6");
foreach($datasetR1 as $value){

            }

            $arrDatasets = array(
                array('label' => "event_name",
                      'fill' => false,
                      'showLine' => false,
                      'pointBackgroundColor' => $intColors,
                      'data' => $datasetR1
                      ));
            $arrReturn = (array('labels' => $labels, 
                    'datasets' => $arrDatasets));
            $mydata = json_encode(($arrReturn));

推荐答案

foreach($datasetR1 as $value){
                if($value == 1){
                    array_push($intColors, "#82f827");
                }
                elseif($value == 2){
                    array_push($intColors, "#ff4040");
                }
                elseif($value == 3){
                    array_push($intColors, "#31698A");
                }
                elseif($value == 4){
                    array_push($intColors, "#6666FF");
                }
                elseif($value == 5){
                    array_push($intColors, "#ff7F50");
                }
                elseif($value == 6){
                    array_push($intColors, "#fe6b60");
                }
                elseif($value == 7){
                    array_push($intColors, "#6c1ba1");
                }
                elseif($value == 8){
                    array_push($intColors, "#97bdd6");
                }
            }

这篇关于Chartjs-遍历数据集并分配颜色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆