vue echarts

  1. // 导入echarts
    //import echarts from 'echarts'
    // 引入基本模板
    let echarts = require('echarts/lib/echarts')
    // 引入饼图组件
    require('echarts/lib/chart/pie')
    // 引入提示框和图例组件
    require('echarts/lib/component/tooltip')
    require('echarts/lib/component/legend')

  2. <!--  
  3.     2017.2.11  
  4.     by vanishment  
  5.     *************  
  6.     echarts小组件  
  7. -->  
  8. <style scoped>  
  9. .content {  
  10.     /*自行添加样式即可*/  
  11. }  
  12.   
  13.   
  14. #main {  
  15.     /*需要制定具体高度,以px为单位*/  
  16.     height: 400px;  
  17. }  
  18. </style>  
  19. <template>  
  20.     <div class="content">  
  21.         <div id="main"></div>  
  22.     </div>  
  23. </template>  
  24. <script>  
  25. // 导入echarts  
  26. import echarts from 'echarts'  
  27. // 方便AJAX,按个人喜好添加,然后对应修改下方获取数据的代码  
  28. import $ from 'jquery'  
  29. // 皮肤引入 import theme-name from theme-folder  
  30.   
  31.   
  32. // 以饼图为例  
  33. // 其他种类图表配置项参见百度echarts官网  
  34.   
  35.   
  36. export default {  
  37.     data() {  
  38.             return {  
  39.                 // 初始化空对象  
  40.                 chart: null,  
  41.                 // 初始化图表配置  
  42.                 opinion: ['高富帅', '矮富帅', '高富挫', '矮富挫', '女生'],  
  43.                 opinionData: [{  
  44.                     value: 26,  
  45.                     name: '高富帅'  
  46.                 }, {  
  47.                     value: 31,  
  48.                     name: '矮富帅'  
  49.                 }, {  
  50.                     value: 18,  
  51.                     name: '高富挫'  
  52.                 }, {  
  53.                     value: 28,  
  54.                     name: '矮富挫'  
  55.                 }, {  
  56.                     value: 21,  
  57.                     name: '女生'  
  58.                 }]  
  59.             }  
  60.         },  
  61.         methods: {  
  62.             // 绘图  
  63.             drawGraph(id) {  
  64.                 // 绘图方法  
  65.                 this.chart = echarts.init(document.getElementById(id))  
  66.                     // 皮肤添加同一般使用方式  
  67.                 this.chart.showLoading()  
  68.                     // 返回到data中  
  69.                 var that = this  
  70.                     // ajax 请求数据  
  71.                 $.ajax({  
  72.                         // 方式  
  73.                         type: "GET",  
  74.                         // 是否异步  
  75.                         async: true,  
  76.                         // 路径||API  
  77.                         url: "xxx",  
  78.                         //返回数据形式为json  
  79.                         dataType: "json",  
  80.                         // 加载成功  
  81.                         success: function(result) {  
  82.                             // 更新初始数据  
  83.                             that.opinionData = result  
  84.                         },  
  85.                         // 加载错误  
  86.                         error: function(errorMsg) {  
  87.                             // alert("请求数据失败!");  
  88.                             console.log(errorMsg)  
  89.                         }  
  90.                     })  
  91.                     // set  
  92.                 this.chart.setOption({  
  93.                     title: {  
  94.                         text: '女生喜欢的男生种类',  
  95.                         subtext: '纯属扯犊子',  
  96.                         x: 'center'  
  97.                     },  
  98.                     tooltip: {  
  99.                         trigger: 'item',  
  100.                         formatter: "{a} <br/>{b} : {c} ({d}%)"  
  101.                     },  
  102.                     legend: {  
  103.                         x: 'center',  
  104.                         y: 'bottom',  
  105.                         data: this.opinion // 别忘了this  
  106.                     },  
  107.                     toolbox: {  
  108.                         show: true,  
  109.                         feature: {  
  110.                             mark: {  
  111.                                 show: true  
  112.                             },  
  113.                             dataView: {  
  114.                                 show: true,  
  115.                                 readOnly: false  
  116.                             },  
  117.                             magicType: {  
  118.                                 show: true,  
  119.                                 type: ['pie']  
  120.                             },  
  121.                             restore: {  
  122.                                 show: true  
  123.                             },  
  124.                             saveAsImage: {  
  125.                                 show: true  
  126.                             }  
  127.                         }  
  128.                     },  
  129.                     calculable: true,  
  130.                     series: [{  
  131.                         name: '种类',  
  132.                         type: 'pie',  
  133.                         // 内圆半径,外圆半径  
  134.                         radius: [30, 100],  
  135.                         // 位置,左右,上下  
  136.                         center: ['50%', '50%'],  
  137.                         roseType: 'area',  
  138.                         data: this.opinionData, // 别忘了this  
  139.   
  140.   
  141.                     }]  
  142.                 })  
  143.                 this.chart.hideLoading()  
  144.             }  
  145.         },  
  146.         // keypoint:执行方法  
  147.         // “将回调延迟到下次 DOM 更新循环之后执行。在修改数据之后立即使用它,然后等待 DOM 更新。”  
  148.         mounted() {  
  149.             this.$nextTick(function() {  
  150.                 this.drawGraph('main')  
  151.             })  
  152.         }  
  153. }  
  154. </script>  
posted @ 2018-01-04 14:57  小牧临风  阅读(109)  评论(0编辑  收藏  举报