Echarts柱狀圖實現(xiàn)同時顯示百分比+原始值+匯總值效果實例
原始效果:
柱狀圖:https://echarts.apache.org/examples/zh/editor.html?c=bar-stack-normalization

二開效果1:

核心邏輯
同時顯示百分比和原始值
label: {
show: true,
position: 'inside',
formatter: (params) => {
const rawValue = rawData[params.seriesIndex][params.dataIndex];
const percentage = Math.round(params.value * 1000) / 10;
return `${rawValue} \n(${percentage}%)`;
}
},顯示匯總值
// Add a new series for displaying total values
series.push({
name: 'Total',
type: 'bar',
stack: 'total',
itemStyle: {
color: 'rgba(0,0,0,0)' // 透明顏色
},
label: {
show: true,
position: 'top',
formatter: params => `Total: ${totalData[params.dataIndex]}`
},
data: totalData.map(value => 0.01) // 微小的值以便能顯示標(biāo)簽但不影響圖形
});代碼解釋
新增顯示總值的系列:
- 您添加了一個名為
'Total'的新系列到series數(shù)組中。- 這個系列使用
type: 'bar',并且堆疊在名為'total'的堆棧中,這與其他系列使用的堆棧名稱一致。這確保了柱狀圖的對齊,即使該系列是不可見的。透明的柱狀圖:
itemStyle被設(shè)置為color: 'rgba(0,0,0,0)',使得該系列的柱狀圖完全透明。這是一個巧妙的方法,可以確保這些柱狀圖不增加任何可見的元素到圖表中,但仍然可以在它們上面放置標(biāo)簽。標(biāo)簽配置:
label對象中的show: true確保顯示標(biāo)簽。position設(shè)置為'top',因此標(biāo)簽顯示在每個柱狀圖堆棧的頂部。formatter函數(shù)自定義了標(biāo)簽的文本。它使用params.dataIndex獲取totalData中對應(yīng)的值,并顯示為Total: {value}。這提供了關(guān)于每個類別(星期幾)中所有堆疊元素的總值的清晰信息。帶有微小值的數(shù)據(jù):
- 該系列的
data數(shù)組被設(shè)置為totalData.map(value => 0.01)。這將每個數(shù)據(jù)點設(shè)置為一個非常小的值(0.01)。這些微小的值的目的是為標(biāo)簽創(chuàng)建一個占位符,而不影響圖表的實際可視化。由于柱狀圖本身是透明的,這個值確保了標(biāo)簽可以正確地定位和顯示,而不會為柱狀圖增加任何視覺重量。
分析:
- 使用透明的柱狀圖來顯示標(biāo)簽:通過使用透明的柱狀圖,您可以在柱狀圖堆棧的頂部放置標(biāo)簽,而不會改變圖表的視覺外觀。這是一種常見的技術(shù),當(dāng)您希望添加額外的信息而不影響數(shù)據(jù)的可視化時。
- 數(shù)據(jù)中的微小值:使用微小值(0.01)確保標(biāo)簽與柱狀圖相關(guān)聯(lián),但不會顯著地影響堆疊柱狀圖的高度。這在ECharts中尤其有用,因為標(biāo)簽是與特定的數(shù)據(jù)點相關(guān)聯(lián)的。
- 堆疊配置:使用相同的堆疊標(biāo)識符('total')使透明柱狀圖與其余堆疊柱狀圖完美對齊,確保標(biāo)簽位置的一致性。
這種方法對于突出顯示總值,同時保持?jǐn)?shù)據(jù)可視化的完整性非常有效。這是一個為圖表提供額外信息而不使其變得混亂或扭曲的巧妙解決方案。
完整版代碼
// There should not be negative values in rawData
const rawData = [
[100, 302, 301, 334, 390, 330, 320],
[320, 132, 101, 134, 90, 230, 210],
[220, 182, 191, 234, 290, 330, 310],
[150, 212, 201, 154, 190, 330, 410],
[820, 832, 901, 934, 1290, 1330, 1320]
];
const totalData = [];
for (let i = 0; i < rawData[0].length; ++i) {
let sum = 0;
for (let j = 0; j < rawData.length; ++j) {
sum += rawData[j][i];
}
totalData.push(sum);
}
const grid = {
left: 100,
right: 100,
top: 50,
bottom: 50
};
const series = [
'Direct',
'Mail Ad',
'Affiliate Ad',
'Video Ad',
'Search Engine'
].map((name, sid) => {
return {
name,
type: 'bar',
stack: 'total',
barWidth: '60%',
label: {
show: true,
position: 'inside',
formatter: (params) => {
const rawValue = rawData[params.seriesIndex][params.dataIndex];
const percentage = Math.round(params.value * 1000) / 10;
return `${rawValue} \n(${percentage}%)`;
}
},
data: rawData[sid].map((d, did) =>
totalData[did] <= 0 ? 0 : d / totalData[did]
)
};
});
// Add a new series for displaying total values
series.push({
name: 'Total',
type: 'bar',
stack: 'total',
itemStyle: {
color: 'rgba(0,0,0,0)' // 透明顏色
},
label: {
show: true,
position: 'top',
formatter: params => `Total: ${totalData[params.dataIndex]}`
},
data: totalData.map(value => 0.01) // 微小的值以便能顯示標(biāo)簽但不影響圖形
});
option = {
legend: {
selectedMode: false
},
grid,
yAxis: {
type: 'value'
},
xAxis: {
type: 'category',
data: ['Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat', 'Sun']
},
series
};二開效果2:

