图表的使用在企业级软件中使用愈来愈广泛,前端开发人员能够使用经常使用的echarts开源库来进行图表展现的开发,公司最近提出须要丰富系统首页的内容,趁此机会分享一下如何在使用vue.js框架下使用echarts.html
1,肯定你须要引入的echarts版本,官网地址:http://echarts.baidu.com/inde...
2,npm或者cnpm安装前端
cnpm install echarts --save
3,按需引入。这里我强烈推荐按需引入,echarts图类型众多,咱们通常不须要将每一个图表都引入,这样也有利于减小打包体积,这里我建议将每个echarts图封装成一个组件。
主要代码以下vue
<el-col :span='24'> <v-charts></v-charts> </el-col>
4组件部分源码git
<template> <div class="line"> <div class="main" ref="main"> </div> </div> </template> <script> let echarts = require('echarts/lib/echarts');//引入echarts require('echarts/lib/chart/bar'); //引入柱状图 // 引入提示框和标题组件 require('echarts/lib/component/tooltip'); require('echarts/lib/component/title'); export default{ data(){ return{ myChart:{} } }, mounted(){ this.initEcharts(); }, methods:{ initEcharts(){ let THIS=this; let mainDiv = THIS.$refs.main; //找到绘制的区域,强烈推荐使用refs var myChart = echarts.init(mainDiv); //初始化 myChart.setOption({ //官网例子 title: { text: 'ECharts 入门示例' }, tooltip: {}, xAxis: { data: ['衬衫', '羊毛衫', '雪纺衫', '裤子', '高跟鞋', '袜子'] }, yAxis: {}, series: [{ name: '销量', type: 'bar', data: [5, 20, 36, 10, 10, 20] }] }); } } } </script> <style lang='stylus' scoped> .line height 100% .main width:300px; //宽高也不可少 height:200px; </style>
4实现效果以下github
5,按需引入其余图表,能够参考能够按需引入的模块列表见 https://github.com/ecomfe/ech...npm