首先安裝echart
npm i echarts -S
加下來以使用這個圖表爲例
在vue組件中像這樣使用:html
<template> <div :class="className" :id="id" :style="{height:height,width:width}" ref="myEchart"> </div> </template> <script> import echarts from 'echarts' export default { props: { className: { type: String, default: 'yourClassName' }, id: { type: String, default: 'yourID' }, width: { type: String, default: '500px' }, height: { type: String, default: '500px' } }, data() { return { chart: null } }, mounted() { this.initChart(); }, beforeDestroy() { if (!this.chart) { return } this.chart.dispose(); this.chart = null; }, methods: { initChart() { this.chart = echarts.init(this.$refs.myEchart); // 把配置和數據放這裏 this.chart.setOption({ color: ['#3398DB'], tooltip: { trigger: 'axis', axisPointer: { // 座標軸指示器,座標軸觸發有效 type: 'shadow' // 默認爲直線,可選爲:'line' | 'shadow' } }, grid: { left: '3%', right: '4%', bottom: '3%', containLabel: true }, xAxis: [{ type: 'category', data: ['Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat', 'Sun'], axisTick: { alignWithLabel: true } }], yAxis: [{ type: 'value' }], series: [{ name: '直接訪問', type: 'bar', barWidth: '60%', data: [10, 52, 200, 334, 390, 330, 220] }] }) } } } </script>
若是是異步獲取的數據,好比用axios,那隻須要把配置項放到then方法後面:vue
initChart() { this.chart = echarts.init(this.$refs.myEchart); // 把配置和數據放這裏 this.axios.get('/url').then((data) => { this.chart.setOption({ }) }) }