vue echarts 饼图(环形图)

vue echarts 饼图(环形图) ,echarts版本为5.3.3

可以自定义颜色

javascript 复制代码
<template>
  <div>
    <div id="pieChart1" 
			ref="pieChartRef1" 
			style="width: 100%; height: 250px"></div>
  </div>
  
</template>

<script>
import * as echarts from 'echarts';
export default {
  name: '',
  props: {},
  components: '',
  data() {
    return {
      chart: null,
	  _thisForChart: null,
      _thisForWindow: null,
      colorItem: {
        "北京": '#ff6161',
        "上海": '#e0815b',
        "广州": '#ffaf33',
        "深圳": '#637be6',
        "成都": '#a1b2ff',
        "西安": '#b3daff',
      },
    }
  },
  created() {},
  mounted () {
	this.$nextTick(() => {
      this.initPieChart();
      this.addEventListenerToSidebarContainer(this)
      this.addEventListenerToWindowResize(this)
    });
	},
	beforeDestroy () {
    this.removeEventListenerToSidebarContainer()
    this.removeEventListenerToWindowResize()
  },
  computed: {},
  watch: {},  
  methods: {
    initPieChart () {
      let echartData = [
        {
          name:'北京',
          value: '0.99',
        },
        {
          name:'上海',
          value: '0.29',
        },
        {
          name:'广州',
          value: '0.44',
        },
        {
          name:'深圳',
          value: '0.14',
        },
        {
          name:'成都',
          value: '0.21',
        },
        {
          name:'西安',
          value: '0.77',
        },
      ]
	  var chartDom = document.getElementById('pieChart1');
	  this.chart = echarts.init(chartDom);
      let handleEchartData = echartData .map((item3) => {
        return {
          value: item3.value,
          name: item3.name,
          itemStyle: {
            color: this.colorItem[item3.name]
          }
        }
      })
	  let option = {
          // color: ['#EE3F3F','#ff6161','#e0815b','#e0a188','#FFB441','#FFDAA1',],
	      // tooltip: {
             // show: true,
             // trigger: 'item',
             // backgroundColor: 'rgba( 0, 0, 0,0.7)',
             // borderColor: 'rgba( 0, 0, 0,0.7)',
             // triggerOn: "click", // 点击事件
             // enterable: true, // 鼠标可移入提示框浮层中,默认为false
             // position: ['53%','40%'],
             // fontSize: 10,
             // formatter:function(params, ticket,callback) {
               // 实际请求接口,获取对应的数据
               // let req_data = {
               //   name: params.name,
               // }
               // testApi.getDetail(req_data).then((res) => {
               //   if (res.data && res.data.length) {
               //     for(let i = 0; i < res.data.length; i++) {
               //     list.push(
               //       "<div style='display:flex;align-items:center;justify-content:space-between;transform:scale(0.9)'><span style='padding-right:10px;margin-bottom: -20px;color: #fff;'>" + res.data[i].code + "</span>" + "<span style='padding-right:10px;margin-bottom: -20px;color: #fff;'>" + res.data[i].name + "</span>"+  "<span style='padding-right:10px;margin-bottom: -20px;color: #fff;'>" + "权重:" + res.data[i].WEIGHT + '%' + "</span></div>"
               //     )
               //   }
               //   listItem = list.length < 12 ? "<div style='margin-bottom:8px;padding-bottom:8px;'>" + list.join('<br/>') + "</div>" : "<div style='height: 235px;overflow-y: auto;margin-bottom:8px;padding-bottom:8px;'>" + list.join('<br/>') + "</div>"
               //   callback(ticket, listItem)
               //   } else {
               //     listItem = "<div>"+ '数据为空' + "</div>"
               //     callback(ticket, listItem)
               //   }
               // })
               // return "<span>"+ "数据请求中..." + "</span>"
              // }
             // },
			legend: {
				bottom: 0,
				textStyle: {
					rich: {
						name: {},
						value: {
							align: 'right',
							padding: [0, -80, 0 ,0],
						}
					}
				},
			  formatter: function(name) {
				let value
				for (let i = 0; i < echartData.length; i++) {
					if (echartData[i].name === name) {
						value = echartData[i].value
					}
				}
				var val = value
				return [`{name|${name}}` + ':' + `{value|${val}%}`]
			    },
				},
				series: [
				  {
				    name: '',
					type: 'pie',
					radius: ['45%', '65%'],
					center:['50%' ,'40%'],
					avoidLabelOverlap: true,
					label: {
                    show: false,
                    position: 'center',
                    // formatter: function(params) {
							// 	return '{name|' +  params.name + '}' + '\n' +  '{value|' + params.data.value + '%' + '}'
							// }, 
							// rich: {
							// 	name: {
							// 		fontSize: 14,
							// 	},
							// 	value: {
							// 		fontSize: 20,
							// 		lineHeight: 30,
							// 	}
							// }
                      },
                    emphasis: {
                    label: {
                       show: true,
								lineHeight: 30,
								formatter: function(params) {
								  return '{name|' +  params.name + '}' + '\n' +  '{value|' + params.data.value + '%' + '}'
							  }, 
								rich: {
									name: {
                    // color: "inherit",
										fontSize: 14,
									},
									value: {
                    color: "inherit",
										// fontSize: 20,
										// lineHeight: 30,
									}
								},
								textStyle: {
                  fontSize: 20,
                  fontWeight: 700,
                               },
                             }  
                         },
                  labelLine: {
                  show: false,
                  length: 7,
                  length2: 6,
                  lineStyle: {}
                  },
			     data: handleEchartData,
			     }
		      ]
		   };
		this.chart.setOption(option);
	},

	// 监听侧边栏导航的宽度发生变化
    addEventListenerToSidebarContainer(_this) {
      let sidebarContainer = document.getElementsByClassName("sidebar-container")[0];
      this._thisForChart = _this;
      sidebarContainer &&
        sidebarContainer.addEventListener("transitionend", this.sidebarResizeHandler);
    },
    removeEventListenerToSidebarContainer() {
      let sidebarContainer = document.getElementsByClassName("sidebar-container")[0];
      this._thisForChart = null
      sidebarContainer &&
        sidebarContainer.removeEventListener("transitionend", this.sidebarResizeHandler);
    },

    sidebarResizeHandler(e) {
      if (e.propertyName === "width") {
        this._thisForChart.chart.resize();
      }
    },

    // window 的尺寸发生变化的时候 会执行图表的resize
    addEventListenerToWindowResize(_this) {
      this._thisForWindow = _this;
      window.addEventListener("resize", this.windowResizeHandler);
    },
    removeEventListenerToWindowResize(_this) {
     this. _thisForWindow = null
      window.removeEventListener("resize", this.windowResizeHandler);
    },

    windowResizeHandler(e) {
      this._thisForWindow.chart.resize();
    },
		
  },
}
</script>

