美文网首页
vue echarts 自定义指令

vue echarts 自定义指令

作者: 邪瓶张起灵 | 来源:发表于2017-10-20 14:25 被阅读0次
import echarts from 'echarts'
export default {
  deep: true,
  // 插入父节点时调用
  inserted: function (el, binding) {
    let myChart = echarts.init(el)
    let option = binding.value

    myChart.showLoading()
    myChart.setOption(option)
    myChart.hideLoading()
    let oldResize = window.onresize
    window.onresize = () => {
      oldResize()
      myChart.resize()
    }
  },
  update: function (el, binding) {
    let myChart = echarts.getInstanceByDom(el)
    let option = binding.value

    myChart.setOption(option)
  }
}

组件内写法如下

<template>
  <!-- year-chart -->
  <div class="year-chart">

    <div v-line-charts="option" class="chart">
    </div>

    <setting-date v-model="date" :show="showSetting" @close="showSetting = false"></setting-date>

  </div>
</template>

<script>

import LineCharts from 'directives/LineCharts'
import echarts from 'echarts'
import Bus from 'buses/user-online'
import { getUsersOnline } from 'api/user-online'
import SettingDate from './SettingDate'
import moment from 'moment'

export default {
  data () {
    return {
      data: [],
      date: [],
      showSetting: true
    }
  },

  computed: {
    startDate () {
      return this.date ? this.date[0] : null
    },
    endDate () {
      return this.date ? this.date[1] : null
    },
    xData () {
      let data = this.data
      let xData = data.map(item => item[0])
      return xData
    },
    option () {
      let start = this.startDate ? this.startDate.toLocaleDateString() : '-'
      let end = this.endDate ? this.endDate.toLocaleDateString() : '-'
      return {
        title: {
          left: 'center',
          text: `${start}-${end}访问人数`,
          textStyle: { color: '#48576a' }
        },
        color: ['rgb(127, 168, 201)'],
        tooltip: {
          trigger: 'axis'
        },
        toolbox: {
          show: false,
          right: 200,
          feature: {
            restore: {},
            saveAsImage: {}
          }
        },
        dataZoom: [{
          type: 'slider',
          filterMode: 'weakFilter',
          showDataShadow: false,
          top: 250,
          height: 10,
          borderColor: 'transparent',
          backgroundColor: '#e2e2e2',
          handleIcon: 'M10.7,11.9H9.3c-4.9,0.3-8.8,4.4-8.8,9.4c0,5,3.9,9.1,8.8,9.4h1.3c4.9-0.3,8.8-4.4,8.8-9.4C19.5,16.3,15.6,12.2,10.7,11.9z M13.3,24.4H6.7v-1.2h6.6z M13.3,22H6.7v-1.2h6.6z M13.3,19.6H6.7v-1.2h6.6z', // jshint ignore:line
          handleSize: 20,
          handleStyle: {
            shadowBlur: 6,
            shadowOffsetX: 1,
            shadowOffsetY: 0,
            shadowColor: '#aaa'
          },
          labelFormatter: ''
        }],
        grid: {
          top: '60px',
          left: '30px',
          right: '30px',
          containLabel: true
        },
        xAxis: [
          {
            type: 'time',
            data: this.xData,
            interval: 3600 * 24 * 1000,
            min: 'dataMin',
            max: 'dataMax',
            axisTick: {
              show: false
            },
            splitLine: {
              show: false
            },
            axisLabel: {
              formatter: function (val) {
                return moment(val).format('MM-DD')
              },
              rotate: this.data.length > 15 ? 45 : 0
            }
          }
        ],
        yAxis: [
          {
            name: '访问人数(人)',
            nameLocation: 'end',
            type: 'value',
            splitLine: {
              show: true
            },
            axisTick: {
              show: false
            }
          }
        ],
        series: [
          {
            name: '访问人数',
            type: 'line',
            barWidth: '30%',
            data: this.data
          }
        ]
      }
    },

    chartDom () {
      let el = this.$el.querySelector('.chart')
      return echarts.getInstanceByDom(el)
    }
  },

  created () {
    let today = new Date()
    let oneWeek = new Date(today.getTime() - 3600 * 1000 * 24 * 7)
    this.date = [oneWeek, today]
  },

  watch: {
    date (val) {
      if (val && val.length) this.getData()
      else this.data = []
    }
  },

  activated () {
    this.getData()
  },

  methods: {
    getData () {
      if (!this.startDate || !this.endDate) return
      let startTime = this.startDate.getTime()
      let endTime = this.endDate.getTime()
      getUsersOnline({ startTime, endTime }).then(res => {
        this.data = res
        this.$nextTick(() => {
          this.chartDom.off('click')
          this.chartDom.on('click', ({ data }) => {
            Bus.$emit('year-chart-click', data)
          })
        })
      })
    }
  },

  directives: { LineCharts },
  components: { SettingDate }
}
</script>

<style scoped>
.year-chart {
  position: relative;
}

.chart {
  flex-shrink: 0;
  width: 1200px;
  height: 300px;
  padding: 10px 20px;
  margin: 0 auto;
}
</style>

相关文章

  • season2-全局API

    第1节:Vue.directive 自定义指令 Vue.directive自定义指令 自定义的指令:changec...

  • Vue div节点滚动事件-加载更多

    使用Vue.directive注册全局指令 绑定事件 对于Vue自定义指令不明白的同学请移步: Vue自定义指令 ...

  • vue echarts 自定义指令

    组件内写法如下

  • VUE-2:自定义指令、事件

    directive自定义指令 我们还可以通过`Vue`提供的directive方法来自定义指令 注册指令 `vue...

  • vue入门6---vue基本指令、自定义指令、插件

    一、vue常用指令概览 二、vue自定义指令 注册全局指令Vue.directive('my-directive'...

  • vue自定义指令初探

    vue自定义指令初探 一、什么是自定义指令 自定义指令是用来操作DOM的。尽管Vue推崇数据驱动视图的理念,但并非...

  • vue 有自定义指令

    vue 的自定义指令,分为全局自定义指令和局部自定义指令,局部自定义指令等价于局部组件。 自定义指令可以对DOM进...

  • Vue指令钩子函数

    Vue指令上篇文章有讲过了,现在分析Vue指令钩子函数。 自定义指令 全局定义:Vue.directive( ' ...

  • vue自定义指令

    除了内置的指令外,Vue 也允许注册自定义指令。 vue用Vue.directive(id,definition)...

  • vue知识集锦(三)

    自定义指令 除了核心功能默认内置的指令 (v-model和v-show),Vue 也允许注册自定义指令。尽管Vue...

网友评论

      本文标题:vue echarts 自定义指令

      本文链接:https://www.haomeiwen.com/subject/zhkvuxtx.html