【问题标题】:How do I change the colours of specific labels in ng2-charts?如何更改 ng2 图表中特定标签的颜色?
【发布时间】:2020-01-24 15:38:24
【问题描述】:

我在 Angular 8 中使用 ng2-charts 制作了一个圆环图。我的圆环图中有 3 个标签,“通过”、“失败”和“打开”。我想分别为这些标签设置“绿色”、“红色”和“蓝色”。我该怎么做呢? 我的 compliance.component.html 文件

<div>
    <div>
        <div style="display: block">
            <canvas baseChart 
            [data]="doughnutChartData" 
            [labels]="doughnutChartLabels" 
            [chartType]="doughnutChartType"
            [options]="doughnutChartOptions"
            [colors]="doughnutChartColor">
            </canvas>
        </div>
    </div>
</div>

我的 compliance.component.ts 文件

export class ComplianceComponent implements OnInit {

  public doughnutChartLabels: Label[] = ['Passed', 'Open', 'Fail'];
  public doughnutChartData: MultiDataSet = [
    [30, 20, 50]
  ];

  public doughnutChartType: ChartType = 'doughnut';
  constructor() { }

  ngOnInit() {
  }

  public doughnutChartColor: Array<any> = [
    { // first color
      'Passed'-color:green;
      'Failed'-color:red;
      'Open'-color:blue;
    }];

    public doughnutChartOptions={
      responsive: true
    };
}

谢谢。

【问题讨论】:

    标签: javascript angular typescript chart.js ng2-charts


    【解决方案1】:

    colors必须定义为arrayinterfaceColor根据以下代码sn-p:

    import { Color } from 'ng2-charts';
    ...
    public doughnutChartColor: Color[] = [
        { backgroundColor: 'green' },
        { backgroundColor: 'red' },
        { backgroundColor: 'blue' }
    ];
    

    【讨论】:

      猜你喜欢
      • 2021-03-12
      • 2017-02-11
      • 2020-09-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-09-13
      • 1970-01-01
      相关资源
      最近更新 更多