我正在尝试将我的查询集传递给highcharts.js。我可以使用以下方法:
这是我的view.py
class chart_data(object): # Grabs the Data
def sum_savings():
data = {'division': [], 'total_actual_savings': [],
'actual_fte': []}
cost = BsoCet.objects.annotate(savings=Sum('total_actual_savings'))
for i in cost:
data['division'].append(i.division)
data['total_actual_savings'].append(i.total_actual_savings)
#data['actual_fte'].append(i.actual_fte)
return data
def plot(request, chartID = 'chart_ID', chart_type = 'column',chart_height
= 500):
data = chart_data.sum_savings()
chart = {
'chart' : {"renderTo": chartID, "type": chart_type, "height":
chart_height},
'title' : {"text": 'Check Cost per Division'},
'xAxis' : {"title": {"text": 'Divisions'}, "categories":
data['division']},
'yAxis' : {"title": {"text": 'Cost'}},
'series' : [
{"name": 'Total Actual Savings', "data":
data['total_actual_savings']}
]
}
return JsonResponse(chart)
这是我的模板
<div id="container" data-url="{% url 'BSO:plot' %}"></div>
<script>
$.ajax({
url: $("#container").attr("data-url"),
dataType: 'json',
success: function (data) {
Highcharts.chart("container", data);
}
});
</script>
当我开始在我的查询集中使用values()来注释和分组列以进行聚合并将它们附加到数据变量时,它会给我这个错误TypeError:values()不带参数(给定1)。
这是我的新views.py使用我的queryset的值():
class chart_data(object): # Grabs the Data
def sum_savings():
data = {'division': [], 'total_actual_savings': [],
'actual_fte': []}
cost = BsoCet.objects.values('division').
annotate(savings=Sum('total_actual_savings'))
for i in cost:
data['division'].append(i.values('division'))
data['total_actual_savings'].
append(i.values('total_actual_savings'))
return data
**** def情节保持不变......
***这是我得到的错误:第21行,在sum_savings数据['division']。append(i.values('division'))TypeError:values()不带参数(给定1)
我是Django的新手,所以请帮助我。
通过互联网找到解决方案。将views.py更改为:
def Chart(request):
dataset = BsoCet.objects.values('division').
annotate(savings=Sum('total_actual_savings')).order_by('savings')
categories = list()
savings_data = list()
for entry in dataset:
categories.append(entry['division'])
savings_data.append(entry['savings'])
savings = {
'name': 'savings',
'data': savings_data,
'color': 'green'
}
chart = {
'chart': {'type': 'column'},
'title': {'text': 'Division Savings'},
'xAxis': {'categories': categories},
'series': [savings]
}
dump = json.dumps(chart)
return render(request, 'BSO/BSO_dashboard.html', {'chart':dump})