我使用aviz_api.py,python和django创建了一个柱形图。
但是,我在设置文档概述的属性时遇到问题: https://developers.google.com/chart/interactive/docs/gallery/columnchart#ColumnStyles
aviz_api.py似乎可以使用方法SetRowsCustomProperties(行/行,自定义属性)设置特定列的属性。
我尝试了很多事情但无济于事。图表继续加载,但不应用自定义属性。这是我最近的尝试:
data_table.SetRowsCustomProperties([1, {"role:style":"gold"})
当我打印数据表时,我得到以下内容:
{"rows":[{"c":[{"v":"Date(2011,0,16)"},{"v":23150}]},{"p":{"role:style":"gold"},.....
显示正在设置属性,但我不知道如何格式化自定义属性。有人有什么想法吗?
编辑:
我发现了这个:https://developers.google.com/chart/interactive/docs/roles#stylerole
似乎表明我需要在data_table的描述中添加" p":{" role":#34; style"}。我通过以下方式做到了这一点:
description = {'energy_kwh': ('number','energy_kwh',{"role":"Style"}), 'billing_date': ('date','billing_date')}
这给了我数据表:
{"cols":[{"label":"billing_date","type":"date","id":"billing_date"},{"label":"energy_kwh","type":"number","id":"energy_kwh","p":{"role":"style"}}],"rows"....
但现在图表根本没有加载。我开始怀疑该属性是否应该与水平轴数据一致。我接下来会尝试。
答案 0 :(得分:0)
我已经解决了这个问题:
我会发布我的解决方案,因为我发现这很难搜索:
qset = bills.values("billing_date","energy_kwh").order_by() # Obtain data from database
description = {'billing_date': ('date','billing_date'), 'zcolor':('string','',{'role':'style'}), 'energy_kwh': ('number','energy_kwh')} # indicate columns as dictionaries {"id": "type", "label", "custom property"}
for row in qset:
row['zcolor'] = 'color: green' #Set color to green for all rows to test
data_table = gviz_api.DataTable(description) # load description
data_table.LoadData(qset) # load dataset
我得到了以下输出:
{"cols":[{"type":"date","id":"billing_date","label":"billing_date"},{"type":"number","id":"energy_kwh","label":"energy_kwh"},{"type":"string","p":{"role":"style"},"id":"zcolor","label":""}],"rows":[{"c":[{"v":"Date(2011,0,16)"},{"v":23150},{"v":"color: green"}]},{"c":[{"v":"Date(2011,1,10)"},{"v":126025},{"v":"color: green"}]},{"c":[{"v":"Date(2011,2,14)"},{"v":18675},{"v":"color: green"}]},{"c":[{"v":"Date(2011,3,11)"},{"v":19850},{"v":"color: green"}]},{"c":[{"v":"Date(2011,4,15)"},{"v":16400},{"v":"color: green"}]},{"c":[{"v":"Date(2011,5,13)"},{"v":19225},{"v":"color: green"}]},{"c":[{"v":"Date(2011,6,13)"},{"v":22400},{"v":"color: green"}]},{"c":[{"v":"Date(2011,7,15)"},{"v":22400},{"v":"color: green"}]},{"c":[{"v":"Date(2011,8,13)"},{"v":18150},{"v":"color: green"}]},{"c":[{"v":"Date(2011,9,18)"},{"v":17000},{"v":"color: green"}]},{"c":[{"v":"Date(2011,10,10)"},{"v":15875},{"v":"color: green"}]},{"c":[{"v":"Date(2011,11,12)"},{"v":7600},{"v":"color: green"}]},{"c":[{"v":"Date(2012,0,12)"},{"v":32850},{"v":"color: green"}]},{"c":[{"v":"Date(2012,1,13)"},{"v":7600},{"v":"color: green"}]},{"c":[{"v":"Date(2012,2,12)"},{"v":20925},{"v":"color: green"}]},{"c":[{"v":"Date(2012,3,19)"},{"v":18950},{"v":"color: green"}]},{"c":[{"v":"Date(2012,4,11)"},{"v":16400},{"v":"color: green"}]},{"c":[{"v":"Date(2012,5,12)"},{"v":18100},{"v":"color: green"}]},{"c":[{"v":"Date(2012,6,12)"},{"v":16975},{"v":"color: green"}]},{"c":[{"v":"Date(2012,7,14)"},{"v":24650},{"v":"color: green"}]},{"c":[{"v":"Date(2012,8,12)"},{"v":16475},{"v":"color: green"}]},{"c":[{"v":"Date(2012,9,12)"},{"v":17000},{"v":"color: green"}]},{"c":[{"v":"Date(2012,10,18)"},{"v":15625},{"v":"color: green"}]},{"c":[{"v":"Date(2012,11,11)"},{"v":1475},{"v":"color: green"}]},{"c":[{"v":"Date(2013,0,14)"},{"v":8225},{"v":"color: green"}]},{"c":[{"v":"Date(2013,1,12)"},{"v":9050},{"v":"color: green"}]},{"c":[{"v":"Date(2013,2,12)"},{"v":7250},{"v":"color: green"}]},{"c":[{"v":"Date(2013,3,11)"},{"v":18950},{"v":"color: green"}]},{"c":[{"v":"Date(2013,4,13)"},{"v":16375},{"v":"color: green"}]},{"c":[{"v":"Date(2013,5,12)"},{"v":18675},{"v":"color: green"}]},{"c":[{"v":"Date(2013,6,11)"},{"v":7600},{"v":"color: green"}]},{"c":[{"v":"Date(2013,7,13)"},{"v":10525},{"v":"color: green"}]},{"c":[{"v":"Date(2013,8,12)"},{"v":44850},{"v":"color: green"}]},{"c":[{"v":"Date(2013,9,10)"},{"v":32250},{"v":"color: green"}]},{"c":[{"v":"Date(2013,10,13)"},{"v":9600},{"v":"color: green"}]},{"c":[{"v":"Date(2013,11,11)"},{"v":15500},{"v":"color: green"}]},{"c":[{"v":"Date(2014,0,13)"},{"v":9600},{"v":"color: green"}]},{"c":[{"v":"Date(2014,1,12)"},{"v":7950},{"v":"color: green"}]},{"c":[{"v":"Date(2014,2,12)"},{"v":7775},{"v":"color: green"}]},{"c":[{"v":"Date(2014,3,16)"},{"v":7950},{"v":"color: green"}]},{"c":[{"v":"Date(2014,4,12)"},{"v":14675},{"v":"color: green"}]},{"c":[{"v":"Date(2014,5,24)"},{"v":15850},{"v":"color: green"}]},{"c":[{"v":"Date(2014,6,14)"},{"v":10225},{"v":"color: green"}]},{"c":[{"v":"Date(2014,9,1)"},{"v":10200},{"v":"color: green"}]},{"c":[{"v":"Date(2014,9,10)"},{"v":18100},{"v":"color: green"}]}]}
请注意,我遇到的问题是订单。具有该属性的列必须在 之后直接来 您正在绘制的数据列。我在文档中的任何地方都找不到这个事实。
我主要使用https://developers.google.com/chart/interactive/docs/roles来帮助我解决问题。