概要 实例 介绍 源码

树图

源文件:index.html

  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <meta http-equiv="Content-Type" content="text/html;charset=utf-8"/>
  5. <link type="text/css" rel="stylesheet" href="style.css"/>
  6. <script type="text/javascript" src="/d3/d3.js"></script>
  7. <script type="text/javascript" src="/d3/d3.layout.js"></script>
  8. <style type="text/css">
  9. .node circle {
  10. cursor: pointer;
  11. fill: #fff;
  12. stroke: steelblue;
  13. stroke-width: 1.5px;
  14. }
  15. .node text {
  16. font-size: 11px;
  17. }
  18. path.link {
  19. fill: none;
  20. stroke: #ccc;
  21. stroke-width: 1.5px;
  22. }
  23. </style>
  24. </head>
  25. <body>
  26. <div id="body">
  27. </div>
  28. <script type="text/javascript">
  29. var m = [20, 120, 20, 120],
  30. w = 1280 - m[1] - m[3],
  31. h = 800 - m[0] - m[2],
  32. i = 0,
  33. root;
  34. var tree = d3.layout.tree()
  35. .size([h, w]);
  36. var diagonal = d3.svg.diagonal()
  37. .projection(function(d) { return [d.y, d.x]; });
  38. var vis = d3.select("#body").append("svg:svg")
  39. .attr("width", w + m[1] + m[3])
  40. .attr("height", h + m[0] + m[2])
  41. .append("svg:g")
  42. .attr("transform", "translate(" + m[3] + "," + m[0] + ")");
  43. d3.json("flare.json", function(json) {
  44. root = json;
  45. root.x0 = h / 2;
  46. root.y0 = 0;
  47. function toggleAll(d) {
  48. if (d.children) {
  49. d.children.forEach(toggleAll);
  50. toggle(d);
  51. }
  52. }
  53. // Initialize the display to show a few nodes.
  54. root.children.forEach(toggleAll);
  55. toggle(root.children[1]);
  56. toggle(root.children[1].children[2]);
  57. toggle(root.children[9]);
  58. toggle(root.children[9].children[0]);
  59. update(root);
  60. });
  61. function update(source) {
  62. var duration = d3.event && d3.event.altKey ? 5000 : 500;
  63. // Compute the new tree layout.
  64. var nodes = tree.nodes(root).reverse();
  65. // Normalize for fixed-depth.
  66. nodes.forEach(function(d) { d.y = d.depth * 180; });
  67. // Update the nodes…
  68. var node = vis.selectAll("g.node")
  69. .data(nodes, function(d) { return d.id || (d.id = ++i); });
  70. // Enter any new nodes at the parent's previous position.
  71. var nodeEnter = node.enter().append("svg:g")
  72. .attr("class", "node")
  73. .attr("transform", function(d) { return "translate(" + source.y0 + "," + source.x0 + ")"; })
  74. .on("click", function(d) { toggle(d); update(d); });
  75. nodeEnter.append("svg:circle")
  76. .attr("r", 1e-6)
  77. .style("fill", function(d) { return d._children ? "lightsteelblue" : "#fff"; });
  78. nodeEnter.append("svg:text")
  79. .attr("x", function(d) { return d.children || d._children ? -10 : 10; })
  80. .attr("dy", ".35em")
  81. .attr("text-anchor", function(d) { return d.children || d._children ? "end" : "start"; })
  82. .text(function(d) { return d.name; })
  83. .style("fill-opacity", 1e-6);
  84. // Transition nodes to their new position.
  85. var nodeUpdate = node.transition()
  86. .duration(duration)
  87. .attr("transform", function(d) { return "translate(" + d.y + "," + d.x + ")"; });
  88. nodeUpdate.select("circle")
  89. .attr("r", 4.5)
  90. .style("fill", function(d) { return d._children ? "lightsteelblue" : "#fff"; });
  91. nodeUpdate.select("text")
  92. .style("fill-opacity", 1);
  93. // Transition exiting nodes to the parent's new position.
  94. var nodeExit = node.exit().transition()
  95. .duration(duration)
  96. .attr("transform", function(d) { return "translate(" + source.y + "," + source.x + ")"; })
  97. .remove();
  98. nodeExit.select("circle")
  99. .attr("r", 1e-6);
  100. nodeExit.select("text")
  101. .style("fill-opacity", 1e-6);
  102. // Update the links…
  103. var link = vis.selectAll("path.link")
  104. .data(tree.links(nodes), function(d) { return d.target.id; });
  105. // Enter any new links at the parent's previous position.
  106. link.enter().insert("svg:path", "g")
  107. .attr("class", "link")
  108. .attr("d", function(d) {
  109. var o = {x: source.x0, y: source.y0};
  110. return diagonal({source: o, target: o});
  111. })
  112. .transition()
  113. .duration(duration)
  114. .attr("d", diagonal);
  115. // Transition links to their new position.
  116. link.transition()
  117. .duration(duration)
  118. .attr("d", diagonal);
  119. // Transition exiting nodes to the parent's new position.
  120. link.exit().transition()
  121. .duration(duration)
  122. .attr("d", function(d) {
  123. var o = {x: source.x, y: source.y};
  124. return diagonal({source: o, target: o});
  125. })
  126. .remove();
  127. // Stash the old positions for transition.
  128. nodes.forEach(function(d) {
  129. d.x0 = d.x;
  130. d.y0 = d.y;
  131. });
  132. }
  133. // Toggle children.
  134. function toggle(d) {
  135. if (d.children) {
  136. d._children = d.children;
  137. d.children = null;
  138. } else {
  139. d.children = d._children;
  140. d._children = null;
  141. }
  142. }
  143. </script>
  144. </body>
  145. </html>

源文件:style.css

  1. body {
  2. background: url(texture-noise.png);
  3. overflow: hidden;
  4. margin: 0;
  5. font-size: 14px;
  6. font-family: "Helvetica Neue", Helvetica;
  7. }
  8. #chart, #header, #footer {
  9. position: absolute;
  10. top: 0;
  11. }
  12. #header, #footer {
  13. z-index: 1;
  14. display: block;
  15. font-size: 36px;
  16. font-weight: 300;
  17. text-shadow: 0 1px 0 #fff;
  18. }
  19. #header.inverted, #footer.inverted {
  20. color: #fff;
  21. text-shadow: 0 1px 4px #000;
  22. }
  23. #header {
  24. top: 80px;
  25. left: 140px;
  26. width: 1000px;
  27. }
  28. #footer {
  29. top: 680px;
  30. right: 140px;
  31. text-align: right;
  32. }
  33. rect {
  34. fill: none;
  35. pointer-events: all;
  36. }
  37. pre {
  38. font-size: 18px;
  39. }
  40. line {
  41. stroke: #000;
  42. stroke-width: 1.5px;
  43. }
  44. .string, .regexp {
  45. color: #f39;
  46. }
  47. .keyword {
  48. color: #00c;
  49. }
  50. .comment {
  51. color: #777;
  52. font-style: oblique;
  53. }
  54. .number {
  55. color: #369;
  56. }
  57. .class, .special {
  58. color: #1181B8;
  59. }
  60. a:link, a:visited {
  61. color: #000;
  62. text-decoration: none;
  63. }
  64. a:hover {
  65. color: #666;
  66. }
  67. .hint {
  68. position: absolute;
  69. right: 0;
  70. width: 1280px;
  71. font-size: 12px;
  72. color: #999;
  73. }

源文件: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. }