概要 实例 介绍 源码

焦点折叠图

源文件: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. <style type="text/css">
  7. circle.node {
  8. cursor: pointer;
  9. stroke: #000;
  10. stroke-width: .5px;
  11. }
  12. line.link {
  13. fill: none;
  14. stroke: #9ecae1;
  15. stroke-width: 1.5px;
  16. }
  17. </style>
  18. </head>
  19. <body>
  20. <h2>
  21. Flare code size<br>
  22. force-directed graph
  23. </h2>
  24. <script type="text/javascript" src="/d3/d3.js"></script>
  25. <script type="text/javascript" src="/d3/d3.geom.js"></script>
  26. <script type="text/javascript" src="/d3/d3.layout.js"></script>
  27. <script type="text/javascript">
  28. var w = 1280,
  29. h = 800,
  30. node,
  31. link,
  32. root;
  33. var force = d3.layout.force()
  34. .on("tick", tick)
  35. .charge(function(d) { return d._children ? -d.size / 100 : -30; })
  36. .linkDistance(function(d) { return d.target._children ? 80 : 30; })
  37. .size([w, h - 160]);
  38. var vis = d3.select("body").append("svg:svg")
  39. .attr("width", w)
  40. .attr("height", h);
  41. d3.json("flare.json", function(json) {
  42. root = json;
  43. root.fixed = true;
  44. root.x = w / 2;
  45. root.y = h / 2 - 80;
  46. update();
  47. });
  48. function update() {
  49. var nodes = flatten(root),
  50. links = d3.layout.tree().links(nodes);
  51. // Restart the force layout.
  52. force
  53. .nodes(nodes)
  54. .links(links)
  55. .start();
  56. // Update the links…
  57. link = vis.selectAll("line.link")
  58. .data(links, function(d) { return d.target.id; });
  59. // Enter any new links.
  60. link.enter().insert("svg:line", ".node")
  61. .attr("class", "link")
  62. .attr("x1", function(d) { return d.source.x; })
  63. .attr("y1", function(d) { return d.source.y; })
  64. .attr("x2", function(d) { return d.target.x; })
  65. .attr("y2", function(d) { return d.target.y; });
  66. // Exit any old links.
  67. link.exit().remove();
  68. // Update the nodes…
  69. node = vis.selectAll("circle.node")
  70. .data(nodes, function(d) { return d.id; })
  71. .style("fill", color);
  72. node.transition()
  73. .attr("r", function(d) { return d.children ? 4.5 : Math.sqrt(d.size) / 10; });
  74. // Enter any new nodes.
  75. node.enter().append("svg:circle")
  76. .attr("class", "node")
  77. .attr("cx", function(d) { return d.x; })
  78. .attr("cy", function(d) { return d.y; })
  79. .attr("r", function(d) { return d.children ? 4.5 : Math.sqrt(d.size) / 10; })
  80. .style("fill", color)
  81. .on("click", click)
  82. .call(force.drag);
  83. // Exit any old nodes.
  84. node.exit().remove();
  85. }
  86. function tick() {
  87. link.attr("x1", function(d) { return d.source.x; })
  88. .attr("y1", function(d) { return d.source.y; })
  89. .attr("x2", function(d) { return d.target.x; })
  90. .attr("y2", function(d) { return d.target.y; });
  91. node.attr("cx", function(d) { return d.x; })
  92. .attr("cy", function(d) { return d.y; });
  93. }
  94. // Color leaf nodes orange, and packages white or blue.
  95. function color(d) {
  96. return d._children ? "#3182bd" : d.children ? "#c6dbef" : "#fd8d3c";
  97. }
  98. // Toggle children on click.
  99. function click(d) {
  100. if (d.children) {
  101. d._children = d.children;
  102. d.children = null;
  103. } else {
  104. d.children = d._children;
  105. d._children = null;
  106. }
  107. update();
  108. }
  109. // Returns a list of all nodes under the root.
  110. function flatten(root) {
  111. var nodes = [], i = 0;
  112. function recurse(node) {
  113. if (node.children) node.size = node.children.reduce(function(p, v) { return p + recurse(v); }, 0);
  114. if (!node.id) node.id = ++i;
  115. nodes.push(node);
  116. return node.size;
  117. }
  118. root.size = recurse(root);
  119. return nodes;
  120. }
  121. </script>
  122. </body>
  123. </html>

源文件:style.css

  1. body {
  2. font: 300 36px "Helvetica Neue";
  3. height: 640px;
  4. margin: 80px 160px 80px 160px;
  5. overflow: hidden;
  6. position: relative;
  7. width: 960px;
  8. }
  9. a:link, a:visited {
  10. color: #777;
  11. text-decoration: none;
  12. }
  13. a:hover {
  14. color: #666;
  15. }
  16. blockquote {
  17. margin: 0;
  18. }
  19. blockquote:before {
  20. content: "“";
  21. position: absolute;
  22. left: -.4em;
  23. }
  24. blockquote:after {
  25. content: "”";
  26. position: absolute;
  27. }
  28. body > ul {
  29. margin: 0;
  30. padding: 0;
  31. }
  32. h1 {
  33. font-size: 64px;
  34. }
  35. h1, h2, h3 {
  36. font-weight: inherit;
  37. margin: 0;
  38. }
  39. h2, h3 {
  40. text-align: right;
  41. font-size: inherit;
  42. position: absolute;
  43. bottom: 0;
  44. right: 0;
  45. }
  46. h2 {
  47. font-size: 24px;
  48. position: absolute;
  49. }
  50. h3 {
  51. bottom: -20px;
  52. font-size: 18px;
  53. }
  54. .invert {
  55. background: #1f1f1f;
  56. color: #dcdccc;
  57. }
  58. .invert h2, .invert h3 {
  59. color: #7f9f7f;
  60. }
  61. .string, .regexp {
  62. color: #f39;
  63. }
  64. .keyword {
  65. color: #00c;
  66. }
  67. .comment {
  68. color: #777;
  69. font-style: oblique;
  70. }
  71. .number {
  72. color: #369;
  73. }
  74. .class, .special {
  75. color: #1181B8;
  76. }
  77. body > svg {
  78. position: absolute;
  79. top: -80px;
  80. left: -160px;
  81. }

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