在vue中使用echart创建树形图

1.引入echart模块

可以选择全部引入或者只引入所用组件,这里为了方便选择全部引入:

import echarts from 'echarts'

2.定义树形图的data

data() {
      return {
        chartData: {
          //这里参考相关图表的data格式
        }
      }
}

3.定义echart初始化方法

methods: {
      initChart() {
        this.chart = echarts.init(document.getElementById('orgManagement'))
        this.chart.on('contextmenu', params => {
          console.log(params)
          if (params.componentType === 'series') {
              this.selectedOrg = params.data
              this.popoverPanelShow = true
          }else {
            return
          }
        });
        this.chart.setOption({
          tooltip: {
            trigger: 'item',
            triggerOn: 'mousemove'
          },
          series: [
              {
                  type: 'tree',

                  data: [this.chartData],

                  top: '1%',
                  left: '15%',
                  bottom: '1%',
                  right: '20%',

                  symbolSize: 12,

                  label: {
                      normal: {
                          position: 'left',
                          verticalAlign: 'middle',
                          align: 'right',
                          fontSize: 12
                      }
                  },

                  leaves: {
                      label: {
                          normal: {
                              position: 'right',
                              verticalAlign: 'middle',
                              align: 'left'
                          }
                      }
                  },
                  expandAndCollapse: true,
                  animationDuration: 550,
                  animationDurationUpdate: 750
              }
          ]
        })
      },
      hidePopoverPanel(){
        this.popoverPanelShow = false;
      }
    }

4.在编译完成,挂载到页面上时执行初始化

mounted() {
      this.initChart()
    }
}
  • 阻止系统contextmenu事件
    在盛放图表的div上加v-click:contextmenu.prevent=""即可
  • 关闭页面时清空echart实例
beforeDestroy() {
      if (!this.chart) {
        return
      }
      this.chart.dispose()
      this.chart = null
    }
}
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 问答题47 /72 常见浏览器兼容性问题与解决方案? 参考答案 (1)浏览器兼容问题一:不同浏览器的标签默认的外补...
    _Yfling阅读 13,815评论 1 92
  • 这篇笔记主要包含 Vue 2 不同于 Vue 1 或者特有的内容,还有我对于 Vue 1.0 印象不深的内容。关于...
    云之外阅读 5,082评论 0 29
  • 幸福来自原始森林的呼唤 万仞云峰护一山,九天风露透衣寒。固知仙境多灵迹,莫向棋坪着意看。——大丰山 大丰山高耸入云...
    NikkoLiu阅读 585评论 0 0
  • 一抹残凉阅读 584评论 2 6
  • 仔细想一下,16年虽然报的课程虽然和那些大牛们相比还是相对不多, 但是一年课程下来,觉得自己进步不大,究竟是为什么...
    trista_chow阅读 166评论 0 0