概要 实例 介绍 源码

矩阵

源文件:index.html

  1. <!DOCTYPE html>
  2. <style>
  3. form {
  4. font-family: "Helvetica Neue", Helvetica, Arial, sans-serif;
  5. }
  6. svg {
  7. font: 10px sans-serif;
  8. }
  9. </style>
  10. <svg width="960" height="570"></svg>
  11. <form>
  12. <label><input type="radio" name="mode" value="sumBySize" checked> Size</label>
  13. <label><input type="radio" name="mode" value="sumByCount"> Count</label>
  14. </form>
  15. <script src="https://d3js.org/d3.v4.min.js"></script>
  16. <script>
  17. var svg = d3.select("svg"),
  18. width = +svg.attr("width"),
  19. height = +svg.attr("height");
  20. var fader = function(color) { return d3.interpolateRgb(color, "#fff")(0.2); },
  21. color = d3.scaleOrdinal(d3.schemeCategory20.map(fader)),
  22. format = d3.format(",d");
  23. var treemap = d3.treemap()
  24. .tile(d3.treemapResquarify)
  25. .size([width, height])
  26. .round(true)
  27. .paddingInner(1);
  28. d3.json("flare.json", function(error, data) {
  29. if (error) throw error;
  30. var root = d3.hierarchy(data)
  31. .eachBefore(function(d) { d.data.id = (d.parent ? d.parent.data.id + "." : "") + d.data.name; })
  32. .sum(sumBySize)
  33. .sort(function(a, b) { return b.height - a.height || b.value - a.value; });
  34. treemap(root);
  35. var cell = svg.selectAll("g")
  36. .data(root.leaves())
  37. .enter().append("g")
  38. .attr("transform", function(d) { return "translate(" + d.x0 + "," + d.y0 + ")"; });
  39. cell.append("rect")
  40. .attr("id", function(d) { return d.data.id; })
  41. .attr("width", function(d) { return d.x1 - d.x0; })
  42. .attr("height", function(d) { return d.y1 - d.y0; })
  43. .attr("fill", function(d) { return color(d.parent.data.id); });
  44. cell.append("clipPath")
  45. .attr("id", function(d) { return "clip-" + d.data.id; })
  46. .append("use")
  47. .attr("xlink:href", function(d) { return "#" + d.data.id; });
  48. cell.append("text")
  49. .attr("clip-path", function(d) { return "url(#clip-" + d.data.id + ")"; })
  50. .selectAll("tspan")
  51. .data(function(d) { return d.data.name.split(/(?=[A-Z][^A-Z])/g); })
  52. .enter().append("tspan")
  53. .attr("x", 4)
  54. .attr("y", function(d, i) { return 13 + i * 10; })
  55. .text(function(d) { return d; });
  56. cell.append("title")
  57. .text(function(d) { return d.data.id + "\n" + format(d.value); });
  58. d3.selectAll("input")
  59. .data([sumBySize, sumByCount], function(d) { return d ? d.name : this.value; })
  60. .on("change", changed);
  61. var timeout = d3.timeout(function() {
  62. d3.select("input[value=\"sumByCount\"]")
  63. .property("checked", true)
  64. .dispatch("change");
  65. }, 2000);
  66. function changed(sum) {
  67. timeout.stop();
  68. treemap(root.sum(sum));
  69. cell.transition()
  70. .duration(750)
  71. .attr("transform", function(d) { return "translate(" + d.x0 + "," + d.y0 + ")"; })
  72. .select("rect")
  73. .attr("width", function(d) { return d.x1 - d.x0; })
  74. .attr("height", function(d) { return d.y1 - d.y0; });
  75. }
  76. });
  77. function sumByCount(d) {
  78. return d.children ? 0 : 1;
  79. }
  80. function sumBySize(d) {
  81. return d.size;
  82. }
  83. </script>

