【问题标题】:Display multiple chart.js charts using for loop in Django template在 Django 模板中使用 for 循环显示多个 chart.js 图表
【发布时间】:2018-08-11 13:23:07
【问题描述】:

我有一个要在其中显示的 Django 模板:

  • 一些数据1
  • 数据图表 1
  • 一些数据2
  • 数据图表2
  • 一些数据3
  • 数据图表2

我的 HTML 中有一个 Django 模板循环,用于显示数据,然后是一个用于显示每个图表的画布。数据显示正确,我可以让它显示第一个图表。

我想不通的是:

如何让它显示多个图表?目前它显示第一个图表但不显示后续图表 - 我想可能是因为画布 id 在每次迭代中总是相同的?

模板

{% for session_data in ratings_by_theme %}
{{ session_data }}

<div class="myChart">
    <canvas id="myChart" width="600" height="400"></canvas>

    <script>


        var ctx = document.getElementById("myChart").getContext('2d');
        var myChart = new Chart(ctx, {
            type: 'bar',

            data: {
                labels: [{% for label in chart_session_labels %} '{{ label }}', {% endfor %}],
                datasets: [{
                    label: "Teacher",
                    data: {{ teacher_chart_data }}[{{ forloop.counter0 }}],
                    backgroundColor: 'rgba(255, 99, 132, 0.4)',
                    borderColor: 'rgba(255,99,132,1)',
                    borderWidth: 1
                },
                    {
                        label: "Parent",
                        data: {{ parent_chart_data }}[{{ forloop.counter0 }}],
                        backgroundColor: 'rgba(255, 206, 86, 0.4)',
                        borderColor: 'rgba(255, 206, 86, 1)',
                        borderWidth: 1

                    },
                    {
                        label: "Learner",
                        data: {{ learner_chart_data }}[{{ forloop.counter0 }}],
                        backgroundColor: 'rgba(75, 192, 192, 0.4)',
                        borderColor: 'rgba(75, 192, 192, 1)',
                        borderWidth: 1

                    },
                    {
                        label: "Leader",
                        data: {{ leader_chart_data }}[{{ forloop.counter0 }}],
                        backgroundColor: 'rgba(255, 159, 64, 0.4)',
                        borderColor: 'rgba(255, 159, 64, 1)',
                        borderWidth: 1

                    }
                ]
            },
            options: {
                responsive: false,
                scales: {
                    yAxes: [{
                        ticks: {
                            beginAtZero: true
                        }
                    }]
                }
            }
        });


    </script>
</div>
{% endfor %}

【问题讨论】:

    标签: javascript django django-templates chart.js


    【解决方案1】:

    在一个画布中显示多个图表的一种方法是使用混合图表。

    更多信息:

    https://www.chartjs.org/docs/latest/charts/mixed.html

    【讨论】:

      【解决方案2】:

      所以为了引用画布,我将forloop索引附加到画布id:

      <canvas id="myChart{{ forloop.counter0 }}" width="600" height="400">
      

      然后在 chart.js 上下文中引用画布:

      var ctx = document.getElementById("myChart" + {{ forloop.counter0 }}).getContext('2d'); 
      

      【讨论】:

      • 马克,我也需要同样的东西,这个词是给你的吗?或者你找到解决方案了吗?
      猜你喜欢
      • 2017-03-27
      • 1970-01-01
      • 2020-09-21
      • 1970-01-01
      • 2011-05-20
      • 2018-12-25
      • 1970-01-01
      • 2015-11-05
      • 1970-01-01
      相关资源
      最近更新 更多