简体   繁体   English

Google图表-PHP中的数据数组格式不正确

[英]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. 我正在尝试显示每月的销售图,但无法为Google图表功能格式化数据数组。 Here's what I have: 这是我所拥有的:

STEP 1: Array formed from resultset: 步骤1:根据结果集形成数组:

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: 步骤2:JSON编码:

$chartdata = json_encode($data);

STEP 3: Load Google Chart Function: 步骤3:载入Google Chart功能:

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 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. 这不起作用,因为arrayToDataTable方法不接受该格式的对象。 This should work: 这应该工作:

In PHP: 在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: 在javascript中:

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

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

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