Aliace
Aliace

Reputation: 23

Yii2 How to pass php array data into highcharts

I am using Yii2 extension miloschuman/yii2-highcharts for charts and get confused passing php array into hightcharts.

My Array Values

Array
(
    [Power Electronics] => 14.00
    [Introduction to Programming] => 3.92
    [Data base Management System] => 3.28
    [Object Oriented Analysis and Design] => 1.96
)

Now simply what I want to add this data to my highcharts I am passing above array like my below code.

FROM MY HIGHCHARTS CODE

'series' => [
    [
        "name" => "Exam Results",
        "data" => $course_data,
        'dataLabels' => [
            'enabled' => true,
            'rotation' => -90,
            'color' => '#FFFFFF',
            'align' => 'right',
            'format' => '{point.y:.1f}', // one decimal
            'y' => 10, // 10 pixels down from the top
            'style' => [],
            'fontSize' => '13px',
            'fontFamily' => 'Verdana, sans-serif',
        ],
    ],
],

I have already try many things but not get any success I wanted output like this charts.

Upvotes: 1

Views: 923

Answers (2)

daniel_s
daniel_s

Reputation: 3732

Have you tried to call json_encode function with mentioned array passed as an argument? Here is the code:

<?php
$data = [
    ['Power Electronics', 14.00],
    ['Introduction to Programming', 3.92],
    ['Data base Management System', 3.28],
    ['Object Oriented Analysis and Design', 1.96],
];

echo json_encode($data);

[EDIT]

You can also try to use SeriesDataHelper feature. There is information about using it in the documentation, here is the link: https://github.com/miloschuman/yii2-highcharts/blob/master/doc/examples/series-data-helper.md#using-numerically-indexed-data

Here is example code:

use miloschuman\highcharts\SeriesDataHelper;

$data = $data = [
    ['Power Electronics', 14.00],
    ['Introduction to Programming', 3.92],
    ['Data base Management System', 3.28],
    ['Object Oriented Analysis and Design', 1.96],
]

'series' => [
    [
        "name" => "Exam Results",
        "data" => new SeriesDataHelper($course_data, ['0:name', '1:y']),
        'dataLabels' => [
            'enabled' => true,
            'rotation' => -90,
            'color' => '#FFFFFF',
            'align' => 'right',
            'format' => '{point.y:.1f}', // one decimal
            'y' => 10, // 10 pixels down from the top
            'style' => [],
            'fontSize' => '13px',
            'fontFamily' => 'Verdana, sans-serif',
        ],
    ],
],

Upvotes: 1

rob006
rob006

Reputation: 22174

According to docs your array has invalid format. It should look like his:

$data = [
    ['Power Electronics', 14.00],
    ['Introduction to Programming', 3.92],
    ['Data base Management System', 3.28],
    ['Object Oriented Analysis and Design', 1.96],
];

You can fix this with simple foreach:

$newData = [];
foreach ($data as $key => $value) {
    $newData[] = [$key, $value];
}

Upvotes: 0

Related Questions