javascript - 在 Angular 4 中更改饼图的颜色

标签 javascript css angular pie-chart

我想更改 Angular 4 中饼图的默认颜色。我的代码很长,但这是我遵循但无法正常工作的结构。

.ts 文件:

// .ts file

export class InfoComponent implements OnInit {
    pieChartLabels: string[];
    pieChartData: number[];
    pieChartType: string = 'pie';
    pieChartLegend: boolean = false;
    pieChartColors: any[];
}

ngOnInit() {
    this.pieChartLabels = ['label 1','label 2', 'label 3', 'label 4', ...];
    this.pieChartData = [data 1, data 2, data 3, .... ];
    this.pieChartColors =
        [
            {backgroundColor:"#F42A0A",borderColor: "rgba(148,159,177,1)" },
            {backgroundColor:"#95382A",borderColor: "rgba(148,159,177,1)"},
            {backgroundColor:"#97bbzd",borderColor: "rgba(148,159,177,1)"},
            {backgroundColor:"#97bbad",borderColor: "rgba(148,159,177,1)"},
            {backgroundColor:"#97bb0d",borderColor: "rgba(148,159,177,1)"},
            {backgroundColor:"#97bb1d",borderColor: "rgba(148,159,177,1)"},
            {backgroundColor:"#97bb6d",borderColor: "rgba(148,159,177,1)"},
            {backgroundColor:"#97bb7d",borderColor: "rgba(148,159,177,1)"},
            {backgroundColor:"#97bb8d",borderColor: "rgba(148,159,177,1)"},
            {backgroundColor:"#97bb9d",borderColor: "rgba(148,159,177,1)"}
        ];
    
    
// .html file


<canvas style="margin-top: 10px; margin-bottom: 10px;" baseChart [data]="pieChartData"
                        [labels]="pieChartLabels" [chartType]="pieChartType"
                        [legend]="pieChartLegend" [colors]="pieChartColors">
</canvas>






    
    
    

最佳答案

通过从 ChartsModule 导入“ng2-charts”解决了这个问题。

关于javascript - 在 Angular 4 中更改饼图的颜色,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48341892/

相关文章:

html - 无法在可滚动表中对齐 thead 和 tbody

html - IE 与 Chrome 中的高度差异 (CSS)

javascript - 在 Angular2 中嵌套 NgIf 和(单击)

angular - 使用 Gradle 的 spring bootRun 和 Angular buildWatch

javascript - 如何通过钩子(Hook)更新 react 中的对象状态

javascript - 如何通过 Bootstrap 在弹出按钮中制作内容文本 "rtl"

javascript - while 循环。无法对 while 语句进行硬编码

jquery - 基本的 Jquery slider 修改

angular - 来自@ionic/angular (Ionic 4) 的 "IonApp"类是来自 ionic-angular (Ionic 3) 的新 "App"类吗?

javascript - 如何在图表值中附加百分号?