2amigos/yii2-chartjs-widget

Want you add it?

shahovkit opened this issue · 5 comments

   public function colorsRand($data){

        $keys = array_keys($data);

        for( $i=0 ; $i < count($keys) ; $i++ )
        {
            $keys[$i] = "rgb(" . rand(100, 200) . "," . rand(100, 200) . "," . rand(100, 200) . ")";
        }
        return $keys;
    }

its randomization if no color init. $data - is array values

ChartJs::widget([
            'type' => 'pie',
            'options' => [
                'id' => 'mychart',
                'class' => 'chartContainer-'.Yii::$app->security->generateRandomString(rand(5,10)),
                'height' => 100,
                'width' => 200
            ],
            'data' => [
                'labels' => $labels,
                'datasets' => [
                    [
                        'label' => "My First dataset",
                        'backgroundColor' => $this->colorsRand($data),
                        'data' => $data
                    ],
                ]
            ]

        ]);

Cool! One small thing, we should provide the mechanism to not repeat colors.

    public function colorsRand($data){


        $keys = array_keys($data);

        for( $i=0 ; $i < count($keys) ; $i++ )
        {
            while (true) {
                $color = "rgb(" . rand(100, 200) . "," . rand(100, 200) . "," . rand(100, 200) . ")";
                
                if (!(array_search($color, $keys))) {
                    
                    $keys[$i] = $color;
                    break;
                    
                }
            }
        }
        return $keys;
    }

but, its worked on colors rgb(255,255,255) and rgb(255,255,254), its very similar colours

Need add verification color in ~20 range on every property(R,G,B)

        foreach ($data as $key => &$val){
            $val['borderColor'] = $this->colorsRand($data)[$key];
        }

for line chart, default clors if didn't set colors

foreach ($data as $key => &$val){
            $val['borderColor'] = $this->colorsRand($data)[$key];
        }

ChartJs::widget([
            'type' => 'line',
            'options' => [
                'id' => 'mychart',
            ],
            'data' => [
                'datasets' => $data,
            ],
            'clientOptions' => [
                'title'=> [
                    'display' => true,
                    'text' => 'Chart.js Time Scale'
                ],
                'scales' => [
                    'xAxes' => [[
                        'type' => 'time',
                        'time'=>[
                            'tooltipFormat'=>'ll HH:mm',
                        ],
                    ]],
                ]
            ]
        ]);

Thanks @shahovkit currently I have no time at all... will add it soon