echarts的legend,如何设置选中系列颜色,代码实例
时间: 2023-06-02 09:07:35 浏览: 159
您好,关于您的问题,可以使用legend中的formatter属性来自定义legend的显示格式,其中可以使用回调函数,实现选中系列颜色的设置。具体代码实例如下:
```
legend: {
data: ['系列1', '系列2', '系列3'],
formatter: function(name) {
// 自定义legend的显示格式
return '{title|' + name + '}{value|}\n\n';
},
selectedMode: 'single', // 设置图例的选中模式为单选
selected: { // 设置默认选中的系列
'系列1': true,
'系列2': false,
'系列3': false
}
},
series: [{
name: '系列1',
type: 'bar',
data: [10, 20, 30],
itemStyle: {
normal: {
color: '#30c4e5'
},
emphasis: {
color: '#1996c9'
}
}
}, {
name: '系列2',
type: 'bar',
data: [20, 30, 40],
itemStyle: {
normal: {
color: '#ffa400'
},
emphasis: {
color: '#ff7c00'
}
}
}, {
name: '系列3',
type: 'bar',
data: [30, 40, 50],
itemStyle: {
normal: {
color: '#34afcd'
},
emphasis: {
color: '#0289cd'
}
}
}]
```
在上述代码中,我们通过设置legend的selected属性,来指定默认选中的系列。然后再通过formatter属性自定义legend的显示格式,在其中可以添加样式来实现选中系列颜色的设置。
希望对您有所帮助。如果您还有其他问题,可以继续提问。