在vue中使用echarts

在vue中使用echarts1 安装

1、安装

npm install echarts --save 

2、在vue中引入(全局引入)

// 引入echarts import echarts from 'echarts' Vue.prototype.$echarts = echarts 

3、在vue中的使用

需要用到echart的地方先设置一个div的id、宽高

提示:
可以在一个页面中引入多个数据报表模板
使用div进行位置的排版放置





4、模板代码放在哪个位置

重点注意:其中const option = { }就是我们需要引进echart图表的代码

 <template> <div> <div ref="chart" style="width:50%;height:376px"></div> </div> </template> 

要在mounted生命周期函数中实例化echarts对象。确保dom元素已经挂载到页面中。

mounted(){ 
       this.getEchartData() }, methods: { 
       getEchartData() { 
       const chart = this.$refs.chart if (chart) { 
       const myChart = this.$echarts.init(chart) const option = { 
      ...} myChart.setOption(option) window.addEventListener("resize", function() { 
       myChart.resize() }) } this.$on('hook:destroyed',()=>{ 
       window.removeEventListener("resize", function() { 
       myChart.resize(); }); }) } } 

5、完整的一个vue页面实例:

 <template> <div> <div ref="chart" style="width:50%;height:376px"></div> <div ref="chart1" style="width:50%;height:376px"></div> </div> </template> <script> export default { 
        data() { 
        }, mounted() { 
        this.getEchartData() this.getEchartData1() }, methods: { 
        getEchartData() { 
        const chart = this.$refs.chart if (chart) { 
        const myChart = this.$echarts.init(chart) const option = { 
        legend: { 
       }, tooltip: { 
       }, dataset: { 
        source: [ ['订单', 43.3, 85.8], ['订单1', 83.1, 73.4], ['订单2', 86.4, 65.2], ['订单3', 72.4, 53.9], ['订单4', 82.4, 53.9], ['订单5', 42.4, 53.9], ['订单6', 72.4, 53.9], ['订单7', 72.4, 53.9] ] }, xAxis: { 
        type: 'category' }, yAxis: { 
       }, series: [ { 
        type: 'bar' }, { 
        type: 'bar' }]} myChart.setOption(option) window.addEventListener("resize", function() { 
        myChart.resize() }) } this.$on('hook:destroyed',()=>{ 
        window.removeEventListener("resize", function() { 
        myChart.resize(); }); }) }, getEchartData1() { 
        const chart1 = this.$refs.chart1 if (chart1) { 
        const myChart = this.$echarts.init(chart1) const option = { 
        title: { 
        text: 'Stacked Line' }, tooltip: { 
        trigger: 'axis' }, legend: { 
        data: ['Email', 'Union Ads', 'Video Ads', 'Direct', 'Search Engine'] }, grid: { 
        left: '3%', right: '4%', bottom: '3%', containLabel: true }, toolbox: { 
        feature: { 
        saveAsImage: { 
       } } }, xAxis: { 
        type: 'category', boundaryGap: false, data: ['一月', '二月', '三月', '四月', '五月', '六月', '七月','八月','九月','十月','十一月','十二月'] }, yAxis: { 
        type: 'value' }, series: [ { 
        name: 'Email', type: 'line', stack: 'Total', data: [120, 132, 101, 134, 90, 230, 210,120, 132, 101, 134, 90, 230] }, { 
        name: 'Union Ads', type: 'line', stack: 'Total', data: [220, 182, 191, 234, 290, 330, 310,220, 182, 191, 234, 290, 330] }, { 
        name: 'Video Ads', type: 'line', stack: 'Total', data: [150, 232, 201, 154, 190, 330, 410,150, 232, 201, 154, 190, 330] }, { 
        name: 'Direct', type: 'line', stack: 'Total', data: [320, 332, 301, 334, 390, 330, 320,320, 332, 301, 334, 390, 330] }, { 
        name: 'Search Engine', type: 'line', stack: 'Total', data: [820, 932, 901, 934, 1290, 1330, 1320,820, 932, 901, 934, 1290, 1330] } ] } myChart.setOption(option) window.addEventListener("resize", function() { 
        myChart.resize() }) } this.$on('hook:destroyed',()=>{ 
        window.removeEventListener("resize", function() { 
        myChart.resize(); }); }) }, }, watch: { 
       }, created() { 
        } } </script> 

6、实现效果

在这里插入图片描述


7、可能遇到的问题,下载不成功。使用

cnpm install echarts --save 

在这里插入图片描述


8、11:25-32 “export ‘default’ (imported as ‘echarts’) was not found in ‘echarts

修改引入的方式

// 引入echarts import *as echarts from 'echarts' Vue.prototype.$echarts = echarts 
在vue中使用echarts
版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请联系我们举报,一经查实,本站将立刻删除。

发布者:全栈程序员-站长,转载请注明出处:https://javaforall.net/202720.html原文链接:https://javaforall.net

(0)
上一篇 2026年3月19日 下午11:27
下一篇 2026年3月19日 下午11:28


相关推荐

  • SecondCopy 2000 简单说明

    SecondCopy 2000 简单说明

    2021年8月7日
    66
  • matlab填充图形_不同图案填充条形图

    matlab填充图形_不同图案填充条形图1、下载填充函数:applyhatch.  2、有四种选择:applyhatch,applyhatchplus, applyhatch_plusC , 和 HatchfillpatternsplusColor&Invert,个人感觉后两种更好一些。3、下载以后,放在自己存放m文件的地方,就可以在其他m文件中调用applyhatch***函数了。文件注释里都有

    2022年10月18日
    3
  • java之MessageDigest简单使用总结

    java之MessageDigest简单使用总结1、说明MessageDigest是Java自带的类2、简单代码比如我们需要实现md5,或者sha加密publicclassMD5AndSHA{publicfinalstaticStringMD5_SHA(Strings,Stringmethod){charhexDigits[]=

    2022年6月16日
    37
  • go语言激活码【注册码】

    go语言激活码【注册码】,https://javaforall.net/100143.html。详细ieda激活码不妨到全栈程序员必看教程网一起来了解一下吧!

    2022年3月18日
    81
  • Python 廖雪峰Python练习答案

    Python 廖雪峰Python练习答案廖雪峰 Python 教程 https www liaoxuefeng com wiki 02400 高级特性切片迭代列表生成式

    2026年3月18日
    1
  • python使用RSA加密算法

    python使用RSA加密算法最近换了工作,由于新公司是做个人和企业征信业务的,所以对数据的隐私性看的非常重要,所有涉及隐私的数据一律都要进行加密进行传输。那么问题来了,对我我要做的工作,就是要实现我们接口自动化,免不了要接触加密-签名-解密-验签等一系列过程。这时候就觉得大学真的白学密码学了。。。基于我用python语言搞,那我这里就说说我是如何实现python对数据进行RSA加解密的吧。。一、查找python支…

    2022年5月4日
    51

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

关注全栈程序员社区公众号