python - Feed google charts custom properties like color through gviz_api -
i'm having trouble propagating custom_properties color google chart through python layer gviz_api.
i create bar chart individually colored bars such in example here: https://developers.google.com/chart/interactive/docs/gallery/barchart#barstyles
but can't figure out how set throug gviz_api (http://code.google.com/p/google-visualization-python/).
i'm fine feeding data in way, dictionaries, lists, tuplets, 1 row @ time, long can color bars individually. here's latest non-working attempt, generate.py:
import gviz_api def main(): # creating data description = {"test" : ("string", "test name"), "duration" : ("number", "duration")} data = [dict(test="test a", duration=1000, custom_properties={"role":"color:green"}), {"test": "test b", "duration": 4000}] # loading gviz_api.datatable data_table = gviz_api.datatable(description, custom_properties={"role":"style"}) data_table.loaddata(data) # creating json string json = data_table.tojson(columns_order=("test", "duration"), order_by="test") # read page_template file f = open('template.html', 'r') page_template = f.read() # putting json string template print page_template.format(json) if __name__ == '__main__': main()
and corresponding template.html:
<html> <script src="https://www.google.com/jsapi" type="text/javascript"></script> <script> google.load('visualization', '1', {{packages:['corechart']}}); google.setonloadcallback(drawchart); function drawchart() {{ var options = {{ title: 'test results', legend: 'none', chartarea: {{ width: "50%", height: "70%" }} }} var json_chart = new google.visualization.barchart(document.getelementbyid('chart_div')); var json_data = new google.visualization.datatable({0}, 0.6); json_chart.draw(json_data, options); }} </script> <body> <div id="chart_div"></div> </body> </html>
i found solution this.
to description/data, add third column contain property.
description = {"test" : ("string", "test name"), "duration" : ("number", "duration"), "property": ("string", '', {'role':'style'})} data = [dict(test="test a", duration=1000, property = "color:green" ), {"test": "test b", "duration": 4000, property = "color:red"}]
this should work unless order of columns gets messed up. make sure order test, duration, property. wont work if it's test, property, duration, , wont come error. if end without test being first pop domain error.
hope helps looking this!
Comments
Post a Comment