vue——echarts 饼图中心默认展示总值

在这里插入图片描述

<span style="font-weight:600">操作系统</span>
<div
  class="echart"
  id="system-echart"
  :style="{ float: 'left', width: '100%', height: '300px' }"
></div>

<script>
import * as echarts from "echarts";
import { pieEchart } from "../util/pieechart";
export default {
  name: "PieEchart",
  props: ["breakdownData"],
  data() {
    return {
      systemName: "操作系统",
      os_types: [
	    {
	        "name": "Windows",
	        "value": 17
	    },
	    {
	        "name": "Mac OS X",
	        "value": 3
	    },
	    {
	        "name": "Android",
	        "value": 1
	    }
      ]
    };
  },
  methods: {
    refreshpies() {
      let system_echart = document.getElementById("system-echart");
      this.renderInitEchart(system_echart, this.systemName, this.os_types);
    },
    renderInitEchart(id, name, pieData) {
      let total = 0;
      if (pieData.length > 0) {
        for (let item of pieData) {
          total += item.value;
        }
      }

      let getchart = echarts.init(id);
      let option = pieEchart(name, pieData, total);
      getchart.setOption(option);

      getchart.on("mouseover", function() {
        getchart.setOption({
          title: {
            text: ""
          }
        });
      });

      getchart.on("mouseout", function() {
        getchart.setOption({
          title: {
            text: ["{value|" + total + "}"]
          }
        });
      });

      //随着屏幕大小调节图表
      window.addEventListener("resize", () => {
        getchart.resize();
      });
    }
  }
};
</script>

util/pieechart.js

function pieEchart(name, data, total) {
  let option = {
    title: {
      text: [`{value|${total > 0 ? total : "无数据"}}`],
      top: "center",
      left: "24%",
      textAlign: "center",
      textStyle: {
        rich: {
          value: {
            fontSize: 30,
            fontWeight: "600"
          }
        }
      }
    },
    tooltip: {
      trigger: "item"
    },
    legend: {
      type: "scroll",
      orient: "vertical",
      left: "50%",
      top: 26,
      icon: "rect",
      itemHeight: 26,
      itemWidth: 8,
      selectedMode: false,
      textStyle: {
        rich: {
          name: {
            fontSize: 12,
            color: "#011",
            padding: [4, 0, 0, 0]
          },
          num: {
            fontSize: 14,
            fontWeight: 600,
            padding: [4, 0, 0, 0],
            color: "#000"
          }
        }
      },
      formatter: function(name) {
        let tarValue;
        if (data.length > 0) {
          for (let i = 0; i < data.length; i++) {
            if (data[i].name == name) {
              tarValue = data[i].value;
            }
          }
        }

        return [`{name|${name}}`, `{num|${tarValue || "无数据"}}`].join("\n");
      }
    },
    series: [
      {
        name,
        center: ["24%", "50%"],
        type: "pie",
        radius: ["50%", "74%"],
        avoidLabelOverlap: false,
        label: {
          show: false,
          position: "center",
          formatter: "{c}",
          fontSize: "30",
          fontWeight: "600"
        },
        emphasis: {
          label: {
            show: true,
            fontSize: "30",
            fontWeight: "600"
          }
        },
        labelLine: {
          show: false
        },
        data
      }
    ]
  };

  return option;
}

export { pieEchart };
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值