ecahrts雷达图tooltip显示axis单轴数据

1、遇到问题

在echarts雷达图中将tooltip.trigger设置为axis时失效,雷达图并不会显示单轴数据,而是整个雷达图的数据

tooltip.trigger

2、实现效果

先贴出我们想要的效果,和折线图柱状图类似的,希望可以在一个轴线上能显示雷达图在这个轴线维度上的数据:

实现效果

3、源码

可以直接在echarts官网运行查看效果的代码:

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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
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