【发布时间】: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