Advertisement
Guest User

Untitled

a guest
May 20th, 2019
84
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. <template>
  2.   <div
  3.     :class="className"
  4.     :style="{height:height,width:width}"/>
  5. </template>
  6.  
  7. <script>
  8. import echarts from 'echarts'
  9. require('echarts/theme/macarons') // echarts theme
  10. import { debounce } from '@/utils'
  11.  
  12. export default {
  13.   props: {
  14.     className: {
  15.       type: String,
  16.       default: 'chart'
  17.     },
  18.     width: {
  19.       type: String,
  20.       default: '100%'
  21.     },
  22.     height: {
  23.       type: String,
  24.       default: '400px'
  25.     },
  26.     autoResize: {
  27.       type: Boolean,
  28.       default: true
  29.     },
  30.     chartData: {
  31.       type: Object,
  32.       required: true
  33.     }
  34.   },
  35.   data() {
  36.     return {
  37.       chart: null,
  38.       sidebarElm: null
  39.     }
  40.   },
  41.   watch: {
  42.     chartData: {
  43.       deep: true,
  44.       handler(val) {
  45.         this.setOptions(val.quantities, val.dates)
  46.       }
  47.     }
  48.   },
  49.   mounted() {
  50.     this.initChart()
  51.     if (this.autoResize) {
  52.       this.__resizeHandler = debounce(() => {
  53.         if (this.chart) {
  54.           this.chart.resize()
  55.         }
  56.       }, 100)
  57.       window.addEventListener('resize', this.__resizeHandler)
  58.     }
  59.  
  60.     // 监听侧边栏的变化
  61.     this.sidebarElm = document.getElementsByClassName('sidebar-container')[0]
  62.     this.sidebarElm && this.sidebarElm.addEventListener('transitionend', this.sidebarResizeHandler)
  63.   },
  64.   beforeDestroy() {
  65.     if (!this.chart) {
  66.       return
  67.     }
  68.     if (this.autoResize) {
  69.       window.removeEventListener('resize', this.__resizeHandler)
  70.     }
  71.  
  72.     this.sidebarElm && this.sidebarElm.removeEventListener('transitionend', this.sidebarResizeHandler)
  73.  
  74.     this.chart.dispose()
  75.     this.chart = null
  76.   },
  77.   methods: {
  78.     sidebarResizeHandler(e) {
  79.       if (e.propertyName === 'width') {
  80.         this.__resizeHandler()
  81.       }
  82.     },
  83.     setOptions({ company, point } = {}, dates) {
  84.       if (!company || !point) {
  85.         return false
  86.       }
  87.  
  88.       this.chart.setOption({
  89.         xAxis: {
  90.           data: dates,
  91.           boundaryGap: false,
  92.           axisTick: {
  93.             show: false
  94.           }
  95.         },
  96.         grid: {
  97.           left: 10,
  98.           right: 10,
  99.           bottom: 20,
  100.           top: 30,
  101.           containLabel: true
  102.         },
  103.         tooltip: {
  104.           trigger: 'axis',
  105.           axisPointer: {
  106.             type: 'cross'
  107.           },
  108.           padding: [5, 10]
  109.         },
  110.         yAxis: {
  111.           axisTick: {
  112.             show: false
  113.           }
  114.         },
  115.         legend: {
  116.           data: [company.name, point.name]
  117.         },
  118.         series: [{
  119.           name: company.name,
  120.           itemStyle: {
  121.             normal: {
  122.               color: '#FF005A',
  123.               lineStyle: {
  124.                 color: '#FF005A',
  125.                 width: 2
  126.               }
  127.             }
  128.           },
  129.           smooth: true,
  130.           type: 'line',
  131.           data: company.data,
  132.           animationDuration: 2800,
  133.           animationEasing: 'cubicInOut'
  134.         },
  135.         {
  136.           name: point.name,
  137.           smooth: true,
  138.           type: 'line',
  139.           itemStyle: {
  140.             normal: {
  141.               color: '#3888fa',
  142.               lineStyle: {
  143.                 color: '#3888fa',
  144.                 width: 2
  145.               },
  146.               areaStyle: {
  147.                 color: '#f3f8ff'
  148.               }
  149.             }
  150.           },
  151.           data: point.data,
  152.           animationDuration: 2800,
  153.           animationEasing: 'quadraticOut'
  154.         }]
  155.       })
  156.     },
  157.     initChart() {
  158.       this.chart = echarts.init(this.$el, 'macarons')
  159.       this.setOptions(this.chartData.quantities, this.chartData.dates)
  160.     }
  161.   }
  162. }
  163. </script>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement