Advertisement
Guest User

Untitled

a guest
Jul 25th, 2016
78
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.03 KB | None | 0 0
  1. // Set the dimensions of the canvas / graph
  2. var margin = {top: 40, right: 30, bottom: 90, left: 70},
  3. width = 800 - margin.left - margin.right,
  4. height = 400 - margin.top - margin.bottom;
  5.  
  6. // Parse the year / time
  7. var parseDate = d3.time.format("%Y").parse;
  8.  
  9. // Set the ranges
  10. var x = d3.time.scale().range([0, width]);
  11. var y = d3.scale.linear().range([height, 0]);
  12.  
  13. // Define the axes
  14. var xAxis = d3.svg.axis().scale(x)
  15. .orient("bottom").ticks(5);
  16.  
  17. var yAxis = d3.svg.axis().scale(y)
  18. .orient("left").ticks(5);
  19.  
  20. // Define the line
  21. var scoreline = d3.svg.line()
  22. .x(function(d) { return x(d.year); })
  23. .y(function(d) { return y(d.score); });
  24.  
  25. // Adds the svg canvas
  26. var svg = d3.select("body")
  27. .append("svg")
  28. .attr("width", width + margin.left + margin.right)
  29. .attr("height", height + margin.top + margin.bottom)
  30. .append("g")
  31. .attr("transform",
  32. "translate(" + margin.left + "," + margin.top + ")");
  33.  
  34. // Get the data
  35. d3.csv("studies.csv", function(error, data) {
  36. data.forEach(function(d) {
  37. d.year = parseDate(d.year);
  38. d.score = +d.score;
  39. });
  40.  
  41. // Scale the range of the data
  42. x.domain(d3.extent(data, function(d) { return d.year; }));
  43. y.domain([45, 60]);
  44.  
  45. // Nest the entries by programme
  46. var dataNest = d3.nest()
  47. .key(function(d) {return d.programme;})
  48. .entries(data);
  49.  
  50. var color = d3.scale.category10(); // set the colour scale
  51.  
  52. legendSpace = width/dataNest.length; // spacing for the legend
  53.  
  54. // Loop through each programme / key
  55. dataNest.forEach(function(d,i) {
  56.  
  57. svg.append("path")
  58. .attr("class", "line")
  59. .style("stroke", function() { // Add the colours dynamically
  60. return d.color = color(d.key); })
  61. .attr("id", 'tag'+d.key.replace(/s+/g, '')) // assign ID
  62. .attr("d", scoreline(d.values));
  63.  
  64. // Add the Legend
  65. svg.append("text")
  66. .attr("x", (legendSpace/2)+i*legendSpace) // space legend
  67. .attr("y", height + (margin.bottom/2)+ 5)
  68. .attr("class", "legend") // style the legend
  69. .style("fill", function() { // Add the colours dynamically
  70. return d.color = color(d.key); })
  71. .on("click", function(){
  72. // Determine if current line is visible
  73. var active = d.active ? false : true,
  74. newOpacity = active ? 0 : 1;
  75. // Hide or show the elements based on the ID
  76. d3.select("#tag"+d.key.replace(/s+/g, ''))
  77. .transition().duration(100)
  78. .style("opacity", newOpacity);
  79. // Update whether or not the elements are active
  80. d.active = active;
  81. })
  82. .text(d.key);
  83.  
  84. });
  85.  
  86. // Add the X Axis
  87. svg.append("g")
  88. .attr("class", "x axis")
  89. .attr("transform", "translate(0," + height + ")")
  90. .call(xAxis);
  91.  
  92. // Add the Y Axis
  93. svg.append("g")
  94. .attr("class", "y axis")
  95. .call(yAxis);
  96.  
  97. });
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement