Guest User

Untitled

a guest
Apr 20th, 2018
77
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.47 KB | None | 0 0
  1. <!DOCTYPE html>
  2. <meta charset="utf-8">
  3. <style>
  4.  
  5. svg {
  6. font: 10px sans-serif;
  7. }
  8.  
  9. .y.axis path {
  10. display: none;
  11. }
  12.  
  13. .y.axis line {
  14. stroke: #fff;
  15. stroke-opacity: .2;
  16. shape-rendering: crispEdges;
  17. }
  18.  
  19. .y.axis .zero line {
  20. stroke: #000;
  21. stroke-opacity: 1;
  22. }
  23.  
  24. .title {
  25. font: 300 78px Helvetica Neue;
  26. fill: #666;
  27. }
  28.  
  29. .birthyear,
  30. .age {
  31. text-anchor: middle;
  32. }
  33.  
  34. .birthyear {
  35. fill: #BD8A53;
  36. }
  37.  
  38. rect {
  39. fill-opacity: .6;
  40. fill: #7F5232;
  41. }
  42.  
  43. rect:first-child {
  44. fill: #432E27;
  45. }
  46.  
  47. </style>
  48. <body>
  49. <script src="//d3js.org/d3.v3.min.js"></script>
  50. <script>
  51.  
  52. var margin = {top: 20, right: 40, bottom: 30, left: 20},
  53. width = 960 - margin.left - margin.right,
  54. height = 500 - margin.top - margin.bottom,
  55. barWidth = Math.floor(width / 19) - 1;
  56.  
  57. var x = d3.scale.linear()
  58. .range([barWidth / 2, width - barWidth / 2]);
  59.  
  60. var y = d3.scale.linear()
  61. .range([height, 0]);
  62.  
  63. var yAxis = d3.svg.axis()
  64. .scale(y)
  65. .orient("right")
  66. .tickSize(-width)
  67. .tickFormat(function(d) { return Math.round(d / 1e6) + "M"; });
  68.  
  69. // An SVG element with a bottom-right origin.
  70. var svg = d3.select("body").append("svg")
  71. .attr("width", width + margin.left + margin.right)
  72. .attr("height", height + margin.top + margin.bottom)
  73. .append("g")
  74. .attr("transform", "translate(" + margin.left + "," + margin.top + ")");
  75.  
  76. // A sliding container to hold the bars by birthyear.
  77. var birthyears = svg.append("g")
  78. .attr("class", "birthyears");
  79.  
  80. // A label for the current year.
  81. var title = svg.append("text")
  82. .attr("class", "title")
  83. .attr("dy", ".71em")
  84. .text("2018");
  85.  
  86. d3.csv("population.csv", function(error, data) {
  87.  
  88. // Convert strings to numbers.
  89. data.forEach(function(d) {
  90. d.people = +d.people;
  91. d.year = +d.year;
  92. d.age = +d.age;
  93. });
  94.  
  95. // Compute the extent of the data set in age and years.
  96. var age1 = d3.max(data, function(d) { return d.age; }),
  97. year0 = d3.min(data, function(d) { return d.year; }),
  98. year1 = d3.max(data, function(d) { return d.year; }),
  99. year = year1;
  100.  
  101. // Update the scale domains.
  102. x.domain([year1 - age1, year1]);
  103. y.domain([0, d3.max(data, function(d) { return d.people; })]);
  104.  
  105. // Produce a map from year and birthyear to [male, female].
  106. data = d3.nest()
  107. .key(function(d) { return d.year; })
  108. .key(function(d) { return d.year - d.age; })
  109. .rollup(function(v) { return v.map(function(d) { return d.people; }); })
  110. .map(data);
  111.  
  112. // Add an axis to show the population values.
  113. svg.append("g")
  114. .attr("class", "y axis")
  115. .attr("transform", "translate(" + width + ",0)")
  116. .call(yAxis)
  117. .selectAll("g")
  118. .filter(function(value) { return !value; })
  119. .classed("zero", true);
  120.  
  121. // Add labeled rects for each birthyear (so that no enter or exit is required).
  122. var birthyear = birthyears.selectAll(".birthyear")
  123. .data(d3.range(year0 - age1, year1 + 1, 5))
  124. .enter().append("g")
  125. .attr("class", "birthyear")
  126. .attr("transform", function(birthyear) { return "translate(" + x(birthyear) + ",0)"; });
  127.  
  128. birthyear.selectAll("rect")
  129. .data(function(birthyear) { return data[year][birthyear] || [0, 0]; })
  130. .enter().append("rect")
  131. .attr("x", -barWidth / 2)
  132. .attr("width", barWidth)
  133. .attr("y", y)
  134. .attr("height", function(value) { return height - y(value); });
  135.  
  136. // Add labels to show birthyear.
  137. birthyear.append("text")
  138. .attr("y", height - 4)
  139. .text(function(birthyear) { return birthyear; });
  140.  
  141. // Add labels to show age (separate; not animated).
  142. svg.selectAll(".age")
  143. .data(d3.range(0, age1 + 1, 5))
  144. .enter().append("text")
  145. .attr("class", "age")
  146. .attr("x", function(age) { return x(year - age); })
  147. .attr("y", height + 4)
  148. .attr("dy", ".71em")
  149. .text(function(age) { return age; });
  150.  
  151. // Allow the arrow keys to change the displayed year.
  152. window.focus();
  153. d3.select(window).on("keydown", function() {
  154. switch (d3.event.keyCode) {
  155. case 37: year = Math.max(year0, year - 10); break;
  156. case 39: year = Math.min(year1, year + 10); break;
  157. }
  158. update();
  159. });
  160.  
  161. function update() {
  162. if (!(year in data)) return;
  163. title.text(year);
  164.  
  165. birthyears.transition()
  166. .duration(750)
  167. .attr("transform", "translate(" + (x(year1) - x(year)) + ",0)");
  168.  
  169. birthyear.selectAll("rect")
  170. .data(function(birthyear) { return data[year][birthyear] || [0, 0]; })
  171. .transition()
  172. .duration(750)
  173. .attr("y", y)
  174. .attr("height", function(value) { return height - y(value); });
  175. }
  176. });
  177.  
  178. </script>
Add Comment
Please, Sign In to add comment