完整版代碼
// There should not be negative values in rawData
const rawData = [
[100, 302, 301, 334, 390, 330, 320],
[320, 132, 101, 134, 90, 230, 210],
[220, 182, 191, 234, 290, 330, 310],
[150, 212, 201, 154, 190, 330, 410],
[820, 832, 901, 934, 1290, 1330, 1320]
];
const totalData = [];
for (let i = 0; i < rawData[0].length; ++i) {
let sum = 0;
for (let j = 0; j < rawData.length; ++j) {
sum += rawData[j][i];
}
totalData.push(sum);
}
const grid = {
left: 100,
right: 100,
top: 50,
bottom: 50
};
const series = [
'Direct',
'Mail Ad',
'Affiliate Ad',
'Video Ad',
'Search Engine'
].map((name, sid) => {
return {
name,
type: 'bar',
stack: 'total',
barWidth: '60%',
label: {
show: true,
position: 'inside', // Position the labels on top of the bars
formatter: (params) => {
const originalValue = rawData[sid][params.dataIndex];
const percentage = (originalValue / totalData[params.dataIndex] * 100).toFixed(2);
return `${originalValue} \n(${percentage}%)`;
},
},
data: rawData[sid].map((d, did) =>
totalData[did] <= 0 ? 0 : d / totalData[did]
)
};
});
option = {
tooltip: {
trigger: 'axis',
axisPointer: {
type: 'shadow'
},
formatter: (params) => {
const total = totalData[params[0].dataIndex];
const header = `<div style="font-weight:bold">${params[0].axisValue}</div>
<div>Total: ${total}</div>`;
const body = params.map(param => {
const originalValue = rawData[param.seriesIndex][param.dataIndex];
const percentage = (originalValue / total * 100).toFixed(2);
return `<div>${param.seriesName}: ${originalValue} (${percentage}%)</div>`;
}).join('');
return header + body;
}
},
legend: {
selectedMode: false
},
grid,
yAxis: {
type: 'value'
},
xAxis: {
type: 'category',
data: ['Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat', 'Sun']
},
series
};實現(xiàn)思路與修改:
計算每天的總訪問數(shù):首先遍歷
rawData并計算每一天所有來源的總訪問數(shù)。這些總數(shù)被存儲在totalData數(shù)組中。配置每個數(shù)據(jù)源系列:為每一個數(shù)據(jù)源創(chuàng)建一個
series對象。每個系列代表一種訪問來源,并包含一些配置選項,如類型、堆疊設(shè)置、標(biāo)簽顯示方式等。配置標(biāo)簽顯示:為了讓用戶在圖表上直觀地看到原始值和占比,我們需要在每個柱形上添加標(biāo)簽。標(biāo)簽的內(nèi)容包括原始值和百分比。
配置提示框(Tooltip):為了提供更豐富的信息,我們配置了一個提示框,當(dāng)用戶懸停在柱形上時會顯示當(dāng)天的總訪問數(shù)和各個來源的具體數(shù)值及占比。
二開效果3:

完整版代碼
// There should not be negative values in rawData
const rawData = [
[100, 302, 301, 334, 390, 330, 320],
[320, 132, 101, 134, 90, 230, 210],
[220, 182, 191, 234, 290, 330, 310],
[150, 212, 201, 154, 190, 330, 410],
[820, 832, 901, 934, 1290, 1330, 1320]
];
const totalData = [];
for (let i = 0; i < rawData[0].length; ++i) {
let sum = 0;
for (let j = 0; j < rawData.length; ++j) {
sum += rawData[j][i];
}
totalData.push(sum);
}
const grid = {
left: 100,
right: 100,
top: 50,
bottom: 50
};
const series = [
'Direct',
'Mail Ad',
'Affiliate Ad',
'Video Ad',
'Search Engine'
].map((name, sid) => {
return {
name,
type: 'bar',
stack: 'total',
barWidth: '60%',
label: {
show: true,
position: 'inside', // Position the labels on top of the bars
formatter: (params) => {
const originalValue = rawData[sid][params.dataIndex];
const percentage = (originalValue / totalData[params.dataIndex] * 100).toFixed(2);
return `${originalValue} (${percentage}%)`;
},
},
itemStyle: {
emphasis: {
// focus : 'series',
label: {
show: true,
position: 'top',
fontSize: 12,
color: 'red',
formatter: (params) => totalData[params.dataIndex]
}
}
},
data: rawData[sid].map((d, did) =>
totalData[did] <= 0 ? 0 : d / totalData[did]
)
};
});
option = {
legend: {
selectedMode: false
},
grid,
yAxis: {
type: 'value'
},
xAxis: {
type: 'category',
data: ['Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat', 'Sun']
},
series
};解釋:
- 添加了
itemStyle選項,其中包含了emphasis子項。- 在
emphasis中設(shè)置了label,用于在鼠標(biāo)懸停時顯示總值。emphasis.label.show設(shè)為true,表示在鼠標(biāo)懸停時顯示標(biāo)簽。emphasis.label.position設(shè)為'bottom',使標(biāo)簽顯示在柱子底部。emphasis.label.fontSize設(shè)為12,調(diào)整字體大小。emphasis.label.formatter使用totalData[params.dataIndex]顯示當(dāng)前柱子對應(yīng)的總值
柱狀圖轉(zhuǎn)換為條形圖

