graph.html 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  1. <html>
  2. <head>
  3. <meta charset="utf-8">
  4. <script src="esl.js"></script>
  5. <script src="config.js"></script>
  6. <script src="lib/jquery.min.js"></script>
  7. <script src="lib/dat.gui.min.js"></script>
  8. </head>
  9. <body>
  10. <style>
  11. html, body, #main {
  12. width: 100%;
  13. height: 100%;
  14. margin: 0;
  15. }
  16. </style>
  17. <div id="main"></div>
  18. <script>
  19. require([
  20. 'echarts',
  21. 'extension/dataTool/gexf',
  22. 'echarts/chart/graph',
  23. 'echarts/component/title',
  24. 'echarts/component/legend',
  25. 'echarts/component/geo',
  26. 'echarts/component/tooltip',
  27. 'echarts/component/visualMap',
  28. 'theme/vintage'
  29. ], function (echarts, gexf) {
  30. var chart = echarts.init(document.getElementById('main'), 'vintage', {
  31. renderer: 'canvas'
  32. });
  33. $.get('./data/les-miserables.gexf', function (xml) {
  34. var graph = gexf.parse(xml);
  35. var categories = [];
  36. for (var i = 0; i < 9; i++) {
  37. categories[i] = {
  38. name: '类目' + i
  39. };
  40. }
  41. graph.nodes.forEach(function (node) {
  42. delete node.itemStyle;
  43. node.value = node.symbolSize;
  44. node.label = {
  45. normal: {
  46. show: node.symbolSize > 30
  47. },
  48. emphasis: {
  49. show: true
  50. }
  51. };
  52. node.category = node.attributes['modularity_class'];
  53. });
  54. graph.links.forEach(function (link) {
  55. delete link.lineStyle;
  56. });
  57. var option = {
  58. tooltip: {},
  59. legend: [{
  60. // selectedMode: 'single',
  61. data: categories.map(function (a) {
  62. return a.name;
  63. })
  64. }],
  65. animationDurationUpdate: 1500,
  66. animationEasingUpdate: 'quinticInOut',
  67. series : [
  68. {
  69. name: 'Les Miserables',
  70. type: 'graph',
  71. layout: 'none',
  72. data: graph.nodes,
  73. links: graph.links,
  74. categories: categories,
  75. roam: true,
  76. draggable: true,
  77. itemStyle: {
  78. normal: {
  79. borderColor: '#fff',
  80. borderWidth: 2,
  81. shadowBlur: 10,
  82. shadowColor: 'rgba(0, 0, 0, 0.3)'
  83. }
  84. },
  85. focusNodeAdjacency: true,
  86. // edgeSymbol: ['none', 'arrow'],
  87. // scaleLimit: {
  88. // min: 1.5,
  89. // max: 2
  90. // },
  91. label: {
  92. normal: {
  93. position: 'right',
  94. formatter: '{b}'
  95. }
  96. },
  97. lineStyle: {
  98. normal: {
  99. color: 'source',
  100. curveness: 0.3
  101. }
  102. }
  103. }
  104. ]
  105. };
  106. chart.setOption(option);
  107. var config = {
  108. layout: 'none',
  109. focusNodeAdjacency: true,
  110. manualFocusNodeAdjacency: function () {
  111. chart.dispatchAction({
  112. type: 'focusNodeAdjacency',
  113. seriesName: 'Les Miserables',
  114. dataIndex: 2
  115. });
  116. },
  117. manualUnfocusNodeAdjacency: function () {
  118. chart.dispatchAction({
  119. type: 'unfocusNodeAdjacency',
  120. seriesName: 'Les Miserables'
  121. });
  122. }
  123. };
  124. chart.on('click', function (params) {
  125. console.log(params, params.data);
  126. });
  127. var gui = new dat.GUI();
  128. gui.add(config, 'layout', ['none', 'circular'])
  129. .onChange(function (value) {
  130. chart.setOption({
  131. series: [{
  132. name: 'Les Miserables',
  133. layout: value
  134. }]
  135. });
  136. });
  137. gui.add(config, 'focusNodeAdjacency')
  138. .onChange(function (value) {
  139. chart.setOption({
  140. series: [{
  141. name: 'Les Miserables',
  142. focusNodeAdjacency: value
  143. }]
  144. });
  145. });
  146. gui.add(config, 'manualFocusNodeAdjacency');
  147. gui.add(config, 'manualUnfocusNodeAdjacency');
  148. });
  149. });
  150. </script>
  151. </body>
  152. </html>