vue使用echarts画组织结构图

昨天,写了一篇关于圆环进度条的博客(请移步:Vue圆环进度条),已经烦不胜烦,今天又遇到了需要展示类似公司的组织结构图的功能需求,要冒了!!!

这种需求,自己用div+css也是可以实现的,但是没有什么动画效果,我的css3又很差劲,而且项目中已经使用到了折线图、饼状图、柱状图之类的图表,用的还是百度的echarts,所以这个组织结构图之类的需求也就用了百度的echarts来实现了。

以前用echarts写折线图、柱状图、饼状图的较多,它的API还算比较熟悉,但是画组织结构这样的树状图就很苦逼了,没用过啊,而且设计给的树状图的展示效果跟echarts树状图的展示效果相去甚远,我滴孩,又得一通费时费力的研究,设计图如下:

如图所示,一个树节点中可能会有两种不同的背景色,还有两种不同的文字颜色,每个节点展示的还是圆角矩形。有同学说了,echarts有设置圆角的API啊,直接设置不就完事了。我想说的是,它是提供的有这样的API,但是按照正常的套路实现不了啊。

从图上还可以看到一个几乎实现不了的效果,就是连接每个节点之间的线的拐角处都是直角而不是平滑的,而且echarts没有给出可以设置拐角处是直角的API,只是给了一个curveness(API的描述是树图边的曲度),这玩意儿使用了之后,也还是实现不了的。

从网上查了资料,有人说可以修改echarts的源码,这种解决办法我不推荐,是因为在vue或react项目中,echarts是需要通过安装在package.json中的,如果是多人并行开发,那么别人安装的echarts就不是你修改后的echarts,这就是问题所在。

最后用echarts画出来的效果还是很不错的,唯一没有实现的就是连接每个节点的线的拐角处不是直角,有好的解决办法的,还望不吝赐教,谢谢!展示一下最终的成果:

说了那么多,还是上代码吧,该代码是基于vue的,如果要使用在react中,稍微修改一下就可以了。

组件tree.vue:

<template>
 <div :class="className" :style="{height:height,width:width}" />
</template>

<script>
import echarts from "echarts";
require("echarts/theme/macarons");
import { debounce } from "@/utils";

export default {
 props: {
  className: {
   type: String,
   default: "chart"
  },
  width: {
   type: String,
   default: "100%"
  },
  height: {
   type: String,
   default: "500px"
  },
  chartData: {
   type: Object,
   required: true
  }
 },
 data() {
  return {
   chart: null,
  };
 },
 watch: {
  chartData: {
   deep: true,
   handler(val) {
    this.setOptions(val);
   }
  }
 },
 mounted() {
  this.initChart();
  //是否需要自适应-加了防抖函数
  this.__resizeHandler = debounce(() => {
   if (this.chart) {
    this.chart.resize();
   }
  }, 100);
  window.addEventListener("resize", this.__resizeHandler);

  // 监听侧边栏的变化以实现自适应缩放
  const sidebarElm = document.getElementsByClassName("sidebar-container")[0];
  sidebarElm.addEventListener("transitionend", this.sidebarResizeHandler);
 },
 beforeDestroy() {
  if (!this.chart) {
   return;
  }
  window.removeEventListener("resize", this.__resizeHandler);
  this.chart.dispose();
  this.chart = null;

  const sidebarElm = document.getElementsByClassName("sidebar-container")[0];
  sidebarElm.removeEventListener("transitionend", this.sidebarResizeHandler);
 },
 methods: {
  initChart() {
   this.chart = echarts.init(this.$el, "macarons");
   this.setOptions(this.chartData);

   const nodes = this.chart._chartsViews[0]._data._graphicEls;
   let allNode = 0;
   for(let index = 0; index < nodes.length; index++) {
    const node = nodes[index];
    if (node === undefined) {
     continue
    }
    allNode++;
   }

   const height = window.innerHeight;
   const width = window.innerWidth - 1000;
   const currentHeight = 85 * allNode;
   const currentWidth = 220 * allNode;
   const newHeight = Math.max(currentHeight, height);
   const newWidth = Math.max(currentWidth, width);
   const tree_ele = this.$el;
   // tree_ele.style.height = newHeight + 'px'; //设置高度自适应
   tree_ele.style.width = newWidth + 'px';  //设置宽度自适应
   this.chart.resize();

   this.chart.on('click', this.chartData.clickCallback);  //节点点击事件
  },
  setOptions(data) {
   this.chart.setOption({
    //提供数据视图、还原、下载的工具
    // toolbox: {
    //  show : true,
    //  feature : {
    //   mark : {show: true},
    //   dataView : {show: true, readOnly: false},
    //   restore : {show: true},
    //   saveAsImage : {show: true}
    //  }
    // },
    series: [
     {
      name: "统一授信视图",
      type: "tree",
      orient: "TB", //竖向或水平  TB代表竖向 LR代表水平
      top: '10%',
      initialTreeDepth: 10, //树图初始展开的层级(深度)
      expandAndCollapse: false,  //点击节点时不收起子节点,default: true
      symbolSize: [135, 65],
      itemStyle: {
       color: 'transparent',
       borderWidth: 0,
      },
      lineStyle: {
       color: '#D5D5D5',
       width: 1,
       curveness: 1,
      },
      data: [data]
     }
    ]
   });
  },
  sidebarResizeHandler(e) {
   if (e.propertyName === "width") {
    this.__resizeHandler();
   }
  }
 }
};
</script>

