当柱形图的柱子颜色渐变时,marker取到的是[object object],因此,需要手动设置新的值
option = {
tooltip: {
trigger: 'axis',
axisPointer: { // 坐标轴指示器,坐标轴触发有效
type: 'shadow' // 默认为直线,可选为:'line' | 'shadow'
},
formatter: function(params) {
var str = params[0].name + ":<br>";
let marker = "";
let unit = "";
let marker1 = "<span style='display:inline-block;margin-right:5px;border-radius:10px;width:9px;height:9px;background-color:rgba(2,242,180,1);'></span>";
let marker2 = "<span style='display:inline-block;margin-right:5px;border-radius:10px;width:9px;height:9px;background-color:rgba(2,209,223,1);'></span>";
for (var i = 0; i < params.length; i++) {
if (i % 2 === 0) { //偶数
marker = marker1;
unit = "人";
} else { //基数
marker = marker2;
unit = "座";
}
console.log(i);
str += marker + params[i].seriesName + ":" + params[i].value + unit + "<br>";
}
return str;
}
},
legend: {
data: ['人口', '城市'],
top:"15%"
},
grid: {
left: '20%',
right: '20%',
bottom: '20%',
top: '20%',
containLabel: true
},
xAxis: [{
type: 'category',
data: ['中国', '美国', '韩国', '日本', '泰国']
}],
yAxis: [{
type: 'value'
}],
series: [{
name: '人口',
type: 'bar',
data: [320, 332, 301, 334, 390, 330, 320],
itemStyle: {
normal: {
color: new echarts.graphic.LinearGradient(
0, 0, 0, 1,
[{
offset: 0,
color: 'rgba(2,242,180,.2)'
},
{
offset: 1,
color: 'rgba(2,242,180,1)'
}
]
)
}
}
},
{
name: '城市',
type: 'bar',
data: [120, 132, 101, 134, 90, 230, 210],
itemStyle: {
normal: {
color: new echarts.graphic.LinearGradient(
0, 0, 0, 1,
[{
offset: 0,
color: 'rgba(2,209,223,.2)'
},
{
offset: 1,
color: 'rgba(2,209,223,1)'
}
]
)
},
},
}
]
};