正如我的评论中所述,您可以使用注释角色来执行此操作。
您的原始代码:
var data = google.visualization.arrayToDataTable([
['time', 'value'],
['12:00', 1],
['13:00', 5],
['14:00', 8],
['15:00', 12],
['16:00', 11],
['17:00', 15],
]);
new google.visualization.LineChart(document.getElementById('visualization')).
draw(data, {});
您需要添加两列——一列用于注释标记,一列用于注释文本。假设您想要在 14:00 和 16:00 发表两条评论,例如:
function drawVisualization() {
var data = new google.visualization.DataTable();
data.addColumn('string', 'time');
data.addColumn('number', 'value');
data.addColumn({type: 'string', role:'annotation'});
data.addColumn({type: 'string', role:'annotationText'});
data.addRows([
['12:00', 1, null, null],
['13:00', 5, null, null],
['14:00', 8, 'A', 'This is Point A'],
['15:00', 12, null, null],
['16:00', 11, 'B', 'This is Point B'],
['17:00', 15, null, null],
]);
new google.visualization.LineChart(document.getElementById('visualization')).
draw(data, {});
}
这是结果:
要添加asgalant为图表添加点的解决方案,您可以执行以下操作:
function drawVisualization() {
var data = new google.visualization.DataTable();
data.addColumn('string', 'time');
data.addColumn('number', 'value');
data.addColumn('number', 'points');
data.addColumn({type: 'string', role:'annotation'});
data.addColumn({type: 'string', role:'annotationText'});
data.addRows([
['12:00', 1, null, null, null],
['13:00', 5, null, null, null],
['14:00', 8, 8, 'A', 'This is Point A'],
['15:00', 12, null, null, null],
['16:00', 11, 11, 'B', 'This is Point B'],
['17:00', 15, null, null, null],
]);
new google.visualization.LineChart(document.getElementById('visualization')).
draw(data, {
series: {
0: {
// set any applicable options on the first series
},
1: {
// set the options on the second series
lineWidth: 0,
pointSize: 5,
visibleInLegend: false
}
}
});
}
结果如下: