How to implement horizontal bar chart with percentage in echarts

How to implement horizontal bar chart with percentage in echarts

Example Code

var data = [220, 182, 191, 234, 290, 120, 65, 444];
var barWidth = 20;
var maxNum = 0;
for (var i = 0; i < data.length; i++) {
    if (data[i] > maxNum) {
        maxNum = data[i];
    }
}
 
option = {
    backgroundColor: 'black',
    grid: {
        top: '10%',
        left: '20%',
        right: '10%',
        bottom: '0%',
    },
 
    title:
        text: 'Percentage column',
    },
    xAxis:
        show: false,
    },
    yAxis: {
        data: data.map((item, index) => `data ${index}`),
        splitLine: {
            show: false,
        },
        axisLabel: {
            textStyle: { fontSize: '80%', color: '#02afff' },
        },
        axisLine: {
            show: false,
        },
        axisTick: false,
    },
    series: [
        {
            type: 'bar',
            barWidth: barWidth,
            zlevel: 2,
            data: data.map(function (item) {
                return {
                    value: item,
                    maxNum: maxNum,
                };
            }),
            label: {
                show: true,
                position: 'inside',
                // distance: 80,
                align: 'center',
                formatter: function (params) {
                    // return params.data.realValue;
                    var percent = Number((params.data.value / params.data.maxNum) * 100).toFixed(2) + '%';
                    return percent;
                },
                color: '#fff',
                fontSize: 12,
            },
            itemStyle: {
                borderRadius: 10,
                color: new echarts.graphic.LinearGradient(0, 1, 1, 1, [
                    { offset: 0, color: '#395CFE' },
                    { offset: 1, color: '#2EC7CF' },
                ]),
            },
        },
        {
            type: 'bar',
            barWidth: barWidth,
            barGap: '-100%',
            data: data.map(function (item) {
                return {
                    realValue: item,
                    value: maxNum,
                };
            }),
            label: {
                show: true,
                position: 'right',
                distance: 80,
                align: 'right',
                formatter: function (params) {
                    return params.data.realValue + 'item';
                },
                color: '#02afff',
                fontSize: 18,
            },
            itemStyle: {
                borderRadius: 10,
                color: 'rgba(3,169,244, 0.5)',
            },
        },
    ],
};

Rendering

Code Analysis

1. Before option, define a data array, then use a for loop to get the maximum value in the array and save it as the maxNum variable.

2. Define two bars in the series. The data of the first bar is mapped, and the value is set to the data in step 1. An object type is returned. The object contains two attributes, value and maxNum (value is a required attribute, maxNum is a custom attribute, and other custom attributes can be added if necessary). The maxNum attribute is added because the formatter of the label needs to use value/maxNum to calculate the percentage. This is the first time I encounter the writing method of returning data as an object type in the series. It is really useful for obtaining data for calculation.

3. The second bar in the series also assigns data through map. The difference is that the value of this bar is all set to maxNum. The second bar serves as the background, which is equivalent to 100% progress. The reason why a realValue attribute is needed is because the value of each item (220, 182, 191, 234, 290, 120, 65, 444, etc.) needs to be displayed on the right side of the bar.

4. The first bar is on top and the second bar is on the bottom. The key is the zlevel:2 of the first bar and the barGap: '-100%' of the second bar.

Reading more cases on the echarts official website really benefits me a lot.

Summarize

This is the end of this article about how to implement horizontal bar charts with percentages in echarts. For more relevant echarts percentage horizontal bar chart content, please search for previous articles on 123WORDPRESS.COM or continue to browse the following related articles. I hope everyone will support 123WORDPRESS.COM in the future!

You may also be interested in:
  • Echarts Bar horizontal bar chart example code
  • Echarts Basic Introduction: General Configuration of Bar Chart and Line Chart

<<:  CSS to achieve the transition animation effect of the picture when the mouse is placed on it

>>:  Solution to the problem of text position jumping when the search text box leaves the focus

Recommend

Vue realizes the percentage bar effect

This article shares the specific code of Vue to r...

How to install git on linux

1. Introduction Git is a free, open source distri...

js realizes 3D sound effects through audioContext

This article shares the specific code of js to ac...

How to solve the mysql error 1033 Incorrect information in file: 'xxx.frm'

Problem Description 1. Database of the collection...

Introduction to Linux environment variables and process address space

Table of contents Linux environment variables and...

Methods and steps to build nginx file server based on docker

1. Create a new configuration file docker_nginx.c...

React handwriting tab switching problem

Parent File import React, { useState } from '...

JavaScript BOM Explained

Table of contents 1. BOM Introduction 1. JavaScri...

Improvements to the web server to improve website performance

<br />In the first section of this series, w...

Detailed explanation of the implementation of nginx process lock

Table of contents 1. The role of nginx process lo...