源文件:flare.json

  1. {
  2. "name": "flare",
  3. "children": [
  4. {
  5. "name": "analytics",
  6. "children": [
  7. {
  8. "name": "cluster",
  9. "children": [
  10. {"name": "AgglomerativeCluster", "size": 3938},
  11. {"name": "CommunityStructure", "size": 3812},
  12. {"name": "HierarchicalCluster", "size": 6714},
  13. {"name": "MergeEdge", "size": 743}
  14. ]
  15. },
  16. {
  17. "name": "graph",
  18. "children": [
  19. {"name": "BetweennessCentrality", "size": 3534},
  20. {"name": "LinkDistance", "size": 5731},
  21. {"name": "MaxFlowMinCut", "size": 7840},
  22. {"name": "ShortestPaths", "size": 5914},
  23. {"name": "SpanningTree", "size": 3416}
  24. ]
  25. },
  26. {
  27. "name": "optimization",
  28. "children": [
  29. {"name": "AspectRatioBanker", "size": 7074}
  30. ]
  31. }
  32. ]
  33. },
  34. {
  35. "name": "animate",
  36. "children": [
  37. {"name": "Easing", "size": 17010},
  38. {"name": "FunctionSequence", "size": 5842},
  39. {
  40. "name": "interpolate",
  41. "children": [
  42. {"name": "ArrayInterpolator", "size": 1983},
  43. {"name": "ColorInterpolator", "size": 2047},
  44. {"name": "DateInterpolator", "size": 1375},
  45. {"name": "Interpolator", "size": 8746},
  46. {"name": "MatrixInterpolator", "size": 2202},
  47. {"name": "NumberInterpolator", "size": 1382},
  48. {"name": "ObjectInterpolator", "size": 1629},
  49. {"name": "PointInterpolator", "size": 1675},
  50. {"name": "RectangleInterpolator", "size": 2042}
  51. ]
  52. },
  53. {"name": "ISchedulable", "size": 1041},
  54. {"name": "Parallel", "size": 5176},
  55. {"name": "Pause", "size": 449},
  56. {"name": "Scheduler", "size": 5593},
  57. {"name": "Sequence", "size": 5534},
  58. {"name": "Transition", "size": 9201},
  59. {"name": "Transitioner", "size": 19975},
  60. {"name": "TransitionEvent", "size": 1116},
  61. {"name": "Tween", "size": 6006}
  62. ]
  63. },
  64. {
  65. "name": "data",
  66. "children": [
  67. {
  68. "name": "converters",
  69. "children": [
  70. {"name": "Converters", "size": 721},
  71. {"name": "DelimitedTextConverter", "size": 4294},
  72. {"name": "GraphMLConverter", "size": 9800},
  73. {"name": "IDataConverter", "size": 1314},
  74. {"name": "JSONConverter", "size": 2220}
  75. ]
  76. },
  77. {"name": "DataField", "size": 1759},
  78. {"name": "DataSchema", "size": 2165},
  79. {"name": "DataSet", "size": 586},
  80. {"name": "DataSource", "size": 3331},
  81. {"name": "DataTable", "size": 772},
  82. {"name": "DataUtil", "size": 3322}
  83. ]
  84. },
  85. {
  86. "name": "display",
  87. "children": [
  88. {"name": "DirtySprite", "size": 8833},
  89. {"name": "LineSprite", "size": 1732},
  90. {"name": "RectSprite", "size": 3623},
  91. {"name": "TextSprite", "size": 10066}
  92. ]
  93. },
  94. {
  95. "name": "flex",
  96. "children": [
  97. {"name": "FlareVis", "size": 4116}
  98. ]
  99. },
  100. {
  101. "name": "physics",
  102. "children": [
  103. {"name": "DragForce", "size": 1082},
  104. {"name": "GravityForce", "size": 1336},
  105. {"name": "IForce", "size": 319},
  106. {"name": "NBodyForce", "size": 10498},
  107. {"name": "Particle", "size": 2822},
  108. {"name": "Simulation", "size": 9983},
  109. {"name": "Spring", "size": 2213},
  110. {"name": "SpringForce", "size": 1681}
  111. ]
  112. },
  113. {
  114. "name": "query",
  115. "children": [
  116. {"name": "AggregateExpression", "size": 1616},
  117. {"name": "And", "size": 1027},
  118. {"name": "Arithmetic", "size": 3891},
  119. {"name": "Average", "size": 891},
  120. {"name": "BinaryExpression", "size": 2893},
  121. {"name": "Comparison", "size": 5103},
  122. {"name": "CompositeExpression", "size": 3677},
  123. {"name": "Count", "size": 781},
  124. {"name": "DateUtil", "size": 4141},
  125. {"name": "Distinct", "size": 933},
  126. {"name": "Expression", "size": 5130},
  127. {"name": "ExpressionIterator", "size": 3617},
  128. {"name": "Fn", "size": 3240},
  129. {"name": "If", "size": 2732},
  130. {"name": "IsA", "size": 2039},
  131. {"name": "Literal", "size": 1214},
  132. {"name": "Match", "size": 3748},
  133. {"name": "Maximum", "size": 843},
  134. {
  135. "name": "methods",
  136. "children": [
  137. {"name": "add", "size": 593},
  138. {"name": "and", "size": 330},
  139. {"name": "average", "size": 287},
  140. {"name": "count", "size": 277},
  141. {"name": "distinct", "size": 292},
  142. {"name": "div", "size": 595},
  143. {"name": "eq", "size": 594},
  144. {"name": "fn", "size": 460},
  145. {"name": "gt", "size": 603},
  146. {"name": "gte", "size": 625},
  147. {"name": "iff", "size": 748},
  148. {"name": "isa", "size": 461},
  149. {"name": "lt", "size": 597},
  150. {"name": "lte", "size": 619},
  151. {"name": "max", "size": 283},
  152. {"name": "min", "size": 283},
  153. {"name": "mod", "size": 591},
  154. {"name": "mul", "size": 603},
  155. {"name": "neq", "size": 599},
  156. {"name": "not", "size": 386},
  157. {"name": "or", "size": 323},
  158. {"name": "orderby", "size": 307},
  159. {"name": "range", "size": 772},
  160. {"name": "select", "size": 296},
  161. {"name": "stddev", "size": 363},
  162. {"name": "sub", "size": 600},
  163. {"name": "sum", "size": 280},
  164. {"name": "update", "size": 307},
  165. {"name": "variance", "size": 335},
  166. {"name": "where", "size": 299},
  167. {"name": "xor", "size": 354},
  168. {"name": "_", "size": 264}
  169. ]
  170. },
  171. {"name": "Minimum", "size": 843},
  172. {"name": "Not", "size": 1554},
  173. {"name": "Or", "size": 970},
  174. {"name": "Query", "size": 13896},
  175. {"name": "Range", "size": 1594},
  176. {"name": "StringUtil", "size": 4130},
  177. {"name": "Sum", "size": 791},
  178. {"name": "Variable", "size": 1124},
  179. {"name": "Variance", "size": 1876},
  180. {"name": "Xor", "size": 1101}
  181. ]
  182. },
  183. {
  184. "name": "scale",
  185. "children": [
  186. {"name": "IScaleMap", "size": 2105},
  187. {"name": "LinearScale", "size": 1316},
  188. {"name": "LogScale", "size": 3151},
  189. {"name": "OrdinalScale", "size": 3770},
  190. {"name": "QuantileScale", "size": 2435},
  191. {"name": "QuantitativeScale", "size": 4839},
  192. {"name": "RootScale", "size": 1756},
  193. {"name": "Scale", "size": 4268},
  194. {"name": "ScaleType", "size": 1821},
  195. {"name": "TimeScale", "size": 5833}
  196. ]
  197. },
  198. {
  199. "name": "util",
  200. "children": [
  201. {"name": "Arrays", "size": 8258},
  202. {"name": "Colors", "size": 10001},
  203. {"name": "Dates", "size": 8217},
  204. {"name": "Displays", "size": 12555},
  205. {"name": "Filter", "size": 2324},
  206. {"name": "Geometry", "size": 10993},
  207. {
  208. "name": "heap",
  209. "children": [
  210. {"name": "FibonacciHeap", "size": 9354},
  211. {"name": "HeapNode", "size": 1233}
  212. ]
  213. },
  214. {"name": "IEvaluable", "size": 335},
  215. {"name": "IPredicate", "size": 383},
  216. {"name": "IValueProxy", "size": 874},
  217. {
  218. "name": "math",
  219. "children": [
  220. {"name": "DenseMatrix", "size": 3165},
  221. {"name": "IMatrix", "size": 2815},
  222. {"name": "SparseMatrix", "size": 3366}
  223. ]
  224. },
  225. {"name": "Maths", "size": 17705},
  226. {"name": "Orientation", "size": 1486},
  227. {
  228. "name": "palette",
  229. "children": [
  230. {"name": "ColorPalette", "size": 6367},
  231. {"name": "Palette", "size": 1229},
  232. {"name": "ShapePalette", "size": 2059},
  233. {"name": "SizePalette", "size": 2291}
  234. ]
  235. },
  236. {"name": "Property", "size": 5559},
  237. {"name": "Shapes", "size": 19118},
  238. {"name": "Sort", "size": 6887},
  239. {"name": "Stats", "size": 6557},
  240. {"name": "Strings", "size": 22026}
  241. ]
  242. },
  243. {
  244. "name": "vis",
  245. "children": [
  246. {
  247. "name": "axis",
  248. "children": [
  249. {"name": "Axes", "size": 1302},
  250. {"name": "Axis", "size": 24593},
  251. {"name": "AxisGridLine", "size": 652},
  252. {"name": "AxisLabel", "size": 636},
  253. {"name": "CartesianAxes", "size": 6703}
  254. ]
  255. },
  256. {
  257. "name": "controls",
  258. "children": [
  259. {"name": "AnchorControl", "size": 2138},
  260. {"name": "ClickControl", "size": 3824},
  261. {"name": "Control", "size": 1353},
  262. {"name": "ControlList", "size": 4665},
  263. {"name": "DragControl", "size": 2649},
  264. {"name": "ExpandControl", "size": 2832},
  265. {"name": "HoverControl", "size": 4896},
  266. {"name": "IControl", "size": 763},
  267. {"name": "PanZoomControl", "size": 5222},
  268. {"name": "SelectionControl", "size": 7862},
  269. {"name": "TooltipControl", "size": 8435}
  270. ]
  271. },
  272. {
  273. "name": "data",
  274. "children": [
  275. {"name": "Data", "size": 20544},
  276. {"name": "DataList", "size": 19788},
  277. {"name": "DataSprite", "size": 10349},
  278. {"name": "EdgeSprite", "size": 3301},
  279. {"name": "NodeSprite", "size": 19382},
  280. {
  281. "name": "render",
  282. "children": [
  283. {"name": "ArrowType", "size": 698},
  284. {"name": "EdgeRenderer", "size": 5569},
  285. {"name": "IRenderer", "size": 353},
  286. {"name": "ShapeRenderer", "size": 2247}
  287. ]
  288. },
  289. {"name": "ScaleBinding", "size": 11275},
  290. {"name": "Tree", "size": 7147},
  291. {"name": "TreeBuilder", "size": 9930}
  292. ]
  293. },
  294. {
  295. "name": "events",
  296. "children": [
  297. {"name": "DataEvent", "size": 2313},
  298. {"name": "SelectionEvent", "size": 1880},
  299. {"name": "TooltipEvent", "size": 1701},
  300. {"name": "VisualizationEvent", "size": 1117}
  301. ]
  302. },
  303. {
  304. "name": "legend",
  305. "children": [
  306. {"name": "Legend", "size": 20859},
  307. {"name": "LegendItem", "size": 4614},
  308. {"name": "LegendRange", "size": 10530}
  309. ]
  310. },
  311. {
  312. "name": "operator",
  313. "children": [
  314. {
  315. "name": "distortion",
  316. "children": [
  317. {"name": "BifocalDistortion", "size": 4461},
  318. {"name": "Distortion", "size": 6314},
  319. {"name": "FisheyeDistortion", "size": 3444}
  320. ]
  321. },
  322. {
  323. "name": "encoder",
  324. "children": [
  325. {"name": "ColorEncoder", "size": 3179},
  326. {"name": "Encoder", "size": 4060},
  327. {"name": "PropertyEncoder", "size": 4138},
  328. {"name": "ShapeEncoder", "size": 1690},
  329. {"name": "SizeEncoder", "size": 1830}
  330. ]
  331. },
  332. {
  333. "name": "filter",
  334. "children": [
  335. {"name": "FisheyeTreeFilter", "size": 5219},
  336. {"name": "GraphDistanceFilter", "size": 3165},
  337. {"name": "VisibilityFilter", "size": 3509}
  338. ]
  339. },
  340. {"name": "IOperator", "size": 1286},
  341. {
  342. "name": "label",
  343. "children": [
  344. {"name": "Labeler", "size": 9956},
  345. {"name": "RadialLabeler", "size": 3899},
  346. {"name": "StackedAreaLabeler", "size": 3202}
  347. ]
  348. },
  349. {
  350. "name": "layout",
  351. "children": [
  352. {"name": "AxisLayout", "size": 6725},
  353. {"name": "BundledEdgeRouter", "size": 3727},
  354. {"name": "CircleLayout", "size": 9317},
  355. {"name": "CirclePackingLayout", "size": 12003},
  356. {"name": "DendrogramLayout", "size": 4853},
  357. {"name": "ForceDirectedLayout", "size": 8411},
  358. {"name": "IcicleTreeLayout", "size": 4864},
  359. {"name": "IndentedTreeLayout", "size": 3174},
  360. {"name": "Layout", "size": 7881},
  361. {"name": "NodeLinkTreeLayout", "size": 12870},
  362. {"name": "PieLayout", "size": 2728},
  363. {"name": "RadialTreeLayout", "size": 12348},
  364. {"name": "RandomLayout", "size": 870},
  365. {"name": "StackedAreaLayout", "size": 9121},
  366. {"name": "TreeMapLayout", "size": 9191}
  367. ]
  368. },
  369. {"name": "Operator", "size": 2490},
  370. {"name": "OperatorList", "size": 5248},
  371. {"name": "OperatorSequence", "size": 4190},
  372. {"name": "OperatorSwitch", "size": 2581},
  373. {"name": "SortOperator", "size": 2023}
  374. ]
  375. },
  376. {"name": "Visualization", "size": 16540}
  377. ]
  378. }
  379. ]
  380. }