mkl_power_box/components/warning-pie-chart.js

182 lines
6.4 KiB
JavaScript

/**
* 顶部header
*/
Vue.component("warning-pie-chart", {
template: `
<div style="position: relative">
<div :style="{'height': height+'px'}" ref="warningPieChart">
</div>
<div class="chart-gif" v-if="typedata.length > 0"></div>
</div>
`,
props: {
typedata:{
type: Array,
},
height:{
type:Number
}
},
data() {
return {
active:0,
option:{}
}
},
mounted(){
// this.init()
},
methods: {
init(){
this.getChartData()
},
getChartData(){
//品类金额占比 饼图
var chChartPie = echarts.init(this.$refs.warningPieChart);
this.echartPie(chChartPie,this.typedata)
},
echartPie(chChart,chartData){
let newPromise = new Promise((resolve) => {
resolve()
})
//然后异步执行echarts的初始化函数
newPromise.then(() => {
var total_datas = 0;
var data = [];
var legendData = [];
var color = ['#4474fa','#52aef7','#2075ae']
for (let i = 0; i <chartData.length ; i++) {
total_datas += Number(chartData[i].value);
legendData.push(chartData[i].name)
data.push(
{
value: chartData[i].value,
name: chartData[i].name,
itemStyle: {
normal: {
//颜色渐变
color: color[i]
},
},
},
)
}
/* let total = chartData.reduce((a, b) => {
return a + b.value;
}, 0);*/
this.option = {
title: {
text: total_datas,
subtext: "台",
x: "center",
y: "80",
textStyle: {
color: "#0dd2fd",
fontSize: 28,
fontWeight: "normal",
align: "center",
width: "200px",
},
subtextStyle: {
color: "#a5b5f0",
fontSize: 16,
fontWeight: "normal",
align: "center",
},
},
tooltip: {
trigger: 'item',
formatter: "{b} <br/>{c} ({d}%)"
},
legend: {
width:'100%',
left: "center",
right: "0",
bottom: "5",
//icon: "circle",
itemWidth: 15,
itemGap:35,
textStyle: {
fontSize: 14,
rich: {
name: {
color: "#c3dbfd",
padding: [10, 5, 30, 5],
// align: 'right'
// width: 60
},
percent: {
color: "#18DB9F",
fontSize: 16,
padding: [0, 5, 0, 5],
// align: 'right'
},
},
},
formatter: function (name) {
let res = chartData.filter((v) => v.name === name);
let percent = total_datas == 0?0:((res[0].value * 100) / total_datas).toFixed(2);
return "{name| " + name + "}\n{percent|" + res[0].value + "}{percent|" + percent + "%}";
},
},
series: [
{
name: "品类金额占比",
type: "pie",
center: ["50%", "40%"],
radius: ["42%", "60%"],
data: data,
label: {
show: false,
},
itemStyle: {
normal: {
borderWidth: 5,
borderColor: "#051a36"
}
},
},
{
name: "外边框",
type: "pie",
clockWise: false, //顺时加载
hoverAnimation: false, //鼠标移入变大
center: ["50%", "40%"],
radius: ["66%", "66%"],
label: {
normal: {
show: false,
},
},
data: [
{
// value: 9,
// name: "",
// itemStyle: {
// normal: {
// borderWidth: 3,
// borderColor: "#152c65",
// },
// },
},
],
},
],
}
chChart.setOption(this.option);
window.onresize = chChart.resize;
})
},
},
watch:{
typedata: function (n,o) {
this.init()
}
}
})