circle-packing-with-d3.html 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. <!DOCTYPE html>
  2. <meta charset="utf-8">
  3. <style>
  4. text {
  5. font: 10px sans-serif;
  6. text-anchor: middle;
  7. }
  8. .node--hover circle {
  9. stroke: #000;
  10. stroke-width: 1.2px;
  11. }
  12. #main {
  13. width: 1000px;
  14. height: 500px;
  15. }
  16. </style>
  17. <div id="main" style="width:960px; height:960px;"></div>
  18. <svg width="960" height="960"><g transform="translate(1,1)"></g></svg>
  19. <script src="https://d3js.org/d3.v4.min.js"></script>
  20. <script src="../dist/echarts.js"></script>
  21. <script>
  22. var stratify = d3.stratify()
  23. .parentId(function(d) {
  24. return d.id.substring(0, d.id.lastIndexOf("."));
  25. });
  26. d3.csv("data/flare.csv", function(error, rawData) {
  27. if (error) throw error;
  28. var root = stratify(rawData)
  29. .sum(function(d) {
  30. return d.value;
  31. })
  32. .sort(function(a, b) {
  33. return b.value - a.value;
  34. });
  35. var maxDepth = 0;
  36. var seriesData = root.descendants().map(function (node) {
  37. maxDepth = Math.max(maxDepth, node.depth);
  38. return [
  39. node.value,
  40. node.depth,
  41. node.id
  42. ];
  43. });
  44. var chart = echarts.init(document.getElementById('main'));
  45. function renderItem(params, api) {
  46. var context = params.context;
  47. if (!context.layout) {
  48. d3.pack()
  49. .size([api.getWidth() - 2, api.getHeight() - 2])
  50. .padding(3)(root);
  51. context.layout = {};
  52. root.descendants().forEach(function (node) {
  53. context.layout[node.id] = {
  54. x: node.x,
  55. y: node.y,
  56. r: node.r,
  57. isLeaf: !node.children || !node.children.length
  58. };
  59. });
  60. }
  61. var nodePath = api.value(2);
  62. var itemLayout = context.layout[nodePath];
  63. var nodeName = '';
  64. var textFont = api.font({
  65. fontSize: 12,
  66. fontFamily: 'Arial'
  67. });
  68. if (itemLayout.isLeaf && itemLayout.r > 10) {
  69. nodeName = nodePath.slice(nodePath.lastIndexOf('.') + 1).split(/(?=[A-Z][^A-Z])/g).join('\n');
  70. nodeName = echarts.format.truncateText(nodeName, itemLayout.r, textFont, '.');
  71. }
  72. return {
  73. type: 'circle',
  74. shape: {
  75. cx: itemLayout.x,
  76. cy: itemLayout.y,
  77. r: itemLayout.r
  78. },
  79. z2: api.value(1) * 2,
  80. style: api.style({
  81. text: nodeName,
  82. textFont: textFont,
  83. textPosition: 'inside'
  84. }),
  85. styleEmphasis: api.style({
  86. text: nodeName,
  87. textPosition: 'inside',
  88. textFont: textFont,
  89. stroke: 'rgba(0,0,0,0.5)',
  90. lineWidth: 3
  91. })
  92. };
  93. }
  94. var option = {
  95. xAxis: {
  96. axisLine: {show: false},
  97. axisTick: {show: false},
  98. axisLabel: {show: false},
  99. splitLine: {show: false}
  100. },
  101. yAxis: {
  102. axisLine: {show: false},
  103. axisTick: {show: false},
  104. axisLabel: {show: false},
  105. splitLine: {show: false}
  106. },
  107. tooltip: {},
  108. visualMap: {
  109. show: false,
  110. min: 0,
  111. max: maxDepth,
  112. dimension: 1,
  113. inRange: {
  114. color: ['#006edd', '#e0ffff']
  115. }
  116. },
  117. series: {
  118. type: 'custom',
  119. renderItem: renderItem,
  120. encode: {
  121. tooltip: 0,
  122. itemName: 2
  123. },
  124. data: seriesData
  125. }
  126. };
  127. chart.setOption(option);
  128. });
  129. </script>