echarts同时显示柱状图 + 折线图
提示框组件(tooltip)柱状图value显示千分位分隔符 折线图显示value + ‘%’
option配置如下:
(总量和占比之间没关系!我拿到的数据是后端直接给的,这里就是随便写的数据记录一下!)
function totalFormat(params) {
const str = params.toString();
const reg = str.indexOf('.') > -1 ? /(\d)(?=(\d{3})+\.)/g : /(\d)(?=(?:\d{3})+$)/g;
return str.replace(reg, '$1,');
}
export const getEchartsOpt = () => {
const echartsOpt = {
title: {
text: '一个title',
x: 'center',
},
legend: {
data: ['总量', '占比'],
right: '10%',
},
tooltip: {
trigger: 'axis',
axisPointer: {
type: 'cross',
label: {
backgroundColor: '#6a7985',
},
},
formatter: (params) => {
let result = `${params[0].name}<br>`;
params.forEach((item) => {
if (item.value) {
if (item.seriesName === '占比') {
result += `${item.marker} ${item.seriesName} : ${item.value} %</br>`;
} else {
result += `${item.marker} ${item.seriesName} : ${totalFormat(item.value)}</br>`;
}
} else {
result += `${item.marker} ${item.seriesName} : - </br>`;
}
});
return result;
},
},
grid: {
left: '3%',
right: '4%',
bottom: '4%',
containLabel: true,
},
xAxis: [
{
type: 'category',
data: ['Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat', 'Sun'],
},
],
yAxis: [
{
type: 'value',
name: '总量',
},
{
type: 'value',
name: '占比',
alignTicks: true,
axisLabel: {
formatter: '{value} %',
},
},
],
series: [
{
name: '总量',
type: 'bar',
barWidth: '50%',
data: [4324234,4563214,9012834,2739002,3901233,5319834,3783344],
},
{
name: '占比',
type: 'line',
yAxisIndex: 1,
data: [40,45,90,35,40,50,35],
},
],
};
return echartsOpt;
};
效果如下: