Skip to content

Instantly share code, notes, and snippets.

@mrnohr
Created October 4, 2012 16:28
Show Gist options
  • Save mrnohr/3834777 to your computer and use it in GitHub Desktop.
Save mrnohr/3834777 to your computer and use it in GitHub Desktop.
Grails and Google Charts
<html>
<head>
<meta name="layout" content="main">
<script type="text/javascript" src="https://www.google.com/jsapi"></script>
<script type="text/javascript">
google.load("visualization", "1", {packages:["corechart"]});
google.setOnLoadCallback(drawChart);
function drawChart() {
data = google.visualization.arrayToDataTable(${chartData})
chart = new google.visualization.LineChart(document.getElementById('chart_div'));
chart.draw(data);
}
</script>
</head>
<body>
<div id="chart_div"></div>
</body>
</html>
function drawChart() {
data = google.visualization.arrayToDataTable(${chartData})
chart = new google.visualization.LineChart(document.getElementById('chart_div'));
options = {
colors: ${colors}
}
chart.draw(data, options);
}
def chart() {
//create the data table (list of lists) - first row is the headers
def totalYardsStats = [["'Week'", "'Chicago'", "'Detroit'", "'Green Bay'", "'Minnesota'"]]
//actual data CHI DET GB MIN
totalYardsStats << ["'Week 1'", 428, 429, 324, 389]
totalYardsStats << ["'Week 2'", 168, 296, 321, 327]
totalYardsStats << ["'Week 3'", 274, 437, 268, 344]
totalYardsStats << ["'Week 4'", 430, 341, 421, 227]
[chartData: totalYardsStats]
}
def chart() {
def totalYardsStats = ... //as before
//make the colors match team colors
def colors = [
"'#FF8000'", //orange - Chicago
"'#0040FF'", //blue - Detroit
"'#21610B'", //green - Green Bay
"'#6A0888'" //purple - Minnesota
]
[chartData: totalYardsStats, colors:colors]
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment