一、 使用 vue-cli 快速構建vue項目, 引入vue-echarts組件
安裝:
> npm i vue-echarts --save
修改 webpack.config.js 配置:
{ test: /\.js$/, loader: 'babel-loader', include: [ resolve('src'), resolve('node_modules/vue-echarts'), resolve('node_modules/resize-detector') ] },
用法示例:
<template> <v-chart :options="polar"/> </template> <script> import ECharts from 'vue-echarts/components/ECharts' import 'echarts/lib/chart/line' import 'echarts/lib/component/polar' export default { components: { 'v-chart': ECharts }, data: function () { let data = [] for (let i = 0; i <= 360; i++) { let t = i / 180 * Math.PI let r = Math.sin(2 * t) * Math.cos(2 * t) data.push([r, i]) } return { polar: { title: { text: '極坐標雙數值軸' }, legend: { data: ['line'] }, polar: { center: ['50%', '54%'] }, tooltip: { trigger: 'axis', axisPointer: { type: 'cross' } }, angleAxis: { type: 'value', startAngle: 0 }, radiusAxis: { min: 0 }, series: [ { coordinateSystem: 'polar', name: 'line', type: 'line', showSymbol: false, data: data } ], animationDuration: 2000 } } } } </script> <style scoped> .echarts { width: 100%; height: 400px; } </style>
效果圖:
更多用法請查詢 echarts 官方文檔 http://echarts.baidu.com/examples/
二、注意事項
問題描述: webpack構建vue項目, 使用 vue-echarts組件時, npm run build 編譯生產版本報錯
ERROR in 0.build.js from UglifyJs
Unexpected token: name (raf) [./node_modules/resize-detector/esm/index.js
原因: 由於 UglifyJs 只支持 ES5 而 vue-echarts可能引入了一部分 ES6 的寫法,所以導致 webpack 打包失敗。
解決: webpack.config.js 配置刪除下面這句, exclude 表示/node_modules/ 目錄下的 .js 文件不要進行 babel-loader , 覆蓋了上一句 include 的設置