【问题标题】:Setting color for bar chart in ng2-chart在 ng2-chart 中设置条形图的颜色
【发布时间】:2019-01-14 21:04:38
【问题描述】:

我正在尝试根据值的范围为条形图中的条形设置颜色。

TS

  public barChartData: any[];
  public chartColors: any[];
    value = [] // This array have my values from DB like  [3.5, 2.5, 1.5, 6.5, 6, 1, 6.5, 3.5, 5.5]

    this.barChartData = [{
        data: this.value,
        label: 'Insulin'
    }]

    var l = this.value.length;
    for (var i = 0; i < l; i++) {
        if (this.value[i] <= 3) {
            this.chartColors = [{
                backgroundColor: 'rgba(255, 205, 86, 9)',
                borderColor: 'rgba(255, 205, 86, 9)'
            }]
        } else if (this.value[i] > 3 && this.value[i] <= 6) {


            this.chartColors = [{
                backgroundColor: 'rgba(255, 99, 132, 62)',
                borderColor: 'rgba(255, 99, 132, 62)'
            }];

        } else if (this.value[i] > 6) {

            this.chartColors = [{
                backgroundColor: 'rgba(54, 162, 235, -12)',
                borderColor: 'rgba(54, 162, 235, -12)'

            }];

        }

    }

这行不通。谁能告诉我正确的方法

【问题讨论】:

    标签: angular typescript charts ng2-charts


    【解决方案1】:

    代码中的问题:

    if (this.value <= 3) {
    this.chartColors = [{
        backgroundColor: 'rgba(255, 99, 132, 62)'
    }]
    } 
    

    当你编写这个 if 条件时,你完全改变了 'this.chartColors' 属性。但是您的要求是仅更改满足上述条件的输入的背景颜色。 下面的示例包含编写图表颜色的正确格式。

    public chartColors: Array<any> = [
    {
      backgroundColor: ['rgba(63, 191, 127,  0.4)','rgba(191, 191, 63, 0.4)'],
    
       borderColor: ['rgba(63, 191, 127, 0.8)', 'rgba(63, 191, 191, 0.8)'],
      hoverBackgroundColor: ['rgba(63, 191, 127, 0.6)', 'rgba(63, 191, 191, 0.6)'],
      borderWidth: 2
    }];
    

    backgroundColor 或 borderColor 或 hoverBackgroundColor 包含颜色数组。每个索引值都指向 barChartData.data 对应的索引值。

    这里是 barChartData 的正确格式

     public barChartData: any[] = [{
      data: [],
      label: ''
    }];
    

    您的数据将放在 barChartData.data = [Here will be your required data]

    结论是

    this.barchartData[0].data[index]this.chartColors[0].backgroundColor[index] 作为条形颜色。

    解决方案:

    所以最后按照下面的代码你可能会解决你的问题:

    for (let index = 0; index < this.barChartData[0].data.length; index++) {
      if (this.barChartData[0].data[index] > 0 && this.barChartData[0].data[index] < 3 ) {
        console.log('>0' , this.barChartData[0].data[index]);
        this.chartColors[0].backgroundColor[index] = 'rgba(63, 191, 127,  0.4)';
      }
      if (this.barChartData[0].data[index] > 3 && this.barChartData[0].data[index] < 5) {
        console.log('>3' , this.barChartData[0].data[index]);
        this.chartColors[0].backgroundColor[index] = 'rgba(191, 63, 127, 0.4)';
      }
      if (this.barChartData[0].data[index] > 5 ) {
        console.log('>5' , this.barChartData[0].data[index]);
        this.chartColors[0].backgroundColor[index] = 'rgba(191, 127, 63,  0.4)';
      }
    }
    

    这是一个示例代码。您可以根据自己的喜好赋予价值。如果您对理解我的解释有任何困难,请随时提问。

    将下面的代码替换为您的代码:

    public barChartData: any[] = [{
        data: this.value,
        label: 'Insulin'
    }];
    public chartColors: any[] = [{
       backgroundColor =[],
       borderColor = []
    }];;
    value = [] // This array have my values from DB like  [3.5, 2.5, 1.5, 
    6.5, 6, 1, 6.5, 3.5, 5.5]
    
    var l = this.value.length;
    for (var i = 0; i < l; i++) {
        if (this.barChartData[0].data[i] <= 3) {
         this.chartColors[0].backgroundColor[i] ='rgba(255, 205, 86,9)';
         this.chartColors[0].borderColor[i] = 'rgba(255, 205, 86, 9)';           
    
        } else if (this.barChartData[0].data[i] > 3 &&        
         this.barChartData[0].data[i] <= 6) {
         this.chartColors[0].backgroundColor[i] ='rgba(255, 99, 132, 62)';
         this.chartColors[0].borderColor[i] = 'rgba(255, 99, 132, 62)';
    
        } else if (this.barChartData[0].data[i] > 6) {
         this.chartColors[0].backgroundColor[i] ='rgba(54, 162, 235,-12)';
          this.chartColors[0].borderColor[i] = 'rgba(54, 162, 235, -12)';
        }
    }
    

    【讨论】:

    • 我从后端获取 barchartData 并将其存储在数组中
    • 是的。 barChartData[0].data = [存储在这里]。常量数据栏 = []; for (let index = 0; index
    • 我现在将编辑我的问题以显示我到底做了什么
    • 遵循我的建议并更改条件主体,我在回答中已对此进行了解释。请参阅我的答案中的解决方案部分。比较您和我的代码中的 if 条件。希望你现在能得到它。
    • 数据在哪里绘制
    猜你喜欢
    • 2023-02-06
    • 1970-01-01
    • 2018-04-07
    • 2021-11-06
    • 1970-01-01
    • 1970-01-01
    • 2013-02-16
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多