vue3+eachrts饼图轮流切换显示高亮数据

本文主要是介绍vue3+eachrts饼图轮流切换显示高亮数据,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

在这里插入图片描述

<template><div class="charts-box"><div class="charts-instance" ref="chartRef"></div>// 自定义legend 样式<div class="charts-note"><span v-for="(items, index) in data.dataList" class="charts-legend"><span class="legend" :style="{background:colorList[index]}"></span><span>{{items.name}}</span><span style="color: #999999;margin-left: 5px;">{{((items.value / props.total)*100).toFixed(2) }}%</span></span></div></div></template><script setup>import {ref, onMounted, onUnmounted, reactive, watch} from 'vue'import * as echarts from 'echarts'import useEmitter from "@/hooks/useEmitter";import {findRightBottomPie, removeTrailingZeros} from "@/views/BangFuData/service"; // 接口const props = defineProps(['timeRange', 'dictItem', 'total'])const emitter = useEmitter()const chartRef = ref()const data = reactive({dataList: [{"name": "中央统战部","value": "4742.93","count": null,"type": null},{"name": "民盟","value": "439.47","count": null,"type": null},{"name": "致公党","value": "377.23","count": null,"type": null},{"name": "农工党","value": "1181.58","count": null,"type": null},{"name": "民建","value": "365.38","count": null,"type": null},{"name": "民进","value": "200.72","count": null,"type": null},{"name": "全国工商联","value": "2112.68","count": null,"type": null},{"name": "民革","value": "102.70","count": null,"type": null},{"name": "九三学社","value": "439.30","count": null,"type": null},{"name": "台盟","value": "24.50","count": null,"type": null}],})const colorList =['#876FFE', '#F85A8E','#E74C3C','#FFC476','#F1C40F','#80C269','#13B5B1','#54DCFF','#1A92FF','#5085FF', '#F85A8E','#E74C3C','#FFC476','#F1C40F','#80C269','#13B5B1']const findData = (callback) => {let rightBottomPieRes = findRightBottomPie(props.timeRange === 'previous' ? 2 : 1)rightBottomPieRes.then(res => {// let arr = []// props.dictItem.find(qhItem => {//   let result = res.find(item => item.name.indexOf(qhItem.label) > -1)//   if (result) {//     arr.push({name: qhItem.label, value: result.value})//   } else {//     arr.push({name: qhItem.label, value: 0})//   }// })data.dataList = rescallback()})}let myChart = nullconst option = ref(null)const buildOption = () => {let _option = {tooltip: {show: false,trigger: 'item',},legend: {show: false,itemWidth: 12,itemHeight: 12,itemGap: 20,icon: 'roundRect',bottom: '5%',left: '45px',right: '45px',formatter: (name) => {let total = 0;let tarValue = 0;for (let i = 0; i < data.dataList.length; i++) {total += parseFloat(data.dataList[i].value);}for (let index = 0; index < data.dataList.length; index++) {if (name.indexOf(data.dataList[index].name)> -1) {if(total && total !== 0){tarValue = ((data.dataList[index].value / total)*100).toFixed(2);}}}// if(tarValue === 0){//   return `{oneone|${name}}`;// }return `{oneone|${name}}   {threethree|${removeTrailingZeros(tarValue)}%}`;},textStyle: {rich: {threethree: {color: "#999",}}}},title: {text: '0',//主标题文本subtext:'资金(万元)',//副标题文本right:'center',top:'42%',textStyle:{fontSize: '1.2rem',color:'#454c5c',align:'center'},subtextStyle:{fontFamily : "微软雅黑",fontSize: '0.8rem',color:'#6c7a89',}},color: colorList,series: [{name: '',type: 'pie',radius: ['35%', '55%'],center: ['50%', '50%'],avoidLabelOverlap: false,itemStyle: {borderRadius: 0,borderColor: '#fff',borderWidth: 2},// label: {//     show: false,//     position: 'center'// },emphasis: {label: {show: true,fontSize: 20,fontWeight: 'bold'},scale: true,scaleSize: 20},// labelLine: {//     show: false// },label: {show: true,alignTo: 'edge',formatter: '{name|{b}} {time|{d}%} \n',minMargin: 5,edgeDistance: 10,lineHeight: 15,rich: {name: {fontSize: 14,color: '#000'},time: {fontSize: 14,color: '#999'}}},labelLine: {show: true,length: 30,length2: 0,maxSurfaceAngle: 80},labelLayout: function (params) {const isLeft = params.labelRect.x < myChart.getWidth() / 2;const points = params.labelLinePoints;// Update the end point.points[2][0] = isLeft  ? params.labelRect.x : params.labelRect.x + params.labelRect.width;return {labelLinePoints: points};},data: data.dataList}]}_option.series[0].data = data.dataList;option.value = _option}const handleChartLoop = (option, myChart) => {if (!myChart) {return;}let currentIndex = -1; // 当前高亮图形在饼图数据中的下标let timeTicket = setInterval(selectPie, 2000); // 设置自动切换高亮图形的定时器// 取消所有高亮并高亮当前图形function highlightPie() {// 遍历饼图数据,取消所有图形的高亮效果for (let idx in option.series[0].data) {myChart.dispatchAction({type: "downplay",seriesIndex: 0,dataIndex: idx,});}// 高亮当前图形myChart.dispatchAction({type: "highlight",seriesIndex: 0,dataIndex: currentIndex,});let data = option.series[0].data[currentIndex] && option.series[0].data[currentIndex].value ? option.series[0].data[currentIndex].value : 0myChart.setOption({title: {text: removeTrailingZeros(parseFloat(data).toFixed(2))},series: {label: {show: false,formatter: () => {if ((option.series[0].data[currentIndex] && option.series[0].data[currentIndex].value === 0)|| !props.total || props.total == 0) {return `{name|${option.series[0].data[currentIndex].name}} {time|0%} \n`}let val = removeTrailingZeros(((option.series[0].data[currentIndex].value/props.total)*100).toFixed(2))return `{name|${option.series[0].data[currentIndex].name}} {time|${val}%} \n`}}}})}// 用户鼠标悬浮到某一图形时,停止自动切换并高亮鼠标悬浮的图形myChart.on("mouseover", (params) => {clearInterval(timeTicket);currentIndex = params.dataIndex;highlightPie();});// 用户鼠标移出时,重新开始自动切换myChart.on("mouseout", (params) => {if (timeTicket) {clearInterval(timeTicket);}timeTicket = setInterval(selectPie, 1000);});// 高亮效果切换到下一个图形function selectPie() {let dataLen = option.series[0].data.length;currentIndex = (currentIndex + 1) % dataLen;highlightPie();}}watch(() => props.timeRange, (nv, ov) => {findData(()=>buildOption())})watch(()=>option.value,(newOption,oldOption)=>{if(myChart == null) {myChart = echarts.init(chartRef.value);}option.value && myChart && myChart.setOption(newOption)if(oldOption==null){option.value && myChart && handleChartLoop(option.value,myChart);} else {//option.value && myChart && handleChartLoop(option.value,myChart);}})onMounted(()=>{myChart = echarts.init(chartRef.value);findData(()=>buildOption());emitter.on('echartsTimeRangeChange',()=>setTimeout(() => myChart && myChart.resize({width:chartRef.value.clientWidth,height:chartRef.value.clientHeight}), 50))emitter.on('echartsUpdate',()=>myChart && myChart.resize({width:chartRef.value.clientWidth,height:chartRef.value.clientHeight}))})onUnmounted(()=>{emitter.off('echartsTimeRangeChange')emitter.off('echartsUpdate')myChart.dispose()})</script><style scoped>.charts-box{height: calc(100% - 48px);}.charts-instance{height: 60%;width: 100%;overflow: hidden;
}
.charts-note{margin-left: 15%;height: 40%;overflow: hidden;
}
.charts-legend {display: inline-block;vertical-align: middle;margin-top: 15px;width: 50%;font-size: 12px;
}
.legend {display: inline-block;vertical-align: middle;margin-right:5px;width: 12px;height: 12px;border-radius: 2px;
}</style>

这篇关于vue3+eachrts饼图轮流切换显示高亮数据的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!


原文地址:
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.chinasem.cn/article/872824

相关文章

Vue3使用router,params传参为空问题

《Vue3使用router,params传参为空问题》:本文主要介绍Vue3使用router,params传参为空问题,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐... 目录vue3使用China编程router,params传参为空1.使用query方式传参2.使用 Histo

CSS Padding 和 Margin 区别全解析

《CSSPadding和Margin区别全解析》CSS中的padding和margin是两个非常基础且重要的属性,它们用于控制元素周围的空白区域,本文将详细介绍padding和... 目录css Padding 和 Margin 全解析1. Padding: 内边距2. Margin: 外边距3. Padd

CSS will-change 属性示例详解

《CSSwill-change属性示例详解》will-change是一个CSS属性,用于告诉浏览器某个元素在未来可能会发生哪些变化,本文给大家介绍CSSwill-change属性详解,感... will-change 是一个 css 属性,用于告诉浏览器某个元素在未来可能会发生哪些变化。这可以帮助浏览器优化

CSS去除a标签的下划线的几种方法

《CSS去除a标签的下划线的几种方法》本文给大家分享在CSS中,去除a标签(超链接)的下划线的几种方法,本文给大家介绍的非常详细,感兴趣的朋友一起看看吧... 在 css 中,去除a标签(超链接)的下划线主要有以下几种方法:使用text-decoration属性通用选择器设置:使用a标签选择器,将tex

