Changing color of annotations on select
This code uses a DataView to separate out a selected annotation into a separate data series to change its color.
HTML
<script src="http://www.google.com/jsapi?fake=.js"></script>
<div id="chart_div"></div>
<br />
<br />
<div id="creativeCommons" style="text-align: center; width: 400px;"> <a rel="license" href="http://creativecommons.org/licenses/by-nc-sa/3.0/deed.en_US"><img alt="Creative Commons License" style="border-width:0" src="http://i.creativecommons.org/l/by-nc-sa/3.0/88x31.png" /></a><br/><span xmlns:dct="http://purl.org/dc/terms/" href="http://purl.org/dc/dcmitype/InteractiveResource" property="dct:title" rel="dct:type">Code to change the color of an annotation on selection</span> by <span xmlns:cc="http://creativecommons.org/ns#" property="cc:attributionName">Andrew Gallant</span> is licensed under a <a rel="license" href="http://creativecommons.org/licenses/by-nc-sa/3.0/deed.en_US">Creative Commons Attribution-NonCommercial-ShareAlike 3.0 Unported License</a>.</div>
JavaScript
google.load("visualization", "1", {packages: ["corechart"]});
google.setOnLoadCallback(drawChart);
function drawChart() {
// this setup assumes you have only 1 data series; it can be expanded to handle multiple data series
var data = new google.visualization.DataTable();
data.addColumn('string', 'Name');
data.addColumn('number', 'Value');
data.addColumn({type: 'string', role: 'annotation'});
data.addColumn({type: 'string', role: 'annotationText'});
data.addRows([
['Foo', 53, '<img src="bar.png"> Foo text', 'Foo description'],
['Bar', 71, 'Bar text', 'Bar description'],
['Baz', 36, 'Baz text', 'Baz description'],
['Cad', 42, 'Cad text', 'Cad description'],
['Qud', 87, 'Qud text', 'Qud description'],
['Pif', 64, 'Pif text', 'Pif description']
]);
var view = new google.visualization.DataView(data);
var baseColumns = [0, 1, {
type: 'string',
role: 'annotation',
calc: function (dt, row) {
return null;
}
}, {
type: 'string',
role: 'annotationText',
calc: function (dt, row) {
return null;
}
}, 1, 2, 3];
view.setColumns(baseColumns);
var chart = new google.visualization.LineChart(document.getElementById('chart_div'));
var options = {
height: 400,
width: 600,
series: {
0: {
// keep the second series (used to change the color of the selected annotation) hidden
// use the color option here to set the color of the annotation
pointSize: 0,
lineWidth: 0,
visibleInLegend: false
}
}
};
google.visualization.events.addListener(chart, 'select', function () {
var sel = chart.getSelection();
if (sel.length > 0) {
if (sel[0].column == 5) {
// set the view to include the...