getGoldCharts() {
this.goldChartLoading = true;
saleWeightChart({
start: this.queryParams.createdDateRange?.[0] ? this.queryParams.createdDateRange[0] + " 00:00:00" : '',
end: this.queryParams.createdDateRange?.[1] ? this.queryParams.createdDateRange[1] + " 23:59:59" : ''
}).then(({ code, data }) => {
if (code == 0) {
this.goldChart = this.echarts.init(document.getElementById('goldCharts'));
var option = {
tooltip: {
trigger: 'axis',
axisPointer: {
type: 'shadow'
}
},
title: {
text: '黃金發(fā)貨量(克)',
},
grid:{
left: '2%',
right: '4%',
bottom: '2%',
containLabel: true
},
yAxis: {
splitLine:{
show:true,
lineStyle:{
type:'dashed'
}
}
},
xAxis: {
type: 'category',
axisTick: {
show: false,
},
axisLabel: {
show: true,
rotate: 30, //傾斜顯示
interval: 0 // 顯示所有標(biāo)簽
},
data: data.map((item, did) => item.preDate)
},
dataZoom: [
{
id: 'dataZoomX',
type: 'slider',
xAxisIndex: [0],
filterMode: 'filter',
backgroundColor: "#C0C4CC",
fillerColor: "#409EFF",
borderColor: "#C0C4CC", // 設(shè)置邊框顏色
height: 20,
bottom: 3, // dataZoom-slider組件離容器下側(cè)的距離
},
],
color: ['#09BD3C', '#C0C4CC', '#FD5353', '#EBEEF5'],
series: [
{
name: '待發(fā)貨',
type: 'bar',
stack: 'total',
barWidth: '30%',
data: data.map((item, did) => item.waitDeliveryGoldWeight),
},
{
name: '已發(fā)貨',
type: 'bar',
stack: 'total',
barWidth: '30%',
data: data.map((item, did) => item.deliveryGoldWeight)
},
{
name: '預(yù)警值',
type: 'bar',
stack: 'total',
barWidth: '30%',
data: data.map((item, did) => item.exceedGodlWeight)
},
],
};
const series = option.series;
series.push({
name: '總計(jì)',
type: 'bar',
stack: 'y', //注意與前面的stack屬性不同1跋АJ癖洹9当ぁ侧但!
barWidth: '30%',
label:{
normal:{
show:true,
position:'top',
}
},
color: 'rgba(0,0,0,0)', //透明
// 計(jì)算每列的總和
data: data.map(item => item.waitDeliveryGoldWeight + item.deliveryGoldWeight + item.exceedGodlWeight),
z: -1, // 隱藏在最底層
barGap: '-100%', //與堆疊柱條重合
})
this.goldChart.setOption(option);
}
}).finally(() => {
this.goldChartLoading = false;
});
},
堆疊柱狀圖數(shù)據(jù)顯示在內(nèi)部的話,會(huì)把返回值為0的也展示出來(lái)航罗,所以需要單獨(dú)處理下
series: [
{
name: '待發(fā)貨',
type: 'bar',
stack: 'total',
barWidth: '30%',
label:{
normal:{
show:true,
position:'inside',// 顯示在柱子里
// 這里處理下
formatter: function(params) {
return params.value > 0 ? params.value : '';
}
}
},
data: data.map((item, did) => item.waitDeliveryGoldWeight),
},
],
注意
因?yàn)檫@里x軸只有一列堆疊柱狀圖禀横,所以采用的是series.push這種方法,百度上查詢出來(lái)說(shuō)x軸只多列堆疊柱狀圖的話不能用此方法(待驗(yàn)證)
我這里的 預(yù)警值 是不包含在原有總計(jì)里面的粥血,所以在總計(jì)這里把每個(gè)返回值都給加起來(lái)了柏锄。