简体   繁体   中英

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).

First, I have my two arrays of X and Y values in PHP:

$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:

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

And I then included this in my Google chart by adding the following line:

data.addRows(num);

However this is not rendering.

var num returns:

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

...which is definitely the 'right' format for the Google Chart I'm trying to produce.

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? Try adding JSON.Parse to convert your JSON-string to a javascript object.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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