由于在项目中需要对数据进行可视化处理,也就是用图表展示,众所周知echarts是非常强大的插件;
1.npm 安装 ECharts
你可以使用如下命令通过 npm 安装 ECharts
npm install echarts --save
2.引入 ECharts
使用 ES Module:
import echarts from 'echarts';
使用 CommonJS:
var echarts = require('echarts')
<template>
<div>
<!-- ECharts 图表 基于canvas 封装的图表插件 数据可视化-->
<div id="main"></div>
</div>
</template>
<script>
var echarts = require('echarts');
export default {
name: "ECharts",
data(){
return{
myChart:{}
}
},
methods: {
initData() {
// 绘制图表
const option = {
title: {
text: 'ECharts 入门示例'
},
tooltip: {},
xAxis: {
data: ['衬衫', '羊毛衫', '雪纺衫', '裤子', '高跟鞋', '袜子']
},
yAxis: {},
series: [{
name: '销量',
type: 'bar',
data: [5, 20, 36, 10, 10, 20]
}]
};
this.myChart.setOption(option)
}
},
mounted() {
// 基于准备好的dom,初始化echarts实例
this.myChart = echarts.init(document.getElementById('main'));
this.initData();
}
}
</script>
<style lang="scss" scoped>
@import "../../Scss/index";
#main{
@include Size(600px, 500px);
}
</style>

注意:我们要在mounted生命周期函数中实例化echarts对象。因为我们要确保dom元素已经挂载到页面中
~~滴滴~~
浙公网安备 33010602011771号