Advertisement
Guest User

Untitled

a guest
Jul 24th, 2020
99
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.00 KB | None | 0 0
  1. // Create a request variable and assign a new XMLHttpRequest object to it.
  2. var request = new XMLHttpRequest()
  3.  
  4. // Open a new connection, using the GET request on the URL endpoint
  5. request.open('GET', 'https://covidtracking.com/api/v1/states/nj/daily.json', true)
  6.  
  7. request.onload = function () {
  8. // Begin accessing JSON data here
  9. var data = JSON.parse(this.response)
  10. let dataY = []
  11. let dataX = []
  12.  
  13. let i = 0;
  14. data.reverse().forEach((cases) => {
  15. // console.log(cases.positiveIncrease)
  16. dataY[i] = cases.positiveIncrease;
  17. dataX[i] = cases.date;
  18. i++;
  19. })
  20. }
  21.  
  22.  
  23.  
  24. // set the dimensions and margins of the graph
  25. var margin = { top: 10, right: 30, bottom: 30, left: 60 },
  26. width = 460 - margin.left - margin.right,
  27. height = 400 - margin.top - margin.bottom;
  28.  
  29. // append the svg object to the body of the page
  30. var svg = d3.select("#my_dataviz")
  31. .append("svg")
  32. .attr("width", width + margin.left + margin.right)
  33. .attr("height", height + margin.top + margin.bottom)
  34. .append("g")
  35. .attr("transform",
  36. "translate(" + margin.left + "," + margin.top + ")");
  37.  
  38. //Read the data
  39. //Removed
  40.  
  41. // When reading the csv, I must format variables:
  42. function (d) {
  43. return { date: d3.timeParse("%Y%m%d")(d.dataX), value: d.dataY }
  44. },
  45.  
  46. // Now I can use this dataset:
  47. function (data) {
  48.  
  49. // Add X axis --> it is a date format
  50. var x = d3.scaleTime()
  51. .domain(d3.extent(data, function (d) { return d.dataX; }))
  52. .range([0, width]);
  53. svg.append("g")
  54. .attr("transform", "translate(0," + height + ")")
  55. .call(d3.axisBottom(x));
  56.  
  57. // Add Y axis
  58. var y = d3.scaleLinear()
  59. .domain([0, d3.max(data, function (d) { return +d.dataY; })])
  60. .range([height, 0]);
  61. svg.append("g")
  62. .call(d3.axisLeft(y));
  63.  
  64. // Add the line
  65. svg.append("path")
  66. .datum(data)
  67. .attr("fill", "none")
  68. .attr("stroke", "steelblue")
  69. .attr("stroke-width", 1.5)
  70. .attr("d", d3.line()
  71. .x(function (d) { return x(d.dataX) })
  72. .y(function (d) { return y(d.dataY) })
  73. )
  74.  
  75. })
  76.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement