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

这篇博客展示了如何利用ECharts库创建一个显示不同操作系统(如Windows, MacOSX, Android)分布情况的饼图。通过JavaScript代码实现图表的初始化、动态调整以及鼠标悬停时的提示信息显示。此外,还提供了数据更新的方法来刷新图表。

在这里插入图片描述

<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 };
### 实现 VueECharts 集成 为了在 Vue 项目中使用 ECharts 创建,需先安装 `echarts` 库以及用于 Vue 的封装组件库 `vue-echarts` 或者直接引入 echarts 并通过 ref 获取 dom 节点来初始化表实例。 #### 安装依赖包 可以通过 npm 来安装所需的软件包: ```bash npm install echarts vue-echarts --save ``` #### 组件创建与配置 下面是一个简单的例子展示如何在一个 Vue 单文件组件里设置并渲染一个多环形[^1]。 ```html <template> <div class="chart-container"> <!-- 使用 v-chart 自定义标签 --> <v-chart :options="pieOptions"/> </div> </template> <script> // 导入必要的模块 import ECharts from 'vue-echarts'; import 'echarts/lib/chart/pie'; // 加载所需类型的表 import 'echarts/lib/component/legend'; export default { name: "PieChart", components: { 'v-chart': ECharts, }, data() { return { pieOptions: { title: { text: '多环形', subtext: '', left: 'center' }, tooltip: { trigger: 'item' }, legend: { orient: 'vertical', left: 'left' }, series: [ { type: 'pie', radius: ['40%', '70%'], avoidLabelOverlap: false, label: { show: true, position: 'outside' }, emphasis: { label: { show: true, fontSize: '20', fontWeight: 'bold' } }, data:[ {value:335, name:'直达'}, {value:310, name:'营销广告'}, {value:234, name:'搜索引擎'} ] } ] } }; } }; </script> <style scoped> .chart-container{ width: 600px; height: 400px; } </style> ``` 此代码片段展示了怎样利用 `vue-echarts` 将 ECharts 表嵌入到 Vue.js 应用程序中的方式。这里设置了基本的选项对象 `pieOptions` ,其中包含了标题、提示框、例和系列数据等属性。对于多层环状结构,则可通过调整 `series.radius` 参数指定内外圆半径比例范围来实现。
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值