Django:如何在 chartit 模块的数据透视图中自定义我的数据
Django: how to customize my data in pivot chart of chartit module
在 django 视图中我有:
females = Demographic.objects.filter(gender__startswith='f')
males = Demographic.objects.filter(gender__startswith='m')
我使用 chartit
来绘制每个性别的人数图表,换句话说,显示 females.count()
和 males.count()
。
您知道如何将我的数据自定义到 chartit
数据透视图中吗?
目前我的代码是下面的代码,但不起作用。
ds = PivotDataPool(
series=[
{'options': {
'source':Demographic.objects.filter(gender__startswith='f'),
'categories':['gender'] },
'terms':{
'sex_count':females.count(),
}
}
]
)
pvcht = PivotChart(
datasource=ds,
series_options =
[{'options':{
'type': 'column',
'stacking': True},
'terms':[
'sex_count']}],
chart_options =
{'title': {
'text': 'Total number'},
'xAxis': {
'title': {
'text': 'Sex'}}}
)
我找到了使用 Django 聚合函数的解决方案。见下文:
ds = PivotDataPool(
series=[
{'options': {
'source':Demographic.objects.all(),
'categories':['gender'] },
'terms':{
'sex_count':Count('gender'),
}
}
]
)
pvcht = PivotChart(
datasource=ds,
series_options =
[{'options':{
'type': 'column',
#'stacking': True
},
'terms':[
'sex_count']}],
chart_options =
{'title': {
'text': 'Total number'},
'xAxis': {
'title': {
'text': 'Sex'}}}
)
在 django 视图中我有:
females = Demographic.objects.filter(gender__startswith='f')
males = Demographic.objects.filter(gender__startswith='m')
我使用 chartit
来绘制每个性别的人数图表,换句话说,显示 females.count()
和 males.count()
。
您知道如何将我的数据自定义到 chartit
数据透视图中吗?
目前我的代码是下面的代码,但不起作用。
ds = PivotDataPool(
series=[
{'options': {
'source':Demographic.objects.filter(gender__startswith='f'),
'categories':['gender'] },
'terms':{
'sex_count':females.count(),
}
}
]
)
pvcht = PivotChart(
datasource=ds,
series_options =
[{'options':{
'type': 'column',
'stacking': True},
'terms':[
'sex_count']}],
chart_options =
{'title': {
'text': 'Total number'},
'xAxis': {
'title': {
'text': 'Sex'}}}
)
我找到了使用 Django 聚合函数的解决方案。见下文:
ds = PivotDataPool(
series=[
{'options': {
'source':Demographic.objects.all(),
'categories':['gender'] },
'terms':{
'sex_count':Count('gender'),
}
}
]
)
pvcht = PivotChart(
datasource=ds,
series_options =
[{'options':{
'type': 'column',
#'stacking': True
},
'terms':[
'sex_count']}],
chart_options =
{'title': {
'text': 'Total number'},
'xAxis': {
'title': {
'text': 'Sex'}}}
)