当前位置:   article > 正文

echarts tree (树图) 实现自定义节点图标 自定义样式 点击节点后线条变色 自适应高度 搜索后节点关键字标红 写的很详细,建议收藏!_echarts树图自定义图标

echarts树图自定义图标

前言

网上找了好久资料,没有一个完整实例,所以研究出来立马发布,共享下,希望能帮助到大家。


 

一、实现效果

方式1:提供视频方式大家看下符不符合要求

 

方式2:Demo演示地址

点击此处,可查看演示地址 https://it__distant_branch.gitee.io/echarts-tree/

二、源码提供

      免费下载源码地址,点击下载 https://download.csdn.net/download/Qin_HongKun/14935641

 

三、代码分享

 

HTML代码如下:

  1. <title>Tree</title>
  2. <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
  3. <style>
  4. #echart-id {
  5. width: 100%;
  6. height: calc(100% - 85px);
  7. padding: 20px;
  8. background-color: #fbfbfb;
  9. overflow: auto;
  10. }
  11. .search-con {
  12. width: 70%;
  13. margin: 0 auto;
  14. text-align: center;
  15. }
  16. #tip-botton {
  17. display: black;
  18. position: absolute;
  19. z-index: 999999999;
  20. pointer-events: none;
  21. }
  22. .height-auto {
  23. height: 100%;
  24. }
  25. .overflow-auto {
  26. height: calc(100% - 45px);
  27. overflow: auto;
  28. overflow-x: hidden;
  29. }
  30. </style>
  31. <body>
  32. <div>
  33. <input class="gxtb-echart-search-input" type="text" id="search-canves-input" name="title" required lay-verify="required" placeholder="请输入搜索内容" autocomplete="off" class="layui-input" style="width:calc(100% - 120px);display: inline-block;height:45px;border: 2px solid #06c;padding-left: 2px;">
  34. <button onclick="searchCanves()" class="layui-btn" style="width:80px;height: 42px;">搜索</button>
  35. </div>
  36. <div class="overflow-auto">
  37. <div id="echart-id">
  38. </div>
  39. </div>
  40. </body>
  41. <script src="./js/jquery.js"></script>
  42. <script src="./echarts/echarts.js"></script>
  43. <script src="./data/data.js"></script>

JS代码如下:

  1. <script>
  2. //存储echart对象
  3. $chart = ''
  4. /**
  5. * 判断是否存在值
  6. * @param {Object} data
  7. * @param {Object} value
  8. */
  9. var is_exit_value = function(data, value) {
  10. var is_exit = false
  11. if ($.isArray(data)) {
  12. $.each(data, function(index, item) {
  13. if (item == value) {
  14. is_exit = true
  15. }
  16. })
  17. }
  18. return is_exit
  19. }
  20. //获取子节点数量
  21. var getEchartNotExpandCount = function(list, options, isjs) {
  22. $.each(list, function(i, k) {
  23. var flag = true;
  24. $.each(options.data, function(i, kk) {
  25. if (kk === k) {
  26. flag = false;
  27. }
  28. })
  29. if (flag && !isjs) {
  30. options.lengthH++;
  31. options.data.push(k)
  32. }
  33. if (k.children.length) {
  34. getEchartNotExpandCount(k.children, options)
  35. }
  36. })
  37. }
  38. //获取某个节点父节点,一直递归获取
  39. var getEchartParentNode = function(item, options) {
  40. var flag = false;
  41. options[item.dataIndex] = item
  42. if (item.parentNode) {
  43. getEchartParentNode(item.parentNode, options)
  44. }
  45. }
  46. //根据某个echart节点中所唯一索引来获取某个节点
  47. var getEchartNode = function(list, dataIndex, options) {
  48. var flag = false;
  49. $.each(list, function(i, k) {
  50. if (flag)
  51. return;
  52. if (k.dataIndex == dataIndex) {
  53. console.log(dataIndex, k)
  54. options.data = k
  55. flag = true;
  56. return
  57. } else if (k.children.length) {
  58. getEchartNode(k.children, dataIndex, options)
  59. }
  60. })
  61. }
  62. //设置点击某个节点之后选中线条变色,其他节点线条变成未选中颜色
  63. var setLineStyle = function(node) {
  64. //获取当前点击的节点,用来获取当前节点下是否存在点击的索引 存在说明是父级,则更换颜色 否则正常颜色
  65. var currentParentNode = {}
  66. getEchartParentNode(currentNode, currentParentNode)
  67. if (node.dataIndex in currentParentNode) {
  68. node.data.lineStyle = {
  69. color: '#ED143D',
  70. borderColor: '#ED143D'
  71. }
  72. } else {
  73. node.data.lineStyle = {
  74. color: '#2979ff',
  75. borderColor: '#2979ff'
  76. }
  77. }
  78. }
  79. /**
  80. * 节点点击收缩或者放大之后,获取隐藏的节点数量跟所有节点的数量,从而实现自适应高度
  81. * */
  82. var resize = function() {
  83. var id = document.getElementById('echart-id')
  84. var elesArr = $chart._chartsViews[0]._data.tree._nodes
  85. var options = {
  86. lengthH: 0,
  87. lengthZh: 0,
  88. data: []
  89. }
  90. var lengthH = elesArr.length
  91. var isTowExpand = false;
  92. $.each(elesArr, function(i, i1) {
  93. //获取点击的节点
  94. if (i1.dataIndex == currentIndex) {
  95. currentNode = i1
  96. }
  97. //获取未展开节点下的子节点数量
  98. if (!i1.isExpand && i1.children.length) {
  99. getEchartNotExpandCount([i1], options, true)
  100. }
  101. //获取所有二级节点,如果都未展示,指定固定宽度
  102. if (i1.depth === 2 && i1.isExpand) {
  103. console.log(i1)
  104. isTowExpand = true;
  105. }
  106. if (i1.isExpand) {
  107. }
  108. })
  109. //减去为展开节点数量
  110. lengthH = lengthH - options.lengthH
  111. var height = $(".overflow-auto").height(); // 这里限制最小高
  112. var currentHeight = 35 * (lengthH - 1) || 200;
  113. var newHeight = Math.max(currentHeight, height);
  114. id.style.height = newHeight + 'px';
  115. var paramOptions = {
  116. width: $("#echart-id").width()
  117. }
  118. if (!isTowExpand) paramOptions = {
  119. width: $("#echart-id").width() / 2 + "px"
  120. }
  121. $chart.resize(paramOptions);
  122. }
  123. //保存点击后的节点值
  124. var currentIndex = ''
  125. var currentNode = {}
  126. //搜索后重新渲染echart
  127. searchCanves = function() {
  128. currentIndex = ''
  129. $chart && $chart.showLoading();
  130. //dataJson 从./data/data.js定义的
  131. setChart(dataJson, [$(".gxtb-echart-search-input").val()])
  132. }
  133. var setChart = function(data, keywords) {
  134. //echart对象存在则销毁,目的当请求后台之后,树的数据发生该百年,重新渲染树,否则会导致树的数据不正确
  135. if ($chart) {
  136. $chart.dispose();
  137. }
  138. $chart = echarts.init(document.getElementById('echart-id'));
  139. //树的节点点击事件
  140. $chart.on('click', function(params, params1) {
  141. //获取当前节点唯一索引值
  142. currentIndex = params.dataIndex
  143. if (params.componentType === 'series') {
  144. // 点击到了 series 上
  145. if (!params.value) {
  146. // 点击节点收缩放大之后,重新计算高度,实现自适应高度
  147. resize()
  148. }
  149. }
  150. //如果点击的是最后一级执行
  151. if (!params.data.children) {
  152. location.href = 'https://blog.csdn.net/Qin_HongKun?spm=1011.2124.3001.5343'
  153. }
  154. });
  155. //初始化echart 参数
  156. $chart.setOption(option = {
  157. height: "100%",
  158. backgroundColor: "#fbfbfb",
  159. series: [{
  160. //默认只展开两级,当有检索条件时全部展开
  161. initialTreeDepth: $("#search-canves-input").val().trim() ? 4 : 1,
  162. // 当此节点下还有子节点时候,设置的节点样式,用于区别 没有子节点的节点的样式
  163. itemStyle: {
  164. normal: {
  165. color: '#06c',
  166. borderColor: '#06c',
  167. },
  168. emphasis: {
  169. borderColor: '#06c',
  170. color: '#06c'
  171. }
  172. },
  173. lineStyle: {
  174. color: '#2979ff',
  175. },
  176. type: 'tree',
  177. data: [data],
  178. top: '1%',
  179. left: '200px',
  180. bottom: '1%',
  181. right: '250px',
  182. symbolSize: 18,
  183. symbol: function(params, params1, params2) {
  184. //点击之后 实现连接线变色
  185. setLineStyle(params1)
  186. //加号svg
  187. var svgAdd = 'image://data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAGoAAABtCAYAAABJJPQvAAAACXBIWXMAABcRAAAXEQHKJvM/AAAJP0lEQVR4nO2dTWgbRxTHR01IcWiwwKWlIcaGQHK0wKf0YoVcU6JTeox6Eu2lKrn1EgV6DVEuKT5VPraXyqTXgHxJTgKbQiGmAYmEhpYaLFJsGhpU/uu3G33s7ryZnf2S5gciIVntrva/782bNzNvCsPhUFiyz3tWo3xwelZ+SKHWLQkhij7/tTvcXD9M4ZaMkhvXV6h1V4UQpZFPkf6+qHCaPSEEROsIIXok4m6Mt22MzApFwpRHPisxXm6HxGtnVbhMCUXiVIQQVSHEWkq3MYBgQojWcHO9k9I9TJEJoQq1rivOjdRvZpw+BCPRemneSGpCFWrdIllPI2a3Zoot3GtagqUiVKHWrZNAKoFAVkhFsESFIhfXzIkFyYBg9aRC/0SEoiABvn4j9osly4Csqxn3VWMXKi43t7hwSpSWz4qi8+dC6LGHx2/F7otjcXj0n9h7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2iIhT4Pwvc8VoJBFhzpslxt7+qdRHPDZFgWNk3OA+gfNR69yu+Gyd6JNLcgR+Em1APPag11uDlYkOYm/9nagtw72cnuYnd0vps1wSIKJChwqKj0lcIwKpR4F7qzdnDzw9mh4MqSU3Y6jTYMfSK0QxEXROyQSMamFRsXSryr9tzm7jAaBPpfEAxlPeMUDeK4H50lsBPcHW6uswZcVYhFKDG+gSVrp1EZsDTUs8WntLygXUQLwzC7L4+c1RRY72VwKdGArEi60EKH2IRyoax7K45aS7C44tnTjrWFWZy7AC/G9V3blLuLbQVF7EKJGKwrQ2BGcV01b6dDIkK50C5vTdovPc8M6Hc047SisWeXpFDeRU8iw0bUYCMltmhUNtFhgVSE8i5+Ilg9BxY2INfdTFogl1SF8m7ixCVWaYcX40FHBPbIxbWTcnFBZEIoFwo6KvRJK/DoUx+wZSL1Y4pMCTUJhfawtnKM7nFAq/s6ZDmZHJLOtFCTkIssUelvdwBOursp0adKKO4HwvSyKswkuRJqnrGlSnOCFSonWKHygBDif9o1JBjHQj7SAAAAAElFTkSuQmCC'
  188. //减号svg
  189. var svgD = 'image://data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAGcAAABnCAYAAAAdQVz5AAAACXBIWXMAABcRAAAXEQHKJvM/AAAI9klEQVR4nO2dwWsbRxTGR45JiUmwwCElwSaGFvdogU7JxYJcE+Je0mOUkyCXKPQPiEJ7LcinolPkay6VSa4B+ZKcBPIlEEPBIiGloQablJgGjMq3eitL8u7Om9nZ1a53fiDqGmt3st++N2/fzL6X6/f7Ii3kKp2SECIvhCjQkAv0/350hRAH9MHPe/1GcS81/96kipOrdHDhS/TBz9cNHn5bCNEmwdr9RvHA4LGNkRhxcpUOLGCdxMB/52M8/Y4QoimEaCXJsqYuTq7SWScx7k91ICe4QjWnbVFTEYespCyEqBp2V6bZFELU+41idxonj1UcEqVKnzjdVlgwR9X6jWI7zpPGJk6u0qmlUJRJIFI5rnkpcnFoTqkn3H2pskGWFOmcFJk45MIwsd6N5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/Ry+1izwsKi5jtKoXtUm+hNXaC1b+V6BmjEgIYMSYvo3HS/YifDUbTmmElMdXUP1W7fbbVfvfXtVN0d0k8KGWUvjLbdNyLO8GCVTpPbFdEPzEkotRhX2UqExc03+44wil1zJ0GKa101XA7CqDjipM9bXacF/yiYlxDZQSTUijHV2AJRV5v2QBgQxGWLmy9Twbg44mQeauq6OS8gFgQqrVx0qi65xX1QRdHLFcIiDo6OHTHwM97Ow+t+Gp3Zg4Abq6m2NeYSiTgu1A+T1fsthWyTtUS20yVSccRJA4smt6VlCuiRtRiZ9IOIXBwXqsJbS7FIhzSX1k3PLX7EJo5LCkWKXRSX2MUZnnggErsh+RTo0U3UilsUl6mJMxzAYE5aJ6GMRXeaHFK2oynbfBEHUxdnlBGh8InL7fVIkDZ3+TguEiXOJOT6SlQ+ntW2TMIhvTnepXRTN8pQOCyJFscLesBFbgcfWWsZzBVOOiUJbkoJIcT/Jk39kPJ448gAAAAASUVORK5CYII='
  190. //最后一级展示圆圈图标
  191. if (!params1.data.children) {
  192. return 'emptyCircle'
  193. } else
  194. if ($chart._chartsViews[0]._data) {
  195. //此列表中可以获取节点是否展开
  196. var treeList = $chart._chartsViews[0]._data.tree._nodes
  197. //获取唯一索引
  198. var dataIndex = params1.dataIndex;
  199. //获取节点
  200. var currentTreeNode = {}
  201. getEchartNode(treeList, dataIndex, currentTreeNode)
  202. console.log("currentTreeNode=>", currentTreeNode)
  203. if (!currentTreeNode.data.isExpand && params1.data.children.length) {
  204. return svgAdd
  205. }
  206. } else {
  207. //节点没有展开,显示+号图标
  208. if (params1.data.children.length) {
  209. return svgAdd
  210. }
  211. }
  212. return svgD
  213. },
  214. label: {
  215. position: 'left',
  216. verticalAlign: 'middle',
  217. align: 'right',
  218. color: '#293c55',
  219. margin: [2, 4],
  220. borderWidth: 1,
  221. borderColor: '#aaa',
  222. backgroundColor: 'white',
  223. borderRadius: 2,
  224. padding: [5, 4],
  225. rich: {
  226. keywords: {
  227. color: "red",
  228. fontSize: '12'
  229. },
  230. index: {
  231. fontSize: 12,
  232. color: '#2979ff',
  233. position: '10%'
  234. },
  235. },
  236. formatter: function(data, data1) {
  237. var newName = data.data.name
  238. //默认样式
  239. data.data.label = {
  240. fontSize: 12,
  241. padding: [5, 4],
  242. backgroundColor: 'rgb(238, 243, 246)',
  243. borderColor: 'rgb(115, 161, 191)',
  244. color: '#000000',
  245. }
  246. //如果是最后一级,设置样式不带框
  247. if (!data.data.children) {
  248. // debugger
  249. data.data.label.borderWidth = 0
  250. data.data.label.fontSize = 12
  251. data.data.label.padding = [5, 4]
  252. data.data.label.backgroundColor = 'transparent'
  253. data.data.symbolSize = 12
  254. data.data.label.color = '#2979ff'
  255. } else
  256. newName = data.data.children.length >= 0 ? newName + '(' + data.data.children.length + '类)' : newName;
  257. var arr = []
  258. //关键字标红
  259. if (keywords.length) {
  260. var splitName = newName
  261. $.each(keywords, function(index, item) {
  262. splitName = splitName.replace(item, '`' + item + '`')
  263. })
  264. $.each(splitName.split('`'), function(i, item) {
  265. if (is_exit_value(keywords, item))
  266. arr.push('{keywords|' + item + '}');
  267. else
  268. arr.push(item);
  269. })
  270. } else {
  271. arr.push(newName);
  272. }
  273. return arr.join('');
  274. },
  275. },
  276. leaves: {
  277. label: {
  278. position: 'right',
  279. verticalAlign: 'middle',
  280. align: 'left',
  281. }
  282. },
  283. expandAndCollapse: true,
  284. animationDuration: 550,
  285. animationDurationUpdate: 750
  286. }]
  287. }, true);
  288. $chart.hideLoading();
  289. resize()
  290. }
  291. /**
  292. * 初始化echarts数据
  293. * */
  294. searchCanves()
  295. </script>

以上代码中引用了三个js 提供链接,点击之后可以打开对应代码,可下载可复制

<script src="./js/jquery.js"></script>

<script src="./echarts/echarts.js"></script>

<script src="./data/data.js"></script>


总结

echart tree中一些实现方式,官方并没有提供,可以通过一些奇妙的方式解决哈哈,希望能帮到大家。

实现 echarts tree  label样式修改、symbol图标自定义、formatter添加样式、​​​​​​

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/Monodyee/article/detail/427587
推荐阅读
相关标签
  

闽ICP备14008679号