1、遇到问题
在echarts雷达图中将tooltip.trigger
设置为axis
时失效,雷达图并不会显示单轴数据,而是整个雷达图的数据
2、实现效果
先贴出我们想要的效果,和折线图柱状图类似的,希望可以在一个轴线上能显示雷达图在这个轴线维度上的数据:
实现效果3、源码
可以直接在echarts官网运行查看效果的代码:
const dataList = [
{
name: '小明',
data: [54, 32, 56, 30, 95, 35],
color: '#5470c6',
unit: '分'
},
{
name: '小红',
data: [84, 63, 23, 59, 78, 95],
color: '#91cc75',
unit: '分'
},
{
name: '小刚',
data: [14, 98, 63, 74, 40, 67],
color: '#fac858',
unit: '分'
}
]
const indicator = [
{ name: '语文', max: 100 },
{ name: '数学', max: 100 },
{ name: '英语', max: 100 },
{ name: '物理', max: 100 },
{ name: '化学', max: 100 },
{ name: '生物', max: 100 }
]
const buildSeries = function (dataIndex) {
const data = dataList[dataIndex].data
const helper = data.map((item, index) => {
const arr = new Array(data.length)
arr.splice(index, 1, item)
return arr
})
return [data, ...helper].map((item, index) => {
return {
name: dataList[dataIndex].name,
type: 'radar',
symbol: index === 0 ? 'circle' : 'none',
symbolSize: 4,
itemStyle: {
color: '#fff'
},
lineStyle: {
color: index === 0 ? dataList[dataIndex].color : 'transparent'
},
areaStyle: {
color: index === 0 ? dataList[dataIndex].color : 'transparent',
opacity: 0.5
},
tooltip: {
show: index === 0 ? false : true,
formatter: () => {
let res = indicator[index - 1].name + ':<br>'
for (let x of dataList) {
let str = '<i style="display: inline-block;width: 10px;height: 10px;background: ' +
x.color + ';margin-right: 5px;border-radius: 50%;}"></i>' +
x.name + ':' + x.data[index - 1] + x.unit + '<br>'
res += str
}
return res
}
},
z: index === 0 ? 1 : 2,
data: [item]
}
})
}
const series = []
for (let i in dataList) {
series.push(...buildSeries(i))
}
option = {
tooltip: {},
radar: {
indicator: indicator
},
series: series
}
4、源码地址
https://github.com/chrischen0405/Demo/blob/master/echarts/7-1radar.js
网友评论