Guest User

Untitled

a guest
Apr 21st, 2018
70
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.55 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: #fff;
  36. }
  37.  
  38. rect {
  39. fill-opacity: .6;
  40. fill: orange;
  41. }
  42.  
  43. rect:first-child {
  44. fill: #7b6888;
  45. }
  46.  
  47. </style>
  48. <body>
  49. <script src="//d3js.org/d3.v3.min.js"></script>
  50. <script>
  51.  
  52. var margin = {top: 100, right: 40, bottom: 30, left: 60},
  53. width = 1200 - margin.left - margin.right,
  54. height = 500 - margin.top - margin.bottom,
  55. barWidth = Math.floor(width / 30) - 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. .style("font-size", "30px")
  85. .style("text-decoration", "underline")
  86. .text("Current Year: 2000");
  87.  
  88. d3.csv("population.csv", function(error, data) {
  89.  
  90. // Convert strings to numbers.
  91. data.forEach(function(d) {
  92. d.people = +d.people;
  93. d.year = +d.year;
  94. d.age = +d.age;
  95. });
  96.  
  97. // Compute the extent of the data set in age and years.
  98. var age1 = d3.max(data, function(d) { return d.age; }),
  99. year0 = d3.min(data, function(d) { return d.year; }),
  100. year1 = d3.max(data, function(d) { return d.year; }),
  101. year = year1;
  102.  
  103. // Update the scale domains.
  104. x.domain([year1 - age1, year1]);
  105. y.domain([0, d3.max(data, function(d) { return d.people; })]);
  106.  
  107. // Produce a map from year and birthyear to [male, female].
  108. data = d3.nest()
  109. .key(function(d) { return d.year; })
  110. .key(function(d) { return d.year - d.age; })
  111. .rollup(function(v) { return v.map(function(d) { return d.people; }); })
  112. .map(data);
  113.  
  114. // Add an axis to show the population values.
  115. svg.append("g")
  116. .attr("class", "y axis")
  117. .attr("transform", "translate(" + width + ",0)")
  118. .call(yAxis)
  119. .selectAll("g")
  120. .filter(function(value) { return !value; })
  121. .classed("zero", true);
  122.  
  123. // Add labeled rects for each birthyear (so that no enter or exit is required).
  124. var birthyear = birthyears.selectAll(".birthyear")
  125. .data(d3.range(year0 - age1, year1 + 1, 5))
  126. .enter().append("g")
  127. .attr("class", "birthyear")
  128. .attr("transform", function(birthyear) { return "translate(" + x(birthyear) + ",0)"; });
  129.  
  130. birthyear.selectAll("rect")
  131. .data(function(birthyear) { return data[year][birthyear] || [0, 0]; })
  132. .enter().append("rect")
  133. .attr("x", -barWidth / 2)
  134. .attr("width", barWidth)
  135. .attr("y", y)
  136. .attr("height", function(value) { return height - y(value); });
  137.  
  138. // Add labels to show birthyear.
  139. birthyear.append("text")
  140. .attr("y", height - 4)
  141. .text(function(birthyear) { return birthyear; });
  142.  
  143. // Add labels to show age (separate; not animated).
  144. svg.selectAll(".age")
  145. .data(d3.range(0, age1 + 1, 5))
  146. .enter().append("text")
  147. .attr("class", "age")
  148. .attr("x", function(age) { return x(year - age); })
  149. .attr("y", height + 4)
  150. .attr("dy", ".71em")
  151. .text(function(age) { return age; });
  152.  
  153. // Allow the arrow keys to change the displayed year.
  154. window.focus();
  155. d3.select(window).on("keydown", function() {
  156. switch (d3.event.keyCode) {
  157. case 37: year = Math.max(year0, year - 10); break;
  158. case 39: year = Math.min(year1, year + 10); break;
  159. }
  160. update();
  161. });
  162.  
  163. function update() {
  164. if (!(year in data)) return;
  165. title.text(year);
  166.  
  167. birthyears.transition()
  168. .duration(750)
  169. .attr("transform", "translate(" + (x(year1) - x(year)) + ",0)");
  170.  
  171. birthyear.selectAll("rect")
  172. .data(function(birthyear) { return data[year][birthyear] || [0, 0]; })
  173. .transition()
  174. .duration(750)
  175. .attr("y", y)
  176. .attr("height", function(value) { return height - y(value); });
  177. }
  178. });
  179.  
  180. </script>
Add Comment
Please, Sign In to add comment