index.vue 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353
  1. <template>
  2. <div class="echarts-container" />
  3. </template>
  4. <script>
  5. import { TreeChart } from 'echarts/charts'
  6. import { TooltipComponent } from 'echarts/components'
  7. import * as echarts from 'echarts/core'
  8. import { CanvasRenderer } from 'echarts/renderers'
  9. echarts.use([TooltipComponent, TreeChart, CanvasRenderer])
  10. let myChart
  11. let option
  12. export default {
  13. name: 'TreeChart',
  14. props: {
  15. treeData: {
  16. type: Object,
  17. default: () => {
  18. return {}
  19. }
  20. },
  21. topDeep: Number,
  22. type: String,
  23. clickNodeList: Array
  24. },
  25. watch: {
  26. clickNodeList: {
  27. handler(newValue, old){
  28. if (newValue) {
  29. this.init()
  30. }
  31. }
  32. },
  33. treeData: {
  34. handler(newValue, old) {
  35. if (newValue) {
  36. this.init()
  37. }
  38. }
  39. }
  40. },
  41. mounted() {
  42. this.init()
  43. // 监听树图节点的点击事件
  44. myChart.on('click', (e) => {
  45. console.log('e:', e)
  46. this.$emit('clickNode', e)
  47. })
  48. },
  49. methods: {
  50. init() {
  51. const that = this
  52. // console.log('$el:', this.$el)
  53. myChart = echarts.init(this.$el)
  54. option = {
  55. tooltip: {
  56. // 提示框浮层设置
  57. trigger: 'item',
  58. triggerOn: 'mousemove', // 提示框触发条件
  59. enterable: true, // 鼠标是否可进入提示框浮层中,默认false
  60. confine: true, // 是否将tooltip框限制在图表的区域内
  61. padding: [5, 10],
  62. formatter: function(params) {
  63. // 提示框浮层内容格式器,支持字符串模板和回调函数两种形式
  64. // console.log('params:', params)
  65. const relationStr = "<div style='display: flex;flex-direction: column;'>" +
  66. '<div>' +
  67. (that.type === 'relation' ?
  68. ( '<span style="color: #606266;">' + that.$t('atlas.algebra') + ': ' + '</span>' + '<span style="color: #000000;">' + (Number(params.data.TOP_RELATION_DEEP) - Number(that.topDeep)) + '</span>' ) :
  69. ( '<span style="color: #606266;">' + that.$t('atlas.numberOfLayers') + ': ' + '</span>') + '<span style="color: #000000;">' + (Number(params.data.TOP_NETWORK_DEEP) - Number(that.topDeep)) + '</span>' ) +
  70. '</div>' +
  71. '<div>' +
  72. '<span style="color: #606266;">' +
  73. that.$t('atlas.memberCode') + ': ' +
  74. '</span>' +
  75. '<span style="color: #000000;">' +
  76. params.data.USER_NAME +
  77. '</span>' +
  78. '</div>' +
  79. '<div>' +
  80. '<span style="color: #606266;">' +
  81. that.$t('atlas.name') + ': ' +
  82. '</span>' +
  83. '<span style="color: #000000;">' +
  84. params.data.REAL_NAME +
  85. '</span>' +
  86. '</div>' +
  87. '<div>' +
  88. '<span style="color: #606266;">' +
  89. that.$t('atlas.level') + ': ' +
  90. '</span>' +
  91. '<span style="color: #000000;">' +
  92. params.data.DEC_LV_NAME +
  93. '</span>' +
  94. '</div>' +
  95. '<div>' +
  96. '<span style="color: #606266;">' +
  97. that.$t('atlas.highest') + ': ' +
  98. '</span>' +
  99. '<span style="color: #000000;">' +
  100. params.data.EMP_LV_NAME + ',' + params.data.CROWN_LV_NAME +
  101. '</span>' +
  102. '</div>' +
  103. '</div>'
  104. const networkStr = "<div style='display: flex;flex-direction: column;'>" +
  105. '<div>' +
  106. (that.type === 'relation' ?
  107. ( '<span style="color: #606266;">' + that.$t('atlas.algebra') + ': ' + '</span>' + '<span style="color: #000000;">' + (Number(params.data.TOP_RELATION_DEEP) - Number(that.topDeep)) + '</span>' ) :
  108. ( '<span style="color: #606266;">' + that.$t('atlas.numberOfLayers') + ': ' + '</span>') + '<span style="color: #000000;">' + (Number(params.data.TOP_NETWORK_DEEP) - Number(that.topDeep)) + '</span>' ) +
  109. '</div>' +
  110. '<div>' +
  111. '<span style="color: #606266;">' +
  112. that.$t('atlas.memberCode') + ': ' +
  113. '</span>' +
  114. '<span style="color: #000000;">' +
  115. params.data.USER_NAME +
  116. '</span>' +
  117. '</div>' +
  118. '<div>' +
  119. '<span style="color: #606266;">' +
  120. that.$t('atlas.name') + ': ' +
  121. '</span>' +
  122. '<span style="color: #000000;">' +
  123. params.data.REAL_NAME +
  124. '</span>' +
  125. '</div>' +
  126. '<div>' +
  127. '<span style="color: #606266;">' +
  128. that.$t('atlas.location') + ': ' +
  129. '</span>' +
  130. '<span style="color: #000000;">' +
  131. (params.data.RELATIVE_LOCATION || '') +
  132. '</span>' +
  133. '</div>' +
  134. '<div>' +
  135. '<span style="color: #606266;">' +
  136. that.$t('atlas.level') + ': ' +
  137. '</span>' +
  138. '<span style="color: #000000;">' +
  139. params.data.DEC_LV_NAME +
  140. '</span>' +
  141. '</div>' +
  142. '<div>' +
  143. '<span style="color: #606266;">' +
  144. that.$t('atlas.highest') + ': ' +
  145. '</span>' +
  146. '<span style="color: #000000;">' +
  147. params.data.EMP_LV_NAME + ',' + params.data.CROWN_LV_NAME +
  148. '</span>' +
  149. '</div>' +
  150. "<div>" +
  151. '<span style="color: #606266;">' +
  152. that.$t('atlas.joiningPeriod') + ': ' +
  153. '</span>' +
  154. '<span style="color: #000000;">' +
  155. params.data.PERIOD_AT +
  156. '</span>' +
  157. '</div>' +
  158. '</div>'
  159. return that.type === 'relation' ? relationStr : networkStr
  160. },
  161. // valueFormatter: function (value) { // tooltip 中数值显示部分的格式化回调函数
  162. // return '$' + value.toFixed(2)
  163. // },
  164. backgroundColor: 'rgba(250,250,250,0.99)', // 提示框浮层的背景颜色
  165. borderColor: '#1890FF', // 提示框浮层的边框颜色
  166. borderWidth: 0.5, // 提示框浮层的边框宽
  167. borderRadius: 8, // 提示框浮层圆角
  168. textStyle: {
  169. // 提示框浮层的文本样式
  170. color: '#333', // 文字颜色
  171. fontWeight: 400, // 字体粗细
  172. fontSize: 14, // 字体大小
  173. lineHeight: 20, // 行高
  174. width: 60, // 文本显示宽度
  175. // 文字超出宽度是否截断或者换行;只有配置width时有效
  176. overflow: 'breakAll', // truncate截断,并在末尾显示ellipsis配置的文本,默认为...;break换行;breakAll换行,并强制单词内换行
  177. ellipsis: '...'
  178. },
  179. extraCssText:
  180. 'min-height:100px;box-shadow: 0 0 9px rgba(0, 0, 0, 0.3);text-align: left;', // 额外添加到浮层的css样式
  181. axisPointer: {
  182. type: 'shadow',
  183. shadowStyle: {
  184. color: new echarts.graphic.LinearGradient(
  185. 0, 0, 0, 1,
  186. [
  187. { offset: 0, color: 'rgba(255, 255, 255, 0)' },
  188. { offset: 1, color: 'rgba(37, 107, 230, 0.18)' }
  189. ]
  190. )
  191. }
  192. }
  193. },
  194. series: [
  195. {
  196. width: '100%',
  197. height: '100%',
  198. type: 'tree',
  199. data: [this.treeData],
  200. name: '树图',
  201. top: '5%', // 组件离容器上侧的距离,像素值20,或相对容器的百分比20%
  202. left: '5%', // 组件离容器左侧的距离
  203. bottom: '1%', // 组件离容器下侧的距离
  204. right: '2%', // 组件离容器右侧的距离
  205. layout: 'orthogonal', // 树图的布局,正交orthogonal和径向radial两种
  206. orient: 'TB', // 树图中正交布局的方向,'LR','RL','TB','BT',只有布局是正交时才生效
  207. edgeShape: 'polyline', // 树图边的形状,有曲线curve和折线polyline两种,只有正交布局下生效
  208. roam: true, // 是否开启鼠标缩放或平移,默认false
  209. zoom: 0.6,
  210. scaleLimit: {
  211. min: 0.5,
  212. max: 10
  213. },
  214. initialTreeDepth: -1, // 树图初始的展开层级(深度),根节点是0,不设置时全部展开
  215. // symbol: 'emptyCircle', // 标记的图形,默认是emptyCircle;circle,rect,roundRect,triangle,diamond,pin,arrow,none
  216. symbol: function(value, params) {
  217. // params.data节点的所有数据
  218. if (params.data.leaf == true) {
  219. return 'emptyCircle'
  220. } else if (params.data.leaf == false) {
  221. return 'circle'
  222. }
  223. },
  224. // symbolRotate: 270, // 配合arrow图形使用效果较好
  225. symbolSize: 10, // 大于0时是圆圈,等于0时不展示,标记的大小
  226. itemStyle: {
  227. // 树图中每个节点的样式
  228. color: '#1890FF', // 节点未展开时的填充色
  229. borderColor: 'rgba(255, 144, 0, 1)', // 图形的描边颜色
  230. borderWidth: 0.5, // 描边线宽,为0时无描边
  231. borderType: 'dotted', // 描边类型
  232. borderCap: 'square', // 指定线段末端的绘制方式butt方形结束,round圆形结束,square
  233. shadowColor: 'rgba(0,121,221,0.3)', // 阴影颜色
  234. shadowBlur: 16, // 图形阴影的模糊大小
  235. opacity: 1 // 图形透明度
  236. },
  237. label: {
  238. // 每个节点对应的文本标签样式
  239. show: true, // 是否显示标签
  240. distance: 15, // 文本距离图形元素的距离
  241. position: 'bottom', // 标签位置
  242. verticalAlign: 'middle', // 文字垂直对齐方式,默认自动,top,middle,bottom
  243. align: 'center', // 文字水平对齐方式,默认自动,left,right,center
  244. fontSize: 14, // 字体大小
  245. color: '#333', // 字体颜色
  246. backgroundColor: '#F0F5FA', // 文字块的背景颜色
  247. borderColor: '#1890FF', // 文字块边框颜色
  248. borderWidth: 0.5, // 文字块边框宽度
  249. borderType: 'solid', // 文字块边框描边类型 solid dashed dotted
  250. borderRadius: 6, // 文字块的圆角
  251. padding: [6, 5], // 文字块内边距
  252. shadowColor: 'rgba(0,121,221,0.6)', // 文字块的背景阴影颜色
  253. shadowBlur: 6, // 文字块的背景阴影长度
  254. // width: 60,
  255. // 文字超出宽度是否截断或者换行;只有配置width时有效
  256. overflow: 'truncate', // truncate截断,并在末尾显示ellipsis配置的文本,默认为...;break换行;breakAll换行,并强制单词内换行
  257. ellipsis: '...',
  258. formatter: function(params) {
  259. // return params.data.USER_NAME + '\n' + '\n' + params.data.REAL_NAME
  260. return params.data.REAL_NAME
  261. }
  262. },
  263. lineStyle: {
  264. // 树图边的样式
  265. color: 'rgba(0,0,0,.35)', // 树图边的颜色
  266. width: 2, // 树图边的宽度
  267. curveness: 0.5, // 树图边的曲度
  268. shadowColor: 'rgba(0, 0, 0, 0.5)', // 阴影颜色
  269. shadowBlur: 10 // 图形阴影的模糊大小
  270. },
  271. emphasis: {
  272. // 树图中图形和标签高亮的样式
  273. disabled: false, // 是否关闭高亮状态,默认false
  274. // 在高亮图形时,是否淡出其它数据的图形已达到聚焦的效果
  275. focus: 'relative', // none不淡出其他图形(默认);self只聚焦当前高亮的数据图形;series聚焦当前高亮的数据所在系列的所有图形;ancestor聚焦所有祖先节点;descendant聚焦所有子孙节点;relative聚焦所有子孙和祖先节点
  276. blurScope: 'coordinateSystem', // 开启focus时,配置淡出的范围,coordinateSystem淡出范围为坐标系(默认);series淡出范围为系列;global淡出范围为全局
  277. itemStyle: {
  278. // 该节点的样式
  279. color: '#1890FF', // 图形的颜色
  280. // borderColor: 'rgba(255, 144, 0, 1)', // 图形的描边颜色
  281. borderWidth: 1, // 描边线宽,为0时无描边
  282. borderType: 'solid', // 描边类型 solid dashed dotted
  283. borderCap: 'square', // 指定线段末端的绘制方式butt方形结束,round圆形结束,square
  284. shadowColor: 'rgba(0,121,221,0.3)', // 阴影颜色
  285. shadowBlur: 12, // 图形阴影的模糊大小
  286. opacity: 1 // 图形透明度
  287. },
  288. lineStyle: {
  289. // 树图边的样式
  290. color: 'rgba(0,0,0,.45)', // 树图边的颜色
  291. width: 2, // 树图边的宽度
  292. curveness: 0.5, // 树图边的曲度
  293. shadowColor: 'rgba(0, 0, 0, 0.5)', // 阴影颜色
  294. shadowBlur: 6 // 图形阴影的模糊大小
  295. },
  296. label: {
  297. // 高亮标签的文本样式
  298. color: '#333',
  299. fontWeight: 600
  300. }
  301. },
  302. blur: {
  303. // 淡出状态的相关配置,开启emphasis.focus后有效
  304. itemStyle: {}, // 节点的样式
  305. lineStyle: {}, // 树图边的样式
  306. label: {} // 淡出标签的文本样式
  307. },
  308. leaves: {
  309. // 叶子节点的特殊配置
  310. label: {
  311. // 叶子节点的文本标签样式
  312. distance: 8,
  313. // color: '#1890FF',
  314. position: 'bottom',
  315. verticalAlign: 'middle',
  316. align: 'center'
  317. },
  318. itemStyle: {}, // 叶子节点的样式
  319. emphasis: {}, // 叶子节点高亮状态的配置
  320. blur: {}, // 叶子节点淡出状态的配置
  321. select: {} // 叶子节点选中状态的配置
  322. },
  323. animation: true, // 是否开启动画
  324. expandAndCollapse: true, // 子树折叠和展开的交互,默认打开
  325. animationDuration: 500, // 初始动画的时长
  326. animationEasing: 'linear', // 初始动画的缓动效果
  327. animationDelay: 0, // 初始动画的延迟
  328. animationDurationUpdate: 500, // 数据更新动画的时长
  329. animationEasingUpdate: 'cubicInOut', // 数据更新动画的缓动效果
  330. animationDelayUpdate: 0 // 数据更新动画的延迟
  331. }
  332. ]
  333. }
  334. myChart.setOption(option)
  335. }
  336. }
  337. }
  338. </script>
  339. <style lang="scss" scoped>
  340. .echarts-container {
  341. width: 100%;
  342. height: calc(100vh - 230px);
  343. }
  344. </style>