如何更改 ng2-charts 的颜色?
How do I change the color for ng2-charts?
我已将 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
。
是否需要导入任何其他包来更改颜色或设置错误?
Chrome html 检查器显示以下 html 呈现的输出:
ng-reflect-colors="[object Object],[object Object],[object Object]"
你应该这样做:
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)'
}];
灰色 颜色默认设置,这意味着您的颜色选项不起作用,因为属性名称错误。
这里有一个例子,如何调用颜色属性:
@更新:
如果只需要更新 backgroundColor 而不更新其他任何内容,下面的代码也可以。
public chartColors: Array<any> = [
{ // all colors in order
backgroundColor: ['#d13537', '#b000b5', '#c0ffee', ...]
}
]
你也可以这样做:
<base-chart class="chart"
[colors]="chartColors"
...
</base-chart>
和
public chartColors: any[] = [
{
backgroundColor:["#FF7360", "#6FC8CE", "#FAFFF2", "#FFFCC4", "#B9E8E0"]
}];
//.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 的回答很好。我将它从 class 的 属性 更改为一个函数,并在我的警报中将颜色设置为动态元素的对象返回。然后我使用该函数绑定我的图表颜色,这很神奇。我在这个问题上被困了几个小时!
希望这对任何试图通过 Angular 向 ng2-charts 传递动态值的人有所帮助!
如果您像我一样在 ng2-charts 的 MultiDataSet 中使用甜甜圈型图表,并且想要更改颜色,那么您需要执行以下操作:
public doughnutChartColors: Color[] = [
{backgroundColor:["#9E120E","#FF5800","#FFB414"]},
{backgroundColor:["#9E120E","#FF5800","#FFB414"]},
{backgroundColor:["#9E120E","#FF5800","#FFB414"]}
];
图表中的颜色 属性 如:
<canvas baseChart
[data]="doughnutChartData"
[labels]="doughnutChartLabels"
[chartType]="doughnutChartType"
[colors]="doughnutChartColors">
</canvas>
图表看起来像:
顺便说一句..有一个很好的答案讨论可访问的颜色。
My stackblitz 带 Doughnut Chart
演示。
可以通过图表数据设置图表颜色。有助于将特定颜色与特定系列匹配。
例如删除或忽略 [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];
}
我已将 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
。
是否需要导入任何其他包来更改颜色或设置错误?
Chrome html 检查器显示以下 html 呈现的输出:
ng-reflect-colors="[object Object],[object Object],[object Object]"
你应该这样做:
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)'
}];
灰色 颜色默认设置,这意味着您的颜色选项不起作用,因为属性名称错误。
这里有一个例子,如何调用颜色属性:
@更新:
如果只需要更新 backgroundColor 而不更新其他任何内容,下面的代码也可以。
public chartColors: Array<any> = [
{ // all colors in order
backgroundColor: ['#d13537', '#b000b5', '#c0ffee', ...]
}
]
你也可以这样做:
<base-chart class="chart"
[colors]="chartColors"
...
</base-chart>
和
public chartColors: any[] = [
{
backgroundColor:["#FF7360", "#6FC8CE", "#FAFFF2", "#FFFCC4", "#B9E8E0"]
}];
//.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 的回答很好。我将它从 class 的 属性 更改为一个函数,并在我的警报中将颜色设置为动态元素的对象返回。然后我使用该函数绑定我的图表颜色,这很神奇。我在这个问题上被困了几个小时!
希望这对任何试图通过 Angular 向 ng2-charts 传递动态值的人有所帮助!
如果您像我一样在 ng2-charts 的 MultiDataSet 中使用甜甜圈型图表,并且想要更改颜色,那么您需要执行以下操作:
public doughnutChartColors: Color[] = [
{backgroundColor:["#9E120E","#FF5800","#FFB414"]},
{backgroundColor:["#9E120E","#FF5800","#FFB414"]},
{backgroundColor:["#9E120E","#FF5800","#FFB414"]}
];
图表中的颜色 属性 如:
<canvas baseChart
[data]="doughnutChartData"
[labels]="doughnutChartLabels"
[chartType]="doughnutChartType"
[colors]="doughnutChartColors">
</canvas>
图表看起来像:
顺便说一句..有一个很好的答案
My stackblitz 带 Doughnut Chart
演示。
可以通过图表数据设置图表颜色。有助于将特定颜色与特定系列匹配。
例如删除或忽略 [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];
}