Advertisement
Guest User

D3 line graph

a guest
Jul 24th, 2020
88
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.31 KB | None | 0 0
  1. <script>
  2. var label = d3.select(".label");
  3. // Set the dimensions of the canvas / graph
  4. var margin = {top: 30, right: 20, bottom: 30, left: 50},
  5. width = 600 - margin.left - margin.right,
  6. height = 270 - margin.top - margin.bottom;
  7.  
  8. // Parse the date / time
  9. var parseDate = d3.time.format("%y%m%d").parse;
  10.  
  11. // Set the ranges
  12. var x = d3.time.scale().range([0, width]);
  13. var y = d3.scale.linear().range([height, 0]);
  14.  
  15. // Define the axes
  16. var xAxis = d3.svg.axis().scale(x)
  17. .orient("bottom").ticks(5);
  18.  
  19. var yAxis = d3.svg.axis().scale(y)
  20. .orient("left").ticks(5);
  21.  
  22. // Define the line
  23. var valueline = d3.svg.line()
  24. .x(function(d) { return x(d.date); })
  25. .y(function(d) { return y(d.positiveIncrease); });
  26.  
  27. // Adds the svg canvas
  28. var svg = d3.select("body")
  29. .append("svg")
  30. .attr("width", width + margin.left + margin.right)
  31. .attr("height", height + margin.top + margin.bottom)
  32. .append("g")
  33. .attr("transform", "translate(" + margin.left + "," + margin.top + ")");
  34.  
  35. // Get the data
  36. // var url = 'https://covidtracking.com/api/v1/states/nj/daily.json'
  37.  
  38.  
  39. // d3.json(url, function(error, data) {
  40. // data.forEach(function(d) {
  41. // d.positiveIncrease = d.positiveIncrease;
  42. // d.date = parseDate(d.date);
  43. // });
  44.  
  45. // Create a request variable and assign a new XMLHttpRequest object to it.
  46. var request = new XMLHttpRequest()
  47.  
  48. // Open a new connection, using the GET request on the URL endpoint
  49. request.open('GET', 'https://covidtracking.com/api/v1/states/nj/daily.json', true)
  50.  
  51. request.onload = function () {
  52. // Begin accessing JSON data here
  53. var data = JSON.parse(this.response)
  54. let dataY = []
  55. let dataX = []
  56. // for (i = data.length - 1; i >= 0; i--) {
  57. // dataArray[i] = data[i].positiveIncrease;
  58. // }
  59.  
  60. let i = 0;
  61. data.reverse().forEach((cases) => {
  62. // console.log(cases.positiveIncrease)
  63. dataY[i] = cases.positiveIncrease;
  64. dataX[i] = cases.date;
  65. i++;
  66. })
  67.  
  68.  
  69. function parseData(data){
  70. var arr = []
  71. for (var i in dataY.length){
  72. arr.push({
  73. date: new dataX[i],
  74. value : dataY[i]
  75. }
  76.  
  77. )
  78. return arr;
  79. }
  80.  
  81. }
  82.  
  83.  
  84.  
  85. // Scale the range of the data
  86. x.domain(d3.extent(data, function(d) { return d.date; }));
  87. y.domain([0, d3.max(data, function(d) { return d.positiveIncrease; })]);
  88.  
  89. // Add the valueline path.
  90. svg.append("path") // Add the valueline path.
  91. .attr("class", "line")
  92. .attr("d", valueline(data));
  93.  
  94. // Add the valueline path.
  95. svg // Add the valueline path.
  96. .selectAll("circle")
  97. .data(data)
  98. .enter()
  99. .append("circle")
  100. .attr("r", 10)
  101. .attr("cx", function(d) {
  102. return x(d.date)
  103. })
  104. .attr("cy", function(d) {
  105. return y(d.close)
  106. })
  107. .on("mouseover", function(d,i) {
  108.  
  109. label.style("transform", "translate("+ x(d.date) +"px," + (y(d.positiveIncrease)) +"px)")
  110. label.text(d.positiveIncrease)
  111.  
  112. });
  113.  
  114.  
  115. // Add the X Axis
  116. svg.append("g") // Add the X Axis
  117. .attr("class", "x axis")
  118. .attr("transform", "translate(0," + height + ")")
  119. .call(xAxis);
  120.  
  121. // Add the Y Axis
  122. svg.append("g") // Add the Y Axis
  123. .attr("class", "y axis")
  124. .call(yAxis);
  125.  
  126. });
  127.  
  128. </script>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement