简体   繁体   English

将Google Charts API与PHP值结合使用

[英]Using Google Charts API with PHP-sourced values

I'm trying to pull data from PHP arrays and then feeding them into an XY Line Chart using Google Charts API (by converting the data to JSON first). 我正在尝试从PHP数组中提取数据,然后使用Google Charts API(通过先将数据转换为JSON)将其馈入XY折线图。

First, I have my two arrays of X and Y values in PHP: 首先,我在PHP中有两个X和Y值数组:

$hit = array(1,2,3);
$hit2 = array(3,4,5);

I then have an array that interlocks the array values (like a zipper): 然后,我得到了一个与数组值互锁的数组(如拉链):

$count = count($hit);
for ($i = 0; $i < $count; $i++) {
$subArray[] = '[' . $hit[$i];
$subArray[] = $hit2[$i] . ']';
$superArray = array($subArray);

I then converted this to JSON by: 然后,我通过以下方式将其转换为JSON:

var num = '[' + <?php echo json_encode(array_values($superArray)); ?> + ']';

And I then included this in my Google chart by adding the following line: 然后,通过添加以下行,将其添加到我的Google图表中:

data.addRows(num);

However this is not rendering. 但是,这不是渲染。

var num returns: var num返回:

[[1,3],[2,4],[3,5]]

...which is definitely the 'right' format for the Google Chart I'm trying to produce. ...这绝对是我要制作的Google图表的“正确”格式。

Test: If I manually test by replacing that line with: 测试:如果我通过以下方式手动测试该行:

data.addRows([[1,3],[2,4],[3,5]]);

then it works perfectly, but not when it's 那么它会完美地工作,但不是

data.addRows(num);

Any thoughts, please? 有什么想法吗? :) :)

Thank you. 谢谢。

If you do console.log(num) I guess it's returning a string? 如果您执行console.log(num)我想它正在返回一个字符串? Try adding JSON.Parse to convert your JSON-string to a javascript object. 尝试添加JSON.Parse将您的JSON字符串转换为javascript对象。

var num = JSON.Parse('<?php echo json_encode(array_values($superArray)); ?>');

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM