Guest User

Untitled

a guest
Apr 20th, 2018
90
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.22 KB | None | 0 0
  1. <!DOCTYPE html>
  2. <meta charset="utf-8">
  3. <style>
  4.  
  5. .dots path {
  6. fill: none;
  7. stroke: steelblue;
  8. stroke-width: 1.5px;
  9. }
  10.  
  11. .axis path,
  12. .axis line {
  13. fill: none;
  14. stroke: #000;
  15. shape-rendering: crispEdges;
  16. }
  17.  
  18. .axis--y line {
  19. stroke-opacity: 0.2;
  20. }
  21.  
  22. .axis--y path {
  23. stroke: none;
  24. }
  25.  
  26. .axis text {
  27. font: 10px sans-serif;
  28. }
  29.  
  30. </style>
  31. <body>
  32. <script src="//d3js.org/d3.v3.min.js"></script>
  33. <script>
  34.  
  35. var parseTime = d3.time.format.utc("%H:%M").parse,
  36. midnight = parseTime("00:00");
  37.  
  38. var margin = {top: 30, right: 30, bottom: 30, left: 30},
  39. width = 960 - margin.left - margin.right,
  40. height = 500 - margin.top - margin.bottom;
  41.  
  42. var x = d3.time.scale.utc()
  43. .domain([midnight, d3.time.day.utc.offset(midnight, 1)])
  44. .range([0, width]);
  45.  
  46. var y = d3.scale.linear()
  47. .range([height, 0]);
  48.  
  49. var svg = d3.select("body").append("svg")
  50. .attr("width", width + margin.left + margin.right)
  51. .attr("height", height + margin.top + margin.bottom)
  52. .append("g")
  53. .attr("transform", "translate(" + margin.left + "," + margin.top + ")");
  54.  
  55. d3.csv("tweets.csv", type, function(error, data) {
  56. if (error) throw error;
  57.  
  58. y.domain([0, d3.max(data, function(d) { return d.rate; })]);
  59.  
  60. svg.append("g")
  61. .attr("class", "axis axis--x")
  62. .attr("transform", "translate(0," + height + ")")
  63. .call(d3.svg.axis()
  64. .scale(x)
  65. .orient("bottom")
  66. .tickFormat(d3.time.format.utc("%I %p")));
  67.  
  68. svg.append("g")
  69. .attr("class", "dots")
  70. .selectAll("path")
  71. .data(data)
  72. .enter().append("path")
  73. .attr("transform", function(d) { return "translate(" + x(d.time) + "," + y(d.rate) + ")"; })
  74. .attr("d", d3.svg.symbol()
  75. .size(40));
  76.  
  77. var tick = svg.append("g")
  78. .attr("class", "axis axis--y")
  79. .call(d3.svg.axis()
  80. .scale(y)
  81. .tickSize(-width)
  82. .orient("left"))
  83. .select(".tick:last-of-type");
  84.  
  85. var title = tick.append("text")
  86. .attr("dy", ".32em")
  87. .text("tweets per hour");
  88.  
  89. tick.select("line")
  90. .attr("x1", title.node().getBBox().width + 6);
  91. });
  92.  
  93. function type(d) {
  94. d.rate = +d.count / 327 * 60; // January 8 to November 30
  95. d.time = parseTime(d.time);
  96. d.time.setUTCHours((d.time.getUTCHours() + 24 - 7) % 24);
  97. return d;
  98. }
  99.  
  100. </script>
Add Comment
Please, Sign In to add comment