Skip to content

Instantly share code, notes, and snippets.

@diegogurgel
Created August 12, 2014 16:57
Show Gist options
  • Save diegogurgel/3c9820b313bf9f336199 to your computer and use it in GitHub Desktop.
Save diegogurgel/3c9820b313bf9f336199 to your computer and use it in GitHub Desktop.
Line Chart - googlecharts
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="content-type" content="text/html; charset=utf-8"/>
<title>
Google Visualization API Sample
</title>
<script type="text/javascript" src="//www.google.com/jsapi"></script>
<script type="text/javascript">
google.load('visualization', '1', {packages: ['corechart']});
</script>
<script type="text/javascript">
function drawVisualization() {
// Create and populate the data table.
dataTable = new google.visualization.DataTable();
dataTable.addColumn('number', 'col');
dataTable.addColumn('number', 'Uberaba');
dataTable.addColumn('number', 'Uberlandia');
dataTable.addRows([
[1,2,2],
[2,4,3],
[3,5,4],
[4,7,1],
[5,1,2]
]);
cidade = new Array();
cidades = ["Uberaba","Uberlandia"];
cidades = cidades.reduce(function(obj, v, i) {
obj[v] = {
index:i,
selected:false
};
return obj;
}, {});
series = {
0:{color:'#DDD'},
1:{color:'#DDD'},
}
// Create and draw the visualization.
chart = new google.visualization.LineChart(document.getElementById('visualization'));
chart.draw(dataTable, {
series:series,
width: 1000,
height: 400,
vAxis:{gridlines:{count:0},baselineColor:"#FFF"},
hAxis:{gridlines:{color: '#DDD', count: 5},baselineColor:'#DDD',textPosition:'none'},
pointSize:10,
}
);
google.visualization.events.addListener(chart, 'select',function(ev){
console.log(getSelection());
selection = getSelection();
console.log(selection.baseNode.nodeValue);
if(cidades[selection.baseNode.nodeValue].selected){
cidades[selection.baseNode.nodeValue].selected=false;
series[cidades[selection.baseNode.nodeValue].index].color = "#DDD";
}else{
cidades[selection.baseNode.nodeValue].selected=true;
series[cidades[selection.baseNode.nodeValue].index].color = "#79b939";
}
chart.draw(dataTable, {
series:series,
width: 1000,
height: 400,
vAxis:{gridlines:{count:0},baselineColor:"#FFF"},
hAxis:{gridlines:{color: '#DDD', count: 5},baselineColor:'#DDD',textPosition:'none'},
pointSize:10,
}
);
});
}
google.setOnLoadCallback(drawVisualization);
</script>
</head>
<body style="font-family: Arial;border: 0 none;">
<div id="visualization" style="width: 500px; height: 400px;"></div>
<script src="scripts/main.js"></script>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment