基于y轴值的角度图js条形颜色

时间:2019-06-13 11:06:41

标签: angular ng2-charts chartjs-2.6.0

我正在使用角形材料,对于图表,我借助ng2-charts使用Chart.js。

所以,我想要一个这样的图表

https://www.screencast.com/t/JQpzuohgWd

为进一步澄清,我需要用角形

https://github.com/chartjs/Chart.js/issues/5302(最后的小提琴)

这也很不清楚(最终代码太乱了)

Setting color for bar chart in ng2-chart

代码:TS文件

ngOnInit() {

var myData = [2, 10, 5, 2, 20, 30, 9];

var ctx = document.getElementById('myChart').getContext('2d');
var chart = new Chart(ctx, {
  // The type of chart we want to create
  type: 'bar',

  // The data for our dataset
  data: {
    labels: ["January", "February", "March", "April", "May", "June", "July"],
    datasets: [{
      label: "Monthly goal reached",
      backgroundColor: "green",
      data: myData.map(function (value) {
        return value >= 10 ? value : null
      }),
    },
    {
      label: "Monthly goal not reached",
      backgroundColor: "red",
      data: myData.map(function (value) {
        return value < 10 ? value : null
      }),
    }
    ]
  },

  // Configuration options go here
  options: {
    scales: {
      xAxes: [{
        stacked: true
      }],
      yAxes: [{
        stacked: true
      }]
    }

  }
});

错误:

Property 'getContext' does not exist on type 'HTMLElement'.

1 个答案:

答案 0 :(得分:0)

这对我有用

        for (let index = 0; index < this.barChartData[0].data.length; index++) {
          if (this.barChartData[0].data[index] <= 600) {
            this.chartColors[0].backgroundColor[index] = 'rgba(255, 205, 86,9)';
          }
          if (this.barChartData[0].data[index] > 600 && this.barChartData[0].data[index] <= 1200) {
            this.chartColors[0].backgroundColor[index] = 'rgba(255, 99, 132, 62)';
          }
          if (this.barChartData[0].data[index] > 1200) {
            this.chartColors[0].backgroundColor[index] = 'rgba(54, 162, 235)';
          }
        }

我给出了一个示例值。以此为参考并采用您的代码