给echarts图表添加弧形动画效果

2024-06-12 19:44

本文主要是介绍给echarts图表添加弧形动画效果,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

         这里在option中一定要设置animation: false,因为echarts的5.0版本以后执行动画效果会卡顿,不清楚为什么。开启定时器时,定时器的间隔应当设置小一点,不然也会出现卡顿效果,希望echarts5.50版本之后能修复ecahrts执行加载效果卡顿的问题吧......

<template><div class="h-120px" ref="pieChart"></div>
</template><script lang="ts" setup>
import { watch, ref, onMounted, onBeforeUnmount } from "vue";
import * as echarts from "echarts";
import { useEcharts } from "@/hooks/useEcharts";
const pieChart = ref();
const props = defineProps({data: {type: Object,default: () => {}}
});
watch(() => props.data,() => {initPieChart();},{deep: true}
);/*** 制作弧形的动画效果*/
let angle = 0; //角度,用来做简单的动画效果的
let timer: NodeJS.Timer; // 定时器
// 更新旋转的弧形的开始角度和结束角度
//获取圆上面某点的坐标(s:起始角度,e:结束角度,angle角度)
const getCirclePoint = (s: number, e: number) => {return {startAngle: ((s + angle) * Math.PI) / 180,endAngle: ((e + angle) * Math.PI) / 180};
};
const draw = (myChart: echarts.ECharts, option: echarts.EChartsCoreOption) => {angle = angle + 3;myChart.setOption(option, true);
};
// 鼠标移入echarts图表暂停动画
const stopAnimation = () => {clearInterval(timer);
};
// 鼠标移出echarts图表开始动画
const startAnimation = (myChart: echarts.ECharts, option: echarts.EChartsCoreOption) => {timer = setInterval(() => {draw(myChart, option);}, 50);
};
// 初始化图表
const initPieChart = () => {// 判断当前echarts是否存在let myChart = echarts.getInstanceByDom(pieChart.value);if (myChart == null) {myChart = echarts.init(pieChart.value);}const productList = ["产品1", "产品2", "产品3", "产品4", "产品5", "产品6", "产品7"];let totalData = [1.5, 1.3, 1.1, 2.3, 2.4, 2.6, 2.4];let totalNumber = 0;totalData.forEach(item => {totalNumber += item;});let finalData = [];for (let i = 0; i < productList.length; i++) {finalData.push({ name: productList[i], value: totalData[i] });}const option = {animation: false,tooltip: {trigger: "item",formatter: "{b} <br/>产能: {c}t"},legend: {type: "plain",orient: "vertical",height: "88%",right: "20",top: "center",itemWidth: 8,itemHeight: 8,data: productList,//格式化每一项内容formatter(name: string) {//只接收一个参数,就是类目名称let value = 0;//使用name去放内容的数组中拿到对应的值finalData.forEach(item => {if (item.name == name) {value = item.value;}});//name, value可以理解为样式标记符,用于在富文本中设置对应的样式return name + " " + ((value / totalNumber) * 100).toFixed(2) + "%";},textStyle: {fontSize: 14,fontWeight: 400,color: "#D1D6DF"}},series: [{type: "pie",radius: ["58%", "88%"],center: ["16%", "50%"],color: ["#4E7BFE", "#6796FD", "#3CB4FF", "#26D080", "#6B72D4", "#FEC704", "#FF4756"],label: {normal: {show: false}},labelLine: {normal: {show: false}},data: finalData},// 内圈阴影{itemStyle: {normal: {color: "rgba(8, 58, 84, 1)"}},type: "pie",hoverAnimation: false,radius: ["50%", "65%"],center: ["16%", "50%"],label: {normal: {show: false}},data: [{value: 1}],z: -1},// 外圈圆弧{name: "ring1",type: "custom",coordinateSystem: "none",renderItem: function (params: any, api: any) {let point = getCirclePoint(60, 120);return {type: "arc",shape: {cx: api.getWidth() * 0.16,cy: api.getHeight() / 2,r: Math.min(api.getWidth(), api.getHeight()) * 0.48,startAngle: point.startAngle,endAngle: point.endAngle},style: {stroke: "#FF0351",fill: "transparent",lineWidth: 1.5},silent: true};},data: [0]},{name: "ring2",type: "custom",coordinateSystem: "none",renderItem: function (params: any, api: any) {let point = getCirclePoint(180, 240);return {type: "arc",shape: {cx: api.getWidth() * 0.16,cy: api.getHeight() / 2,r: Math.min(api.getWidth(), api.getHeight()) * 0.48,startAngle: point.startAngle,endAngle: point.endAngle},style: {stroke: "#FF0351",fill: "transparent",lineWidth: 1.5},silent: true};},data: [0]},{name: "ring3",type: "custom",coordinateSystem: "none",renderItem: function (params: any, api: any) {let point = getCirclePoint(300, 360);return {type: "arc",shape: {cx: api.getWidth() * 0.16,cy: api.getHeight() / 2,r: Math.min(api.getWidth(), api.getHeight()) * 0.48,startAngle: point.startAngle,endAngle: point.endAngle},style: {stroke: "#FF0351",fill: "transparent",lineWidth: 1.5},silent: true};},data: [0]}]};useEcharts(myChart, option);// 开始定时器执行动画效果startAnimation(myChart, option);// 给echarts图表绑定鼠标移入移除事件myChart.on("mouseover", function () {stopAnimation();});myChart.on("mouseout", function () {startAnimation(myChart, option);});
};
onMounted(() => {initPieChart();
});
onBeforeUnmount(() => {stopAnimation();
});
</script><style lang="scss" scoped></style>

