如何将自定义属性用于 Python Google 图表 aviz_api.py

How to use Custom Properties with Python Google Charts aviz_api.py

我使用 aviz_api.py、python 和 django 创建了一个柱形图。

但是,我无法设置文档概述的属性: https://developers.google.com/chart/interactive/docs/gallery/columnchart#ColumnStyles

aviz_api.py 似乎能够使用方法 SetRowsCustomProperties(Row/Rows, Custom 属性).

为特定列设置属性

我试了很多东西都无济于事。该图继续加载,但不应用自定义 属性。这是我最近的尝试:

data_table.SetRowsCustomProperties([1, {"role:style":"gold"})

当我打印数据时 table 我得到以下信息:

{"rows":[{"c":[{"v":"Date(2011,0,16)"},{"v":23150}]},{"p":{"role:style":"gold"},.....

显示正在设置 属性,但我不知道如何格式化自定义 属性。有人有什么想法吗?

编辑:

我发现了这个:https://developers.google.com/chart/interactive/docs/roles#stylerole

这似乎表明我需要将 "p":{"role":"style"} 添加到 data_table 的描述中。我已经完成了以下操作:

description = {'energy_kwh': ('number','energy_kwh',{"role":"Style"}), 'billing_date': ('date','billing_date')}

这给了我数据table:

{"cols":[{"label":"billing_date","type":"date","id":"billing_date"},{"label":"energy_kwh","type":"number","id":"energy_kwh","p":{"role":"style"}}],"rows"....

但现在图表根本没有加载。我开始怀疑 属性 是否应该与水平轴数据一起使用。我接下来试试。

我已经解决了这个问题:

我将 post 我的解决方案用于其他人,因为我发现这很难搜索:

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来帮我解决问题。