使用tree.vue的方法:

<template>
  <tree :chartData="treeData" />
</template>

<script>
import tree from './tree';

export default {
 data() {
  return {
   treeData: {
    label: {
     backgroundColor: '#F4F4F4',
     borderRadius: [0, 0, 5, 5],
     formatter: [
      '{first|综合授信额度}',
      '{second|(CR20190912000013)\n获批金额:100\n币种:人民币}',
     ].join('\n'),
     rich: {
      first: {
       backgroundColor: '#078E34',
       color: '#fff',
       align: 'center',
       width: 135,
       height: 30,
       borderRadius: [5, 5, 0, 0],
      },
      second: {
       color: '#888',
       align: 'center',
       lineHeight: 17,
      },
     }
    },
    children: [
     {
      label: {
       formatter: [
        '{first|渠道额度}',
       ].join('\n'),
       rich: {
        first: {
         backgroundColor: '#3AC082',
         color: '#fff',
         align: 'center',
         width: 135,
         height: 65,
         borderRadius: 5,
        },
       }
      },
      children: [{
       label: {
        formatter: [
         '{first|保理额度}',
        ].join('\n'),
        rich: {
         first: {
          backgroundColor: '#3AC082',
          color: '#fff',
          align: 'center',
          width: 135,
          height: 65,
          borderRadius: 5,
         },
        }
       },
       children: [{
        label: {
         backgroundColor: '#F4F4F4',
         borderRadius: [0, 0, 5, 5],
         formatter: [
          '{first|反向保理}',
          '{second|(CR20190912000013)\n获批金额:100\n币种:人民币}',
         ].join('\n'),
         rich: {
          first: {
           backgroundColor: '#078E34',
           color: '#fff',
           align: 'center',
           width: 135,
           height: 30,
           borderRadius: [5, 5, 0, 0],
          },
          second: {
           color: '#888',
           align: 'center',
           lineHeight: 17,
          },
         }
        },
       }]
      }]
     },
     {
      label: {
       formatter: [
        '{first|担保/(乐)集团/其他额度}',
       ].join('\n'),
       rich: {
        first: {
         backgroundColor: '#3AC082',
         color: '#fff',
         align: 'center',
         width: 135,
         height: 65,
         borderRadius: 5,
        },
       }
      },
      children: [{
       label: {
        formatter: [
         '{first|保理额度}',
        ].join('\n'),
        rich: {
         first: {
          backgroundColor: '#3AC082',
          color: '#fff',
          align: 'center',
          width: 135,
          height: 65,
          borderRadius: 5,
         },
        }
       },
       children: [{
        label: {
         backgroundColor: '#F4F4F4',
         borderRadius: [0, 0, 5, 5],
         formatter: [
          '{first|正向保理}',
          '{second|(CR20190912000013)\n获批金额:100\n币种:人民币}',
         ].join('\n'),
         rich: {
          first: {
           backgroundColor: '#B8D87E',
           color: '#fff',
           align: 'center',
           width: 135,
           height: 30,
           borderRadius: [5, 5, 0, 0],
          },
          second: {
           color: '#888',
           align: 'center',
           lineHeight: 17,
          },
         }
        },
       }]
      },
      {
       label: {
        formatter: [
         '{first|租赁额度}',
        ].join('\n'),
        rich: {
         first: {
          backgroundColor: '#3AC082',
          color: '#fff',
          align: 'center',
          width: 135,
          height: 65,
          borderRadius: 5,
         },
        }
       },
       children: [
        {
         label: {
          backgroundColor: '#F4F4F4',
          borderRadius: [0, 0, 5, 5],
          formatter: [
           '{first|车辆租赁}',
           '{second|(CR20190912000013)\n获批金额:100\n币种:人民币}',
          ].join('\n'),
          rich: {
           first: {
            backgroundColor: '#FF6C6A',
            color: '#fff',
            align: 'center',
            width: 135,
            height: 30,
            borderRadius: [5, 5, 0, 0],
           },
           second: {
            color: '#888',
            align: 'center',
            lineHeight: 17,
           },
          }
         },
        },
       ]
      }]
     }
    ]
   }
  }
 },
 components: {
  tree,
 }
};
</script>

