Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <script>
- var label = d3.select(".label");
- // Set the dimensions of the canvas / graph
- var margin = {top: 30, right: 20, bottom: 30, left: 50},
- width = 600 - margin.left - margin.right,
- height = 270 - margin.top - margin.bottom;
- // Parse the date / time
- var parseDate = d3.time.format("%y%m%d").parse;
- // Set the ranges
- var x = d3.time.scale().range([0, width]);
- var y = d3.scale.linear().range([height, 0]);
- // Define the axes
- var xAxis = d3.svg.axis().scale(x)
- .orient("bottom").ticks(5);
- var yAxis = d3.svg.axis().scale(y)
- .orient("left").ticks(5);
- // Define the line
- var valueline = d3.svg.line()
- .x(function(d) { return x(d.date); })
- .y(function(d) { return y(d.positiveIncrease); });
- // Adds the svg canvas
- var svg = d3.select("body")
- .append("svg")
- .attr("width", width + margin.left + margin.right)
- .attr("height", height + margin.top + margin.bottom)
- .append("g")
- .attr("transform", "translate(" + margin.left + "," + margin.top + ")");
- // Get the data
- // var url = 'https://covidtracking.com/api/v1/states/nj/daily.json'
- // d3.json(url, function(error, data) {
- // data.forEach(function(d) {
- // d.positiveIncrease = d.positiveIncrease;
- // d.date = parseDate(d.date);
- // });
- // Create a request variable and assign a new XMLHttpRequest object to it.
- var request = new XMLHttpRequest()
- // Open a new connection, using the GET request on the URL endpoint
- request.open('GET', 'https://covidtracking.com/api/v1/states/nj/daily.json', true)
- request.onload = function () {
- // Begin accessing JSON data here
- var data = JSON.parse(this.response)
- let dataY = []
- let dataX = []
- // for (i = data.length - 1; i >= 0; i--) {
- // dataArray[i] = data[i].positiveIncrease;
- // }
- let i = 0;
- data.reverse().forEach((cases) => {
- // console.log(cases.positiveIncrease)
- dataY[i] = cases.positiveIncrease;
- dataX[i] = cases.date;
- i++;
- })
- function parseData(data){
- var arr = []
- for (var i in dataY.length){
- arr.push({
- date: new dataX[i],
- value : dataY[i]
- }
- )
- return arr;
- }
- }
- // Scale the range of the data
- x.domain(d3.extent(data, function(d) { return d.date; }));
- y.domain([0, d3.max(data, function(d) { return d.positiveIncrease; })]);
- // Add the valueline path.
- svg.append("path") // Add the valueline path.
- .attr("class", "line")
- .attr("d", valueline(data));
- // Add the valueline path.
- svg // Add the valueline path.
- .selectAll("circle")
- .data(data)
- .enter()
- .append("circle")
- .attr("r", 10)
- .attr("cx", function(d) {
- return x(d.date)
- })
- .attr("cy", function(d) {
- return y(d.close)
- })
- .on("mouseover", function(d,i) {
- label.style("transform", "translate("+ x(d.date) +"px," + (y(d.positiveIncrease)) +"px)")
- label.text(d.positiveIncrease)
- });
- // Add the X Axis
- svg.append("g") // Add the X Axis
- .attr("class", "x axis")
- .attr("transform", "translate(0," + height + ")")
- .call(xAxis);
- // Add the Y Axis
- svg.append("g") // Add the Y Axis
- .attr("class", "y axis")
- .call(yAxis);
- });
- </script>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement