首页 技术 正文
技术 2022年11月11日
0 收藏 473 点赞 3,697 浏览 1656 个字

在vue中使用echarts有两种方法
一、第一种方法
1、通过npm获取echarts

npm install echarts --save

2、在vue项目中引入echarts

在 main.js 中添加下面两行代码

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

注:import echarts from ‘echarts’ 引入echarts后,不能全局使用echarts,所以通过Vue.prototype将echarts保存为全局变量。原则上$echarts可以为任意变量名。

3、新建Echarts.vue文件

在 template 中添加一个存放echarts的‘div’容器
添加 myEcharts() 方法,将官方文档中的script内容复制到myEcharts()中
修改 echarts.init() 为 this.$echarts.init() ,因为上面第二部,将echarts保存到全局变量$echarts中。
在mounted中调用myEcharts()方法

//在Echarts.vue文件中
<template>
<div class="Echarts">
<div id="main" style="width: 600px;height: 400px;"></div>
</div>
</template><script>
export default {
name: 'Echarts',
methods: {
myEcharts(){
var myChart = this.$echarts.init(document.getElementById('main'));
//配置图表
var option = {
title: {
text: 'echarts入门示例',
},
tooltip: {},
legend: {
data: ['销量']
},
xAxis: {
data: ['衬衫','羊毛衫','雪纺衫','裤子','高跟鞋','袜子']
},
yAxis: {},
series: [{
name: '销量',
type: 'bar',
data: [5,20,36,10,10,20]
}]
};
myChart.setOption(option);
}
},
mounted(){
this.myEcharts();
}
}
</script><style></style>

注:本例函数中使用ES6写法。mounted(){}等同于mounted:function(){}。myEcharts()方法同理。最后添加进行路由配置。

二、使用vue-echarts
1、先npm安装vue-echarts

npm install echarts vue-echarts

2、除了全量引用echarts,我们还可以采用按需引入的方式

//在main.js中引入import Echarts from 'vue-echarts'
import 'echarts/lib/chart/line'Vue.component('chart',Echarts)

3、然后在组件中使用

//hello.vue中<template>
<div class="hello">
<chart ref="chart1" :options="orgOptions" :auto-resize="true"></chart>
</div>
</template><script>
export default {
name: 'hello',
data() {
return {
orgOptions: {},
}
},
mounted() {
this.orgOptions = {
xAxis: {
type: 'category',
data: ['Mon','Tue','Wed','Thu','Fri','Sat','Sun']
},
yAxis: {
type: 'value'
},
series: [{
data: [820,932,901,934,1290,1330,1320],
type: 'line',
smooth: true
}]
}
}
}
</script><style></style>
相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:9,491
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,907
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,740
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,493
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:8,132
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:5,294