附上自己封装的useEcharts函数:给ecahrts添加响应式

import { onBeforeUnmount, onActivated, onDeactivated } from "vue";
import { useDebounceFn } from "@vueuse/core";
import * as echarts from "echarts";/*** @description 使用Echarts(只是为了添加图表响应式)* @param {Element} myChart Echarts实例(必传)* @param {Object} options 绘制Echarts的参数(必传)* @return void* */
export const useEcharts = (myChart: echarts.ECharts, options: echarts.EChartsCoreOption) => {if (options && typeof options === "object") {myChart.setOption(options);}const echartsResize = useDebounceFn(() => {myChart && myChart.resize();}, 100);window.addEventListener("resize", echartsResize);onBeforeUnmount(() => {window.removeEventListener("resize", echartsResize);});onActivated(() => {window.addEventListener("resize", echartsResize);});onDeactivated(() => {window.removeEventListener("resize", echartsResize);});
};

这篇关于给echarts图表添加弧形动画效果的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/1055155

相关文章

以canvas方式绘制粒子背景效果,感觉还可以

这个是看到项目中别人写好的,感觉这种写法效果还可以,就存留记录下 就是这种的背景效果。如果想改背景颜色可以通过canvas.js文件中的fillStyle值改。 附上demo下载地址。 https://download.csdn.net/download/u012138137/11249872

echarts省份标注加散点效果

这个是安徽的效果图,鼠标移到红色标注或者对应的市区位置都会显示对应的数值。 先直接上代码: import anhuiMapJson from './anhui.json'getCoords: function(city) {var res = [];if (city != null) {for (var c in this.cityMap.features) {if (this.cityMa

如何利用echarts编写立体的柱状图表

1、引入 import * as echarts from 'echarts' 2、创建图标容器 3、调用渲染 <template><div ref="eachrtsBox" style="width: 200px;height: 200px;"></div></template><script>import * as echarts from 'echarts'export d

XMG 抽屉效果

1.比如说我创建了3个View -(void)viewDidLoad{  [ super viewDidLoad]; [self setUpChild] ;         UIPanGestureRecognizer *pan=[UIPanGestureRecognizer alloc]initWithTarget:self action:@selector(pan:)];

ArkTS开发系列之导航 (2.7动画)

上篇回顾: ArkTS开发系列之导航 (2.6 图形) 本篇内容:动画的学习使用 一、 知识储备 1. 布局更新动画 包含显式动画(animateTo)和属性动画(animation) 动画类型名称特点显式动画闭包内的变化都会触发动画执行, 可以做较复杂的动画属性动画属性变化时触发动画执行, 设置简单 说白了,显示动画就是靠闭包事件触发,属性动画是挂在组件身上的属性变化触发 显式动画

Android 扇形网络控件 - 无网络视图(动画)

前言 一般在APP没有网络的情况下,我们都会用一个无网络的提示图标,在提示方面为了统一app的情况,我们一般使用简单的提示图标,偶尔只需要改变一下图标的颜色就一举两得,而不需要让PS来换一次颜色。当然app有图标特殊要求的就另当别论了。 效果图 当你第一眼看到这样的图,二话不说直接让UI给你切一张图标来的快对吧,我其实开始也是这么想的,但是到了做的app越来越多的时候,你就会发现就算是用

33个jQuery与CSS3实现的绚丽鼠标悬停效果

只要你有创意,完全可以使用CSS3来实现漂亮的动效,当然如果配合jQuery,这样会更加强大,实现更多高级绚丽的动画效果。鼠标hover效果是很常用的,虽然很细微的东西,但网站的细节注定的网站的体验,所以也不要忽视这些小细节。 今天设计达人网整理了33个使用jQuery与CSS3实现绚丽的鼠标悬停效果,有些是纯CSS3的,这些效果你完全可以用在你的网页上,让网站获得更好的体验。 Anim

自定义recyclerView实现时光轴效果

时光轴效果在很多app上都有出现,例如淘宝中快递的跟踪,本文将使用recyclerView实现时光轴效果,我们会到自定义控件,首先先看一下效果图: 接下来是步骤分析 1自定义属性 这个大家应该都了解了,根据我们之前的分析,直接在attrs.xml中进行声明 <declare-styleable name="TimeLine"><attr name="beginLine" f

Android滑动回弹效果

原理: addHeaderView里做的事: 1.测量出header的宽高,调用了measureView方法 2.设置LayoutParams,宽:MATCH_PARENT,高:10 3.设置topMargin的值为负的header的高度,即将header隐藏在屏幕最上方 onInterceptTouchEvent: 如果滑动距离为零,让onInterceptTouchEvent处理。屏

PriorityQueue详解(含动画演示)

目录 PriorityQueue详解1、PriorityQueue简介2、PriorityQueue继承体系3、PriorityQueue数据结构PriorityQueue类属性注释完全二叉树、大顶堆、小顶堆的概念☆PriorityQueue是如何利用数组存储小顶堆的?☆利用数组存储完全二叉树的好处? 4、PriorityQueue的`offer`方法动画演示offer插入过程: 5、Pri