直到我知道可能的数量时为止,下面的代码可以正常工作。
function drawChart() {
var data = google.visualization.arrayToDataTable([
['Task', 'Boys age'],
['1 year old', <?php echo $one; ?>],
['2 year old', <?php echo $two; ?>],
['3 year old', <?php echo $three; ?>],
['4 year old', <?php echo $four; ?>],
['5 year old', <?php echo $five; ?>],
['6 year old', <?php echo $six; ?>],
['7 year old', <?php echo $seven; ?>]
]);
var options = {
title: '<?php echo $total; ?> Boys age ',
pieSliceText: 'value',
pieHole: 0.4
};
var chart = new google.visualization.PieChart(document.getElementById('BoysAge'));
chart.draw(data, options);
}
但是现在我有了一个包含年龄的数组。 该行:
print_r(array_count_values($boysAge));
结果进入此输出:
Array ( [10] => 5 [7] => 12 [8] => 9 [0] => 1 [9] => 10 [12] => 2 [11] => 3 )
是否有简单(但很聪明)的方法将数组数据解析为图表数据?
亲切的问候,
弗兰斯
答案 0 :(得分:0)
您可以将-> array_count_values($boysAge)
的结果转换为JSON
然后将每个计数作为一行添加到数据表中
请参阅以下代码段...
function drawChart() {
var data = new google.visualization.DataTable();
data.addColumn('string', 'Task');
data.addColumn('number', 'Boys age');
var ageCount = <?php echo json_encode(array_count_values($boysAge)); ?>
for (var age in ageCount) {
data.addRow([age, ageCount[age]]);
}
var options = {
title: '<?php echo $total; ?> Boys age ',
pieSliceText: 'value',
pieHole: 0.4
};
var chart = new google.visualization.PieChart(document.getElementById('BoysAge'));
chart.draw(data, options);
}