概要 实例 介绍 源码

柱状图

源文件: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. svg {
  8. font-size: 14px;
  9. }
  10. rect.background {
  11. fill: none;
  12. pointer-events: all;
  13. }
  14. .axis {
  15. shape-rendering: crispEdges;
  16. }
  17. .axis path, .axis line {
  18. fill: none;
  19. stroke: #000;
  20. stroke-width: .5px;
  21. }
  22. </style>
  23. </head>
  24. <body>
  25. <h2>
  26. Flare code size<br>
  27. hierarchical bar chart
  28. </h2>
  29. <script type="text/javascript" src="/d3/d3.js"></script>
  30. <script type="text/javascript" src="/d3/d3.layout.js"></script>
  31. <script type="text/javascript">
  32. var m = [80, 160, 0, 160], // top right bottom left
  33. w = 1280 - m[1] - m[3], // width
  34. h = 800 - m[0] - m[2], // height
  35. x = d3.scale.linear().range([0, w]),
  36. y = 25, // bar height
  37. z = d3.scale.ordinal().range(["steelblue", "#aaa"]); // bar color
  38. var hierarchy = d3.layout.partition()
  39. .value(function(d) { return d.size; });
  40. var xAxis = d3.svg.axis()
  41. .scale(x)
  42. .orient("top");
  43. var svg = d3.select("body").append("svg:svg")
  44. .attr("width", w + m[1] + m[3])
  45. .attr("height", h + m[0] + m[2])
  46. .append("svg:g")
  47. .attr("transform", "translate(" + m[3] + "," + m[0] + ")");
  48. svg.append("svg:rect")
  49. .attr("class", "background")
  50. .attr("width", w)
  51. .attr("height", h)
  52. .on("click", up);
  53. svg.append("svg:g")
  54. .attr("class", "x axis");
  55. svg.append("svg:g")
  56. .attr("class", "y axis")
  57. .append("svg:line")
  58. .attr("y1", "100%");
  59. d3.json("flare.json", function(root) {
  60. hierarchy.nodes(root);
  61. x.domain([0, root.value]).nice();
  62. down(root, 0);
  63. });
  64. function down(d, i) {
  65. if (!d.children || this.__transition__) return;
  66. var duration = d3.event && d3.event.altKey ? 7500 : 750,
  67. delay = duration / d.children.length;
  68. // Mark any currently-displayed bars as exiting.
  69. var exit = svg.selectAll(".enter").attr("class", "exit");
  70. // Entering nodes immediately obscure the clicked-on bar, so hide it.
  71. exit.selectAll("rect").filter(function(p) { return p === d; })
  72. .style("fill-opacity", 1e-6);
  73. // Enter the new bars for the clicked-on data.
  74. // Per above, entering bars are immediately visible.
  75. var enter = bar(d)
  76. .attr("transform", stack(i))
  77. .style("opacity", 1);
  78. // Have the text fade-in, even though the bars are visible.
  79. // Color the bars as parents; they will fade to children if appropriate.
  80. enter.select("text").style("fill-opacity", 1e-6);
  81. enter.select("rect").style("fill", z(true));
  82. // Update the x-scale domain.
  83. x.domain([0, d3.max(d.children, function(d) { return d.value; })]).nice();
  84. // Update the x-axis.
  85. svg.selectAll(".x.axis").transition()
  86. .duration(duration)
  87. .call(xAxis);
  88. // Transition entering bars to their new position.
  89. var enterTransition = enter.transition()
  90. .duration(duration)
  91. .delay(function(d, i) { return i * delay; })
  92. .attr("transform", function(d, i) { return "translate(0," + y * i * 1.2 + ")"; });
  93. // Transition entering text.
  94. enterTransition.select("text").style("fill-opacity", 1);
  95. // Transition entering rects to the new x-scale.
  96. enterTransition.select("rect")
  97. .attr("width", function(d) { return x(d.value); })
  98. .style("fill", function(d) { return z(!!d.children); });
  99. // Transition exiting bars to fade out.
  100. var exitTransition = exit.transition()
  101. .duration(duration)
  102. .style("opacity", 1e-6)
  103. .remove();
  104. // Transition exiting bars to the new x-scale.
  105. exitTransition.selectAll("rect").attr("width", function(d) { return x(d.value); });
  106. // Rebind the current node to the background.
  107. svg.select(".background").data([d]).transition().duration(duration * 2); d.index = i;
  108. }
  109. function up(d) {
  110. if (!d.parent || this.__transition__) return;
  111. var duration = d3.event && d3.event.altKey ? 7500 : 750,
  112. delay = duration / d.children.length;
  113. // Mark any currently-displayed bars as exiting.
  114. var exit = svg.selectAll(".enter").attr("class", "exit");
  115. // Enter the new bars for the clicked-on data's parent.
  116. var enter = bar(d.parent)
  117. .attr("transform", function(d, i) { return "translate(0," + y * i * 1.2 + ")"; })
  118. .style("opacity", 1e-6);
  119. // Color the bars as appropriate.
  120. // Exiting nodes will obscure the parent bar, so hide it.
  121. enter.select("rect")
  122. .style("fill", function(d) { return z(!!d.children); })
  123. .filter(function(p) { return p === d; })
  124. .style("fill-opacity", 1e-6);
  125. // Update the x-scale domain.
  126. x.domain([0, d3.max(d.parent.children, function(d) { return d.value; })]).nice();
  127. // Update the x-axis.
  128. svg.selectAll(".x.axis").transition()
  129. .duration(duration * 2)
  130. .call(xAxis);
  131. // Transition entering bars to fade in over the full duration.
  132. var enterTransition = enter.transition()
  133. .duration(duration * 2)
  134. .style("opacity", 1);
  135. // Transition entering rects to the new x-scale.
  136. // When the entering parent rect is done, make it visible!
  137. enterTransition.select("rect")
  138. .attr("width", function(d) { return x(d.value); })
  139. .each("end", function(p) { if (p === d) d3.select(this).style("fill-opacity", null); });
  140. // Transition exiting bars to the parent's position.
  141. var exitTransition = exit.selectAll("g").transition()
  142. .duration(duration)
  143. .delay(function(d, i) { return i * delay; })
  144. .attr("transform", stack(d.index));
  145. // Transition exiting text to fade out.
  146. exitTransition.select("text")
  147. .style("fill-opacity", 1e-6);
  148. // Transition exiting rects to the new scale and fade to parent color.
  149. exitTransition.select("rect")
  150. .attr("width", function(d) { return x(d.value); })
  151. .style("fill", z(true));
  152. // Remove exiting nodes when the last child has finished transitioning.
  153. exit.transition().duration(duration * 2).remove();
  154. // Rebind the current parent to the background.
  155. svg.select(".background").data([d.parent]).transition().duration(duration * 2);
  156. }
  157. // Creates a set of bars for the given data node, at the specified index.
  158. function bar(d) {
  159. var bar = svg.insert("svg:g", ".y.axis")
  160. .attr("class", "enter")
  161. .attr("transform", "translate(0,5)")
  162. .selectAll("g")
  163. .data(d.children)
  164. .enter().append("svg:g")
  165. .style("cursor", function(d) { return !d.children ? null : "pointer"; })
  166. .on("click", down);
  167. bar.append("svg:text")
  168. .attr("x", -6)
  169. .attr("y", y / 2)
  170. .attr("dy", ".35em")
  171. .attr("text-anchor", "end")
  172. .text(function(d) { return d.name; });
  173. bar.append("svg:rect")
  174. .attr("width", function(d) { return x(d.value); })
  175. .attr("height", y);
  176. return bar;
  177. }
  178. // A stateful closure for stacking bars horizontally.
  179. function stack(i) {
  180. var x0 = 0;
  181. return function(d) {
  182. var tx = "translate(" + x0 + "," + y * i * 1.2 + ")";
  183. x0 += x(d.value);
  184. return tx;
  185. };
  186. }
  187. </script>
  188. </body>
  189. </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. }