ali
2024-09-03 0ae83a895e80a4b9777a27f613d721a7e5e2ac18
energy_management_ui/src/views/energyStatistics/energyConsumption/pieChartCost.vue
@@ -1,29 +1,29 @@
<template>
  <div :class="className" :style="{height:height,width:width}" />
  <div :class="className" :style="{ height: height, width: width }" />
</template>
<script>
import echarts from 'echarts'
require('echarts/theme/macarons') // echarts theme
import resize from '../mixins/resize'
import echarts from "echarts";
require("echarts/theme/macarons"); // echarts theme
import resize from "../mixins/resize";
export default {
  mixins: [resize],
  props: {
    className: {
      type: String,
      default: 'chart'
      default: "chart"
    },
    width: {
      type: String,
      default: '100%'
      default: "100%"
    },
    height: {
      type: String,
      default: '350px'
      default: "350px"
    },
    chartData: {
      type:Object,
      type: Object
      //required: true
    }
  },
@@ -31,91 +31,94 @@
    chartData: {
      deep: true,
      handler(val) {
        this.setOptions(val)
        this.setOptions(val);
      }
    }
  },
  data() {
    return {
      chart: null
    }
    };
  },
  mounted() {
    this.$nextTick(() => {
      this.initChart()
    })
      this.initChart();
    });
  },
  beforeDestroy() {
    if (!this.chart) {
      return
      return;
    }
    this.chart.dispose()
    this.chart = null
    this.chart.dispose();
    this.chart = null;
  },
  methods: {
    initChart() {
      this.chart = echarts.init(this.$el, 'macarons')
      this.setOptions(this.chartData)
      this.chart = echarts.init(this.$el, "macarons");
      this.setOptions(this.chartData);
    },
    setOptions({newVisitis,datas,title } = {}) {
    setOptions({ newVisitis, datas, title } = {}) {
      let scale = 1;
      this.chart.setOption({
        title: {
          text: '能源消费成本分析',
          x:'left',
          y: 'top',
          text: "能源消费成本分析",
          x: "left",
          y: "top",
          textStyle: {
            color: "#333",
          },
            color: "#fff"
          }
        },
        tooltip: {
          trigger: 'item',
          trigger: "item",
          formatter: datas
        },
        grid: {
          top: 40,
          left: '2%',
          right: '2%',
          bottom: '3%',
          left: "2%",
          right: "2%",
          bottom: "3%",
          containLabel: true
        },
        legend: {
          // orient 设置布局方式,默认水平布局,可选值:'horizontal'(水平) ¦ 'vertical'(垂直)
          orient: 'vertical',
          orient: "vertical",
          // x 设置水平安放位置,默认全图居中,可选值:'center' ¦ 'left' ¦ 'right' ¦ {number}(x坐标,单位px)
          x: 'left',
          x: "left",
          // y 设置垂直安放位置,默认全图顶端,可选值:'top' ¦ 'bottom' ¦ 'center' ¦ {number}(y坐标,单位px)
          y: '10%',
          y: "10%",
          //left: 10,
          itemWidth: 24,   // 设置图例图形的宽
          itemHeight: 18,  // 设置图例图形的高
          itemWidth: 24, // 设置图例图形的宽
          itemHeight: 18, // 设置图例图形的高
          textStyle: {
            color: '#666'  // 图例文字颜色
            color: "#fff" // 图例文字颜色
          },
          // itemGap设置各个item之间的间隔,单位px,默认为10,横向布局时为水平间隔,纵向布局时为纵向间隔
         // itemGap: 30,
          backgroundColor: '#eee',  // 设置整个图例区域背景颜色
          // itemGap: 30,
          backgroundColor: "#eee", // 设置整个图例区域背景颜色
          data: datas,
          formatter:function(item){
          formatter: function(item) {
            let target;
            let unitId;
            for(let i=0;i<newVisitis.length;i++){
              if(datas[i].name===item){
                target=datas[i].value;
                unitId=datas[i].unitId
            for (let i = 0; i < newVisitis.length; i++) {
              if (datas[i].name === item) {
                target = datas[i].value;
                unitId = datas[i].unitId;
              }
            }
            let arr=[item+ +target+ unitId]/*["{a|"+target+"}","{b|"+item+"}"]*/
            return arr/*.join("\n")*/
          },
            let arr = [
              item + +target + unitId
            ]; /*["{a|"+target+"}","{b|"+item+"}"]*/
            return arr; /*.join("\n")*/
          }
        },
        series: [{
        series: [
          {
            //name: '用水',
            type: 'pie',
            type: "pie",
            avoidLabelOverlap: false,
            radius: ['40%', '60%'], // 设置环形饼状图, 第一个百分数设置内圈大小,第二个百分数设置外圈大小
            center: ['65%', '50%'],  // 设置饼状图位置,第一个百分数调水平位置,第二个百分数调垂直位置
            data: newVisitis,//[{value:335, name:'用水' },{value:310, name:'用电'},{value:234, name:'用煤'}],/*selected: true*/
            radius: ["40%", "60%"], // 设置环形饼状图, 第一个百分数设置内圈大小,第二个百分数设置外圈大小
            center: ["65%", "50%"], // 设置饼状图位置,第一个百分数调水平位置,第二个百分数调垂直位置
            data: newVisitis, //[{value:335, name:'用水' },{value:310, name:'用电'},{value:234, name:'用煤'}],/*selected: true*/
            // itemStyle 设置饼状图扇形区域样式
            itemStyle: {
              // emphasis:英文意思是 强调;着重;(轮廓、图形等的)鲜明;突出,重读
@@ -123,26 +126,26 @@
              emphasis: {
                shadowBlur: 10,
                shadowOffsetX: 0,
                shadowColor: 'rgba(30, 144, 255,0.5)'
                shadowColor: "rgba(30, 144, 255,0.5)"
              }
            },
            // 设置值域的那指向线
            labelLine: {
              normal: {
                show: false   // show设置线是否显示,默认为true,可选值:true ¦ false
                show: false // show设置线是否显示,默认为true,可选值:true ¦ false
              }
            },
            // 设置值域的标签
          label: {
            normal: {
              show: true,
              position: 'center',
              formatter: ['累计成本',title].join('\n'),
            },
            emphasis: {
              show: true,
            },
          },
            label: {
              normal: {
                show: true,
                position: "center",
                formatter: ["累计成本", title].join("\n")
              },
              emphasis: {
                show: true
              }
            }
            /*label: {
              normal: {
                position: 'inner',  // 设置标签位置,默认在饼状图外 可选值:'outer' ¦ 'inner(饼状图上)'
@@ -153,9 +156,9 @@
              }
            }*/
          }
        ],
      })
        ]
      });
    }
  }
}
};
</script>