首页 > 其他 > 详细

vue 引入 echarts 子组件

时间:2020-10-30 19:02:22      阅读:74      评论:0      收藏:0      [点我收藏+]

一:创建 以.vue 结尾的 文件,也就是 子组件

如下,这是一个完整 的可以引用 一个 echarts 图标  的vue子组件。 我们对以下进行分析

几乎 引用 所有的 echarts 图标都可以用这个方式。 

<template>
// :class 绑定的是类名 :stle 绑定了它的宽度和高度, 这里width 和height 可以是自己 默认的 (比如下面的width ),也可以是 父组件传过来的, 比如下面的height <div :class="className" :style="{height:this.chartData.chartHeight,width:width}" /> </template> <script>
// 这里是单页面引用echarts ,优点是 哪里用哪里 引用。 也可以全局引用,自行百度 import echarts from ‘echarts‘ require(‘echarts/theme/macarons‘) // echarts theme export default {
// props 这里是一些默认参数,定义了默认的echarts的样式,上面 的 class 和 style 中的没有定义的变量会从这里找 props: { className: { type: String, default: ‘chart‘ }, width: { type: String, default: ‘100%‘ }, height: { type: String, default: ‘400px‘ }, autoResize: { type: Boolean, default: true }, chartData: { type: Object, required: true } }, data() { return { chart: null, } },
// 这里是当 数据 chartData 比较复杂时,需要用到深度监视, 这里参考博客 https://www.cnblogs.com/yesu/p/9546458.html 和 https://www.cnblogs.com/wsz168/p/8763284.html
// 我自己的思考, 这里 深度监听chartData ,并且把最新的数据复制个 setOptions这个方法
watch: { chartData: { deep: true, handler(val) { this.setOptions(val) } } },
// 如果要获取数据更新导致的DOM更新后的新DOM对象需要使用$nextTick回调 ,在dom对象 加载完执行 图标初始化 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({ dataList, 1,2,3} = {}) { this.chart.setOption({ title: { text: ‘三大平台七日新增条数‘, left: ‘center‘, textStyle:{ // 设置标题文字大小 fontSize:18, align:‘center‘, //水平对齐 color:‘black‘ }, }, xAxis: { data: dataList, boundaryGap: false, axisTick: { show: false } }, grid: { left: 10, right: 20, bottom: 20, top: 60, containLabel: true }, tooltip: { trigger: ‘axis‘, axisPointer: { type: ‘cross‘ }, padding: [5, 10] }, yAxis: { axisTick: { show: false } }, legend: { data: [‘1新增条数‘, ‘2新增条数‘,‘3新增条数‘], padding:[40,50,0,0], }, series: [{ name: ‘2新增条数‘, itemStyle: { normal: { color: ‘#FF005A‘, lineStyle: { color: ‘#FF005A‘, width: 2 } } }, smooth: true, type: ‘line‘, data: 111, animationDuration: 2800, animationEasing: ‘cubicInOut‘ }, { name: ‘2新增条数‘, smooth: true, type: ‘line‘, itemStyle: { normal: { color: ‘#3888fa‘, lineStyle: { color: ‘#3888fa‘, width: 2 }, areaStyle: { color: ‘#f3f8ff‘ }} }, data: 222, animationDuration: 2800, animationEasing: ‘quadraticOut‘ }, { name: ‘1新增条数‘, smooth: true, type: ‘line‘, itemStyle: { normal: { color: ‘#FF7E00‘, lineStyle: { color: ‘#FF7E00‘, width: 2 }, areaStyle: { color: ‘#f3f8ff‘ }} }, data: 333, animationDuration: 2800, animationEasing: ‘quadraticOut‘ }] }) } } } </script> <style scoped> </style>

  

vue 引入 echarts 子组件

原文:https://www.cnblogs.com/tdcqcrtd/p/13902491.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!