to Charts.js和django的新功能。好像我使它起作用,但不如我想要的那么好。想整合在django方面进行的两个计算:
我的Views.py:
def graph(request):
bug_all = BugTable.objects.filter()
bug_all_total = bug_all.count()
bug_posted = BugTable.objects.filter(
status=BugTable.BUG_POSTED)
bug_posted_total = bug_posted.count()
context = {'bug_all_total': bug_all_total,
'bug_posted_total': bug_posted_total}
return render(request, 'graph.html', context)
我的graphs.html
<canvas id="Bug-status-bar" class="col-md-6"></canvas>
<script THERE GOES CHART CDN LINK></script>
<script type="text/javascript">
var ctx = document.getElementById('Bug-status-bar');
var dataArray = [{{bug_all_total|safe}}, {{bug_posted_total|safe}}]
var myChart = new Chart(ctx, {
type: 'bar',
data: {
labels: ['All', 'Posted', 'Pending', 'Fixed'],
datasets: [{
label: 'Statistic on bug activity',
data: dataArray,
backgroundColor: [
'rgba(255, 99, 132, 0.4)'
'rgba(54, 162, 235, 0.2)',
],
borderColor: [
'rgba(255, 99, 132, 1)'
'rgba(54, 162, 235, 1)',
],
borderWidth: 1
}]
},
options: {
scales: {
yAxes: [{
ticks: {
beginAtZero: true
}
}]
}
}
});
</script>
当我将其中一个元素(bug_all_total或bug_posted_total(放在Graph.html数据部分中时,它可以正常工作,但是由于某些原因,如果我将两个都放在两个方面。有什么建议为什么?任何帮助都将不胜感激。
一切看起来都不错,您只是在rgba
字符串之后缺少几个逗号。
而是尝试一下:
var myChart = new Chart(ctx, {
type: 'bar',
data: {
labels: ['All', 'Posted', 'Pending', 'Fixed'],
datasets: [{
label: 'Statistic on bug activity',
data: dataArray,
backgroundColor: [
'rgba(255, 99, 132, 0.4)', // <----
'rgba(54, 162, 235, 0.2)',
],
borderColor: [
'rgba(255, 99, 132, 1)', // <---
'rgba(54, 162, 235, 1)',
],
borderWidth: 1
}]
},
options: {
scales: {
yAxes: [{
ticks: {
beginAtZero: true
}
}]
}
}
});