How to display sum stack bar in echarts
Asked Answered
S

1

7

I'm using eChatrs as chart library. Needed chart should look like this :

enter image description here

What i need, is to show totals above each column. What is the best practise for doing this ?

Stannite answered 3/12, 2018 at 14:27 Comment(0)
S
8

You can use formatter to achieve this, check this demo:

let echartsObj = echarts.init(document.querySelector('#canvas'));
series = [{
    name: 'Server #1',
    data: [100, 115, 165, 107, 67]
}, {
    name: 'Server #2',
    data: [85, 106, 129, 161, 123]
}, {
    name: 'Server #3',
    data: [67, 87, 86, 167, 157]
}]

genFormatter = (series) => {
    return (param) => {
        console.log(param);
        let sum = 0;
        series.forEach(item => {
            sum += item.data[param.dataIndex];
        });
        return sum
    }
};

option = {
    color: ['#b8d6f2', '#c6e19a', '#f3b879' ],
    legend: {
        data: series.map(item => item.name),
        right: 'right',

    },
    xAxis: {
        type: 'category',
        data: ['Mon', 'Tue', 'Wed', 'Thu', 'Fri']
    },

    yAxis: {
        type: 'value'
    },
    series: series.map((item, index) => Object.assign(item, {
        type: 'bar',
        stack: true,
        label: {
            show: index == series.length - 1 ? true : false,
            formatter: genFormatter(series),
            fontSize: 20,
            color: 'black',
            position: 'top'
        },
    }))
};
echartsObj.setOption(option)
<html>
      <header>
        <script src="https://cdn.bootcss.com/echarts/4.1.0.rc2/echarts-en.min.js"></script>
      </header>
      <body>
        <div id="canvas" style="width: 70%; height: 300px">
        </div>
      </body>
</html>

However, as we can see, this demo only display total value above each column.

if you want both total value and value of each series,

here is a trick to achieve this: use a blank series to display total value.

check this demo:

let echartsObj = echarts.init(document.querySelector('#canvas'));

series = [{
    name: 'Server #1',
    data: [100, 115, 165, 107, 67]
}, {
    name: 'Server #2',
    data: [85, 106, 129, 161, 123]
}, {
    name: 'Server #3',
    data: [67, 87, 86, 167, 157]
}, {
    name: 'Server #3',
    data: [0,0,0,0,0]
}]

genFormatter = (series) => {
    return (param) => {
        console.log(param);
        let sum = 0;
        series.forEach(item => {
            sum += item.data[param.dataIndex];
        });
        return sum
    }
};

function isLastSeries(index) {
    return index === series.length - 1
}
option = {
    color: ['#b8d6f2', '#c6e19a', '#f3b879' ],
    legend: {
        data: series.map(item => item.name),
        right: 'right',
        
    },
    xAxis: {
        type: 'category',
        data: ['Mon', 'Tue', 'Wed', 'Thu', 'Fri']
    },
   
    yAxis: {
        type: 'value'
    },
    series: series.map((item, index) => Object.assign(item, {
        type: 'bar',
        stack: true,
        label: {
            show: true,
            formatter: isLastSeries(index) ? genFormatter(series) : null,
            fontSize:  isLastSeries(index) ? 20 : 15,
            color: 'black',
            position: isLastSeries(index) ? 'top' : 'inside'
        },
    }))
};
echartsObj.setOption(option)
<html>
      <header>
        <script src="https://cdn.bootcss.com/echarts/4.1.0.rc2/echarts-en.min.js"></script>
      </header>
      <body>
        <div id="canvas" style="width: 70%; height: 400px">
        </div>
      </body>
    </html>
Switzerland answered 18/12, 2018 at 7:45 Comment(3)
You can simplify this code: show: index === series.length - 1,Loveinamist
Nice trick on creating a dataset filled with empty values!; But would it be possible to have 2 labels on a same series?Deel
Can you guide me how we can achieve this in VUE js?Leninist

© 2022 - 2024 — McMap. All rights reserved.