Chart.js integration to Django project

牧云@^-^@ 提交于 2019-12-11 19:26:53

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!