我正在使用Google Analytics Api V3并尝试在图表上显示查询结果。我现在有这个代码,但它没有显示在图表上。我想我没有正确地将行传递给图表因为我弄乱了foreach循环
google.load('visualization', '1.0', {'packages':['corechart']});
google.setOnLoadCallback(drawChart);
function drawChart() {
var data = new google.visualization.DataTable();
data.addColumn('string', 'Topping');
data.addColumn('number', 'Slices');
data.addRows([
<?php
$first_date = '30daysAgo';
$last_date = 'yesterday';
$metrics = 'ga:sessions';
$optParams = array(
'dimensions' => 'ga:browser',
'sort' => '-ga:sessions',
'max-results' => '10'
);
try {
$results = $analytics->data_ga->get($analytics_id, $first_date, $last_date, $metrics, $optParams);
function printDataTable($results) {
if (count($results->getRows()) > 0) {
$value .= '';
foreach ($results->getRows() as $row) {
$value .= '';
foreach ($row as $cell) {
$value .= ''
. htmlspecialchars($cell, ENT_NOQUOTES)
. ' ';
}
}
} else {
$value .= 'No results found';
}
print $value;
}
print_r(printDataTable($results));
}
catch(Exception $e) {
printf('There was an Error in your query: %s', $e->getMessage());
}
?>
]);
var options = {'title':'Title',
'width':400,
'height':300};
var chart = new google.visualization.PieChart(document.getElementById('chart_div'));
chart.draw(data, options);
}