在vue项目中使用echarts

使用npm添加package.json文件中的配置并下载相关npm包依赖

npm install echarts --save

引用echarts

import echarts from 'echarts'
Vue.prototype.$echarts = echarts

以组件的形式使用echarts

每次在使用echarts的时候,都必须要重复的引入,这样很不方便
vue作为一个组件化开发的框架,我们可以使用组件的方式进行集成。每次我们引入组件,进行使用,这样就方便的多。

<template>
    <div :id="id" :data="data"></div>
</template>
<script>
import rainbow from '../lib/rainbow'//引用自定义主题
export default {
    props:["id","data"],
    data(){
        return{
            graph:null,
        }
    },
    methods:{
        drawGraph(id,data){
            let _this = this;
            let myChart = document.getElementById(id);
            this.graph = this.$echarts.init(myChart,'rainbow');
            this.graph.setOption(data);
            //echarts图表自适应
            window.addEventListener("resize",function(){
                _this.graph.resize();
            });
        }
    },
    mounted(){
        this.$echarts.registerTheme("rainbow",rainbow);
        this.drawGraph(this.id,this.data);
    },
    beforeDestroy(){
        if(this.graph) this.graph.clear();
    },
    watch:{
        data:{
            handler(newValue,oldValue){
                this.drawGraph(this.id,newValue);
            },
            deep:true
        }
    }
}
</script>

1、echarts多次进行SetOption的时候数据遗留问题

myChart.setOption(option,true);

2、echarts图表自适应

mychart.resize();
/*(opts?: {
    width?: number|string,
    height?: number|string,
    silent?: boolean
}) => ECharts*/

https://www.echartsjs.com/api.html#echartsInstance.resize
当浏览器发生resize事件的时候,让其触发echart的resize事件,重绘canvas。

window..addEventListener("resize",()=>{
	mychart1.resize();
	mychart2.resize();
})

3.暂无数据

echarts 4.0取消暂无数据判断,需要自己判断给出用户提示;

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值