前端高级CSS用法示例详解

《前端高级CSS用法示例详解》在前端开发中,CSS(层叠样式表)不仅是用来控制网页的外观和布局,更是实现复杂交互和动态效果的关键技术之一,随着前端技术的不断发展,CSS的用法也日益丰富和高级,本文将深... 前端高级css用法在前端开发中,CSS(层叠样式表)不仅是用来控制网页的外观和布局,更是实现复杂交

Python将博客内容html导出为Markdown格式

《Python将博客内容html导出为Markdown格式》Python将博客内容html导出为Markdown格式,通过博客url地址抓取文章,分析并提取出文章标题和内容,将内容构建成html,再转... 目录一、为什么要搞?二、准备如何搞?三、说搞咱就搞!抓取文章提取内容构建html转存markdown

Python获取中国节假日数据记录入JSON文件

《Python获取中国节假日数据记录入JSON文件》项目系统内置的日历应用为了提升用户体验,特别设置了在调休日期显示“休”的UI图标功能,那么问题是这些调休数据从哪里来呢?我尝试一种更为智能的方法:P... 目录节假日数据获取存入jsON文件节假日数据读取封装完整代码项目系统内置的日历应用为了提升用户体验,

在React中引入Tailwind CSS的完整指南

《在React中引入TailwindCSS的完整指南》在现代前端开发中,使用UI库可以显著提高开发效率,TailwindCSS是一个功能类优先的CSS框架,本文将详细介绍如何在Reac... 目录前言一、Tailwind css 简介二、创建 React 项目使用 Create React App 创建项目

vue使用docxtemplater导出word

《vue使用docxtemplater导出word》docxtemplater是一种邮件合并工具,以编程方式使用并处理条件、循环,并且可以扩展以插入任何内容,下面我们来看看如何使用docxtempl... 目录docxtemplatervue使用docxtemplater导出word安装常用语法 封装导出方

Java利用JSONPath操作JSON数据的技术指南

《Java利用JSONPath操作JSON数据的技术指南》JSONPath是一种强大的工具,用于查询和操作JSON数据,类似于SQL的语法,它为处理复杂的JSON数据结构提供了简单且高效... 目录1、简述2、什么是 jsONPath?3、Java 示例3.1 基本查询3.2 过滤查询3.3 递归搜索3.4