angular - ng2-charts & chart.js - 更改背景颜色

标签 angular twitter-bootstrap charts chart.js

我正在使用 Angular 6,并尝试更改我刚刚使用 chart.js 创建的圆环图的背景颜色。

我一直在关注此处完成的示例:https://www.js-tutorials.com/angularjs-tutorial/angular-6-chart-tutorial-using-chart-js/

但是,无论我如何尝试更改背景颜色,无论是示例中显示的方式还是其他方式,颜色始终与库提供的默认颜色相同。

谁能帮我展示一种方法来覆盖它,好吗?

组件.html:

  <canvas baseChart
          [data]="doughnutChartData"
          [labels]="doughnutChartLabels"
          [chartType]="doughnutChartType"
          [options]="doughnutChartOptions"
          (chartHover)="chartHovered($event)"
          (chartClick)="chartClicked($event)"></canvas>

组件.ts:

  public doughnutChartLabels: string[] = ['Running', 'Paused', 'Stopped'];
  public doughnutChartData: number[] = [this.activeProducers, this.pausedProducers, this.invalidProducers];
  public doughnutChartType = 'doughnut';
  public doughnutChartOptions: any = {
    backgroundColor: [
      'Red',
      'Yellow',
      'Blue',
    ],
    responsive: false,
  };

enter image description here

我想要的颜色是:

  • 运行:#ced
  • 暂停:#fda
  • 停止:#fdd

创建了一个 stackblitz:https://stackblitz.com/edit/angular-ctydcu

最佳答案

添加以下属性:

 private donutColors = [
    {
      backgroundColor: [
        '#ced',
        '#fda',
        '#fdd',
      ]
    }
  ];

注意这是数组,不是对象

然后,在模板中添加以下属性

[colors]="donutColors"

Here is a Stackblitz demo

关于angular - ng2-charts & chart.js - 更改背景颜色,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53253167/

相关文章:

Angular-CLI 和 ThreeJS

html - 如何使按钮包裹图像并使用 Bootstrap 减少它们之间的间距?

ios - 快速图表

javascript - d3.js 带有类别和数字的数组

javascript - 谷歌饼图百分比计算

Angular2 我可以得到 ngFor 的计数吗?

javascript - 具有动态内容的 Angular 通用更新元标记

javascript - Angular 6按日期排序对象数组

jquery - 如何动态设置 bootstrap-datepicker 日期值?

twitter-bootstrap - 推特 Bootstrap : Hide sub-menus in small screens