echarts-utils.js 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848
  1. import * as echarts from 'echarts'
  2. export function pieUtils(id, name, data) {
  3. // 验证 这个 是否被创建了
  4. let linearBarDom = echarts.getInstanceByDom(id)
  5. if (linearBarDom == null) {
  6. linearBarDom = echarts.init(id)
  7. }
  8. linearBarDom.setOption({
  9. title: {
  10. text: name, // 设置主标题文本
  11. show: true, // 显示标题组件
  12. x: 'left', //水平安放位置,默认为'left',可选为:'center' | 'left' | 'right' | {number}(x坐标,单位px)
  13. y: 'top', //垂直安放位置,默认为top,可选为:'top' | 'bottom' | 'center' | {number}(y坐标,单位px)
  14. padding: [10, 0, 0, 0], //可设定图例[距上方距离,距右方距离,距下方距离,距左方距离]
  15. },
  16. // 这个就是旁边的那个颜色小方块
  17. legend: {
  18. //朝向 : 垂直
  19. orient: 'vertical',
  20. x: 'right', //可设定图例在left、right、center
  21. y: 'top', //可设定图例在top、bottom、center
  22. padding: [10, 50, 0, 0], //可设定图例[距上方距离,距右方距离,距下方距离,距左方距离]
  23. // 这个是 圆形旁边的 小方块 自定义
  24. formatter: function (name) {
  25. // 通过name获取到数组对象中的单个对象
  26. let singleData = data.filter(function (item) {
  27. return item.name == name
  28. })
  29. return `${name} | ${singleData[0].value} ${singleData[0].unit}`
  30. },
  31. },
  32. tooltip: {
  33. //提示框组件
  34. trigger: 'item', //item数据项图形触发,主要在散点图,饼图等无类目轴的图表中使用。
  35. // backgroundColor: 'rgba(255,255,255,0.8)', //设置背景图片 rgba格式
  36. // color: 'black',
  37. // borderWidth: '1', //边框宽度设置1
  38. // borderColor: 'gray', //设置边框颜色
  39. // textStyle: {
  40. // color: 'black', //设置文字颜色
  41. // },
  42. axisPointer: {
  43. // 坐标轴指示器,坐标轴触发有效
  44. type: 'shadow', // 默认为直线,可选为:'line' | 'shadow'
  45. },
  46. // formatter: '{a} <br/>{b} : {c} <br/>百分比 : {d}%', //{a}(系列名称),{b}(数据项名称),{c}(数值), {d}(百分比)
  47. //原来的 替换成我需要的 这个是鼠标移上去出现的
  48. formatter: function (data) {
  49. return `
  50. <span style="background-color:${data.color};display:inline-block;margin-right:4px;border-radius:10px;width:10px;height:10px"></span>
  51. <span>${data.seriesName}<span><br/>
  52. <span>${data.name} : <span style="color:red;">${data.value}</span> ${data.data.unit}<span><br/>
  53. <span>百分比 : ${data.percent} %</span>
  54. `
  55. },
  56. },
  57. series: [
  58. {
  59. center: ['50%', '50%'],
  60. radius: '50%', // 饼图的半径,外半径为可视区尺寸(容器高宽中较小一项)的 55% 长度。
  61. type: 'pie', // 类型 圆形
  62. data: data, // 数据
  63. name: name, // 名字
  64. minAngle: 10, //设置扇形的最小占比 不然有些太小了 就看不到了
  65. },
  66. ],
  67. //color: createColorCode(data),
  68. })
  69. window.onresize = function () {
  70. //自适应大小, 不用的话不会自适应大小。
  71. linearBarDom.resize()
  72. }
  73. }
  74. export function pieUtilsOne(id, name, data) {
  75. // 验证 这个 是否被创建了
  76. let linearBarDom = echarts.getInstanceByDom(id)
  77. if (linearBarDom == null) {
  78. linearBarDom = echarts.init(id)
  79. }
  80. linearBarDom.setOption({
  81. title: {
  82. text: name, // 设置主标题文本
  83. show: true, // 显示标题组件
  84. x: 'left', //水平安放位置,默认为'left',可选为:'center' | 'left' | 'right' | {number}(x坐标,单位px)
  85. y: 'top', //垂直安放位置,默认为top,可选为:'top' | 'bottom' | 'center' | {number}(y坐标,单位px)
  86. padding: [10, 0, 0, 0], //可设定图例[距上方距离,距右方距离,距下方距离,距左方距离]
  87. },
  88. // 这个就是旁边的那个颜色小方块
  89. legend: {
  90. //朝向 : 垂直(vertical),水平(horizontal)
  91. orient: 'horizontal',
  92. type: 'scroll', // 分页滚动
  93. x: 'center', //可设定图例在left、right、center
  94. y: 'bottom', //可设定图例在top、bottom、center
  95. padding: [0, 10, 0, 10], //可设定图例[距上方距离,距右方距离,距下方距离,距左方距离]
  96. // 这个是 圆形旁边的 小方块 自定义
  97. formatter: function (name) {
  98. // 通过name获取到数组对象中的单个对象
  99. let singleData = data.filter(function (item) {
  100. return item.name == name
  101. })
  102. return `${name} | ${singleData[0].value} ${singleData[0].unit}`
  103. },
  104. textStyle: {
  105. rich: {
  106. a: {
  107. width: 120,
  108. lineHeight: 12
  109. }
  110. }
  111. }
  112. },
  113. tooltip: {
  114. //提示框组件
  115. trigger: 'item', //item数据项图形触发,主要在散点图,饼图等无类目轴的图表中使用。
  116. // backgroundColor: 'rgba(255,255,255,0.8)', //设置背景图片 rgba格式
  117. // color: 'black',
  118. // borderWidth: '1', //边框宽度设置1
  119. // borderColor: 'gray', //设置边框颜色
  120. // textStyle: {
  121. // color: 'black', //设置文字颜色
  122. // },
  123. axisPointer: {
  124. // 坐标轴指示器,坐标轴触发有效
  125. type: 'shadow', // 默认为直线,可选为:'line' | 'shadow'
  126. },
  127. // formatter: '{a} <br/>{b} : {c} <br/>百分比 : {d}%', //{a}(系列名称),{b}(数据项名称),{c}(数值), {d}(百分比)
  128. //原来的 替换成我需要的 这个是鼠标移上去出现的
  129. formatter: function (data) {
  130. return `
  131. <span style="background-color:${data.color};display:inline-block;margin-right:4px;border-radius:10px;width:10px;height:10px"></span>
  132. <span>${data.seriesName}<span><br/>
  133. <span>${data.name} : <span style="color:red;">${data.value}</span> ${data.data.unit}<span><br/>
  134. <span>百分比 : ${data.percent} %</span>
  135. `
  136. },
  137. },
  138. toolbox: {
  139. // 导出组件
  140. show: 1,
  141. itemSize: 24,
  142. x: 'right',
  143. y: 'top',
  144. feature: {
  145. saveAsImage: {
  146. show: true,
  147. title: '下载图片',
  148. type: 'png'
  149. }
  150. }
  151. },
  152. series: [
  153. {
  154. center: ['50%', '50%'],
  155. radius: '50%', // 饼图的半径,外半径为可视区尺寸(容器高宽中较小一项)的 55% 长度。
  156. type: 'pie', // 类型 圆形
  157. data: data, // 数据
  158. name: name, // 名字
  159. minAngle: 15, //设置扇形的最小占比 不然有些太小了 就看不到了
  160. },
  161. ],
  162. //color: createColorCode(data),
  163. })
  164. window.onresize = function () {
  165. //自适应大小, 不用的话不会自适应大小。
  166. linearBarDom.resize()
  167. }
  168. }
  169. export function barUtils(id, name, Xdata, Ydata) {
  170. // 验证 这个 是否被创建了
  171. let linearBarDom = echarts.getInstanceByDom(id)
  172. if (linearBarDom == null) {
  173. linearBarDom = echarts.init(id)
  174. }
  175. linearBarDom.setOption({
  176. title: {
  177. text: name,
  178. },
  179. xAxis: {
  180. type: 'category',
  181. data: Xdata,
  182. },
  183. yAxis: {
  184. type: 'value',
  185. },
  186. tooltip: {},
  187. dataZoom: [
  188. // 这个是数据过多 就可以横线拉动
  189. {
  190. type: 'slider',
  191. show: true,
  192. start: 0,
  193. end: 50,
  194. xAxisIndex: [0],
  195. },
  196. ],
  197. series: [
  198. {
  199. data: Ydata,
  200. type: 'bar',
  201. // 这个是在柱状图中显示 数字
  202. label: {
  203. // 开启
  204. show: true,
  205. // 显示在上方
  206. position: 'top',
  207. //颜色
  208. color: 'black',
  209. },
  210. },
  211. ],
  212. })
  213. window.onresize = function () {
  214. //自适应大小, 不用的话不会自适应大小。
  215. linearBarDom.resize()
  216. }
  217. }
  218. // 重构柱状图
  219. /**
  220. * 参数
  221. * @param {*} id 图渲染容器id
  222. * @param {*} name 图标题
  223. * @param {*} Xdata x轴数据
  224. * @param {*} Ydata y轴数据
  225. * @param {*} seriesName 图数据种类
  226. * @param {*} yAxisName y轴单位说明
  227. */
  228. export function barUtilsOne(id, name, Xdata, Ydata, seriesName, yAxisName) {
  229. // 验证 这个 是否被创建了
  230. let linearBarDom = echarts.getInstanceByDom(id)
  231. if (linearBarDom == null) {
  232. linearBarDom = echarts.init(id)
  233. }
  234. if (seriesName.length < 0) {
  235. return
  236. }
  237. let series = []
  238. let gradientColor = getGradientColor(seriesName.length)
  239. for (let i = 0; i < seriesName.length; i++) {
  240. let arrColor = gradientColor[i]
  241. series.push({
  242. name: seriesName[i],
  243. type: 'bar',
  244. // stack: 'all',
  245. barMaxwidth: 35,
  246. barGap: '10%',
  247. color: {
  248. type: 'linear',
  249. x: 0,
  250. y: 1,
  251. colorStops: [
  252. {
  253. offset: 0,
  254. // color: '#6633ff',
  255. color: arrColor[0]
  256. },
  257. {
  258. offset: 0.5,
  259. // color: '#6699ff'
  260. color: arrColor[1]
  261. },
  262. {
  263. offset: 1,
  264. // color: '#66ffff'
  265. color: arrColor[2]
  266. }
  267. ]
  268. },
  269. label: {
  270. show: true,
  271. color: '#000000',
  272. position: 'top',
  273. formatter(p) {
  274. return p.value > 0 ? p.value : ''
  275. }
  276. },
  277. data: Ydata[i]
  278. })
  279. }
  280. linearBarDom.setOption({
  281. title: {
  282. text: name,
  283. },
  284. xAxis: {
  285. type: 'category',
  286. data: Xdata,
  287. axisLabel: {
  288. color: '#bb3137',
  289. fontSize: 16
  290. }
  291. },
  292. yAxis: {
  293. type: 'value',
  294. name: yAxisName,
  295. nameLocation: 'end',
  296. },
  297. legend: {
  298. data: seriesName,
  299. top: "0%"
  300. },
  301. tooltip: {},
  302. toolbox: {
  303. show: 1,
  304. itemSize: 30,
  305. x: 'right',
  306. y: 'top',
  307. feature: {
  308. saveAsImage: {
  309. show: true,
  310. title: '下载图片',
  311. type: 'png'
  312. }
  313. }
  314. },
  315. series: series,
  316. })
  317. window.onresize = function () {
  318. //自适应大小, 不用的话不会自适应大小。
  319. linearBarDom.resize()
  320. }
  321. }
  322. // 主题注册 也没有用到
  323. export function echartsZhuTiZhuCe() {
  324. zhuTi.filter((item) => {
  325. echarts.registerTheme(item.zhuTiName, item)
  326. })
  327. }
  328. // 随机生成 颜色 这个占时没有用到
  329. export function createColorCode(data) {
  330. var list = []
  331. for (let i = 0; i < data.length; i++) {
  332. let color = ''
  333. let r = Math.floor(Math.random() * 256)
  334. let g = Math.floor(Math.random() * 256)
  335. let b = Math.floor(Math.random() * 256)
  336. color = `rgb(${r},${g},${b})`
  337. list.push(color)
  338. }
  339. return list
  340. }
  341. function getGradientColor(num) {
  342. let arr = createGradientColor()
  343. let return_arr = []
  344. for (let i = 0; i < num; i++) {
  345. if (arr.length > 0) {
  346. let arrIndex = Math.floor(Math.random() * arr.length)
  347. return_arr[i] = arr[arrIndex]
  348. arr.splice(arrIndex, 1)
  349. } else {
  350. break
  351. }
  352. }
  353. return return_arr
  354. }
  355. function createGradientColor() {
  356. let list = []
  357. let a = ['#00C5CD', '#00E5EE', '#00F5FF']
  358. let b = ['#CD4F39', '#EE5C42', '#FF6347']
  359. let c = ['#6633FF', '#6699FF', '#66FFFF']
  360. let d = ['#1E90FF', '#63B8FF', '#00BFFF']
  361. let e = ['#BF3EFF', '#9B30FF', '#AB82FF']
  362. let f = ['#6A5ACD', '#7B68EE', '#8470FF']
  363. let g = ['#1E90FF', '#00BFFF', '#87CEFA']
  364. let h = ['#00CED1', '#48D1CC', '#40E0D0']
  365. let i = ['#00CD66', '#00FF7F', '#9AFF9A']
  366. let j = ['#9400D3', '#A020F0', '#BA55D3']
  367. list.push(a)
  368. list.push(b)
  369. list.push(c)
  370. list.push(d)
  371. list.push(e)
  372. list.push(f)
  373. list.push(g)
  374. list.push(h)
  375. list.push(i)
  376. list.push(j)
  377. return list
  378. }
  379. // unit:新增tip单位
  380. export function barUtilsTwo(id, name, Xdata, Ydata, seriesName, yAxisName, unit) {
  381. // 验证 这个 是否被创建了
  382. let linearBarDom = echarts.getInstanceByDom(id)
  383. if (linearBarDom == null) {
  384. linearBarDom = echarts.init(id)
  385. }
  386. linearBarDom.setOption({
  387. title: {
  388. text: name,
  389. x: 'center',
  390. y: 'bottom',
  391. textAlign: 'left',
  392. },
  393. xAxis: {
  394. type: 'category',
  395. data: Xdata,
  396. axisLabel: {
  397. color: '#bb3137',
  398. fontSize: 16
  399. }
  400. },
  401. yAxis: {
  402. type: 'value',
  403. name: yAxisName,
  404. nameLocation: 'end',
  405. axisLabel: {
  406. show: true,
  407. interval: 0,
  408. formatter: function (value, index) {
  409. return value / 1000000
  410. }
  411. }
  412. },
  413. legend: {
  414. data: [
  415. {
  416. name: seriesName,
  417. icon: 'rect',
  418. }
  419. ],
  420. top: "0%"
  421. },
  422. tooltip: {
  423. trigger: 'axis',
  424. confine: true,
  425. formatter: function (param) {
  426. let relVal = param[0].name + '<br>'
  427. param.forEach((item, index) => {
  428. if (item.value && 'bar' == param[index].seriesType) {
  429. relVal += item.marker + ' ' + item.seriesName + ' : ' + item.value + unit + '</br>'
  430. } else if (item.value && 'line' == param[index].seriesType) {
  431. } else {
  432. relVal += item.marker + ' ' + item.seriesName + ' : - </br>'
  433. }
  434. })
  435. return relVal
  436. }
  437. },
  438. toolbox: {
  439. show: 1,
  440. itemSize: 24,
  441. x: 'right',
  442. y: 'top',
  443. feature: {
  444. saveAsImage: {
  445. show: true,
  446. title: '下载图片',
  447. type: 'png'
  448. }
  449. }
  450. },
  451. series: [
  452. {
  453. name: seriesName,
  454. type: 'bar',
  455. // stack: 'all',
  456. barMaxwidth: 35,
  457. barGap: '10%',
  458. color: {
  459. type: 'linear',
  460. x: 0,
  461. y: 1,
  462. colorStops: [
  463. {
  464. offset: 0,
  465. color: '#6633ff',
  466. },
  467. {
  468. offset: 0.35,
  469. color: '#6699ff'
  470. },
  471. {
  472. offset: 1,
  473. color: '#66ffff'
  474. }
  475. ]
  476. },
  477. label: {
  478. show: true,
  479. color: '#000000',
  480. position: 'top',
  481. formatter(p) {
  482. return (p.value / 1000000).toFixed(2)
  483. }
  484. },
  485. data: Ydata,
  486. emphasis: {
  487. itemStyle: {
  488. borderRadius: 30
  489. }
  490. },
  491. itemStyle: {
  492. borderRadius: [12, 12, 0, 0],
  493. }
  494. },
  495. {
  496. name: seriesName,
  497. type: 'line',
  498. data: Ydata,
  499. smooth: true,
  500. symbol: 'circle',
  501. symbolSize: 1,
  502. itemStyle: {
  503. lineStyle: {
  504. with: 3,
  505. color: '#27dba0'
  506. }
  507. },
  508. emphasis: {
  509. itemStyle: {
  510. color: '#4ccefe'
  511. }
  512. }
  513. }
  514. ]
  515. })
  516. window.onresize = function () {
  517. //自适应大小, 不用的话不会自适应大小。
  518. linearBarDom.resize()
  519. }
  520. }
  521. /**
  522. * 柱状图动态显示排名
  523. * @param {*} id
  524. * @param {*} name 主题
  525. * @param {*} months 月份
  526. * @param {*} data 数据
  527. * @param {*} num 排名前几
  528. * @param {*} unit 单位
  529. */
  530. export function barUtilsThree(id, name, months, data, num, unit) {
  531. let updateFrequency = 5000
  532. let dimension = 0
  533. // 预置柱状图颜色(35种)
  534. let barColors = [
  535. '#C1232B', '#B5C334', '#FCCE10', '#E87C25', '#27727B',
  536. '#FE8463', '#9BCA63', '#FAD860', '#F3A43B', '#60C0DD',
  537. '#D7504B', '#C6E579', '#F4E001', '#F0805A', '#26C0C0',
  538. '#ff7f50', '#87cefa', '#da70d6', '#32cd32', '#6495ed',
  539. '#ff69b4', '#ba55d3', '#cd5c5c', '#ffa500', '#40e0d0',
  540. '#1e90ff', '#ff6347', '#7b68ee', '#00fa9a', '#ffd700',
  541. '#6b8e23', '#ff00ff', '#3cb371', '#b8860b', '#30e0e0'
  542. ]
  543. // 验证 这个 是否被创建了
  544. let linearBarDom = echarts.getInstanceByDom(id)
  545. if (linearBarDom == null) {
  546. linearBarDom = echarts.init(id)
  547. }
  548. let startIndex = 1
  549. let startMoth = months[startIndex]
  550. let option = {
  551. title: {
  552. text: name,
  553. },
  554. grid: {
  555. top: '15%',
  556. bottom: '10%',
  557. left: '10%',
  558. right: '10%',
  559. containLabel: true
  560. },
  561. xAxis: {
  562. max: 'dataMax',
  563. type: 'value',
  564. axisLabel: {
  565. formatter: function (n) {
  566. return Math.round(n) + unit
  567. }
  568. },
  569. axisLine: {
  570. show: true,
  571. interval: 24,
  572. lineStyle: {
  573. color: '#000',
  574. }
  575. },
  576. },
  577. dataset: {
  578. source: data.filter(function (d) {
  579. return d[2] === startMoth;
  580. })
  581. },
  582. yAxis: {
  583. type: 'category',
  584. inverse: true,
  585. max: num,
  586. axisLabel: {
  587. show: true,
  588. fontSize: 15,
  589. formatter: function (value) {
  590. return value
  591. },
  592. },
  593. animationDuration: 300,
  594. animationDurationUpdate: 300,
  595. },
  596. series: [
  597. {
  598. realtimeSort: true,
  599. seriesLayoutBy: 'column',
  600. type: 'bar',
  601. itemStyle: {
  602. color: function (param) {
  603. return barColors[param.dataIndex] || '#5470C6'
  604. }
  605. },
  606. encode: {
  607. x: dimension,
  608. y: 3
  609. },
  610. // 这个是在柱状图中显示 数字
  611. label: {
  612. show: true,
  613. position: 'right',
  614. formatter: function (value) {
  615. return Math.ceil(value.value[0]) + unit
  616. },
  617. fontSize: 16,
  618. precision: 1,
  619. fontFamily: 'monospace',
  620. valueAnimation: true
  621. },
  622. },
  623. ],
  624. animationDuration: 0,
  625. animationDurationUpdate: updateFrequency,
  626. animationEasing: 'linear',
  627. animationEasingUpdate: 'linear',
  628. graphic: {
  629. elements: [
  630. {
  631. type: 'text',
  632. right: '10%',
  633. bottom: '15%',
  634. style: {
  635. text: startMoth,
  636. font: 'bolder 80px monospace',
  637. fill: 'rgba(100, 100, 100, 0.25)'
  638. },
  639. z: 100
  640. }
  641. ]
  642. }
  643. }
  644. linearBarDom.setOption(option)
  645. window.addEventListener("resize", function () {
  646. linearBarDom.resize();
  647. })
  648. for (var i = startIndex; i < months.length - 1; ++i) {
  649. (function (i) {
  650. setTimeout(function () {
  651. updateMonth(linearBarDom, option, data, months[i + 1]);
  652. }, (i - startIndex) * updateFrequency)
  653. })(i);
  654. }
  655. //option && linearBarDom.setOption(option)
  656. }
  657. function updateMonth(myChart, option, data, month) {
  658. let source = data.filter(function (d) {
  659. return d[2] === month
  660. })
  661. option.series[0].data = source
  662. option.graphic.elements[0].style.text = month
  663. myChart.setOption(option, true)
  664. window.addEventListener("resize", function () {
  665. myChart.resize();
  666. })
  667. }
  668. // 树状图
  669. export function treeChartRoot(id, name, data, nodes) {
  670. let linearBarDom = echarts.getInstanceByDom(id)
  671. if (linearBarDom == null) {
  672. linearBarDom = echarts.init(id)
  673. }
  674. // 解决重新加载树会残留上次数据线条的问题
  675. if (linearBarDom != null) {
  676. linearBarDom.setOption({}, true)
  677. }
  678. let option;
  679. option = {
  680. tooltip: {
  681. trigger: 'item',
  682. triggerOn: 'mousemove'
  683. },
  684. series: [
  685. {
  686. type: 'tree',
  687. id: 0,
  688. name: name,
  689. data: [data],
  690. top: '2%',
  691. left: '8%',
  692. bottom: '2%',
  693. right: '20%',
  694. layout: 'orthogonal',
  695. orient: 'LR',
  696. symbol: 'emptyCircle', // emptyCircle
  697. symbolSize: 7,
  698. edgeShape: 'polyline',
  699. edgeForkPosition: '63%',
  700. initialTreeDepth: 3,
  701. lineStyle: {
  702. width: 2
  703. },
  704. label: {
  705. backgroundColor: '#fff',
  706. position: 'left',
  707. verticalAlign: 'middle',
  708. align: 'right',
  709. formatter: function (params) {
  710. let flag = false
  711. for (let i = 0; i < nodes.length; i++) {
  712. if (nodes[i] != '' && params.name.match(nodes[i])) {
  713. flag = true
  714. }
  715. }
  716. if (flag) {
  717. return '{a|' + params.name + ': ' + params.value + '}'
  718. } else {
  719. return params.name + ': ' + params.value
  720. }
  721. },
  722. rich: {
  723. a: {
  724. color: 'red',
  725. backgroundColor: 'yellow'
  726. }
  727. }
  728. },
  729. leaves: {
  730. label: {
  731. position: 'right',
  732. verticalAlign: 'middle',
  733. align: 'left'
  734. }
  735. },
  736. emphasis: {
  737. focus: 'descendant'
  738. },
  739. expandAndCollapse: true,
  740. animationDuration: 550,
  741. animationDurationUpdate: 750
  742. }
  743. ]
  744. }
  745. //linearBarDom.clear()
  746. option && linearBarDom.setOption(option, true)
  747. linearBarDom.on('click', function (param) {
  748. if (param.data.collapsed === false) {
  749. param.data.collapsed = true
  750. } else {
  751. param.data.collapsed = false
  752. }
  753. callChangeCollapsed(linearBarDom, param, option)
  754. })
  755. window.addEventListener("resize", function () {
  756. linearBarDom.resize();
  757. })
  758. }
  759. function callChangeCollapsed(charts, param, option) {
  760. for (let i = 0; i < option.series[0].data[0].children.length; i++) {
  761. if (option.series[0].data[0].children[i].id === param.data.id && option.series[0].data[0].children[i].pid === param.data.pid) {
  762. if (option.series[0].data[0].children[i].collapsed != param.data.collapsed) {
  763. option.series[0].data[0].children[i].collapsed = param.data.collapsed
  764. charts.setOption({}, true)
  765. charts.setOption(option, true)
  766. } else {
  767. if (option.series[0].data[0].children[i].collapsed === false) {
  768. option.series[0].data[0].children[i].collapsed = true
  769. } else {
  770. option.series[0].data[0].children[i].collapsed = false
  771. }
  772. charts.setOption({}, true)
  773. charts.setOption(option, true)
  774. }
  775. return
  776. } else {
  777. if (option.series[0].data[0].children[i].children) {
  778. for (let j = 0; j < option.series[0].data[0].children[i].children.length; j++) {
  779. if (option.series[0].data[0].children[i].children[j].id === param.data.id && option.series[0].data[0].children[i].children[j].pid === param.data.pid) {
  780. if (option.series[0].data[0].children[i].children[j].collapsed != param.data.collapsed) {
  781. option.series[0].data[0].children[i].children[j].collapsed = param.data.collapsed
  782. charts.setOption({}, true)
  783. charts.setOption(option, true)
  784. } else {
  785. if (option.series[0].data[0].children[i].children[j].collapsed === false) {
  786. option.series[0].data[0].children[i].children[j].collapsed = true
  787. } else {
  788. option.series[0].data[0].children[i].children[j].collapsed = false
  789. }
  790. charts.setOption({}, true)
  791. charts.setOption(option, true)
  792. }
  793. return
  794. }
  795. }
  796. }
  797. }
  798. }
  799. }