Apache ECharts官网地址:https://echarts.apache.org/zh/index.html
1、安装echarts库
npm install echarts --save
2、导入在main.js导入,以便全局使用
import * as echarts from 'echarts'
Vue.prototype.$echarts = echarts;
3、以柱状图为例
(1)创建vue页面,准备一个dom元素放置柱状图
<template>
<div id="app-echarts">
<div id="main" style="width: 600px;height:400px;"></div>
</div>
</template>
(2)使用官网例子,进行对js进行修改
在全局引用之后需要修改,this.$echarts.init 这样写才可以正常显示绘制图
// 基于准备好的dom,初始化echarts实例
var myChart = this.$echarts.init(document.getElementById('main'));
<script>
export default{
data(){
return{}
},
mounted() {
this.initeCharts();
},
methods:{
initeCharts(){
// 基于准备好的dom,初始化echarts实例
var myChart = this.$echarts.init(document.getElementById('main'));
// 绘制图表
myChart.setOption({
title: {
text: 'ECharts 入门示例'
},
tooltip: {},
xAxis: {
data: ['衬衫', '羊毛衫', '雪纺衫', '裤子', '高跟鞋', '袜子']
},
yAxis: {},
series: [{
name: '销量',
type: 'bar',
data: [5, 20, 36, 10, 10, 20]
}]
});
}
}
}
</script>
(3)完整代码
<template>
<div id="app-echarts">
<div id="main" style="width: 600px;height:400px;"></div>
</div>
</template>
<script>
export default{
data(){
return{}
},
mounted() {
this.initeCharts();
},
methods:{
initeCharts(){
// 基于准备好的dom,初始化echarts实例
var myChart = this.$echarts.init(document.getElementById('main'));
// 绘制图表
myChart.setOption({
title: {
text: 'ECharts 入门示例'
},
tooltip: {},
xAxis: {
data: ['衬衫', '羊毛衫', '雪纺衫', '裤子', '高跟鞋', '袜子']
},
yAxis: {},
series: [{
name: '销量',
type: 'bar',
data: [5, 20, 36, 10, 10, 20]
}]
});
}
}
}
</script>
<style>
</style>
注意点
- myChart.setOption({}) 这个方法的setOption部分是没有 s 的,有 s 的方法是不正确的