javascript - 无法读取属性 'transition' null chartjs

标签 javascript ionic-framework chart.js

我有一个动态数据,需要将其放入 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 循环。有没有其他方法可以解决这个问题?

最佳答案

一些明显的问题是 backgroundColorborderColor 的长度如何小于 this.monthcoSTLabor 导致 通过 bard 迭代器加载时未定义的 值。

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

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(); */

关于javascript - 无法读取属性 'transition' null chartjs,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54566061/

相关文章:

ionic-framework - 使用 Target SDK 版本 30 创建 ionic 3 应用程序,在 google play 控制台中出现错误

javascript - 如何使用 Chart.js 向图表添加结束线?

javascript - Chart JS 图例样式

javascript - 传递给 Chart.js 的 PHP 字符串数据渲染不佳

javascript - JS 定位特定 div 中的图像,即使它们共享类

javascript - 全日历,点击按钮删除事件

html - 我怎样才能修复我的虚线边框只发生在 ios 而不是 android 上的错误?

javascript - ionic 或更好的 UI 元素中的可点击切换解决方案

javascript - jQuery 之类的自定义函数

javascript - jQuery 表单验证在失败时不显示错误