|
|
/**
|
|
|
* https://echarts.apache.org/examples/zh/editor.html?c=pie-simple
|
|
|
* <p>
|
|
|
* 作者: Wang
|
|
|
* 时间:2021/12/3 14:30
|
|
|
*/
|
|
|
export default {
|
|
|
name: 'barStack',
|
|
|
template: '',
|
|
|
components: {},
|
|
|
props: {
|
|
|
// 单位px
|
|
|
width: {
|
|
|
type: String,
|
|
|
default: '100%'
|
|
|
},
|
|
|
height: {
|
|
|
type: String,
|
|
|
default: '100%'
|
|
|
},
|
|
|
title: {
|
|
|
type: Object,
|
|
|
default: {
|
|
|
show: false,
|
|
|
text: '标题',
|
|
|
subtext: '辅助标题',
|
|
|
left: 'center'
|
|
|
}
|
|
|
},
|
|
|
sourceData: {
|
|
|
type: Array,
|
|
|
default: [
|
|
|
['product', '2015', '2016', '2017'],
|
|
|
['示例1', 43.3, 85.8, 93.7],
|
|
|
['示例2', 83.1, 73.4, 55.1],
|
|
|
['示例3', 86.4, 65.2, 82.5],
|
|
|
['示例4', 72.4, 53.9, 39.1]
|
|
|
]
|
|
|
}
|
|
|
},
|
|
|
data() {
|
|
|
return {}
|
|
|
},
|
|
|
setup(props, {attrs, slots, emit}) {
|
|
|
const {proxy} = Vue.getCurrentInstance();
|
|
|
|
|
|
let id = "line" + (new Date()).getTime();
|
|
|
|
|
|
|
|
|
let getOption = () => {
|
|
|
|
|
|
let sourceData = props.sourceData;
|
|
|
let arr = [];
|
|
|
for (let i = 1; i < sourceData[0].length; i++) {
|
|
|
let nameItem = sourceData[0][i];
|
|
|
let name = nameItem;
|
|
|
if(nameItem && nameItem.name){
|
|
|
name = nameItem.name;
|
|
|
}
|
|
|
arr.push({
|
|
|
name: name,
|
|
|
type: 'bar',
|
|
|
stack: 'total',
|
|
|
label: {
|
|
|
show: false
|
|
|
},
|
|
|
emphasis: {
|
|
|
focus: 'series'
|
|
|
},
|
|
|
itemStyle: {
|
|
|
normal: {
|
|
|
color: nameItem.color,
|
|
|
lineStyle: {
|
|
|
color: nameItem.color,
|
|
|
width: 1
|
|
|
}
|
|
|
}
|
|
|
},
|
|
|
})
|
|
|
}
|
|
|
let option = {
|
|
|
legend: {
|
|
|
itemWidth: 25,
|
|
|
itemHeight: 2,
|
|
|
textStyle: {
|
|
|
color: '#fff',
|
|
|
fontSize: "12px"
|
|
|
}
|
|
|
},
|
|
|
tooltip: {
|
|
|
trigger: 'axis',
|
|
|
axisPointer: {
|
|
|
type: 'shadow'
|
|
|
},
|
|
|
backgroundColor: 'rgba(50,50,50,0.7)',
|
|
|
borderColor: '#333',
|
|
|
textStyle: {
|
|
|
color: '#fff',
|
|
|
align: 'left'
|
|
|
}
|
|
|
},
|
|
|
dataset: {
|
|
|
source: props.sourceData
|
|
|
},
|
|
|
grid: {
|
|
|
top: '30px',
|
|
|
left: '10px',
|
|
|
right: '10px',
|
|
|
bottom: '10px',
|
|
|
containLabel: true
|
|
|
},
|
|
|
|
|
|
xAxis: {
|
|
|
type: 'category',
|
|
|
splitLine: {
|
|
|
lineStyle: {
|
|
|
type: 'dashed',
|
|
|
color: '#313c5e'
|
|
|
}
|
|
|
},
|
|
|
axisLine: {
|
|
|
type: 'do',
|
|
|
show: true,//是否显示坐标线
|
|
|
lineStyle: {
|
|
|
color: '#21bad6'
|
|
|
}
|
|
|
},
|
|
|
axisLabel: {
|
|
|
color: '#ffffff',
|
|
|
fontSize: 10,
|
|
|
formatter: function (params) {
|
|
|
var newParamsName = '';
|
|
|
let paramsNameNumber = params.length;
|
|
|
let provideNumber = 3;
|
|
|
let rowNumber = Math.ceil(paramsNameNumber / provideNumber);
|
|
|
if (paramsNameNumber > provideNumber) {
|
|
|
for (let i = 0; i < rowNumber; i++) {
|
|
|
let tempStr = '';
|
|
|
let start = i * provideNumber;
|
|
|
let end = start + provideNumber;
|
|
|
if (i == rowNumber - 1) {
|
|
|
tempStr = params.substring(start, paramsNameNumber);
|
|
|
} else {
|
|
|
tempStr = params.substring(start, end) + "\n";
|
|
|
}
|
|
|
newParamsName += tempStr;
|
|
|
}
|
|
|
} else {
|
|
|
newParamsName = params;
|
|
|
}
|
|
|
return newParamsName;
|
|
|
},
|
|
|
textStyle: {
|
|
|
rich: {
|
|
|
white: {
|
|
|
padding: [1, 0, 0, 0]
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
},
|
|
|
},
|
|
|
yAxis: {
|
|
|
axisLabel: {
|
|
|
formatter: '{value}',//以百分比显示
|
|
|
color: '#ffffff',
|
|
|
},
|
|
|
splitLine: {
|
|
|
lineStyle: {
|
|
|
type: 'dashed',
|
|
|
color: '#313c5e'
|
|
|
}
|
|
|
},
|
|
|
axisLine: {
|
|
|
type: 'do',
|
|
|
show: true,//是否显示坐标线
|
|
|
lineStyle: {
|
|
|
color: '#21bad6'
|
|
|
}
|
|
|
},
|
|
|
},
|
|
|
series: arr
|
|
|
};
|
|
|
|
|
|
let line = echarts.init(document.getElementById(id));
|
|
|
setTimeout(function () {
|
|
|
line.clear();
|
|
|
line.setOption(option);
|
|
|
}, 200);
|
|
|
}
|
|
|
|
|
|
// 挂载完
|
|
|
Vue.onMounted(() => {
|
|
|
getOption();
|
|
|
})
|
|
|
|
|
|
return {
|
|
|
id
|
|
|
}
|
|
|
}
|
|
|
} |
...
|
...
|
|