vue echarts封装使用

ops/2025/3/18 13:30:48/

echarts 尺寸自动调节 resize.js 

柱状图

components/dashboard/lineChart.vue

javascript"><template><div :class="className" :style="{height:height,width:width}" />
</template><script>
import echarts from 'echarts'
require('echarts/theme/macarons') // echarts theme
import resize from './mixins/resize'export default {mixins: [resize],props: {className: {type: String,default: 'chart'},width: {type: String,default: '100%'},height: {type: String,default: '350px'},autoResize: {type: Boolean,default: true},chartData: {type: Object,required: true}},data() {return {chart: null}},watch: {chartData: {deep: true,handler(val) {this.setOptions(val)}}},mounted() {this.$nextTick(() => {this.initChart()})},beforeDestroy() {if (!this.chart) {return}this.chart.dispose()this.chart = null},methods: {initChart() {this.chart = echarts.init(this.$el, 'macarons')this.setOptions(this.chartData)},setOptions({ expectedData, actualData } = {}) {this.chart.setOption({xAxis: {data: ['Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat', 'Sun'],boundaryGap: false,axisTick: {show: false}},grid: {left: 10,right: 10,bottom: 20,top: 30,containLabel: true},tooltip: {trigger: 'axis',axisPointer: {type: 'cross'},padding: [5, 10]},yAxis: {axisTick: {show: false}},legend: {data: ['expected', 'actual']},series: [{name: 'expected', itemStyle: {normal: {color: '#FF005A',lineStyle: {color: '#FF005A',width: 2}}},smooth: true,type: 'line',data: expectedData,animationDuration: 2800,animationEasing: 'cubicInOut'},{name: 'actual',smooth: true,type: 'line',itemStyle: {normal: {color: '#3888fa',lineStyle: {color: '#3888fa',width: 2},areaStyle: {color: '#f3f8ff'}}},data: actualData,animationDuration: 2800,animationEasing: 'quadraticOut'}]})}}
}
</script>

饼图

components/dashboard/pieChart.vue

javascript"><template><div :class="className" :style="{height:height,width:width}" />
</template><script>
import echarts from 'echarts'
require('echarts/theme/macarons') // echarts theme
import resize from './mixins/resize'export default {mixins: [resize],props: {className: {type: String,default: 'chart'},width: {type: String,default: '100%'},height: {type: String,default: '300px'}},data() {return {chart: null}},mounted() {this.$nextTick(() => {this.initChart()})},beforeDestroy() {if (!this.chart) {return}this.chart.dispose()this.chart = null},methods: {initChart() {this.chart = echarts.init(this.$el, 'macarons')this.chart.setOption({tooltip: {trigger: 'item',formatter: '{a} <br/>{b} : {c} ({d}%)'},legend: {left: 'center',bottom: '10',data: ['Industries', 'Technology', 'Forex', 'Gold', 'Forecasts']},series: [{name: 'WEEKLY WRITE ARTICLES',type: 'pie',roseType: 'radius',radius: [15, 95],center: ['50%', '38%'],data: [{ value: 320, name: 'Industries' },{ value: 240, name: 'Technology' },{ value: 149, name: 'Forex' },{ value: 100, name: 'Gold' },{ value: 59, name: 'Forecasts' }],animationEasing: 'cubicInOut',animationDuration: 2600}]})}}
}
</script>

 雷达图 

 components/dashboard/RaddarChart.vue

javascript"><template><div :class="className" :style="{height:height,width:width}" />
</template><script>
import echarts from 'echarts'
require('echarts/theme/macarons') // echarts theme
import resize from './mixins/resize'const animationDuration = 3000export default {mixins: [resize],props: {className: {type: String,default: 'chart'},width: {type: String,default: '100%'},height: {type: String,default: '300px'}},data() {return {chart: null}},mounted() {this.$nextTick(() => {this.initChart()})},beforeDestroy() {if (!this.chart) {return}this.chart.dispose()this.chart = null},methods: {initChart() {this.chart = echarts.init(this.$el, 'macarons')this.chart.setOption({tooltip: {trigger: 'axis',axisPointer: { // 坐标轴指示器,坐标轴触发有效type: 'shadow' // 默认为直线,可选为:'line' | 'shadow'}},radar: {radius: '66%',center: ['50%', '42%'],splitNumber: 8,splitArea: {areaStyle: {color: 'rgba(127,95,132,.3)',opacity: 1,shadowBlur: 45,shadowColor: 'rgba(0,0,0,.5)',shadowOffsetX: 0,shadowOffsetY: 15}},indicator: [{ name: 'Sales', max: 10000 },{ name: 'Administration', max: 20000 },{ name: 'Information Techology', max: 20000 },{ name: 'Customer Support', max: 20000 },{ name: 'Development', max: 20000 },{ name: 'Marketing', max: 20000 }]},legend: {left: 'center',bottom: '10',data: ['Allocated Budget', 'Expected Spending', 'Actual Spending']},series: [{type: 'radar',symbolSize: 0,areaStyle: {normal: {shadowBlur: 13,shadowColor: 'rgba(0,0,0,.2)',shadowOffsetX: 0,shadowOffsetY: 10,opacity: 1}},data: [{value: [5000, 7000, 12000, 11000, 15000, 14000],name: 'Allocated Budget'},{value: [4000, 9000, 15000, 15000, 13000, 11000],name: 'Expected Spending'},{value: [5500, 11000, 12000, 15000, 12000, 12000],name: 'Actual Spending'}],animationDuration: animationDuration}]})}}
}
</script>

在 components文件夹dashboard里新建mixins文件夹——>mixins文件夹里新建resize.js文件,代码如下:

使用时在echats图页引入即可

javascript">import { debounce } from '@/utils'export default {data() {return {$_sidebarElm: null,$_resizeHandler: null}},mounted() {this.initListener()},activated() {if (!this.$_resizeHandler) {// avoid duplication initthis.initListener()}// when keep-alive chart activated, auto resizethis.resize()},beforeDestroy() {this.destroyListener()},deactivated() {this.destroyListener()},methods: {// use $_ for mixins properties// https://vuejs.org/v2/style-guide/index.html#Private-property-names-essential$_sidebarResizeHandler(e) {if (e.propertyName === 'width') {this.$_resizeHandler()}},initListener() {this.$_resizeHandler = debounce(() => {this.resize()}, 100)window.addEventListener('resize', this.$_resizeHandler)this.$_sidebarElm = document.getElementsByClassName('sidebar-container')[0]this.$_sidebarElm && this.$_sidebarElm.addEventListener('transitionend', this.$_sidebarResizeHandler)},destroyListener() {window.removeEventListener('resize', this.$_resizeHandler)this.$_resizeHandler = nullthis.$_sidebarElm && this.$_sidebarElm.removeEventListener('transitionend', this.$_sidebarResizeHandler)},resize() {const { chart } = thischart && chart.resize()}}
}

与components同级新建utils文件夹——>utils文件夹里新建index.js文件,代码如下:

javascript">/*** @param {Function} func* @param {number} wait* @param {boolean} immediate* @return {*}*/
export function debounce(func, wait, immediate) {let timeout, args, context, timestamp, resultconst later = function() {// 据上一次触发时间间隔const last = +new Date() - timestamp// 上次被包装函数被调用时间间隔 last 小于设定时间间隔 waitif (last < wait && last > 0) {timeout = setTimeout(later, wait - last)} else {timeout = null// 如果设定为immediate===true,因为开始边界已经调用过了此处无需调用if (!immediate) {result = func.apply(context, args)if (!timeout) context = args = null}}}return function(...args) {context = thistimestamp = +new Date()const callNow = immediate && !timeout// 如果延时不存在,重新设定延时if (!timeout) timeout = setTimeout(later, wait)if (callNow) {result = func.apply(context, args)context = args = null}return result}
}


http://www.ppmy.cn/ops/166777.html

相关文章

冯 • 诺依曼体系结构

冯 • 诺依曼体系结构 一、冯 • 诺依曼体系结构推导阶段 1&#xff1a;初始计算机体系结构&#xff08;仅输入、运算、输出&#xff09;阶段 2&#xff1a;加入控制功能&#xff0c;初步形成 CPU 概念阶段 3&#xff1a;性能瓶颈与引入内存阶段 4&#xff1a;最终冯诺依曼体系…

[特殊字符] 深度实战:Android 13 系统定制之 Recovery 模式瘦身指南

&#x1f31f; 核心需求 在 Android 13 商显设备开发中&#xff0c;需精简 Recovery 模式的菜单选项&#xff08;如Reboot to bootloader/Enter rescue&#xff09;&#xff0c;但直接修改g_menu_actions后在User 版本出现黑屏卡死问题&#xff0c;需综合方案解决。 &#x1f5…

SvelteKit 最新中文文档教程(4)—— 表单 actions

前言 Svelte&#xff0c;一个语法简洁、入门容易&#xff0c;面向未来的前端框架。 从 Svelte 诞生之初&#xff0c;就备受开发者的喜爱&#xff0c;根据统计&#xff0c;从 2019 年到 2024 年&#xff0c;连续 6 年一直是开发者最感兴趣的前端框架 No.1&#xff1a; Svelte …

利用github部署项目

挂载GitHub Pages的方法 基本步骤 创建仓库&#xff1a; 在GitHub上创建一个新的仓库。如果使用自定义域名&#xff0c;则仓库名应为<username>.github.io&#xff1b;否则可以是任意名称。 启用GitHub Pages&#xff1a; 进入仓库的设置页面&#xff0c;在“Pages”部…

【从零开始学习计算机科学】信息安全(八)防火墙

【从零开始学习计算机科学】信息安全(八)防火墙 防火墙防火墙概述防火墙设计原则防火墙的优点防火墙的不足防火墙类别防火墙技术包过滤技术代理技术应用层网关电路级网关状态检查技术地址翻译技术NAT防火墙体系结构包过滤路由器双宿主机网关屏蔽主机体系结构屏蔽子网体系结构…

Chat2DB:让数据库管理像聊天一样简单

数据库工具的痛点与破局 在数据爆炸的时代&#xff0c;数据库管理工具已成为企业高效运营的刚需。然而&#xff0c;传统工具如Navicat、DBeaver虽功能强大&#xff0c;却让非技术人员和SQL新手望而却步。复杂的界面、繁琐的手动操作、晦涩的语法规则&#xff0c;成为横亘在数据…

LeeCode题库第2379题

2379.得到K个黑块的最小涂色次数 项目场景&#xff1a; 给你一个长度为 n 下标从 0 开始的字符串 blocks &#xff0c;blocks[i] 要么是 W 要么是 B &#xff0c;表示第 i 块的颜色。字符 W 和 B 分别表示白色和黑色。 给你一个整数 k &#xff0c;表示想要 连续 黑色块的数…

ES6(1) 简介与基础概念

1. ES6 简介 ES6&#xff08;ECMAScript 6&#xff09;是 JavaScript 的一个重要版本&#xff0c;它在 ES5 的基础上进行了扩展和优化。ES6 主要应用于现代 Web 开发&#xff0c;提高了 JavaScript 的编程效率和可读性。 2. ES6 与 JavaScript 的关系 JavaScript 是一种基于 E…