ChartJS 无法读取null的属性(阅读'transition'),这是什麽意思?

rqenqsqc  于 2022-11-06  发布在  Chart.js
关注(0)|答案(1)|浏览(124)

我用chartjs库从axios中创建了动态数据的图表。但是当我刷新浏览器图表时,一开始什么都没有,就像这样:

如果我变更浏览器视窗的尺寸,则会显示所有图形:

来自控制台的错误:

Uncaught TypeError: Cannot read properties of null (reading 'transition')
    at Chart.transition (Chart.js?473e:9865:1)
    at Chart.draw (Chart.js?473e:9827:1)
    at Chart.render (Chart.js?473e:9799:1)
    at Object.callback (Chart.js?473e:2208:1)
    at Object.advance (Chart.js?473e:3544:1)
    at Object.startDigest (Chart.js?473e:3517:1)
    at eval (Chart.js?473e:3506:1)

我不知道,为什么会这样,该怎么做。看起来像是第一次浏览器尝试建立图表(但数据还没有收到)。在所有的计算大约580 ms。

用于构建图表的MainChart.vue:

<template>
  <div>
    <canvas id="main-chart" height="400"></canvas>
  </div>
</template>

<script>
import Chart from 'chart.js'

export default {
  name: 'MainChart',
  props: ['chartData'],
  mounted() {
    const ctx = document.getElementById('main-chart');
    new Chart(ctx, this.chartData);
  }
}
</script>

这个常见的页面Analytics-test.vue.我跳过了所有的方法.

<template>
  <div> Прибыль/посещаемость <div class="small">
      <MainChart :chart-data="datacollection" />
    </div>
  </div>
</template>

<script>
import MainChart from '../MainChart.vue'
export default {
  components: { MainChart },

  data: () => ({
    flagStartDate: false,
    chartData: null,
    labels: [],
    datasets: {},
    draftData: null,
    data: [],
    datacollection: { type: 'line', },
    clubsId: ['5c3c5e12ba86198828baa4a7', '5c3c5e20ba86198828baa4c5', '60353d6edbb58a135bf41856', '61e9995d4ec0f29dc8447f81', '61e999fc4ec0f29dc844835e'],
      }),
  methods: {
...some a lot of code..},

 async mounted() {
    await this.loadIncomings(this.clubsId),
      await this.loadAvgIncomings(this.clubsId),
      await this.loadAvgPayments(this.clubsId),
      await this.loadAvgSchedule(this.clubsId),
      await this.loadTrainings(this.clubsId)

  },

</script>

方法中有19个函数,我决定在这里不输入。

hivapdat

hivapdat1#

此错误似乎与此GitHub问题中描述的错误类似:https://github.com/chartjs/Chart.js/issues/5149这听起来像是由于图表立即加载了数据而导致的,但是在关于该问题的注解中有几个解决方案。

相关问题