【问题标题】:How to display sum stack bar in echarts如何在echarts中显示总和堆栈栏
【发布时间】:2018-12-03 14:27:13
【问题描述】:

我使用 eChatrs 作为图表库。所需图表应如下所示:

我需要的是在每列上方显示总计。这样做的最佳做法是什么?

【问题讨论】:

    标签: javascript reactjs charts echarts


    【解决方案1】:

    您可以使用格式化程序来实现这一点,请查看此演示:

    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>

    但是,正如我们所见,此演示仅在每列上方显示总值。

    如果你想要每个系列的总价值和价值,

    这里有一个技巧来实现这一点:使用空白系列来显示总值。

    查看此演示:

    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>

    【讨论】:

    • 你可以简化这段代码:show: index === series.length - 1,
    • 创建一个填充空值的数据集的好技巧!;但是同一个系列可以有两个标签吗?
    猜你喜欢
    • 1970-01-01
    • 2021-02-19
    • 1970-01-01
    • 1970-01-01
    • 2020-04-16
    • 2017-07-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多