Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Display multiple chart.js charts using for loop in Django template

I have a Django template in which I want to display:

  • some data 1
  • a chart of data 1
  • some data 2
  • a chart of data 2
  • some data 3
  • a chart of data 2

I have a Django template loop in my HTML which displays the data, and then a canvas for displaying each chart. The data displays correctly, and I can get it to display the first chart.

What I can't figure out is:

How do I get it to display more than one chart? At the moment it displays the first chart but not subsequent charts - I think maybe because the canvas id is always the same in each iteration?

Template

{% 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 %}
like image 418
Mark__C Avatar asked Dec 18 '25 09:12

Mark__C


1 Answers

So in order to reference the canvas, I appended the forloop index to the canvas id:

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

And then to reference the canvas within the chart.js context:

var ctx = document.getElementById("myChart" + {{ forloop.counter0 }}).getContext('2d'); 
like image 149
Mark__C Avatar answered Dec 20 '25 22:12

Mark__C



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!