我遇到了这个线程,这正是我想要的 Google Charts API:始终在图表中显示数据点值,但那里给出的答案适用于使用硬编码值时。
就我而言,我使用 MySQL 数据来填充图表,因此无法确定在何处以及如何准确使用该role:annonation
属性。我正在使用以下代码来绘制图表。
PHP
$table = array();
$table['cols'] = array(
array('label' => 'Month', 'type' => 'string'),
array('label' => 'Benchmark', 'type' => 'number')
);
$rows = array();
foreach($someArray as $mnth=>$array)
{
$temp = array();
$temp[] = array('v' => (string) $mnth);
$temp[] = array('v' => (int) $benchMark);
foreach($array as $pt)
{
$temp[] = array('v' => (int) $pt);
}
$rows[] = array('c' => $temp);
}
$table['rows'] = $rows;
$jsonTable = json_encode($table);
Javascript
google.load('visualization', '1', {'packages':['corechart']});
google.setOnLoadCallback(drawChart);
function drawChart() {
var data = new google.visualization.DataTable(<?php echo $jsonTable; ?>);
var options = {
title: 'TNS',titleTextStyle: {color: "green"}, hAxis: {title: "MONTH", titleTextStyle: {color: "green"}}, vAxis: {title: "Percentage", titleTextStyle: {color: "green"},viewWindowMode: 'explicit',
viewWindow: {
max: 110,
min: 0
}},
is3D: 'true',
height:600,
colors: ['black', 'red', 'green', 'blue', 'yellow']
};
var chart = new google.visualization.LineChart(document.getElementById('tns1'));
chart.draw(data, options);
}