jhbigscreen/src/components/project-overview-chart.js

218 lines
7.5 KiB
JavaScript
Raw Normal View History

2023-08-10 01:16:23 +08:00
import Vue from 'vue'
Vue.component("project-overview-chart", {
template: `
2023-08-12 14:54:19 +08:00
<div class='project-overview-chart' style="position: relative" @click="doClick">
2024-06-16 22:03:02 +08:00
<div :style="'height:'+height+'px;'+(width?('width:'+width+'px;'):'')" ref="warningPieChart">
2023-08-10 01:16:23 +08:00
</div>
2023-10-15 00:26:35 +08:00
<div class="chart-gif chart-overview-gif" style="{'top':gifTop}"></div>
</div>
2023-08-10 01:16:23 +08:00
`,
props: {
2023-10-15 00:26:35 +08:00
gifTop:{
type:String,
default:'63px'
},
2023-09-23 00:06:58 +08:00
fn:{
type:Function
},
2023-08-10 01:16:23 +08:00
typedata:{
type: Array,
},
2024-06-16 22:03:02 +08:00
width:{
type:Number
},
2023-08-10 01:16:23 +08:00
height:{
type:Number
},
text:{
type:String
},
legendOpt:{
type:Object,
default:()=>{}
},
maintitle:{
type:[String,Number],
default:''
},
sp:{
type:String,
default:"\n"
}
},
data() {
return {
active:0,
option:{}
}
},
mounted(){
this.init()
},
methods: {
doClick(){
this.$emit("clickme");
},
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 = ['#4974ff','#52aef7','#6863d7','#1d5d89','#20e6ff','#67feef']
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,
2023-10-14 19:52:07 +08:00
itemStyle: {
//颜色渐变
color: color[i]
2023-08-10 01:16:23 +08:00
},
},
)
2023-10-14 19:52:07 +08:00
}
2023-08-10 01:16:23 +08:00
/* let total = chartData.reduce((a, b) => {
return a + b.value;
}, 0);*/
let legendOption = {
top: "center",
orient: "vertical",
icon: "circle",
2024-06-16 22:03:02 +08:00
itemGap: 0,
2023-08-10 01:16:23 +08:00
textStyle: {
2023-10-14 19:52:07 +08:00
color: "#c3dbfd",
2024-06-16 22:03:02 +08:00
fontSize: 6,
2023-10-14 19:52:07 +08:00
rich: {
2023-08-10 01:16:23 +08:00
name: {
color: "#c3dbfd",
2024-06-16 22:03:02 +08:00
padding: [0, 0, 0, 0],
2023-08-10 01:16:23 +08:00
},
percent: {
color: "#18DB9F",
2024-06-16 22:03:02 +08:00
fontSize: 6,
padding: [0, 0, 0, 0],
2023-08-10 01:16:23 +08:00
},
},
},
formatter: (name) =>{
let res = chartData.filter((v) => v.name === name);
let percent = ((res[0].value * 100) / total_datas).toFixed(1);
if(total_datas==0){
percent=0;
}
2024-06-16 22:03:02 +08:00
return "{name| " + name + "}"+this.sp+" {val|" + res[0].value + "} {percent|" + percent + "%}";
2023-08-10 01:16:23 +08:00
},
};
let opt={...legendOption,...(this.legendOpt||{})};
2023-09-23 00:06:58 +08:00
this.option = {
2023-08-10 01:16:23 +08:00
title: {
text: this.maintitle||total_datas,
subtext: this.text,
2023-08-12 14:16:43 +08:00
textAlign:'center',
top:'top',
itemGap :10,
2023-08-10 01:16:23 +08:00
textStyle: {
color: "#0dd2fd",
2024-06-16 22:03:02 +08:00
fontSize: 14,
2023-08-10 01:16:23 +08:00
fontWeight: "bold",
2023-08-12 14:16:43 +08:00
align: "center",
2023-08-10 01:16:23 +08:00
},
subtextStyle: {
color: "#a5b5f0",
fontSize: 12,
align: "center",
},
2023-08-12 14:16:43 +08:00
padding:[95,0,0,110],
left:'left'
2023-08-10 01:16:23 +08:00
},
tooltip: {
trigger: 'item',
formatter: "{b} <br/>{c} ({d}%)"
},
legend: [
{
right: 10,
data: legendData,
align: "left",
...opt,
},
],
series: [
{
name: "品类金额占比",
2024-06-16 22:03:02 +08:00
type: "pie",
center: ["0%", "50%"],
2023-08-10 01:16:23 +08:00
radius: ["46%", "63%"],
data: data,
label: {
show: false,
},
itemStyle: {
normal: {
borderWidth: 5,
borderColor: "#051a36"
}
},
},
{
name: "外边框",
type: "pie",
clockWise: false, //顺时加载
hoverAnimation: false, //鼠标移入变大
2024-06-16 22:03:02 +08:00
center: ["0%", "50%"],
2023-08-10 01:16:23 +08:00
radius: ["70%", "70%"],
label: {
normal: {
show: false,
},
},
data: [
{
value: 9,
name: "",
itemStyle: {
normal: {
borderWidth: 3,
borderColor: "#152c65",
},
},
},
],
},
],
}
2023-09-23 00:06:58 +08:00
if(this.fn){
this.option=this.fn(this.option);
}
2023-08-10 01:16:23 +08:00
chChart.setOption(this.option);
window.onresize = chChart.resize;
})
},
},
})