vue中使用echarts
1、安装echarts依赖
安装echarts依赖
cnpm install echarts -S
或者
npm install echarts -S
2、创建图表
2.1、全局引入
全局引入会将所有的echarts图表打包,导致体积过大,因此项目中用的按需引入,但是这里还是记录用法。
// main.js中引入echarts
import echarts from 'echarts'
Vue.prototype.$echarts = echarts
使用echarts
export default {
data () {
return {
}
},
mounted(){
this.drawEcharts();
},
methods: {
drawEcharts(){
// 基于准备好的dom,初始化echarts实例
let myChart = this.$echarts.init(document.getElementById('myChart'))
// 绘制图表
myChart.setOption(
tooltip: {},
xAxis: {
data: [], // 实际数据为周一至周日组成的数组
},
yAxis: {},
series: [{
name: '电量',
type: 'bar',
data: [5, 20, 36, 10, 10, 20]
}]
});
}
}
}
2.1、按需引入与效果
按需引入在需要的时候引入echarts,比全局引入小很多,下面的echarts很多具体样式很多都省略掉了,没有写上去,因此实际效果与代码会有出入。
export default {
data () {
return {
}
},
mounted(){
this.drawEcharts();
},
methods: {
drawEcharts(){
// 基于准备好的dom,初始化echarts实例
let myChart = echarts.init(document.getElementById('myChart'))
// 绘制图表
myChart.setOption(
color: ['#596a75'],
title: {
top: 70,
text: '', // 图表标题
textStyle: {
color: '#6997b3',
fontSize: 14,
fontWeight: 'normal'
}
},
tooltip: {},
xAxis: {
data: [], // 实际数据为周一至周日组成的数组
},
yAxis: {},
series: [{
name: '电量',
type: 'bar',
data: [5, 20, 36, 10, 10, 20],
itemStyle: {
normal: {
color: function(params) {
if (params.value < 40) {
return '#f13834';
}
return '#3695d3';
}
}
}
}]
});
}
}
}