graph.html 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  1. <html>
  2. <head>
  3. <meta charset="utf-8">
  4. <script src="lib/esl.js"></script>
  5. <script src="lib/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',
  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, dataTool) {
  30. var gexf = dataTool.gexf;
  31. var chart = echarts.init(document.getElementById('main'), 'vintage', {
  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. aria: {
  59. show: true,
  60. description: 'Les Miserables 的关系主要分为六个区域,这张图描述了他们之间的相互关联。'
  61. },
  62. tooltip: {},
  63. legend: [{
  64. // selectedMode: 'single',
  65. data: categories.map(function (a) {
  66. return a.name;
  67. })
  68. }],
  69. animationDurationUpdate: 1500,
  70. animationEasingUpdate: 'quinticInOut',
  71. series : [
  72. {
  73. name: 'Les Miserables',
  74. type: 'graph',
  75. layout: 'none',
  76. data: graph.nodes,
  77. links: graph.links,
  78. categories: categories,
  79. roam: true,
  80. draggable: true,
  81. itemStyle: {
  82. normal: {
  83. borderColor: '#fff',
  84. borderWidth: 2,
  85. shadowBlur: 10,
  86. shadowColor: 'rgba(0, 0, 0, 0.3)'
  87. }
  88. },
  89. focusNodeAdjacency: true,
  90. // edgeSymbol: ['none', 'arrow'],
  91. // scaleLimit: {
  92. // min: 1.5,
  93. // max: 2
  94. // },
  95. label: {
  96. normal: {
  97. position: 'right',
  98. formatter: '{b}'
  99. }
  100. },
  101. lineStyle: {
  102. normal: {
  103. color: 'source',
  104. curveness: 0.3
  105. },
  106. emphasis: {
  107. width: 10
  108. }
  109. }
  110. }
  111. ]
  112. };
  113. chart.setOption(option);
  114. var config = {
  115. layout: 'none',
  116. focusNodeAdjacency: true,
  117. manualFocusNodeAdjacency: function () {
  118. chart.dispatchAction({
  119. type: 'focusNodeAdjacency',
  120. seriesName: 'Les Miserables',
  121. dataIndex: 2
  122. });
  123. },
  124. manualUnfocusNodeAdjacency: function () {
  125. chart.dispatchAction({
  126. type: 'unfocusNodeAdjacency',
  127. seriesName: 'Les Miserables'
  128. });
  129. },
  130. 'circular.rotateLabel': false
  131. };
  132. chart.on('click', function (params) {
  133. console.log(params, params.data);
  134. });
  135. var gui = new dat.GUI();
  136. gui.add(config, 'layout', ['none', 'circular'])
  137. .onChange(function (value) {
  138. chart.setOption({
  139. series: [{
  140. name: 'Les Miserables',
  141. layout: value
  142. }]
  143. });
  144. });
  145. gui.add(config, 'focusNodeAdjacency')
  146. .onChange(function (value) {
  147. chart.setOption({
  148. series: [{
  149. name: 'Les Miserables',
  150. focusNodeAdjacency: value
  151. }]
  152. });
  153. });
  154. gui.add(config, 'manualFocusNodeAdjacency');
  155. gui.add(config, 'manualUnfocusNodeAdjacency');
  156. gui.add(config, 'circular.rotateLabel')
  157. .onChange(function (value) {
  158. chart.setOption({
  159. series: [{
  160. name: 'Les Miserables',
  161. circular: {rotateLabel: !!value}
  162. }]
  163. });
  164. });
  165. });
  166. });
  167. </script>
  168. </body>
  169. </html>