【问题标题】:Chart not rendering on ChartJS图表未在 ChartJS 上呈现
【发布时间】:2018-08-03 00:29:23
【问题描述】:

我在服务器端有一个 Express 服务器运行我的应用程序,我正在向前端发送数据。我正在使用 Mongoose 作为数据库。

我猜数据发送正确,我可以在我测试的 console.log 上检查它。

目前我只想使用一个条来测试数据是否正在呈现。

所以,我在发送数据的路线上有以下代码:

router.get('/home', isAuthenticated, async (req, res) =>  {

    let final;
    atendimentos.find({})
        .then(atendimentos => {
            final = atendimentos.filter(atendimentos => atendimentos.status === 'F')
            res.render('home', {user: req.user, fin: final.length});
            //Funciona
            console.log(final.length)
        })
        .catch(err => console.error(err));
   })

这个final返回一个数组,里面有一个我从数据库中得到的数字,它返回所有状态为F的东西。

现在,来自前端 ChartJS 的部分:

<div id="container">
    <canvas id="myChart" width="400" height="200" </canvas>
</div>

<script src="https://cdnjs.cloudflare.com/ajax/libs/Chart.js/2.7.1/Chart.min.js"></script>

<script>

 var ctx = document.getElementById('myChart').getContext('2d');
    var myChart = new Chart(ctx, {
    type: 'bar',
    data: {
         labels: "Atendimentos",
         datasets: [{
         label: 'Gráfico de atendimento',
         data: [<%= fin %>], // parâmetros do banco
         backgroundColor: [
             'rgb(71, 87, 112)'
             ],
         borderColor: [
             'rgb(71, 87, 112)'
             ],
         borderWidth: 1
         }]
     },
     options : {
         scales: {
             yAxes: [{
                 ticks: {
                     beginAtZero: true
                     }
                 }]
             }
         }
    });

    </script>

我收到以下错误:

Uncaught TypeError: t.ticks.map is not a function and Uncaught TypeError: Cannot read property 'skip' of undefined.

希望我能得到一些帮助,在此先感谢!

【问题讨论】:

    标签: javascript chart.js


    【解决方案1】:

    修复它。标签字段应该是一个数组,我只是放了[],现在它可以工作了!

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-09-07
      • 1970-01-01
      • 1970-01-01
      • 2020-06-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多