问题
New to Charts.js and Django. Seems like I make it working, but not as good as I want it to. Would like to integrate two calculations made on Django side:
my 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)
my 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>
When I put one of those elements (bug_all_total or bug_posted_total) to graph.html data section it works fine, but for some reasons it does not work if i put both of them. Any suggestions why? Any help is appreciated.
回答1:
Everything looks good, you are simply missing a few commas after the rgba
strings.
Try this instead:
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
}
}]
}
}
});
来源:https://stackoverflow.com/questions/56082109/chart-js-integration-to-django-project