3

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 %}

2 Answers 2

2

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'); 
Sign up to request clarification or add additional context in comments.

1 Comment

Mark, I need the same thing, did that word for you? Or did you find a solution?
0

A way for showing multiple charts in one canvas is using Mixed Chart.

more info :

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.