Example Codevar 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)', }, }, ], }; RenderingCode Analysis1. 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. SummarizeThis 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:
|
<<: 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
When the amount of data in MySQL is large, limit ...
This article shares the specific code of Vue to r...
Cause: NVIDIA graphics card driver is damaged Sol...
1. Introduction Git is a free, open source distri...
MySQL creates users and authorizes and revokes us...
This article shares the specific code of js to ac...
1. In addition to the default port 8080, we try t...
Problem Description 1. Database of the collection...
Table of contents Linux environment variables and...
1. Create a new configuration file docker_nginx.c...
Introduction to the polling algorithm Many people...
Parent File import React, { useState } from '...
Table of contents 1. BOM Introduction 1. JavaScri...
<br />In the first section of this series, w...
Table of contents 1. The role of nginx process lo...