核心代碼修改,變更xAxis,yAxis 中的 x y 即可

條形圖同時展示百分比、原始值、匯總值功能
// There should not be negative values in rawData
const rawData = [
[100, 302, 301, 334, 390, 330, 320],
[320, 132, 101, 134, 90, 230, 210],
[220, 182, 191, 234, 290, 330, 310],
[150, 212, 201, 154, 190, 330, 410],
[820, 832, 901, 934, 1290, 1330, 1320]
];
const totalData = [];
for (let i = 0; i < rawData[0].length; ++i) {
let sum = 0;
for (let j = 0; j < rawData.length; ++j) {
sum += rawData[j][i];
}
totalData.push(sum);
}
const grid = {
left: 100,
right: 100,
top: 50,
bottom: 50
};
const series = [
'Direct',
'Mail Ad',
'Affiliate Ad',
'Video Ad',
'Search Engine'
].map((name, sid) => {
return {
name,
type: 'bar',
stack: 'total',
barWidth: '60%',
label: {
show: true,
position: 'inside',
formatter: (params) => {
const rawValue = rawData[params.seriesIndex][params.dataIndex];
const percentage = Math.round(params.value * 1000) / 10;
return `${rawValue} \n(${percentage}%)`;
}
},
data: rawData[sid].map((d, did) =>
totalData[did] <= 0 ? 0 : d / totalData[did]
)
};
});
series.push({
name: 'Total',
type: 'bar',
stack: 'total',
itemStyle: {
color: 'red' // 透明顏色
},
label: {
show: true,
// position: 'middle',
formatter: params => `Total: ${totalData[params.dataIndex]}`
},
data: totalData.map(value => 0.0) // 微小的值以便能顯示標(biāo)簽但不影響圖形
});
option = {
legend: {
selectedMode: false
},
grid,
xAxis: {
type: 'value'
},
yAxis: {
type: 'category',
data: ['Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat', 'Sun']
},
series
};效果展示

總結(jié)
到此這篇關(guān)于Echarts柱狀圖實現(xiàn)同時顯示百分比+原始值+匯總值效果的文章就介紹到這了,更多相關(guān)Echarts柱狀圖同時顯示百分比原始值內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
JS正則表達(dá)式學(xué)習(xí)之貪婪和非貪婪模式實例總結(jié)
這篇文章主要介紹了JS正則表達(dá)式學(xué)習(xí)之貪婪和非貪婪模式用法,結(jié)合實例形式總結(jié)分析了JS正則表達(dá)式中貪婪模式與非貪婪模式的具體功能、使用方法與相關(guān)注意事項,需要的朋友可以參考下2016-12-12
Javascript中arguments對象的詳解與使用方法
ECMAScript中的函數(shù)并不介意傳遞的參數(shù)有多少,也不介意是什么類型。由于JavaScript允許函數(shù)有不定數(shù)目的參數(shù),所以我們需要一種機(jī)制,可以在 函數(shù)體內(nèi) 部讀取所有參數(shù)。這就是arguments對象的由來。這篇文章將詳細(xì)介紹Javascript中的arguments對象和使用方法。2016-10-10
JavaScript學(xué)習(xí)筆記之ES6數(shù)組方法
ES6給數(shù)組添加了一些新特性,而這些新特性到目前為止完全可以運(yùn)用到自己的業(yè)務(wù)層。在這一節(jié)中將總結(jié)有關(guān)于ES6給數(shù)組提供一些新特性的使用方法2016-03-03
Three.js利用orbit controls插件(軌道控制)控制模型交互動作詳解
這篇文章主要給大家介紹了關(guān)于Three.js利用orbit controls插件,也就是軌道控制來控制模型交互動作的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對大家具有一定的參考學(xué)習(xí)價值,需要的朋友們下面來一起看看吧。2017-09-09
詳談innerHTML innerText的使用和區(qū)別
下面小編就為大家?guī)硪黄斦刬nnerHTML innerText的使用和區(qū)別。小編覺得挺不錯的,現(xiàn)在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2017-08-08

