0

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?

7
  • probably the default_data that you are passing to the chart object is not an array and data key in chart options expects it to be an array. Commented Nov 10, 2020 at 1:06
  • I am not sure because in the chart.js documentation, they show example with passing lists: chartjs.org/docs/latest Commented Nov 10, 2020 at 1:08
  • [1,2,3,....] -> This represents an array in JavaScript. As per the docs, you need an array. I haven't worked on Django but this might help: stackoverflow.com/questions/21947237/… Commented Nov 10, 2020 at 1:10
  • I was able to generate an array using the link that you shared but unfortunately it still does not work Commented Nov 10, 2020 at 1:28
  • What is the error? Commented Nov 10, 2020 at 1:30

0

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.