由於在項目中需要對數據進行可視化處理,也就是用圖表展示,眾所周知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元素已經掛載到頁面中
~~滴滴~~