简体   繁体   中英

Google Chart - Incorrect Data Array Format in PHP

I'm trying to display a monthly sales graph but unable to format data array for google chart function. Here's what I have:

STEP 1: Array formed from resultset:

Array ( [Jan] => 0 [Feb] => 0 [Mar] => 0 [Apr] => 0 [May] => 0 [Jun] => 0 [Jul] => 0 [Aug] => 0 [Sep] => 0 [Oct] => 60.123 [Nov] => 0 [Dec] => 0 ) 

STEP 2: JSON encode:

$chartdata = json_encode($data);

STEP 3: Load Google Chart Function:

var data = google.visualization.arrayToDataTable($chartdata, true);

But it doesn't display any chart. Any suggestions?

Google API: https://google-developers.appspot.com/chart/interactive/docs/datatables_dataviews

This doesn't work because the arrayToDataTable method does not accept an object in that format. This should work:

In PHP:

$data = array (
    array('Month', 'Sales'),
    array('Jan', 0),
    array('Feb', 0),
    array('Mar', 0),
    array('Apr', 0),
    array('May', 0),
    array('Jun', 0),
    array('Jul', 0),
    array('Aug', 0),
    array('Sep', 0),
    array('Oct', 60.123),
    array('Nov', 0),
    array('Dec', 0)
);
$chartdata = json_encode($data);

In javascript:

var data = google.visualization.arrayToDataTable(<?php echo $chartdata; ?>);

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