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
Preface In fact, the humble "!" has man...
Since I used this plugin when writing a demo and ...
Several common paging methods: 1. Escalator metho...
Section Course content Hours 1 Web Design Overvie...
Xhtml has many tags that are not commonly used but...
Using SSH terminal (such as putty, xshell) to con...
This article will introduce how to save IP addres...
Table of contents Preface NULL in MySQL 2 NULL oc...
Table of contents Preface 1. MySQL master-slave r...
Reflections on the two viewpoints of “people-orie...
<br />For some time, I found that many peopl...
I'm using a placeholder in a text input and i...
Table of contents Preface What is VueUse Easy to ...
SQL Left Join, Right Join, Inner Join, and Natura...
1. Built-in functions 1. Mathematical functions r...