vue中使用Echars
1.安装依赖
- vue2.0框架
npm install echarts
- vue3.0框架
npm add echarts
2.引入相关文件
· 全局引入
在 min.js 中引入 import echarts from 'echarts Vue.prototype.$echarts = echarts
· 按需引入
在单个组件中引用
// echarts 按需引入
let echarts2 = require('echarts/lib/echarts')
// 引入折线图等组件
require('echarts/lib/chart/line')
require('echarts/lib/chart/bar')
require('echarts/lib/chart/radar')
// 引入提示框和title组件,图例
require('echarts/lib/component/tooltip')
require('echarts/lib/component/title')
require('echarts/lib/component/legend')
Vue.prototype.$echarts2 = echarts2
3.代码示例
<template>
<div class="app">
<div id="box" class="bos"></div>
</div>
</template>
<script>
export default {
name: "app",
components: {},
mounted() {
this.initChart();
},
data() {
return {};
},
methods: {
initChart() {
let bos = this.$echarts.init(document.getElementById("box"));
// 绘制图表
bos.setOption(this.setOption1("我是柱状图"));
},
setOption1(title) {
let option = {
title: { text: title },
tooltip: {},
xAxis: {
data: ["衬衫", "羊毛衫", "雪纺衫", "裤子", "高跟鞋", "袜子"]
},
yAxis: {},
series: [
{
name: "销量",
type: "bar",
data: [5, 20, 36, 10, 10, 20]
}
]
};
return option;
}
}
};
</script>
<style scoped>
.bos {
width: 500px;
height: 300px;
margin: 0 auto;
}
</style>