option= {
tooltip: {
trigger: 'axis',
axisPointer: {
type: 'cross',
crossStyle: {
color: '#999'
}
}
},
toolbox: {
feature: {
saveAsImage: { show: true }
}
},
legend: {
data: ['频次', '累计百分比']
},
dataZoom: [
{
id: 'dataZoomX',
type: 'slider',
XAxisIndex: [0],
filterMode: 'empty'
}
],
xAxis: [
{
type: 'category',
data: ['Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat', 'Sun'],
axisPointer: {
type: 'shadow'
}
}
],
yAxis: [
{
type: 'value',
name: '频次',
axisLabel: {
formatter: '{value}'
},
axisLine:{
show:true
},
},
{
type: 'value',
name: '累计百分比',
axisLabel: {
formatter: '{value} %'
},
axisLine:{
show:true
},
}
],
series: [
{
name: '频次',
type: 'bar',
tooltip: {
valueFormatter: function (value) {
return value;
}
},
data: []
},
{
name: '累计百分比',
type: 'line',
yAxisIndex: 1,
tooltip: {
valueFormatter: function (value) {
return value + ' %';
}
},
data: []
}
]
}
formatFun: (params) => {
return new Promise((resolve, reject) => {
let res = params.res
let option = params.option
option.xAxis[0].data = res.value.x_axis
option.series[0].data = res.value.y_axis
let data = res.value.y_axis
data.sort((a, b) => b - a);
const total = data.reduce((sum, value) => sum + value, 0);
let cumulativePercentage = 0;
const cumulativePercentages = data.map(value => {
const percentage = (value / total) * 100;
cumulativePercentage += percentage;
return cumulativePercentage.toFixed(2);
});
option.series[1].data = cumulativePercentages
resolve({ option: option })
})
}