I'm using gviz_api (google-visualization-python) to build some line charts. http://code.google.com/p/google-visualization-python/
I'e edited a line chart example taken from the google documentation.
However, I'm not sure how I would pass a date into the DataTable
Here's the edited example I've been working with. https://gist.github.com/3941946
Here's the code I had a question about
# 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)
How would I load a date into the DataTable using gviz_api?
The google documentation describes how to creat a new Date() using javascript, however, I'd like to keep using gviz_api.py.
Notes from the google documentation from https://developers.google.com/chart/interactive/docs/dev/implementing_data_source#jsondatatable
*JSON Modifications Google's helper libraries, and all queries sent to Google, return a slightly non-standard version of JSON/JSONP. If you are not parsing the returned code yourself, this should not matter to you. The Visualization API client supports both standard and the modified versions of JSON. Here is a summary of the differences:
JSON does not support JavaScript Date values (for example, "new Date(2008,1,28,0,31,26)"; the API implementation does. However, the API does now support a custom valid JSON representation of dates as a string in the following format: Date(year, month, day[,hour, minute, second[, millisecond]]) where everything after day is optional, and months are zero-based.
JSON uses double-quotes for dictionary keys; the API implementation uses unquoted keys.
JSON requires double-quotes around string values; the API implementation uses single quotes.*
You can actually handle creating dates just like you would in standard Python - the API handles the conversion to JS for you. All you have to do is import datetime
at the beginning of your script, change the column type for year
from string
to date
and then create the dates as you would in standard Python:
# This is the first modification - importing the library
import datetime
import gviz_api
# page_template stays the same
# ...
def main():
# Creating the data
# Here we change the type of column "year" to "date"
description = {"year": ("date", "Year"),
"sales": ("number", "Sales"),
"expenses": ("number", "Expenses")}
# Here we switch out the string dates with an actual Python datetime.date
# The conversion happens in the the subsequent functions, giving you a
# date that is usable in the JS
data = [{"year": datetime.date(2007,3,7), "sales": 1000, "expenses": 300},
{"year": datetime.date(2009,6,11), "sales": 1200, "expenses": 400},
{"year": datetime.date(2009,3,1), "sales": 1300, "expenses": 500},
{"year": datetime.date(2010,8,6), "sales": 1401, "expenses": 600},
{"year": datetime.date(2011,7,13), "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()
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With