不能根据值改变柱形图的颜色
Cannot change the color of column chart according to its value
我需要完全使用它来实现这段代码。这是我的代码示例:
barChartOptions: ChartOptions = {
responsive: true,
};
barChartColors: Color[] = [
{
backgroundColor: 'forestgreen',
borderColor: 'white',
borderWidth: 3
}, {
backgroundColor: 'red',
borderColor: 'white',
borderWidth: 3
}];
barChartLabels: Label[] = ['1', '2', '3', '4', '5', '6', '7', '8', '9', '10', '11', '12', '13', '14', '15',
'16', '17', '18', '19', '20', '21', '22', '23', '24', '25', '26', '27', '28', '29', '30', '31'];
barChartType: ChartType = 'bar';
barChartLegend = false;
barChartPlugins = [];
barChartData: ChartDataSets[] = [
{
data: [11, 12, 12, 14, 14, 19, 22, 28, 29, -30, 31, 31, 35, 35, 35, -37, 37, 40, 40, 41, 45, 45, 48, 51, 57,
62, 65, 69, 69, 71, 75]
}
];
这是我的 HTML 代码,但它不起作用:
<div class="chart-wrapper" style="width: 100%">
<canvas baseChart
[colors]="barChartData>0 ? barChartColors[0] : barChartColors[1]"
[datasets]="barChartData"
[labels]="barChartLabels"
[options]="barChartOptions"
[plugins]="barChartPlugins"
[legend]="barChartLegend"
[chartType]="barChartType">
</canvas>
</div>
[colors]="barChartData>0 ? barChartColors[0] : barChartColors[1]" 这一行给了我这个错误:
- 类型颜色不可分配给类型颜色[]
- DashboardComponent.barChartData:ChartDataSets[]
- DashboardComponent.barChartColors: 颜色[]
另外,编译器给我这个:
运算符 '>' 不能应用于类型 'ChartDataSets[]' 和 'number'.[colors]="barChartData>0 ? barChartColors[[=44=]] : barChartColors ['red']"
你能帮我解决这个问题吗?
barChartData
是一个包含单个元素的 ChartDataSets
数组。因此,要使其正常工作,您可以按如下方式更改代码:
[colors]="barChartData[0].data.map(v => v > 0 ? barChartColors[0] : barChartColors[1])"
或更紧凑...
[colors]="barChartData[0].data.map(v => barChartColors[v > 0 ? 0 : 1])"
This code uses Array.map()
, on the data
array inside ChartDataSets
. It returns a new array
with the appropriate color for each value.
我需要完全使用它来实现这段代码。这是我的代码示例:
barChartOptions: ChartOptions = {
responsive: true,
};
barChartColors: Color[] = [
{
backgroundColor: 'forestgreen',
borderColor: 'white',
borderWidth: 3
}, {
backgroundColor: 'red',
borderColor: 'white',
borderWidth: 3
}];
barChartLabels: Label[] = ['1', '2', '3', '4', '5', '6', '7', '8', '9', '10', '11', '12', '13', '14', '15',
'16', '17', '18', '19', '20', '21', '22', '23', '24', '25', '26', '27', '28', '29', '30', '31'];
barChartType: ChartType = 'bar';
barChartLegend = false;
barChartPlugins = [];
barChartData: ChartDataSets[] = [
{
data: [11, 12, 12, 14, 14, 19, 22, 28, 29, -30, 31, 31, 35, 35, 35, -37, 37, 40, 40, 41, 45, 45, 48, 51, 57,
62, 65, 69, 69, 71, 75]
}
];
这是我的 HTML 代码,但它不起作用:
<div class="chart-wrapper" style="width: 100%">
<canvas baseChart
[colors]="barChartData>0 ? barChartColors[0] : barChartColors[1]"
[datasets]="barChartData"
[labels]="barChartLabels"
[options]="barChartOptions"
[plugins]="barChartPlugins"
[legend]="barChartLegend"
[chartType]="barChartType">
</canvas>
</div>
[colors]="barChartData>0 ? barChartColors[0] : barChartColors[1]" 这一行给了我这个错误:
- 类型颜色不可分配给类型颜色[]
- DashboardComponent.barChartData:ChartDataSets[]
- DashboardComponent.barChartColors: 颜色[]
另外,编译器给我这个:
运算符 '>' 不能应用于类型 'ChartDataSets[]' 和 'number'.[colors]="barChartData>0 ? barChartColors[[=44=]] : barChartColors ['red']"
你能帮我解决这个问题吗?
barChartData
是一个包含单个元素的 ChartDataSets
数组。因此,要使其正常工作,您可以按如下方式更改代码:
[colors]="barChartData[0].data.map(v => v > 0 ? barChartColors[0] : barChartColors[1])"
或更紧凑...
[colors]="barChartData[0].data.map(v => barChartColors[v > 0 ? 0 : 1])"
This code uses
Array.map()
, on thedata
array insideChartDataSets
. It returns a newarray
with the appropriate color for each value.