【问题标题】:Chartjs - Loop thru dataset and assign colorsChartjs - 遍历数据集并分配颜色
【发布时间】:2018-05-02 12:47:58
【问题描述】:

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

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。
我现在需要知道如何根据条件定义颜色
例如如果 value > 2 那么 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));

【问题讨论】:

    标签: php arrays loops chart.js linechart


    【解决方案1】:
    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");
                    }
                }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-06-05
      • 1970-01-01
      • 1970-01-01
      • 2013-05-06
      • 1970-01-01
      • 2017-04-06
      相关资源
      最近更新 更多