无法读取未定义图表谷歌的属性'1'

时间:2014-10-13 10:13:47

标签: javascript php ajax json

我阅读了旧帖子,但我找不到解决问题的方法。

我有这个代码(服务器端)用于构造JSON对象:

$time = array();
$time["id"]='';
$time["label"]='Time';
$time["pattern"]= '';
$time["type"]= 'datetime';
$cols = array();

array_push($cols, $time);

foreach($requests as $request){
    $col = array();
    $col["id"]= '';
    $col["label"]= $request;//substr((string)$request, 0, 10); //@TODO ABBREVIARE IL TESTO
    $col["pattern"]='';
    $col["type"]= 'number';
    array_push($cols, $col); 
}   

$rows = array();
$now= time();
for($i=23; $i>=0; $i--){
    $row = array();
    $next_date = date('d-m-Y H:i:s', $now - 3600*$i);
    $prev_date = date('d-m-Y H:i:s', $now - 3600*($i+1));
    $cell = array();
    $cell[] = array("v" => $next_date);//'new Date(' . date("Y,n,d,H,i,s", $now - 3600*$i).')'); 
    foreach($requests as $request){

        $cell[]= array("v" => (int)rand(5, 100)); 


    }
    array_push($row,array("c" => $cell));
    array_push($rows, $row);

}
$data = json_encode(array("cols" => $cols, "rows" => $rows));   
echo($data);        

好。我有以下结果:

 {
"cols": [
    {
        "label": "Time",
        "type": "string"
    },
    {
        "label": "1-#performing+OR+#teatre+OR+eclap_eu+OR+#music+OR+#dance+OR+#europeana+OR+#Culture",
        "type": "number"
    },
    {
        "label": "2-dms+OR+sentiment+OR+multimedia+OR+research+OR+visual+OR+knowledge",
        "type": "number"
    },
    {
        "label": "3-DMS+OR+SEKE+OR+sentient+OR+multimedia+OR+research+OR+visual+OR+knowledge",
        "type": "number"
    }
],
"rows": [
    [
        {
            "c": [
                {
                    "v": "12-10-2014 00:27:58"
                },
                {
                    "v": 42
                },
                {
                    "v": 44
                },
                {
                    "v": 35
                }
            ]
        }
    ],
    [
        {
            "c": [
                {
                    "v": "12-10-2014 01:27:58"
                },
                {
                    "v": 87
                },
                {
                    "v": 13
                },
                {
                    "v": 65
                }
            ]
        }
    ],
    [
        {
            "c": [
                {
                    "v": "12-10-2014 02:27:58"
                },
                {
                    "v": 37
                },
                {
                    "v": 16
                },
                {
                    "v": 92
                }
            ]
        }
    ],
    [
        {
            "c": [
                {
                    "v": "12-10-2014 22:27:58"
                },
                {
                    "v": 61
                },
                {
                    "v": 62
                },
                {
                    "v": 79
                }
            ]
        }
    ],
    [
        {
            "c": [
                {
                    "v": "12-10-2014 23:27:58"
                },
                {
                    "v": 39
                },
                {
                    "v": 35
                },
                {
                    "v": 24
                  }
              ]
          }
       ]
   ]

}

确定。我认为这是正确的JSON:在客户端我编写了以下代码:

$(document).ready(
    function(){

        setTimeout(function(){google.load('visualization', '1', {'callback':'alert("2 sec wait")', 'packages':['corechart']})}, 2000);
        //google.load("visualization", "1", { packages : [ 'corechart' ]});
        google.setOnLoadCallback(drawChart);
        // Custom drawChart function to create the charts

        function drawChart() {
            try {
                var jsonData = $.ajax({
                    url: Drupal.settings.axmedistwitter.load_points,
                    dataType: "json",
                    async: false
                    }).responseText;

                    console.log(jsonData);

        var data = new google.visualization.DataTable(jsonData);
        var chart = new google.visualization.LineChart(document.getElementById("chart" ));
                var options = {
                    title: 'Andamento dei retweet'
                };
                chart.draw(data, options);


            } catch (err) {

            alert( err.message );
            }
        }

    }

);

}

但我有以下错误

无法读取未定义的属性“1”。

请帮帮我。在一个星期内,我无法理解为什么它不起作用。 抱歉我的语言。 谢谢你的进步..

0 个答案:

没有答案