<style lang="scss" scoped>

</style>

展示效果图:

相关推荐
shoa_top几秒前
一文带你掌握 JSONP:从 Script 标签到手写实现
前端·面试
Crazy_Urus几秒前
深入解析 React 史上最严重的 RCE 漏洞 CVE-2025-55182
前端·安全·react.js
八荒启_交互动画1 分钟前
【基础篇007】GeoGebra工具系列_多边形(Polygon)
前端·javascript
清风扶我腰_直上青天三万里2 分钟前
vue框架无痛开发浏览器插件,好用!!本人使用脚手架开发了一款浏览器tab主页加收藏网址弹窗,以后可以自己开发需要的插件了!!
前端
想努力找到前端实习的呆呆鸟3 分钟前
vscode编写vue代码的时候一聚焦就代码块变白?怎么回事如何解决
vue.js·visual studio code
知其然亦知其所以然3 分钟前
小米的奇幻编程之旅:当 JavaScript 语法变成了一座魔法城
前端·javascript·面试
webkubor3 分钟前
一次 H5 表单事故:100vh 在 Android 上到底坑在哪
前端·javascript·vue.js
是一碗螺丝粉4 分钟前
突破小程序5层限制:如何用“逻辑物理分离”思维实现无限跳转
前端·架构
Aniugel5 分钟前
Vue2怎么搭建前端性能/错误/行为监控体系
vue.js·面试·监控
神秘的猪头5 分钟前
🎉 React 的 JSX 语法与组件思想:开启你的前端‘搭积木’之旅(深度对比 Vue 哲学)
前端·vue.js·react.js