chart.html 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234
  1. <!DOCTYPE html>
  2. <html lang="zh-CN" style="height: 100%">
  3. <head>
  4. <meta charset="utf-8">
  5. <meta name="viewport" content="width=device-width, initial-scale=1.0">
  6. <title>chart</title>
  7. </head>
  8. <style>
  9. *{
  10. margin: 0;
  11. padding: 0;
  12. box-sizing: border-box;
  13. }
  14. </style>
  15. <body style="height: 100%; margin: 0">
  16. <div id="container" style="height: 100%"></div>
  17. <script src="https://cdn.jsdelivr.net/npm/axios/dist/axios.min.js"></script>
  18. <script type="text/javascript" src="https://cdn.staticfile.org/echarts/5.4.2/echarts.min.js"></script>
  19. <script src="https://cdn.jsdelivr.net/npm/axios/dist/axios.min.js"></script>
  20. <script type="text/javascript">
  21. const timeList = [
  22. '开埠通商',
  23. '曲折发展',
  24. '步履维艰',
  25. '筚路蓝缕',
  26. '改革开放',
  27. '战略负重',
  28. '创新驱动',
  29. '追梦未来',
  30. ]
  31. function randomColor() {
  32. return '#' + Math.floor(
  33. (
  34. Math.random() * (1 - 0.3) + 0.3
  35. ) * 0xffffff
  36. ).toString(16)
  37. }
  38. const dataForRender = {}
  39. let myChart = null
  40. var dom = document.getElementById('container');
  41. myChart = echarts.init(dom, null, {
  42. renderer: 'canvas',
  43. useDirtyRect: false
  44. });
  45. async function showChart(timeIdx) {
  46. myChart.clear()
  47. const res = await axios({
  48. method: 'post',
  49. url: `https://sit-shgybwg.4dage.com/api/show/history/pageList`,
  50. headers: {
  51. "Content-Type": "application/json",
  52. },
  53. data: {
  54. stage: timeList[timeIdx]
  55. },
  56. })
  57. const corpList = res.data.data.records
  58. dataForRender.nodes = [
  59. {
  60. name: timeList[timeIdx],
  61. id: '-1',
  62. symbolSize: 100,
  63. itemStyle: {
  64. color: randomColor()
  65. },
  66. },
  67. ]
  68. dataForRender.edges = []
  69. for (const iterator of corpList) {
  70. const newNode = {
  71. name: iterator.name || iterator.companyName,
  72. id: iterator.id.toString(),
  73. symbolSize: iterator.importance || (100 * Math.random()),
  74. itemStyle: {
  75. color: randomColor()
  76. }
  77. }
  78. dataForRender.nodes.push(newNode)
  79. }
  80. // 除了代表时代的那个节点,其他节点的尺寸归一化
  81. let vMax = Number.NEGATIVE_INFINITY
  82. let vMin = Number.POSITIVE_INFINITY
  83. const vMaxNew = 50
  84. const vMinNew = 30
  85. for (let index = 1; index < dataForRender.nodes.length; index++) {
  86. const size = dataForRender.nodes[index].symbolSize
  87. if (size > vMax) {
  88. vMax = size
  89. }
  90. if (size < vMin) {
  91. vMin = size
  92. }
  93. }
  94. let scale = null
  95. if (vMax === vMin) {
  96. scale = 1
  97. for (const iterator of dataForRender.nodes.slice(1)) {
  98. iterator.symbolSize = vMinNew + (vMaxNew - vMinNew) / 2
  99. }
  100. } else {
  101. scale = (vMaxNew - vMinNew) / (vMax - vMin)
  102. for (const iterator of dataForRender.nodes.slice(1)) {
  103. iterator.symbolSize = vMinNew + (iterator.symbolSize - vMin) * scale
  104. }
  105. }
  106. // 画边
  107. for (let i = 0; i < dataForRender.nodes.length; i++) {
  108. for (let j = i + 1; j < dataForRender.nodes.length; j++) {
  109. const hasCenterNode = (i === 0 || j === 0)
  110. if (hasCenterNode) {
  111. const newEdge = {
  112. source: dataForRender.nodes[i].id,
  113. target: dataForRender.nodes[j].id,
  114. value: dataForRender.nodes[i].symbolSize * dataForRender.nodes[j].symbolSize * 10, // 值越大,连接的两个节点间斥力越小。
  115. }
  116. dataForRender.edges.push(newEdge)
  117. } else {
  118. const newEdge = {
  119. source: dataForRender.nodes[i].id,
  120. target: dataForRender.nodes[j].id,
  121. value: dataForRender.nodes[i].symbolSize * dataForRender.nodes[j].symbolSize * 1, // 值越大,连接的两个节点间斥力越小。
  122. lineStyle: {
  123. opacity: 0,
  124. }
  125. }
  126. dataForRender.edges.push(newEdge)
  127. }
  128. }
  129. }
  130. // console.log(dataForRender);
  131. myChart.setOption({
  132. animationDurationUpdate: 1500,
  133. animationEasingUpdate: 'quinticInOut',
  134. series: [
  135. {
  136. type: 'graph',
  137. layout: 'force',
  138. draggable: false,
  139. // 力引导布局是模拟弹簧电荷模型在每两个节点之间添加一个斥力,每条边的两个节点之间添加一个引力
  140. force: {
  141. initLayout: 'circular', // 进行力引导布局前的初始化布局,初始化布局会影响到力引导的效果。默认不进行任何布局,使用节点中提供的 x, y 作为节点的位置。如果不存在的话会随机生成一个位置。也可以选择使用环形布局 'circular'。
  142. repulsion: 100, // 节点之间的斥力因子。傻逼文档把edgeLength当数组用的用法写到这上边了。
  143. gravity: 0.1, // 节点受到的向中心的引力因子。该值越大节点越往中心点靠拢。
  144. edgeLength: [50, 400], // 把各个边的两个节点之间的距离归一化到这个范围内。与repulsion共同作用。
  145. layoutAnimation: true,
  146. friction: 0.1, // 这个参数能减缓节点的移动速度。取值范围 0 到 1。但是仍然是个试验性的参数,参见 #11024。
  147. },
  148. data: dataForRender.nodes,
  149. // 或者叫edges
  150. links: dataForRender.edges,
  151. selectedMode: 'single',
  152. select: {
  153. itemStyle: {
  154. shadowBlur: 50,
  155. shadowColor: 'rgba(255, 255, 125, 0.7)',
  156. },
  157. label: {
  158. position: 'right',
  159. show: true,
  160. color: '#fff',
  161. fontSize: '20px',
  162. fontWeight: 'bold',
  163. }
  164. },
  165. // 高亮状态的图形样式
  166. emphasis: {
  167. scale: false,
  168. label: {
  169. position: 'right',
  170. show: true,
  171. color: '#fff',
  172. fontSize: '20px',
  173. fontWidth: 'bold',
  174. }
  175. },
  176. // 图表是否可以移动、缩放
  177. roam: true,
  178. lineStyle: {
  179. width: 0.5,
  180. curveness: 0.3,
  181. opacity: 0.7,
  182. }
  183. }
  184. ]
  185. }, true)
  186. setTimeout(() => {
  187. // 等myChart上注册了select回调后再执行
  188. // 一开始自动选中表示时代的那个节点
  189. myChart.dispatchAction({
  190. type: 'select',
  191. seriesIndex: 0,
  192. name: timeList[timeIdx],
  193. })
  194. }, 0);
  195. }
  196. showChart(0)
  197. // 用户选中节点后,向父窗口post message
  198. function onSelect(params) {
  199. if (params.dataType === 'node') { // 用户选中节点触发的
  200. window.parent.postMessage(`node-selected: ${dataForRender.nodes[params.dataIndexInside].id}`, '*')
  201. } else if (params.type === 'select') { // 程序里调用dispatchAction触发的,且type为select
  202. window.parent.postMessage(`node-selected: ${dataForRender.nodes[0].id}`, '*')
  203. }
  204. }
  205. myChart.on('select', onSelect)
  206. window.addEventListener('resize', myChart.resize);
  207. // 切换时代
  208. window.changeTime = function (idx) {
  209. if (Number.isInteger(idx) && idx >= 0) {
  210. showChart(idx)
  211. } else {
  212. console.error('[page using echart] changeTime: invalid param!', idx);
  213. }
  214. }
  215. </script>
  216. </body>
  217. </html>