无法在Django和Chart.js之间传递数据。

huangapple go评论65阅读模式
英文:

cant pass data between Django and Chartjs

问题

I wanted to pass an array to chartjs for a chart the data is getting passed but for some strange reason the data gets changed, the array contains days of the month [1,2,3,so on] but on chart it displays "[ 0 1 3 5 7 9 0 so on ]"

the data is good when I print it to console

return render(request, 'notes/home.html',{
    'day': daysOfMonth,
})
 <div class="mainChart">
      <canvas id="myChart"></canvas>

      <script src="https://cdn.jsdelivr.net/npm/chart.js"></script>
      
      <script>
        const ctx = document.getElementById('myChart');
        const days = '{{day}}';
        
        new Chart(ctx, {
          type: 'bar',
          data: {
            labels: days,
            datasets: [{
              // label: '# of Votes',
              data: [1,2,3,4,5,6,7,8,9,10,1,2,3,4,5,6,7,8,9,10,1,2,3,4,5,6,7,8,9,10,],
              borderWidth: 10
            }]
          },
          options: {
            scales: {
              y: {
                beginAtZero: true
              }
            }
          }
        });
      </script>
      
    </div>
英文:

I wanted to pass an array to chartjs for a chart the data is getting passed but for some strange reason the data gets changed, the array contains days of the month [1,2,3,so on] but on chart it displays "[ 0 1 3 5 7 9 0 so on ]"

the data is good when I print it to console

    return render(request, &#39;notes/home.html&#39;,{
        &#39;day&#39;:daysOfMonth,
    })
 &lt;div class=&quot;mainChart&quot;&gt;
          &lt;canvas id=&quot;myChart&quot;&gt;&lt;/canvas&gt;
  
          &lt;script src=&quot;https://cdn.jsdelivr.net/npm/chart.js&quot;&gt;&lt;/script&gt;
          
          &lt;script&gt;
            const ctx = document.getElementById(&#39;myChart&#39;);
            const days = &#39;{{day}}&#39;;
            
            new Chart(ctx, {
              type: &#39;bar&#39;,
              data: {
                labels: days,
                datasets: [{
                  // label: &#39;# of Votes&#39;,
                  data: [1,2,3,4,5,6,7,8,9,10,1,2,3,4,5,6,7,8,9,10,1,2,3,4,5,6,7,8,9,10,],
                  borderWidth: 10
                }]
              },
              options: {
                scales: {
                  y: {
                    beginAtZero: true
                  }
                }
              }
            });
          &lt;/script&gt;
          
        &lt;/div&gt;

答案1

得分: 0

Sure, here's the translated content:

更改上下文数据如下

'day': json.dumps(daysOfMonth)

并且模板更改如下

var days = JSON.parse('{{day|safe}}');
英文:

Change the context data like this

&#39;day&#39;:json.dumps(daysOfMonth)

and is the template change is to this

 var days = JSON.parse(&#39;{{day|safe}}&#39;);

huangapple
  • 本文由 发表于 2023年6月4日 23:20:41
  • 转载请务必保留本文链接:https://go.coder-hub.com/76401091.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定