echarts 堆叠柱状图 在顶部显示数据总数
1.效果图
2.实现步骤:
const myChart = echarts.init(this.leakTypeDivide)
const option = {
title: {
text: '漏洞类型划分',
x: 'center',
y: 'bottom',
textStyle: { // 副标题样式
color: '#838080',
fontWeight: 900,
fontSize:15,
lineHeight: 20,
fontFamily: 'Microsoft Yahei'
}
},
tooltip: {
trigger: 'axis',
axisPointer: {
type: 'shadow'
}
},
legend: {},
grid: {
left: '3%',
right: '4%',
bottom: '10%',
containLabel: true
},
xAxis: {
type: 'value'
},
yAxis: {
type: 'category',
data: ['Code Execution','Denial-of-Service','Information Exposure', 'Directory Traversal', 'Malicious Packages', 'AccessRestriction Bypass', 'CSRF', 'XXE', 'Authentication Bypass']
},
series: [
{
name: '2014',
type: 'bar',
stack: 'x',
//label: { // 是否显示每一个柱状图的数据
//show: true
//},
emphasis: {
focus: 'series'
},
data: [100, 500, 100, 500, 300, 700, 500, 100, 500]
},
{
name: '2015',
type: 'bar',
stack: 'x',
emphasis: {
focus: 'series'
},
data: [500, 100, 100, 100, 500, 200, 200, 200, 200]
},
{
name: '2016',
type: 'bar',
stack: 'x',
emphasis: {
focus: 'series'
},
data: [100, 300, 100, 200, 200, 300, 300, 200, 200]
},
{
name: '2017',
type: 'bar',
stack: 'x',
emphasis: {
focus: 'series'
},
data: [50, 200, 200, 100, 100, 300, 400, 200, 200]
},
{
name: '2018',
type: 'bar',
stack: 'x',
emphasis: {
focus: 'series'
},
data: [300, 800, 200, 100, 100, 500, 200, 200, 200]
},
// 在series的最后一项,添加一个data都为零的数组
{
name: '总和',
type: 'bar',
stack: 'x',
label: {
show: true,
color: '#4ba034',
position:'right',
},
emphasis: {
focus: 'series'
},
data: [0, 0, 0, 0, 0, 0, 0, 0, 0],
}
]
};
// 接着在option外部对数据进行操作,使得最后一项的数据是前几项的总和
var series = option.series
function getSum (params) {
let dataValue = 0
for (let i = 0; i < series.length; i++) {
dataValue += series[i].data[params.dataIndex]
}
return dataValue
}
series[series.length - 1].label.formatter = getSum
myChart.setOption(option, true);
3.总结:
(1)在series的最后一项,添加一个data都为零的数组
(2)接着在option外部对数据进行操作,使得最后一项的数据是前几项的总和