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

How to deploy Go web applications using Docker

Table of contents Why do we need Docker? Docker d...

Detailed explanation of how to reduce memory usage in MySql

Preface By default, MySQL will initialize a large...

How to set focus on HTML elements

Copy code The code is as follows: <body <fo...

Understanding JSON (JavaScript Object Notation) in one article

Table of contents JSON appears Json structure Jso...

Docker win ping fails container avoidance guide

Using win docker-desktop, I want to connect to co...

Use href in html to pop up a file download dialog box when clicking a link

I learned a new trick today. I didn’t know it befo...

Configuring MySQL and Squel Pro on Mac

In response to the popularity of nodejs, we have ...

Apache Spark 2.0 jobs take a long time to finish when they are finished

Phenomenon When using Apache Spark 2.x, you may e...

What are the new CSS :where and :is pseudo-class functions?

What are :is and :where? :is() and :where() are p...

Native JS to achieve drag photo wall

This article shares with you a draggable photo wa...

Analysis of MySQL joint index function and usage examples

This article uses examples to illustrate the func...

HTML head tag detailed introduction

There are many tags and elements in the HTML head ...

MySQL 8.0.13 installation and configuration method graphic tutorial

This article shares the installation and configur...

Sample code for cool breathing effect using CSS3+JavaScript

A simple cool effect achieved with CSS3 animation...