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