ng2-chart – 柱状图颜色定制未生效

huangapple go评论46阅读模式
英文:

ng2-chart - barChart color customization not working

问题

Angular 14, ChartJS: 4.1.1, NG2-Charts: 4.1.1.
当尝试自定义条形图颜色时,我遇到了一些问题。我还尝试了ng2-charts 2.0.0。目前,我得到了默认颜色。

public barChartColors: Array = [{
backgroundColor: ['#fc5858', '#19d863', '#fdf57d'],
borderColor: ['rgba(252, 235, 89, 0.2)', 'rgba(77, 152, 202, 0.2)', 'rgba(241, 107, 119, 0.2)']
}];


英文:

Angular 14, ChartJS: 4.1.1, NG2-Charts: 4.1.1.
I am facing some trouble when trying to customize bar chart colors. I also tried ng2-charts 2.0.0. As of now I am getting default colors.

  public barChartColors: Array < any > = [{
   backgroundColor: ['#fc5858', '#19d863', '#fdf57d'],
   borderColor: ['rgba(252, 235, 89, 0.2)', 'rgba(77, 152, 202, 0.2)', 'rgba(241, 107, 119, 0.2)']
  }];

<div style="display: block; width: 900px; ">
    <canvas baseChart
        [data]="barChartData"
        [options]="barChartOptions"   
        [colors]="barChartColors"
        [type]="chartType">     
    </canvas>
</div>

答案1

得分: 1

对于ng2-charts 4.1.1版本。

从画布中移除[colors]。
并将barChartData更新如下:

this.barChartData = {
    labels: this.barchart_Labels,     
    datasets: [
        { data: this.dataset1, label: 'Label1', backgroundColor: ['#fc5858'] },
        { data: this.dataset2, label: 'Label2', backgroundColor: ['#19d863'], },
        { data: this.dataset3, label: 'Label3', backgroundColor: ['#fdf57d'] }    
    ]     
};
英文:

For ng2-charts 4.1.1.

Remove [colors] from canvas.
And update barChartData as below :

this.barChartData = {
    labels: this.barchart_Labels,     
      datasets: [
        { data: this.dataset1, label: 'Label1', backgroundColor: [
          '#fc5858'        
        ] },
        { data: this.dataset2, label: 'Label2', backgroundColor: [
          '#19d863'        
        ], },
        { data: this.dataset3, label: 'Label3', backgroundColor: [         
          '#fdf57d'      
        ] }    
      ]     
    };

huangapple
  • 本文由 发表于 2023年2月6日 11:39:20
  • 转载请务必保留本文链接:https://go.coder-hub.com/75357141.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定