【问题标题】:Chart.js integration to Django projectChart.js 与 Django 项目的集成
【发布时间】:2019-05-10 17:13:22
【问题描述】:

Charts.js 和 Django 的新手。好像我让它工作了,但没有我想要的那么好。想整合在 Django 端进行的两个计算:

我的意见.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)

我的图表.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 数据部分时,它可以正常工作,但由于某些原因,如果我将它们都放入,它就无法正常工作。任何建议为什么?任何帮助表示赞赏。

【问题讨论】:

    标签: python django chart.js2


    【解决方案1】:

    一切看起来都不错,您只是在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
                }
            }]
        }
    }
    });
    

    【讨论】:

    • 你太棒了,先生 :) 非常感谢。
    • Eddie 你能分享你的views.py 完成和chart.js 模板完成你的问题可以帮助我
    猜你喜欢
    • 1970-01-01
    • 2010-10-31
    • 1970-01-01
    • 1970-01-01
    • 2012-12-02
    • 2017-01-29
    • 1970-01-01
    • 2021-12-15
    • 1970-01-01
    相关资源
    最近更新 更多