miloschuman / yii2-highcharts

Highcharts widget for Yii 2 Framework
http://www.yiiframework.com/extension/yii2-highcharts-widget/
MIT License
166 stars 63 forks source link

Yii2 and 'pie' type chart #9

Closed meksikanos closed 9 years ago

meksikanos commented 9 years ago

Hi. I'm trying to run an example code to display 'pie' chart in my Yii2 view file.

     echo Highcharts::widget([
         'options' => [
            'title' => ['text' => 'Sample title - pie chart'],
            'plotOptions' => [
                'pie' => [
                    'cursor' => 'pointer',
                ],
            ],
            'series' => [
                'type'=>'pie',                                                             
                'name'=>'Elements',
                'data' => [
                    [
                        'name' => 'Jane',
                        'y' => 13,
                    ],
                    [
                        'name' => 'John',
                        'y' => 23,
                    ],
                    [
                        'name' => 'Joe',
                        'y' => 19,
                    ],
                ],
            ],
         ],
      ]);

Above code renders no chart at all, just title with empty space. I installed the latest version using composer.

Even for another set of data the result is the same - no chart rendered.

            'series' => [
                'type'=>'pie',                                                             
                'name'=>'Elements',
                'data' => [
                            ['Firefox',   45.0],
                            ['IE',       26.8],
                            ['Safari',    8.5],
                            ['Opera',     6.2],
                            ['Others',   0.7]
                ],
            ],

In Mozilla Firebug there is no JS errors at all in console.

Btw. Bar type chart is rendering ok. I'm using following code.

     echo Highcharts::widget([

        'scripts' => [
            'highcharts-3d',   
         ],         

         'options' => [
            'title' => ['text' => 'Elements count per type'],
            'credits' => ['enabled' => false],
            'exporting' => ['enabled' => true],
            'chart' => [
                'type' => 'column',

                'options3d'=>[
                    'enabled'=>true,
                    'alpha'=>15,
                    'beta'=>10,
                ],                  
            ],
            'xAxis' => [
               'categories' => ['Actual state label']
            ],
            'yAxis' => [
               'title' => ['text' => 'Amount']
            ],
            'series' => $barYSeries, //data array passed from controller
         ]
      ]);
miloschuman commented 9 years ago

The series option requires an array of arrays. Just wrap your single series in another set of brackets like this:

echo Highcharts::widget([
    'options' => [
        'title' => ['text' => 'Sample title - pie chart'],
        'plotOptions' => [
            'pie' => [
                'cursor' => 'pointer',
            ],
        ],
        'series' => [
            [ // new opening bracket
                'type' => 'pie',
                'name' => 'Elements',
                'data' => [
                    ['Firefox', 45.0],
                    ['IE', 26.8],
                    ['Safari', 8.5],
                    ['Opera', 6.2],
                    ['Others', 0.7]
                ],
            ] // new closing bracket
        ],
    ],
]);

It's easily overlooked if you have only one series because the word is both singular and plural.

cosoftmoh commented 6 years ago

my data array passed from controller $ data_name, $ data_numbre, how should I place? thank you

cosoftmoh commented 6 years ago

how to replace this 'data' => [ ['Firefox', 45.0], ['IE', 26.8], ['Safari', 8.5], ['Opera', 6.2], ['Others', 0.7] ],

by this [$data_name , $data_numbre ]
thancks

gpacheco67 commented 4 years ago

I have the same question.

how to replace this 'data' => [ ['Firefox', 45.0], ['IE', 26.8], ['Safari', 8.5], ['Opera', 6.2], ['Others', 0.7] ],

by this [$data_name , $data_number ] thanks

dmmabiria commented 3 years ago

Anyone found a solution to this?