I've created a vertical stacked bar chart with Baidu ECharts.
Is it possible to show the sum of all values on top of the stack?
Edited : i have edited my example using the (very well explained) answer from @jeffrey but i get Uncaught TypeError: Cannot read property 'forEach' of undefined error. What am i doing wrong here ?
<!DOCTYPE html>
<html style="height: 100%">
<head>
<meta charset="utf-8">
</head>
<body style="height: 100%; margin: 0">
<div id="container" style="height: 100%"></div>
<script type="text/javascript" src="echarts-en.min.js"></script>
<script type="text/javascript">
var mySeries = [
{
name: 'Department 1',
type: 'bar',
stack: 'stack1',
data: [320, 302, 301, 334, 390, 330, 320]
},
{
name: 'Department 2',
type: 'bar',
stack: 'stack1',
data: [120, 132, 101, 134, 90, 230, 210]
},
{
name: 'Department 3',
type: 'bar',
stack: 'stack1',
data: [220, 182, 191, 234, 290, 330, 310]
},
{
name: 'Department 4',
type: 'bar',
stack: 'stack1',
data: [150, 212, 201, 154, 190, 330, 410]
},
{
name: 'Department 5',
type: 'bar',
stack: 'stack1',
data: [185, 120, 55, 66, 77, 88, 40],
label: {
normal: {
show: true,
position: 'top',
formatter: (params) => {
let total = 0;
this.series.forEach(serie => {
total += serie.data[params.dataIndex];
})
return total;
}
}
}
}
];
var dom = document.getElementById("container");
var myChart = echarts.init(dom);
var option = null;
option = {
tooltip : {
trigger: 'axis',
axisPointer : {
type : 'shadow'
}
},
legend: {
data: ['Department 1', 'Department 2','Department 3','Department 4','Department 5'],
},
toolbox: {
show: true,
orient: 'vertical',
left: 'right',
top: 'center',
feature: {
dataView: {show: false, readOnly: false},
magicType: {show: true, type: ['line', 'bar', 'stack', 'tiled']},
restore: {show: true},
saveAsImage: {show: true}
}
},
grid: {
left: '3%',
right: '4%',
bottom: '3%',
containLabel: true
},
yAxis: {
type: 'value'
},
xAxis: {
type: 'category',
data: ["Jen\n2018", "Feb\n2018", "Mar\n2018", "Apr\n2018", "May\n2018", "Jun\n2018", "Jul\n2018"]
}
};;
myChart.setOption(option, true);
myChart.setOption({
series: mySeries
})
</script>
</body>
</html>
var mySeries
, but in the formatter, you reference it withthis.mySeries
, therefore it'sundefined
. – Hairsplitterthis.mySeries
. i've made a jsfiddle to test it here : link – Heliotropethis.mySeries.forEach()
. Now your fiddle works :) – Hairsplitter