当前位置:   article > 正文

Vue2使用echarts树图(tree)_vue 树状图

vue 树状图

Vue3使用echarts树图(tree)

本文使用echarts版本:v5.3.3  项目相关依赖版本信息

参考文档:Documentation - Apache ECharts

自定义传入初始化数据treeData

效果如下图:

①安装echarts:yarn add echarts 

②创建树图组件TreeChart.vue:

  1. <template>
  2. <div class="echarts-container"></div>
  3. </template>
  4. <script>
  5. import * as echarts from 'echarts/core'
  6. import { TooltipComponent } from 'echarts/components'
  7. import { TreeChart } from 'echarts/charts'
  8. import { CanvasRenderer } from 'echarts/renderers'
  9. echarts.use([TooltipComponent, TreeChart, CanvasRenderer])
  10. var myChart
  11. var option
  12. export default {
  13. name: 'TreeChart',
  14. props: {
  15. treeData: { // 树图数据
  16. type: Object,
  17. default: () => {
  18. return {}
  19. }
  20. }
  21. },
  22. mounted () {
  23. this.init()
  24. // 监听树图节点的点击事件
  25. myChart.on('click', (e) => {
  26. console.log('e:', e)
  27. this.$emit('clickNode', e.data)
  28. })
  29. },
  30. methods: {
  31. init () {
  32. // console.log('$el:', this.$el)
  33. myChart = echarts.init(this.$el)
  34. option = {
  35. tooltip: { // 提示框浮层设置
  36. trigger: 'item',
  37. triggerOn: 'mousemove', // 提示框触发条件
  38. enterable: true, // 鼠标是否可进入提示框浮层中,默认false
  39. confine: true, // 是否将tooltip框限制在图表的区域内
  40. formatter: function (params) { // 提示框浮层内容格式器,支持字符串模板和回调函数两种形式
  41. // console.log('params:', params)
  42. return params.marker + params.name + '<br/>' + '$ ' + (params.value || '--')
  43. },
  44. // valueFormatter: function (value) { // tooltip 中数值显示部分的格式化回调函数
  45. // return '$' + value.toFixed(2)
  46. // },
  47. backgroundColor: '#FFF', // 提示框浮层的背景颜色
  48. borderColor: '#1890FF', // 提示框浮层的边框颜色
  49. borderWidth: 1, // 提示框浮层的边框宽
  50. borderRadius: 8, // 提示框浮层圆角
  51. padding: [6, 8], // 提示框浮层的内边距
  52. textStyle: { // 提示框浮层的文本样式
  53. color: '#333', // 文字颜色
  54. fontWeight: 400, // 字体粗细
  55. fontSize: 16, // 字体大小
  56. lineHeight: 20, // 行高
  57. width: 60, // 文本显示宽度
  58. // 文字超出宽度是否截断或者换行;只有配置width时有效
  59. overflow: 'breakAll', // truncate截断,并在末尾显示ellipsis配置的文本,默认为...;break换行;breakAll换行,并强制单词内换行
  60. ellipsis: '...'
  61. },
  62. extraCssText: 'box-shadow: 0 0 9px rgba(0, 0, 0, 0.3);text-align: right;' // 额外添加到浮层的css样式
  63. },
  64. series: [
  65. {
  66. type: 'tree',
  67. data: [this.treeData],
  68. name: '树图',
  69. top: '1%', // 组件离容器上侧的距离,像素值20,或相对容器的百分比20%
  70. left: '7%', // 组件离容器左侧的距离
  71. bottom: '1%', // 组件离容器下侧的距离
  72. right: '20%', // 组件离容器右侧的距离
  73. layout: 'orthogonal', // 树图的布局,正交orthogonal和径向radial两种
  74. orient: 'LR', // 树图中正交布局的方向,'LR','RL','TB','BT',只有布局是正交时才生效
  75. edgeShape: 'curve', // 树图边的形状,有曲线curve和折线polyline两种,只有正交布局下生效
  76. roam: false, // 是否开启鼠标缩放或平移,默认false
  77. initialTreeDepth: 2, // 树图初始的展开层级(深度),根节点是0,不设置时全部展开
  78. // symbol: 'arrow', // 标记的图形,默认是emptyCircle;circle,rect,roundRect,triangle,diamond,pin,arrow,none
  79. // symbolRotate: 270, // 配合arrow图形使用效果较好
  80. symbolSize: 16, // 大于0时是圆圈,等于0时不展示,标记的大小
  81. itemStyle: { // 树图中每个节点的样式
  82. color: '#1890FF', // 节点未展开时的填充色
  83. borderColor: 'rgba(255, 144, 0, 1)', // 图形的描边颜色
  84. borderWidth: 1, // 描边线宽,为0时无描边
  85. borderType: 'dotted', // 描边类型
  86. borderCap: 'square', // 指定线段末端的绘制方式butt方形结束,round圆形结束,square
  87. shadowColor: 'rgba(0,121,221,0.3)', // 阴影颜色
  88. shadowBlur: 16, // 图形阴影的模糊大小
  89. opacity: 1 // 图形透明度
  90. },
  91. label: { // 每个节点对应的文本标签样式
  92. show: true, // 是否显示标签
  93. distance: 8, // 文本距离图形元素的距离
  94. position: 'left', // 标签位置
  95. verticalAlign: 'middle', // 文字垂直对齐方式,默认自动,top,middle,bottom
  96. align: 'center', // 文字水平对齐方式,默认自动,left,right,center
  97. fontSize: 16, // 字体大小
  98. color: '#333', // 字体颜色
  99. backgroundColor: '#F0F5FA', // 文字块的背景颜色
  100. borderColor: '#1890FF', // 文字块边框颜色
  101. borderWidth: 1, // 文字块边框宽度
  102. borderType: 'solid', // 文字块边框描边类型 solid dashed dotted
  103. borderRadius: 4, // 文字块的圆角
  104. padding: [6, 12], // 文字块内边距
  105. shadowColor: 'rgba(0,121,221,0.3)', // 文字块的背景阴影颜色
  106. shadowBlur: 6, // 文字块的背景阴影长度
  107. width: 60,
  108. // 文字超出宽度是否截断或者换行;只有配置width时有效
  109. overflow: 'truncate', // truncate截断,并在末尾显示ellipsis配置的文本,默认为...;break换行;breakAll换行,并强制单词内换行
  110. ellipsis: '...'
  111. },
  112. lineStyle: { // 树图边的样式
  113. color: 'rgba(0,0,0,.35)', // 树图边的颜色
  114. width: 2, // 树图边的宽度
  115. curveness: 0.5, // 树图边的曲度
  116. shadowColor: 'rgba(0, 0, 0, 0.5)', // 阴影颜色
  117. shadowBlur: 10 // 图形阴影的模糊大小
  118. },
  119. emphasis: { // 树图中图形和标签高亮的样式
  120. disabled: false, // 是否关闭高亮状态,默认false
  121. // 在高亮图形时,是否淡出其它数据的图形已达到聚焦的效果
  122. focus: 'self', // none不淡出其他图形(默认);self只聚焦当前高亮的数据图形;series聚焦当前高亮的数据所在系列的所有图形;ancestor聚焦所有祖先节点;descendant聚焦所有子孙节点;relative聚焦所有子孙和祖先节点
  123. blurScope: 'coordinateSystem', // 开启focus时,配置淡出的范围,coordinateSystem淡出范围为坐标系(默认);series淡出范围为系列;global淡出范围为全局
  124. itemStyle: { // 该节点的样式
  125. color: '#1890FF', // 图形的颜色
  126. // borderColor: 'rgba(255, 144, 0, 1)', // 图形的描边颜色
  127. borderWidth: 1, // 描边线宽,为0时无描边
  128. borderType: 'solid', // 描边类型 solid dashed dotted
  129. borderCap: 'square', // 指定线段末端的绘制方式butt方形结束,round圆形结束,square
  130. shadowColor: 'rgba(0,121,221,0.3)', // 阴影颜色
  131. shadowBlur: 12, // 图形阴影的模糊大小
  132. opacity: 1 // 图形透明度
  133. },
  134. lineStyle: { // 树图边的样式
  135. color: 'rgba(0,0,0,.45)', // 树图边的颜色
  136. width: 2, // 树图边的宽度
  137. curveness: 0.5, // 树图边的曲度
  138. shadowColor: 'rgba(0, 0, 0, 0.5)', // 阴影颜色
  139. shadowBlur: 6 // 图形阴影的模糊大小
  140. },
  141. label: { // 高亮标签的文本样式
  142. color: '#333',
  143. fontWeight: 600
  144. }
  145. },
  146. blur: { // 淡出状态的相关配置,开启emphasis.focus后有效
  147. itemStyle: {}, // 节点的样式
  148. lineStyle: {}, // 树图边的样式
  149. label: {} // 淡出标签的文本样式
  150. },
  151. leaves: { // 叶子节点的特殊配置
  152. label: { // 叶子节点的文本标签样式
  153. distance: 8,
  154. // color: '#1890FF',
  155. position: 'right',
  156. verticalAlign: 'middle',
  157. align: 'left'
  158. },
  159. itemStyle: {}, // 叶子节点的样式
  160. emphasis: {}, // 叶子节点高亮状态的配置
  161. blur: {}, // 叶子节点淡出状态的配置
  162. select: {} // 叶子节点选中状态的配置
  163. },
  164. animation: true, // 是否开启动画
  165. expandAndCollapse: true, // 子树折叠和展开的交互,默认打开
  166. animationDuration: 550, // 初始动画的时长
  167. animationEasing: 'linear', // 初始动画的缓动效果
  168. animationDelay: 0, // 初始动画的延迟
  169. animationDurationUpdate: 750, // 数据更新动画的时长
  170. animationEasingUpdate: 'cubicInOut', // 数据更新动画的缓动效果
  171. animationDelayUpdate: 0 // 数据更新动画的延迟
  172. }
  173. ]
  174. }
  175. myChart.setOption(option)
  176. }
  177. }
  178. }
  179. </script>
  180. <style lang="less" scoped>
  181. .echarts-container {
  182. width: 100%;
  183. height: 100%;
  184. }
  185. </style>

