index.js 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289
  1. function anonymous() {
  2. const urlAll = window.location.href
  3. const isMobile = window.innerWidth <= 500
  4. if (!urlAll.includes('?id=')) return alert('参数错误')
  5. const id = urlAll.split('?id=')[1]
  6. const obj = cardNames.find(v => v.id == id)
  7. if (!obj) return alert('参数错误')
  8. let dom = document.getElementById('container')
  9. let myChart = echarts.init(dom, null, {
  10. renderer: 'canvas',
  11. useDirtyRect: false
  12. })
  13. let option
  14. const data1 = []
  15. const data2 = []
  16. if (obj.son) {
  17. const ids = []
  18. obj.son.forEach((c1, i) => {
  19. c1.forEach(c2 => {
  20. data2.push({
  21. source: c2.source + '',
  22. target: c2.target + '',
  23. value: c2.value
  24. })
  25. if (!ids.map(a => a.id).includes(c2.source + '')) {
  26. ids.push({
  27. id: c2.source + '',
  28. symbolSize: 200 - (i + 1) * 50,
  29. tit: i === obj.son.length - 1,
  30. value: c2.value
  31. })
  32. }
  33. if (!ids.map(a => a.id).includes(c2.target + '')) {
  34. ids.push({
  35. id: c2.target + '',
  36. symbolSize: 200 - (i + 1) * 50,
  37. tit: i === obj.son.length - 1,
  38. value: c2.value
  39. })
  40. }
  41. })
  42. })
  43. ids.forEach(v => {
  44. const isCenter = id == v.id
  45. const centerX = window.innerWidth / 2 // 中心坐标x
  46. const centerY = window.innerHeight / 2 // 中心坐标y
  47. const obj = cardNames.find(c => c.id == v.id)
  48. data1.push({
  49. fixed: isCenter, // 固定中心节点位置
  50. x: isCenter ? centerX : null, // 设置中心节点坐标
  51. y: isCenter ? centerY : null,
  52. // category: id == v.id ? '中心' : '',
  53. symbolSize: id == v.id ? 200 : v.symbolSize,
  54. id: v.id,
  55. name: obj.name,
  56. symbol: `image://../../three/assets/out/${v.id}.png`
  57. // tit:v.tit
  58. })
  59. })
  60. }
  61. option = {
  62. tooltip: {
  63. show: true, // 默认显示
  64. showContent: true, // 是否显示提示框浮层
  65. trigger: 'item', // 触发类型,默认数据项触发
  66. triggerOn: 'mousemove', // 提示触发条件,mousemove鼠标移至触发,还有click点击触发
  67. alwaysShowContent: false, // 默认离开提示框区域隐藏,true为一直显示
  68. showDelay: 100, // 浮层显示的延迟,单位为 ms,默认没有延迟,也不建议设置。在 triggerOn 为 'mousemove' 时有效。
  69. hideDelay: 2000, // 浮层隐藏的延迟,单位为 ms,在 alwaysShowContent 为 true 的时候无效。
  70. enterable: false, // 鼠标是否可进入提示框浮层中,默认为false,如需详情内交互,如添加链接,按钮,可设置为 true。
  71. position: 'right', // 提示框浮层的位置,默认不设置时位置会跟随鼠标的位置。只在 trigger 为'item'的时候有效。
  72. confine: true, // 是否将 tooltip 框限制在图表的区域内。
  73. // 外层的 dom 被设置为 'overflow: hidden',或者移动端窄屏,导致 tooltip 超出外界被截断时,此配置比较有用。
  74. transitionDuration: 0.2, // 提示框浮层的移动动画过渡时间,单位是秒,设置为 0 的时候会紧跟着鼠标移动。
  75. formatter: function (params) {
  76. // 判断当前节点是否为第三级节点
  77. // 假设层级信息可以通过 params.data.tit 获取,或者通过其他逻辑判断
  78. if (params.data.tit) {
  79. // 如果是第三级节点,返回自定义的悬浮框内容
  80. return params.name
  81. } else {
  82. // 如果不是第三级节点,返回默认的悬浮框内容或不显示
  83. return '' // 不显示悬浮框
  84. }
  85. },
  86. // 自定义提示框的样式
  87. borderColor: '#896a43',
  88. backgroundColor: 'rgba(0,0,0,0.5)',
  89. textStyle: {
  90. color: '#dfd5c5',
  91. fontFamily: 'sk'
  92. },
  93. blur: {
  94. itemStyle: {
  95. opacity: 0.2 // 非突出节点的透明度
  96. },
  97. label: {
  98. show: false // 非突出节点隐藏标签
  99. }
  100. }
  101. },
  102. series: [
  103. {
  104. type: 'graph', // 关系图
  105. symbolKeepAspect: true, // 保持图片宽高比
  106. // name: "Name:", // 系列名称,用于tooltip的显示,legend 的图例筛选,在 setOption 更新数据和配置项时用于指定对应的系列。
  107. layout: 'force', // 图的布局,类型为力导图,'circular' 采用环形布局,见示例 Les Miserables
  108. legendHoverLink: true, // 是否启用图例 hover(悬停) 时的联动高亮。
  109. hoverAnimation: false, // 是否开启鼠标悬停节点的显示动画
  110. coordinateSystem: null, // 坐标系可选
  111. // xAxisIndex: 0, // x轴坐标 有多种坐标系轴坐标选项
  112. // yAxisIndex: 0, // y轴坐标
  113. force: {
  114. // 力引导图基本配置
  115. // initLayout: 'circular',
  116. // initLayout: , // 力引导的初始化布局,默认使用xy轴的标点
  117. repulsion: 600, //节点之间的斥力因子。支持数组表达斥力范围,值越大斥力越大。
  118. gravity: 0.01, //节点受到的向中心的引力因子。该值越大节点越往中心点靠拢。
  119. edgeLength: 240, //边的两个节点之间的距离,这个距离也会受 repulsion。[10, 50] 。值越小则长度越长
  120. layoutAnimation: true, // 因为力引导布局会在多次迭代后才会稳定,这个参数决定是否显示布局的迭代动画
  121. // 在浏览器端节点数据较多(>100)的时候不建议关闭,布局过程会造成浏览器假死。
  122. // 防止节点重叠
  123. friction: 0.8, // 增加摩擦力
  124. // 增加防止重叠的参数
  125. nodeOverlap: 20, // 防止节点重叠的参数
  126. // 优化布局迭代
  127. coolingFactor: 0.99, // 冷却因子,使布局逐渐稳定
  128. minMovement: 0.5, // 最小移动距离
  129. maxIteration: 2000 // 增加最大迭代次数
  130. },
  131. roam: true, // 是否开启鼠标缩放和平移漫游。默认不开启,true 为都开启。如果只想要开启缩放或者平移,可以设置成 'scale' 或者 'move'
  132. // zoom: 1,
  133. scaleLimit: {
  134. // 缩放限制,只在使用 force 布局时有效。
  135. min: isMobile ? 0.5 : 0.8, // 最小缩放比例
  136. max: isMobile ? 2 : 3 // 最大缩放比例
  137. },
  138. nodeScaleRatio: 0.6, // 鼠标漫游缩放时节点的相应缩放比例,当设为0时节点不随着鼠标的缩放而缩放
  139. draggable: true, // 节点是否可拖拽,只在使用力引导布局的时候有用。
  140. focusNodeAdjacency: !window.isMobile ? true : false, // 是否在鼠标移到节点上的时候突出显示节点以及节点的边和邻接节点。
  141. emphasis: {
  142. itemStyle: {
  143. opacity: 1 // 突出节点的透明度
  144. }
  145. },
  146. blur: {
  147. itemStyle: {
  148. opacity: 0.3 // 非突出节点的透明度
  149. },
  150. lineStyle: {
  151. opacity: 0.3 // 非突出节点的透明度
  152. },
  153. label: {
  154. opacity: 0.3 // 非突出节点的透明度
  155. }
  156. },
  157. // symbol:'roundRect', // 关系图节点标记的图形。
  158. // ECharts 提供的标记类型包括:'circle'(圆形), 'rect'(矩形), 'roundRect'(圆角矩形),
  159. // 'triangle'(三角形), 'diamond'(菱形), 'pin'(大头针), 'arrow'(箭头)
  160. // 也可以通过 'image://url' 设置为图片,其中 url 为图片的链接。'path:// 这种方式可以任意改变颜色并且抗锯齿
  161. // symbolSize: 10 , // 也可以用数组分开表示宽和高,例如 [20, 10] 如果需要每个数据的图形大小不一样,
  162. // 可以设置为如下格式的回调函数:(value: Array|number, params: Object) => number|Array
  163. // symbolRotate:, // 关系图节点标记的旋转角度。注意在 markLine 中当 symbol 为 'arrow' 时会忽略 symbolRotate 强制设置为切线的角度。
  164. // symbolOffset:[0,0], // 关系图节点标记相对于原本位置的偏移。[0, '50%']
  165. edgeSymbol: ['circle', 'circle'], // 边两端的标记类型,可以是一个数组分别指定两端,也可以是单个统一指定。
  166. // 默认不显示标记,常见的可以设置为箭头,如下:edgeSymbol: ['circle', 'arrow']
  167. edgeSymbolSize: [0, 10], // 边两端的标记大小,可以是一个数组分别指定两端,也可以是单个统一指定。
  168. symbolSize: 120, // 关系图节点标记的大小,可以是一个数组分别指定两端,也可以是单个统一指定。
  169. itemStyle: {
  170. // ========图形样式,有 normal 和 emphasis 两个状态。
  171. // normal 是图形在默认状态下的样式;
  172. // emphasis 是图形在高亮状态下的样式,比如在鼠标悬浮或者图例联动高亮时。
  173. normal: {
  174. // 默认样式
  175. label: {
  176. show: true
  177. },
  178. borderType: 'solid', // 图形描边类型,默认为实线,支持 'solid'(实线), 'dashed'(虚线), 'dotted'(点线)。
  179. borderColor: '#fad166', // 设置图形边框为淡金色,透明度为0.4
  180. borderWidth: 2, // 图形的描边线宽。为 0 时无描边。
  181. opacity: 1 // 图形透明度。支持从 0 到 1 的数字,为 0 时不绘制该图形。默认0.5
  182. },
  183. emphasis: {
  184. // 高亮状态
  185. // lineStyle: {
  186. // width: 10,
  187. // },
  188. }
  189. },
  190. lineStyle: {
  191. // ========关系边的公用线条样式。
  192. normal: {
  193. color: '#fad166',
  194. width: '1', //线的粗细
  195. type: 'dashed', // 线的类型 'solid'(实线)'dashed'(虚线)'dotted'(点线)
  196. curveness: 0.3, // 线条的曲线程度,从0到1
  197. opacity: 0.5 // 图形透明度。支持从 0 到 1 的数字,为 0 时不绘制该图形。默认0.5
  198. },
  199. emphasis: {
  200. // 高亮状态
  201. }
  202. },
  203. label: {
  204. // ========结点图形上的文本标签
  205. normal: {
  206. show: true, // 是否显示标签。
  207. position: 'bottom', // 标签的位置。['50%', '50%'] [x,y]
  208. textStyle: {
  209. // 标签的字体样式
  210. color: '#fad166', // 字体颜色
  211. fontStyle: 'normal', // 文字字体的风格 'normal'标准 'italic'斜体 'oblique' 倾斜
  212. fontWeight: 'normal', // 'normal'标准,'bold'粗的,'bolder'更粗的,'lighter'更细的,或100 | 200 | 300 | 400...
  213. fontFamily: 'sk', // 文字的字体系列
  214. fontSize: 16
  215. }
  216. }
  217. },
  218. edgeLabel: {
  219. // ========连接线上的文本标签
  220. normal: {
  221. show: true, // 不显示连接线上的文字,如果显示只能显示结点的value值,而不是连接线的值
  222. // position: "middle", // 标签的位置。['50%', '50%'] [x,y]
  223. textStyle: {
  224. color: '#f4e6bf',
  225. fontSize: 16
  226. },
  227. formatter: '{c}'
  228. }
  229. },
  230. data: data1,
  231. // 设置连线edges的数据,
  232. links: data2, // 设置连线edges的数据
  233. // 添加节点布局优化
  234. circular: {
  235. rotateLabel: false
  236. },
  237. // 优化节点间距
  238. categories: [
  239. {
  240. name: '中心',
  241. itemStyle: {
  242. color: '#ff6b6b'
  243. }
  244. }
  245. ]
  246. }
  247. ]
  248. }
  249. if (myChart) {
  250. if (option && typeof option === 'object') {
  251. setTimeout(function () {
  252. myChart.setOption(option)
  253. }, 500)
  254. }
  255. } else {
  256. console.error('ECharts 初始化失败')
  257. }
  258. // 点击节点跳转链接
  259. myChart.on('click', function (params, e) {
  260. // if (params.componentType === "series" && params.dataType === "node") {
  261. // const nodeData = params.data;
  262. // openDetail(nodeData.idinfo, e);
  263. // }
  264. })
  265. window.addEventListener('resize', myChart.resize)
  266. }
  267. anonymous()