Javascript Yii2如何将php数组数据传递到highcharts
我正在对图表使用Yi2扩展,在将php数组传递到hightcharts时会感到困惑 我的数组值Javascript Yii2如何将php数组数据传递到highcharts,javascript,php,highcharts,yii2,Javascript,Php,Highcharts,Yii2,我正在对图表使用Yi2扩展,在将php数组传递到hightcharts时会感到困惑 我的数组值 Array ( [Power Electronics] => 14.00 [Introduction to Programming] => 3.92 [Data base Management System] => 3.28 [Object Oriented Analysis and Design] => 1.96 ) 现在,简单地说,我想将这
Array
(
[Power Electronics] => 14.00
[Introduction to Programming] => 3.92
[Data base Management System] => 3.28
[Object Oriented Analysis and Design] => 1.96
)
现在,简单地说,我想将这些数据添加到我的highcharts中,我在上面传递数组,就像下面的代码一样
来自我的海图代码
'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',
],
],
],
我已经尝试了很多东西,但没有得到任何成功,我想输出这样的结果。根据文档,您的数组的格式无效。它应该看起来像他的:
$data = [
['Power Electronics', 14.00],
['Introduction to Programming', 3.92],
['Data base Management System', 3.28],
['Object Oriented Analysis and Design', 1.96],
];
您可以使用简单的foreach解决此问题:
$newData = [];
foreach ($data as $key => $value) {
$newData[] = [$key, $value];
}
您是否尝试使用作为参数传递的数组调用
json\u encode
函数?代码如下:
<?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);
这个不适合我隐藏图表栏,在屏幕上什么也不显示。这个数组将由小部件编码(在PHP级别),如果在这里编码数据,字符串将作为
Highcharts.chart()
选项中的data
传递。我仍然不明白这一点。如果您有格式正确的数组,则不需要使用SeriesDataHelper
或手动编码为JSON。直接传递数组就足够了——Yii helper会将其编码为类似于的格式。请参阅该包装器的文档,在那里您可以找到许多关于该包装器的具体信息。
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',
],
],
],