I am across a problem that I cannot handle. I passing queryset data from a django view to its corresponding template and I want to use this data to plot a graph with chart.js
I am trying to achieve this without using Ajax requests.
However, when I try to acquire the queryset data with javascript to pass it in chart.js it renders an error.
Here is what I have done in html:
<div id="default_data">
<p> {{ default_items }} </p>
</div>
<div id="labels">
<p>{{ labels }} </p>
<div class="col-lg-6">
<div id="tag3" class="card-footer small text-muted">{% trans 'ITEMS WITH HIGHEST NUMBER OF SALES (3 MONTHS)' %}</div>
<div class="bar-chart block">
<canvas id="myChart3" height="90"></canvas>
</div>
</div>
and here is what labels and default_data render:
<QuerySet [58.0, 62.0, 74.0, 60.0, 16.0, 1.0, 1.0, 1.0, 1.0, 2.0]>
<QuerySet ['372750', '372600', '372650', '372700', '372150', '289807', '289922', '289840', '289923', '372310']>
and javascript:
<script>
var ctx3 = document.getElementById('myChart3').getContext('2d');
var labels = document.getElementById("labels");
var myChart3 = new Chart(ctx3, {
type: 'bar',
data: {
labels: labels,
datasets: [{
label:'références avec nombre de ventes élevé',
data: default_data,
label: '# of Votes',
borderWidth: 1,
}]
},
options: {
scales: {
yAxes: [{
ticks: {
beginAtZero: true
}
}]
}
}
});
</script>
I don't understand the error that gets outputed in chrome dev:
Chart.min.js:10 Uncaught TypeError: e.slice is not a function
Can someone explain me how to pass django Queryset data in chart.js without using ajax requests?