【问题标题】:How do I change the color for ng2-charts?如何更改 ng2 图表的颜色?
【发布时间】:2017-02-11 11:21:05
【问题描述】:

我已将 ng2-charts 添加到我的项目中并显示 2 个图表 - 圆环图和条形图。自从我添加后,两者都显示为灰色

    <base-chart class="chart"
                [colors]="chartColors"
                ...
    </base-chart> 

component.template.html,和

public chartColors:Array<any> =[
    {
      fillColor:'rgba(225,10,24,0.2)',
      strokeColor:'rgba(11,255,20,1)',
      pointColor:'rgba(111,200,200,1)',
      pointStrokeColor:'#fff',
      pointHighlightFill:'#fff',
      pointHighlightStroke:'rgba(200,100,10,0.8)'
    }, ... (3x)

component.ts

是否需要任何其他包导入来更改颜色或设置错误?

Chromes html 检查器显示以下呈现的 html 输出:

ng-reflect-colors="[object Object],[object Object],[object Object]"

【问题讨论】:

    标签: angular ng2-charts


    【解决方案1】:

    你应该这样做:

      public chartColors: Array<any> = [
        { // first color
          backgroundColor: 'rgba(225,10,24,0.2)',
          borderColor: 'rgba(225,10,24,0.2)',
          pointBackgroundColor: 'rgba(225,10,24,0.2)',
          pointBorderColor: '#fff',
          pointHoverBackgroundColor: '#fff',
          pointHoverBorderColor: 'rgba(225,10,24,0.2)'
        },
        { // second color
          backgroundColor: 'rgba(225,10,24,0.2)',
          borderColor: 'rgba(225,10,24,0.2)',
          pointBackgroundColor: 'rgba(225,10,24,0.2)',
          pointBorderColor: '#fff',
          pointHoverBackgroundColor: '#fff',
          pointHoverBorderColor: 'rgba(225,10,24,0.2)'
        }];
    

    灰色默认设置,这意味着您的颜色选项不起作用,因为属性名称错误。

    这里有一个例子,如何调用颜色属性:

    ng2-charts-github-source-code

    @UPDATE:

    如果只需要更新 backgroundColor 而不需要更新,下面的代码也可以。

    public chartColors: Array<any> = [
        { // all colors in order
          backgroundColor: ['#d13537', '#b000b5', '#c0ffee', ...]
        }
    ]
    

    【讨论】:

    • @ulon 在我的例子中它只显示第一种颜色
    • @ulou 有什么办法可以改变甜甜圈图的颜色吗?。
    • 有没有办法改变图例颜色?在我的用例中,我的 Web 应用程序有一个深色主题,并且不知道如何为 ng2-chart 设置深色主题,或者至少将图例文本颜色更改为白色。我正在使用 angular 和 ng2-charts 顺便说一句。
    【解决方案2】:

    可以通过图表数据设置图表颜色。有助于将特定颜色与特定系列匹配。

    例如删除或忽略 [colors] 属性和数据规范

    <canvas style="height: 600px" baseChart
            chartType="bar"
            [datasets]="chartData"
    

    设置图表数据

    [
      {
        "data": [
          {
            "y": 18353021615,
            "t": 2014
          },
        ],
        "label": "Energy",
        "backgroundColor": "rgb(229,229,229)",
        "pointBackgroundColor": "rgba(229, 229, 229, 1)",
        "pointHoverBackgroundColor": "rgba(151,187,205,1)",
        "borderColor": "rgba(0,0,0,0)",
        "pointBorderColor": "#fff",
        "pointHoverBorderColor": "rgba(151,187,205,1)"
      },
    

    所以下面的测试会通过

    expect(component.chartData[0].backgroundColor).toBe('rgba(242,200,124,1)');
    

    我有一组默认颜色的模板

    public readonly localChartColours: Color[] = [
    {
      backgroundColor: 'rgb(78,180,189)',
      pointBackgroundColor: 'rgba(78, 180, 189, 1)',
      pointHoverBackgroundColor: 'rgba(151,187,205,1)',
      borderColor: 'rgba(0,0,0,0)',
      pointBorderColor: '#fff',
      pointHoverBorderColor: 'rgba(151,187,205,1)'
    }, {
    

    设置数据时设置颜色

    const chartDataPrototype = {data: points, label: seriesLabel};
    const coloursForItem = this.findColours(this.chartData.length);
    Object.keys(coloursForItem).forEach(colourProperty => {
      chartDataPrototype[colourProperty] = coloursForItem[colourProperty];
    });
    this.chartData.push(chartDataPrototype);
    

    并根据需要在特定的基础上覆盖它们

    this.chartData.forEach(item => {
      if (uopColours[item.label]) {
          item.backgroundColor = uopColours[item.label];
      }
    

    【讨论】:

      【解决方案3】:

      如果您像我一样使用带有 ng2-charts 的 MultiDataSet 的圆环型图表,并且想要更改颜色,那么您需要执行以下操作:

      public doughnutChartColors: Color[] = [
        {backgroundColor:["#9E120E","#FF5800","#FFB414"]},
        {backgroundColor:["#9E120E","#FF5800","#FFB414"]},
        {backgroundColor:["#9E120E","#FF5800","#FFB414"]}
      ];
      

      图表中的colors属性如下:

      <canvas baseChart
        [data]="doughnutChartData"
        [labels]="doughnutChartLabels"
        [chartType]="doughnutChartType"
        [colors]="doughnutChartColors">
      </canvas>
      

      图表如下:

      顺便说一句.. 有一个很好的答案 here 讨论可访问的颜色。

      My stackblitzDoughnut Chart 演示。

      【讨论】:

      • 有没有办法改变图例颜色?在我的用例中,我的 Web 应用程序有一个深色主题,并且不知道如何为 ng2-chart 设置深色主题,或者至少将图例文本颜色更改为白色。我正在使用角度和 ng2-charts 顺便说一句
      • 更新了我的 stackblitz 链接的答案。阅读有关ThemeService:valor-software.com/ng2-charts 的文档,看看你是否可以让它在那里工作。然后分享你的叉子。
      【解决方案4】:
      //.ts_file     
      
          public chartColors() {
                  return [{
                    backgroundColor: this.alert.severityColor,
                    borderColor: 'rgba(225,10,24,0.2)',
                    pointBackgroundColor: 'rgba(225,10,24,0.2)',
                    pointBorderColor: '#fff',
                    pointHoverBackgroundColor: '#fff',
                    pointHoverBorderColor: 'rgba(225,10,24,0.2)'
                }]
              }
      
      
      //.html_file
      
      <div style="display: block">
                <canvas baseChart
                        [datasets]="barChartData()"
                        [options]="barChartOptions"
                        [legend]="barChartLegend"
                        [chartType]="barChartType"
                        [colors]="chartColors()"
                        (chartHover)="chartHovered($event)"
                        (chartClick)="chartClicked($event)"></canvas>
              </div>
      

      我需要根据附加到警报的值和颜色动态更改图表的颜色。我发现@uluo 有一个很好的答案。我将其从类的属性更改为函数,并将颜色设置为警报中动态元素的对象返回。然后我用这个函数来绑定我的图表颜色,这很神奇。我在这个问题上被困了几个小时!

      希望这对尝试使用 Angular 将动态值传递给 ng2-charts 的任何人有所帮助!

      【讨论】:

        【解决方案5】:

        你也可以这样做:

        <base-chart class="chart"
                    [colors]="chartColors"
                    ...
        </base-chart>
        

        public chartColors: any[] = [
              { 
                backgroundColor:["#FF7360", "#6FC8CE", "#FAFFF2", "#FFFCC4", "#B9E8E0"] 
              }];
        

        【讨论】:

        • 两个小时后,这就是答案。他们在其他任何地方都没有显示将数组放入数组中对象的 backgroundColor 属性。在这篇文章之前,我已经走到了尽头!
        • 刚才对我有用的一个细微变化:[ { backgroundColor: '#FF7360' }, { backgroundColor: '#6FC8Ce' }, ... ]
        猜你喜欢
        • 2021-03-12
        • 1970-01-01
        • 2021-12-08
        • 1970-01-01
        • 2020-09-08
        • 2020-03-20
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多