Chart.js在重新加载时未加载

fv2wmkja  于 2022-12-13  发布在  Chart.js
关注(0)|答案(1)|浏览(194)

我有一个相当奇怪的问题与Chart.js在流星。
我在页面上有一个图表,它使用Session.get('values');来获取数据。如果你使用菜单更改路线,这个功能很好。但是如果你按下页面上的重新加载按钮,图表会加载,但数据不会加载,如果我将鼠标悬停在图表上的数据点上,它只显示标签,没有任何数字或任何东西。
如果我在控制台中运行Session.get('values');,它会很好地返回数据,只要我单击一个菜单项,然后单击其中有图表的菜单项,它就会很好地加载!
我使用Template.templatename.onRendered();加载图表数据,否则图表根本不会加载。
任何帮助都是感激不尽的!

vuv7lop3

vuv7lop31#

Found a simple solution to this.
Just declare a new variable, say for example chartUpdated . Then in your created or mounted hook, after you have updated the dynamic values to the chart object, update this.chartUpdated to true.

data() {
    return {
      chartUpdated: false,
      areaChartData: {
        labels: [],
        datasets: [
          {
            label: '',
            data: [],
            borderColor: colors.themeColor1,
            pointBackgroundColor: colors.foregroundColor,
            pointBorderColor: colors.themeColor1,
            pointHoverBackgroundColor: colors.themeColor1,
            pointHoverBorderColor: colors.foregroundColor,
            pointRadius: 4,
            pointBorderWidth: 2,
            pointHoverRadius: 5,
            fill: true,
            borderWidth: 2,
            backgroundColor: colors.themeColor1_10
          }
        ]
      }
    };
  },
  async created() {
    const response = await axios.get('https://yourapi.com')
    const data = response.data.data
    this.areaChartData.labels = data.map((x) => x.day)
    this.areaChartData.datasets[0].data = data.map((x) => x.total_member)
    this.chartUpdated = true
  }

Then in your template

<area-chart v-if="chartUpdated" :data="areaChartData" container-class="chart" shadow />

相关问题