Chart.js 集成到 Django 项目
Chart.js integration to Django project
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
}
}]
}
}
});
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
}
}]
}
}
});