【问题标题】:Cannot read property 'transition' null chartjs无法读取属性 'transition' null chartjs
【发布时间】:2021-06-27 03:08:34
【问题描述】:

我有一个动态数据需要放入 for 循环中以显示感兴趣的数据。我测试了将这个issue 中的动画放入 0 并尝试了相同的 update() 函数。

这是我下面的代码

barChartMonth() {
  let backgroundColor: any = [
    'rgba(255, 99, 132, 0.2)',
    'rgba(54, 162, 235, 0.2)',
    'rgba(255, 206, 86, 0.2)',
    'rgba(75, 192, 192, 0.2)',
    'rgba(153, 102, 255, 0.2)',
    'rgba(255, 159, 64, 0.2)',
    'rgba(255, 99, 132, 0.2)',
    'rgba(54, 162, 235, 0.2)',
    'rgba(255, 206, 86, 0.2)',
    'rgba(75, 192, 192, 0.2)',
    'rgba(153, 102, 255, 0.2)',
    'rgba(255, 159, 64, 0.2)'
  ];

  let borderColor: any = [
    'rgba(255,99,132,1)',
    'rgba(54, 162, 235, 1)',
    'rgba(255, 206, 86, 1)',
    'rgba(75, 192, 192, 1)',
    'rgba(153, 102, 255, 1)',
    'rgba(255, 159, 64, 1)',
    'rgba(255,99,132,1)',
    'rgba(54, 162, 235, 1)',
    'rgba(255, 206, 86, 1)',
    'rgba(75, 192, 192, 1)',
    'rgba(153, 102, 255, 1)',
    'rgba(255, 159, 64, 1)'
  ];


  this.barChart = new Chart(this.barCanvas.nativeElement, {
    type: 'bar',
    data: {
      labels: this.monthcostlabor,
    },
    options: {
      scales: {
        yAxes: [{
          ticks: {
            beginAtZero: true
          }
        }]
      }
    }
  });
  for(let bard = 0; bard < this.monthcostlabor.length; bard++){
    this.barChart.data.datasets.push({ label: this.monthcostlabor[bard], backgroundColor: backgroundColor[bard], borderColor: borderColor[bard], borderWidth: 1 });
  }
  this.barChart.data.datasets.forEach(element => {
    element.data.push(this.monthcostglobal)
  });
  this.barChart.update();
}

上面的代码根据记录的数据动态选择颜色、悬停和悬停。请参阅 for 循环。有没有其他方法可以解决这个问题?

【问题讨论】:

    标签: javascript ionic-framework chart.js


    【解决方案1】:

    一些明显的问题是backgroundColorborderColor 的长度如何小于this.monthcostlabor,从而在通过bard 迭代器加载时导致undefined 值。

    另外,为什么不能在构造函数中循环?

    this.barChart = new Chart(this.barCanvas.nativeElement, {
      type: 'bar',
      data: {
        labels: this.monthcostlabor,
        datasets: this.monthcostlabor.map((elem,i) => ({ 
             label: elem, 
             backgroundColor: backgroundColor[(i % backgroundColor.length)], 
             borderColor: borderColor[(i % borderColor.length)], 
             borderWidth: 1, 
             data:[this.monthcostglobal] })); // the second push() can be included here?    
      },
      options: {
        scales: {
          yAxes: [{
            ticks: {
              beginAtZero: true
            }
          }]
        }
      }
    });
    
    /*
      for(let bard = 0; bard < this.monthcostlabor.length; bard++){
      this.barChart.data.datasets.push({ label: this.monthcostlabor[bard], backgroundColor: backgroundColor[bard], borderColor: borderColor[bard], borderWidth: 1 });
    }
    this.barChart.data.datasets.forEach(element => {
      element.data.push(this.monthcostglobal)
    });
    this.barChart.update(); */
    

    【讨论】:

    • monthcostlabor 数据是 this = [February,June] 和 monthcostglobal is this = [2550,4200] 仍然没有显示输出背景颜色和边框颜色我假设它总是有最大值在数组中
    • 我不认为我可以通过构造函数循环我也是初学者
    • 还有1件事,我怎么只有左侧的条形图请看图片image
    【解决方案2】:

    我猜这个问题与异步 api 调用有关

    我解决了:

      async setChart () {
        this.myChart = await this.apiService.setChartData(this.myChart, this.myCanvas);
        this.myChart.update();
      }
    
    
      async this.apiService.setChartData(mChart, mCanvas) {
        mChart.data = this.getChrData(mLabel, dtSets, this.colourList, this.colourBordList, cType);
        mChart.data.labels = mLabel;
        return mChart;
      }
    

    【讨论】:

      【解决方案3】:

      同样的问题发生在我身上。我使用 setTimeout(function(){chart.update()},50) 解决了它,但您的解决方案更优雅。 不错

      【讨论】:

        猜你喜欢
        • 2019-06-28
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2020-01-29
        • 2016-11-24
        相关资源
        最近更新 更多