③在要使用的页面引入:

  1. <TreeChart :treeData="treeData" @clickNode="getNodeData" />
  2. import TreeChart from '@/components/TreeChart'
  3. components: {
  4. TreeChart
  5. },
  6. treeData: {
  7. name: 'tree',
  8. children: [
  9. {
  10. name: '比较',
  11. value: 29,
  12. children: [
  13. {
  14. name: '折线图',
  15. value: 1
  16. },
  17. {
  18. name: '面积图',
  19. value: 2
  20. },
  21. {
  22. name: '柱状图',
  23. value: 3
  24. }
  25. ]
  26. },
  27. {
  28. name: '趋势趋势趋势趋势趋势趋势趋势趋势趋势趋势',
  29. value: 9,
  30. children: [
  31. {
  32. name: '折线图',
  33. value: 1
  34. },
  35. {
  36. name: '阶梯图',
  37. value: 2
  38. },
  39. {
  40. name: '面积图',
  41. value: 3
  42. },
  43. {
  44. name: '堆叠面积图',
  45. value: 4
  46. }
  47. ]
  48. },
  49. {
  50. name: '组成'
  51. }
  52. ]
  53. },
  54. getNodeData (data) {
  55. console.log('data:', data)
  56. }
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/weixin_40725706/article/detail/427636
推荐阅读
相关标签
  

闽ICP备14008679号