看着代码不多,但是实现起来,各种查echarts的API和网上的资料,而且,由于效果图中一个节点处的文字可能会换行,文字的颜色也不同,同时有些节点处的背景色还会有两种,以及每个节点处显示的样式和文字都是不固定的,所以我们可能还要面临着将接口返回的数据再改造处理成我们想要的数据的繁琐问题,就如同传递给树节点的treeData的格式一样,相当麻烦,如果每个节点的样式都是一样的,那就好办多了,如官网的一个树状图的例子:https://www.echartsjs.com/examples/zh/editor.html?c=tree-vertical

从echarts的v4.7.0版本开始,给配置项series中加入一个API:edgeShape:'polyline'可实现树形图表连接每个节点的线的拐角处呈直角。

以上就是vue使用echarts画组织结构图的详细内容,更多关于vue 画组织结构图的资料请关注我们其它相关文章!

(0)

相关推荐

  • Vue中使用Echarts仪表盘展示实时数据的实现

    在vue中echarts仪表盘实时数据 彩笔一枚,简单记录一下. 业务场景:通过websocket实时推送数据,将数据渲染到仪表盘中. 第一步: 基于准备好的dom,初始化echarts仪表盘实例. 第二步: 我是通过父子组件传值把数据接收过来,在data中定义upPressure参数,并将接收来的devicePressure参数赋值给它,便于后面将值传入到echarts中 父组件中 <div class="chart" shadow="always">

  • vue 项目引入echarts 添加点击事件操作

    main.js中 import echarts from 'echarts' Vue.prototype.$echarts = echarts vue文件中 _this.calendarChart=_this.$echarts.init(document.getElementById('earlyWarningCalendar')) _this.calendarChart.on('click',function (param) { console.log(param) }) _this.cale

  • vue中使用echarts的示例

    1:首先要用到echarts 2:在vue中安装这个依赖 3:引入要用的页面 import echarts from 'echarts'; 4:然后初始化 <template> <a-col span="12" style="min-height:343px;width:100%;background:#fff" ref="getwidth" :style="'display:'+ model"> &l

  • 在vue中实现清除echarts上次保留的数据(亲测有效)

    因为我是将echarts封装好后,父组件只要传递值就可以进行渲染. 但是点击多次数据请求的时候echarts会多次渲染.如果试过 clear() 与setOption(this.options, true)没用之后.可以试一下下面的方法. 首先是在父组件中对数据进行请求,在赋值之前,先清空. data里定义的三组echarts数据 在axios发送请求后 先清空再赋值. 补充知识:vue.js使用vue-echarts给柱形图绑定点击事件 我就废话不多说了,大家还是直接看代码吧~ <templa

  • vue基于Echarts的拖拽数据可视化功能实现

    背景 我司产品提出了一个需求,做一个数据基于Echars的可拖拽缩放的数据可视化,上网百度了一番,结果出现了两种结局,一种花钱买成熟产品(公司不出钱),一种没有成熟代码,只能自己写了,故事即将开始,敬请期待.......  不,还是先上一张效果图吧,请看...... 前期知识点 1. offset(偏移量) 定义:当前元素在屏幕上占用的空间,如下图: 其中: offsetHeight: 该元素在垂直方向上的占用的空间,单位为px,不包括margin. offsetWidth:该元素在水平方向上的

  • 在vue项目中封装echarts的步骤

    为什么需要封装echarts 每个开发者在制作图表时都需要从头到尾书写一遍完整的option配置,十分冗余 在同一个项目中,各类图表设计十分相似,甚至是相同,没必要一直做重复工作 可能有一些开发者忘记考虑echarts更新数据的特性,以及窗口缩放时的适应问题.这样导致数据更新了echarts视图却没有更新,窗口缩放引起echarts图形变形问题 我希望这个echarts组件能设计成什么样 业务数据和样式配置数据分离,我只需要传入业务数据就行了 它的大小要完全由使用者决定 不会因为缩放出现变形问题

  • 在项目vue中使用echarts的操作步骤

    1.在组件中创建该模块 <template> <div id = "testChart"></div> </template> 2.导入echarts 前提是:已经在项目中配置过echarts 在<script></script>中导入echarts <script> import {echartInit} from "../../../utils/echartUtils" <

  • vue使用echarts实现水平柱形图实例

    文件结构: testData.js文件 const dtuEdition = { name: '有方有线', number: 60, proportion: 40, edition: { '有方有线V1.0.0': 20, '有方有线V1.2.0': 15, '有方有线V2.0.1': 10, '有方有线V3.0.0': 8, '有方有线V3.2.0': 5, '有方有线V3.4.0': 4, '有方有线V4.0.0': 3, '有方有线V4.0.2': 2, '有方有线V4.0.3': 1 }

  • vue+echarts+datav大屏数据展示及实现中国地图省市县下钻功能

    随着前端技术的飞速发展,大数据时代的来临,我们在开发项目时越来越多的客户会要求我们做一个数据展示的大屏,可以直观的展示用户想要的数据,同时炫酷的界面也会深受客户的喜欢. 大屏展示其实就是一堆的图表能够让人一目了然地看到该系统下的一些基本数据信息的汇总,也会有一些实时数据刷新,信息预警之类的.笔者在之前也做过一些大屏类的数据展示,但是由于都是一些图表类的,觉得没什么可说的,加之数据也都牵扯到公司,所以没有沉淀下来什么. 最近有朋友要做一个大屏,问了自己一个问题,自己也刚好做了一个简单的大屏数据展示

  • vue+echarts实现中国地图流动效果(步骤详解)

    @vue+echarts实现中国地图流动效果 #话不多说看效果图 操作步骤: 执行命令:npm run echarts -s 并回车 看到这样的提示代表安装成功 PS:网络不好的情况建议用cnpm淘宝镜像(全局终端执行命令:npm i -g cnpm --registry=https://registry.npm.taobao.org) 下载china.js 链接: https://pan.baidu.com/s/1EODVh9tJNEbFebbrhKyd_Q 提取码: gjz4 引入 impo

  • 基于vue+echarts数据可视化大屏展示的实现

    获取 ECharts 的路径有以下几种,请根据您的情况进行选择: 1) 最直接的方法是在 ECharts 的官方网站中挑选适合您的版本进行下载,不同的打包下载应用于不同的开发者功能与体积的需求,或者您也可以直接下载完整版本:开发环境建议下载源代码版本,包含了常见的错误提示和警告. 2) 也可以在 ECharts 的 GitHub 上下载最新的 release 版本,解压出来的文件夹里的 dist 目录里可以找到最新版本的 echarts 库. 3) 或者通过 npm 获取 echarts,npm

  • vue使用echarts图表自适应的几种解决方案

    1.使用window.onresize let myChart = echarts.init(document.getElementById(dom)) window.onresize = function () { myChat.resize() } 优点:可以根据窗口大小实现自适应 缺点: 多个图表自适应写法比较麻烦(当一个页面的图表太多时,这样写法不是很灵活): let myChart1 = echarts.init(document.getElementById(dom1)) let m

随机推荐