Advertisement
Guest User

Untitled

a guest
Dec 18th, 2014
151
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.13 KB | None | 0 0
  1. // set dimensions
  2. var margin = {top: 20, right: 50, bottom: 20, left: 20},
  3. width = 600 - margin.left - margin.right,
  4. height = 400 - margin.top - margin.bottom;
  5.  
  6. // add svg element
  7. var svg = d3.select("#seasons-chart")
  8. .append('svg')
  9. .attr("width", width + margin.left + margin.right)
  10. .attr("height", height + margin.top + margin.bottom)
  11. .append('g')
  12. .attr("transform", "translate(" + margin.left + "," + margin.top + ")");
  13.  
  14. // set ranges
  15. var x = d3.scale.linear().range([0, width - margin.right]);
  16. var y = d3.scale.linear().range([0, height]);
  17.  
  18. // set color scale
  19. var color = d3.scale.category10();
  20.  
  21. // define axes
  22. var x_axis = d3.svg.axis()
  23. .scale(x)
  24. .orient('bottom');
  25.  
  26. var y_axis = d3.svg.axis()
  27. .scale(y)
  28. .ticks(22)
  29. .orient('left');
  30.  
  31. // define line
  32. var line = d3.svg.line()
  33. .defined(function(d) { return d.position !== 0; })
  34. .x(function(d) { return x(d.round); })
  35. .y(function(d) { return y(d.position); })
  36. .interpolate('linear');
  37.  
  38. // read data
  39. d3.csv('../../../../data/arsenal-history.csv', function(error, data) {
  40.  
  41. vars = d3.keys(data[0]).filter(function(key) { return key !== 'round'; });
  42. color.domain(vars);
  43.  
  44. var seasons = vars.map(function(name) {
  45. return {
  46. name: name,
  47. values: data.map(function(d) {
  48. return { round: d.round, position: parseInt(d[name]) };
  49. })
  50. };
  51. });
  52.  
  53. // determine ranges of input data (N.B. x hard-coded)
  54. x.domain([1, 42]);
  55. y.domain([1,
  56. d3.max(seasons, function(c) { return d3.max(c.values, function(v) {
  57. return v.position; }); })
  58. ]);
  59.  
  60. svg.append('g')
  61. .attr('class', 'x axis')
  62. .attr('transform', 'translate(0,' + height + ')')
  63. .call(x_axis);
  64.  
  65. svg.append('g')
  66. .attr('class', 'y axis')
  67. .call(y_axis);
  68.  
  69. // label for x-axis
  70. svg.append('text')
  71. .attr('text-anchor', 'middle')
  72. .attr('x', margin.right)
  73. .attr('y', height)
  74. .attr('dy', '.9em')
  75. .attr('dx', '-1.75em')
  76. .text('Round:');
  77.  
  78. // add dashed lines
  79. svg.append('line')
  80. .attr('x1', 0)
  81. .attr('y1', y(20))
  82. .attr('x2', width - margin.right)
  83. .attr('y2', y(20))
  84. .attr('stroke-weight', '2px')
  85. .style("stroke-dasharray", ("1, 3"))
  86. .style('stroke', '#000');
  87.  
  88. svg.append('line')
  89. .attr('x1', x(38))
  90. .attr('y1', 0)
  91. .attr('x2', x(38))
  92. .attr('y2', height)
  93. .attr('stroke-weight', '2px')
  94. .style("stroke-dasharray", ("1, 3"))
  95. .style('stroke', '#000');
  96.  
  97. var season = svg.selectAll('.season')
  98. .data(seasons)
  99. .enter().append('g')
  100. .attr('class', 'seasons');
  101. console.log('season', season);
  102.  
  103. function highlight(d, index, object) {
  104. if (object.classList.contains('active')) {
  105. d3.selectAll('.season' + index).classed('active', false);
  106. } else {
  107. d3.selectAll('.season' + index).classed('active', true);
  108. d3.selectAll('.season' + index + '.line')
  109. .append('circle')
  110. .style('fill', 'black')
  111. .attr('x', 100)
  112. .attr('y', 100)
  113. .attr('r', 30);
  114. }
  115. };
  116.  
  117. var lines = season.append('path')
  118. .attr('d', function(d) { return line(d.values); })
  119. .attr("class", function(d, i) { return "season" + i + " line"; })
  120. .style('stroke', function(d) { return color(d.name); })
  121. .on('click', function(d,i) { highlight(d, i, this); });
  122.  
  123. var box_dim = 10;
  124.  
  125. // animate drawing of lines
  126. //var total_length = lines.node().getTotalLength();
  127. // lines
  128. // .attr('stroke-dasharray', total_length + ' ' + total_length)
  129. // .attr('stroke-dashoffset', total_length)
  130. // .transition()
  131. // .duration(5000)
  132. // .ease('linear')
  133. // .attr('stroke-dashoffset', 0);
  134.  
  135. // add final position label
  136. season
  137. .append('text')
  138. .datum(function(d) {
  139. var j = d.values.length - 1;
  140. while (d.values[j].position == 0 && j > 0) { j--; }
  141. return { name: d.name, value: d.values[j] };
  142. })
  143. .attr("transform", function(d) { return "translate(" + x(d.value.round) + "," + y(d.value.position) + ")"; })
  144. .attr('dy', '.30em')
  145. .attr('dx', '.35em')
  146. .attr('class', function(d, i) { return 'season' + i + ' label'; })
  147. //.attr('opacity', 0)
  148. .text(function(d) { return d.value.position; })
  149.  
  150. season.append('circle')
  151. .datum(function(d) {
  152. var j = d.values.length - 1;
  153. while (d.values[j].position == 0 && j > 0) { j--; }
  154. return { name: d.name, value: d.values[j] };
  155. })
  156. .attr('cx', function(d) { return x(d.value.round); })
  157. .attr('cy', function(d) { return y(d.value.position); })
  158. .style('fill', function(d, i) { return color(d.name) })
  159. .attr('class', function(d, i) { return 'season' + i + ' point'; })
  160. //.attr('opacity', 0)
  161. .attr('r', 3);
  162.  
  163. // alternative for rolling back animation
  164. // svg.on('click', function() {
  165. // lines
  166. // .transition()
  167. // .duration(2000)
  168. // .ease('linear')
  169. // .attr('stroke-dashoffset', total_length);
  170. // })
  171.  
  172. var legend = svg.selectAll('.legend')
  173. .data(vars.slice())
  174. .enter().append('g')
  175. .attr("class", function(d, i) { return "season" + i + " legend"; })
  176. .attr("transform", function (d, i) { return "translate(0," + i * (box_dim + 5) + ")"; })
  177. .on('click', function(d,i) { highlight(d, i, this); });
  178.  
  179. legend.append('rect')
  180. .attr('x', width)
  181. .attr('y', function(d) { return height - (seasons.length * box_dim * 1.5); })
  182. .attr('width', box_dim)
  183. .attr('height', box_dim)
  184. .style('fill', color);
  185.  
  186. legend.append('text')
  187. .attr('x', width)
  188. .attr('y', function(d) { return height - (seasons.length * box_dim * 1.5); })
  189. .attr('dy', 8.5)
  190. .attr('dx', -2)
  191. .style('text-anchor', 'end')
  192. .text(function(d) { return d; });
  193.  
  194. svg.append('text')
  195. .attr('x', width)
  196. .attr('y', 5)
  197. .attr('dx', 10)
  198. .attr('dy', 4)
  199. .style('font-weight', 'bold')
  200. .style('text-anchor', 'end')
  201. .text('Season');
  202.  
  203. // reduce opacity of all lines but current season
  204. d3.selectAll('.line:not(.season22)')
  205. .attr('opacity', .10);
  206. d3.selectAll('.point:not(.season22)')
  207. .attr('opacity', 0);
  208. d3.selectAll('.label:not(.season22)')
  209. .attr('opacity', 0);
  210. d3.selectAll('.legend:not(.season22)')
  211. .attr('opacity', .1);
  212.  
  213. // add 'clear' button
  214. svg.append('text')
  215. .attr('x', x(38))
  216. .attr('y', y(20))
  217. .attr('dy', '-.2em')
  218. .attr('dx', '-.2em')
  219. .attr('font-size', '12px')
  220. .attr('text-anchor', 'end')
  221. .attr('class', 'button')
  222. .text('Clear selection')
  223. .on('click', function() {
  224. for (var i = 0; i <= 23; i++) {
  225. d3.selectAll('.season' + i).classed('active', false);
  226. }
  227. });
  228. });
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement