Advertisement
Guest User

Untitled

a guest
Jul 6th, 2015
171
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.56 KB | None | 0 0
  1. var margin = {top: 20, right: 30, bottom: 50, left: 40},
  2. width = 960 - margin.left - margin.right,
  3. height = 500 - margin.top - margin.bottom;
  4.  
  5. var xScale = d3.scale.ordinal()
  6. .rangeRoundBands([0, width],.15);
  7.  
  8. var yScale = d3.scale.linear()
  9. .range([height, 0]);
  10.  
  11. var xAxis = d3.svg.axis()
  12. .scale(xScale)
  13. .orient("bottom");
  14.  
  15. var yAxis = d3.svg.axis()
  16. .scale(yScale)
  17. .orient("left");
  18.  
  19. var tooltip = d3.select("body").append("div")
  20. .style("position", "absolute")
  21. .style("padding", "0 10px")
  22. .style("background", "white")
  23. .style("opacity", 0);
  24.  
  25. var chart1 = d3.select(".chart1")
  26. .attr("width", width + margin.right + margin.left)
  27. .attr("height", height + margin.top + margin.bottom)
  28. .append("g")
  29. .attr("transform", "translate(" + margin.left + "," + margin.right + ")");
  30.  
  31. // Force a given string of data to a number
  32. function typecheck(d) {
  33. d.makes = +d.makes;
  34. d.attempts = +d.attempts;
  35. return d;
  36. }
  37.  
  38. // Chart 1 - Three Point Makes
  39. d3.tsv("data.tsv", typecheck, function(error, data) {
  40. xScale.domain(data.map(function(d) {return d.year}));
  41. yScale.domain([0, d3.max(data, function (d) { return d.makes; })]);
  42.  
  43. chart1.append("g")
  44. .attr("class", "x axis")
  45. .attr("transform", "translate(0," + height + ")")
  46. .call(xAxis)
  47. .selectAll("text")
  48. .attr("y", 0)
  49. .attr("x", 9)
  50. .attr("dy", ".35em")
  51. .attr("transform", "rotate(90)")
  52. .style("text-anchor", "start");
  53.  
  54. chart1.append("g")
  55. .attr("class", "y axis")
  56. .call(yAxis);
  57.  
  58. chart1.selectAll(".bar")
  59. .data(data)
  60. .enter().append("rect")
  61. .attr("class", "bar")
  62. .attr("x", function(d) { return xScale(d.year); })
  63. .attr("y", function(d) { return yScale(d.makes); })
  64. .attr("height", function(d) { return height - yScale(d.makes) })
  65. .attr("width", xScale.rangeBand())
  66. .style("fill", '#d33682')
  67.  
  68. .on("mouseover", function(d) {
  69. d3.select(this)
  70. .transition().duration(100)
  71. .style("fill", '#b58900');
  72.  
  73. tooltip.transition()
  74. .style("opacity", .9);
  75.  
  76. tooltip.html(d.makes)
  77. .style("left", (d3.event.pageX + 5) + "px")
  78. .style("top", (d3.event.pageY - 25) + "px")
  79. })
  80. .on("mouseout", function(d) {
  81. d3.select(this)
  82. .transition().duration(250)
  83. .style("fill", '#d33682');
  84.  
  85. tooltip.transition()
  86. .style("opacity", 0);
  87. })
  88.  
  89. });
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement