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

Summary of the unknown usage of "!" in Linux

Preface In fact, the humble "!" has man...

Solve the problem of using swiper plug-in in vue

Since I used this plugin when writing a demo and ...

MySQL paging performance exploration

Several common paging methods: 1. Escalator metho...

Web Design Teaching or Learning Program

Section Course content Hours 1 Web Design Overvie...

Uncommon but useful tags in Xhtml

Xhtml has many tags that are not commonly used but...

MySQL uses inet_aton and inet_ntoa to process IP address data

This article will introduce how to save IP addres...

This article takes you to explore NULL in MySQL

Table of contents Preface NULL in MySQL 2 NULL oc...

Design theory: people-oriented green design

Reflections on the two viewpoints of “people-orie...

Introduction to Common XHTML Tags

<br />For some time, I found that many peopl...

Problems with creating placeholders for HTML selection boxes

I'm using a placeholder in a text input and i...

Specific usage of Vue's new toy VueUse

Table of contents Preface What is VueUse Easy to ...

Detailed explanation of various join summaries of SQL

SQL Left Join, Right Join, Inner Join, and Natura...

MySQL: mysql functions

1. Built-in functions 1. Mathematical functions r...