【问题标题】:Angular 4: Different color for bar in bar chart dynamically using ChartJSAngular 4:使用ChartJS动态地为条形图中的条形图提供不同的颜色
【发布时间】:2017-12-16 21:38:45
【问题描述】:

我想为条形图中的每个条形设置不同的颜色。我从后端 api 获得颜色。

我试过了:

<div>
  <div style="display: block">
    <canvas class="dashboard-tile-chart" baseChart
            [datasets]="barChartData"
            [labels]="barChartLabels"
            [options]="barChartOptions"
            [colors]="barChartColors"
            [legend]="barChartLegend"
            [chartType]="barChartType"
            (chartHover)="chartHovered($event)"
            (chartClick)="chartClicked($event)"></canvas>
  </div>
</div>

.componenet.ts

export class BarGraphComponent implements OnInit {

    // initially setting some dummy colors: this works.
    public barChartColors: Array<any> = [{
        backgroundColor: [
           '#5cb85c',
           '#65C6BB',
           '#1BBC9B',
           '#f0ad4e',
           '#d9534f',
           '#5cb85c',
           '#f0ad4e'
        ]
    }];

    // but when I try to set the colors dynamically like below it's not working. Dummy colors that I initially set are still showing
    setGraphData() {
        const colors = [];
        this.vitalValues.forEach(value => {
            colors.push(value.color);
        });
        console.log('colors------');
        console.log(colors);
        const barColors: Array<any> = [{
            backgroundColor: colors
        }];

        this.barChartColors = barColors;
    }
}

任何帮助表示赞赏..

【问题讨论】:

    标签: angularjs colors bar-chart chart.js


    【解决方案1】:

    你可以试试

    public barChartColors: Array<any> = 
            [
               '#5cb85c',
               '#65C6BB',
               '#1BBC9B',
               '#f0ad4e',
               '#d9534f',
               '#5cb85c',
               '#f0ad4e'
            ];
    

    【讨论】:

      猜你喜欢
      • 2015-08-09
      • 2014-10-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-04-07
      相关资源
      最近更新 更多