antv G2Plot条形图使用color属性使用
G2Plot条形图的color属性用于指定不同类别的颜色。在使用color属性时,可以使用以下两种方式:
- 使用字符串数组指定颜色
例如,将不同类别分别指定为红色、绿色和蓝色:
const data = [
{ type: 'A', value: 10 },
{ type: 'B', value: 20 },
{ type: 'C', value: 30 }
];
const chart = new Bar('container', {
data,
xField: 'type',
yField: 'value',
color: ['red', 'green', 'blue']
});
chart.render();
- 使用函数指定颜色
可以使用一个函数,根据数据的某个属性值返回对应的颜色。例如,将type为A的数据项颜色设为红色,其余数据项颜色设为蓝色:
const data = [
{ type: 'A', value: 10 },
{ type: 'B', value: 20 },
{ type: 'C', value: 30 }
];
const chart = new Bar('container', {
data,
xField: 'type',
yField: 'value',
color: (data) => {
if (data.type === 'A') {
return 'red';
} else {
return 'blue';
}
}
});
chart.render();
原文地址: https://www.cveoy.top/t/topic/FeY 著作权归作者所有。请勿转载和采集!