在Chart.js中使用自己的标签分离数据

时间:2019-02-07 07:48:20

标签: javascript ionic-framework chart.js

我在barchart中有动态数据,并且动态需要一个标签,它应该可以工作,但是问题是它显示了标签数,并且输出的数据与此image相关。

下面是我的工作代码

    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,
        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
            }
          }]
        }
      }
    });

0 个答案:

没有答案