Skip to content

Instantly share code, notes, and snippets.

@ricksolis
Created October 23, 2012 22:04
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ricksolis/3941946 to your computer and use it in GitHub Desktop.
Save ricksolis/3941946 to your computer and use it in GitHub Desktop.
#!/usr/bin/python
import gviz_api
page_template = """
<html>
<script src="https://www.google.com/jsapi" type="text/javascript"></script>
<script>
google.load('visualization', '1', {packages:['corechart']});
google.setOnLoadCallback(drawChart);
function drawChart() {
%(jscode)s
var jscode_table = new google.visualization.LineChart(document.getElementById('table_div_jscode'));
jscode_table.draw(jscode_data, {showRowNumber: true});
var json_line = new google.visualization.LineChart(document.getElementById('table_div_json'));
var json_data = new google.visualization.DataTable(%(json)s, 0.6);
json_line.draw(json_data);
}
</script>
<body>
<H1>Table created using ToJSCode</H1>
<div id="table_div_jscode"></div>
<H1>Table created using ToJSon</H1>
<div id="table_div_json"></div>
</body>
</html>
"""
def main():
# Creating the data
description = {"year": ("string", "Year"),
"sales": ("number", "Sales"),
"expenses": ("number", "Expenses")}
data = [{"year": '2004', "sales": 1000, "expenses": 300},
{"year": '2005', "sales": 1200, "expenses": 400},
{"year": '2006', "sales": 1300, "expenses": 500},
{"year": '2007', "sales": 1400, "expenses": 600},
{"year": '2008', "sales": 1500, "expenses": 800}]
# Loading it into gviz_api.DataTable
data_table = gviz_api.DataTable(description)
data_table.LoadData(data)
# Creating a JavaScript code string
jscode = data_table.ToJSCode("jscode_data",
columns_order=("year", "sales", "expenses"),
order_by="year")
# Creating a JSon string
json = data_table.ToJSon(columns_order=("year", "sales", "expenses"),
order_by="year")
# Putting the JS code and JSon string into the template
print "Content-type: text/html"
print
print page_template % vars()
if __name__ == '__main__':
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment