var parseTime = d3.timeParse('%a %b %d %H:%M:%S +0000 %Y'); var formatTime = d3.timeFormat('%Y-%m-%d'); var parseFormattedTime = d3.timeParse('%Y-%m-%d'); var lineChart = function(data){ // set the dimensions and margins of the graph var margin = {top: 20, right: 20, bottom: 60, left: 50}, width = 800 - margin.left - margin.right, height = 600 - margin.top - margin.bottom; // set the ranges var x = d3.scaleTime().range([0, width]); var y = d3.scaleLinear().range([height, 0]); //add a new svg to hold the chart 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 + ")"); // Scale the range of the data x.domain(d3.extent(data, function(d) { return parseTime(d.created_at); })); y.domain(d3.extent(data, function(d) { return +d.favorite_count; })); // Add the X Axis svg.append("g") .attr('class', 'x axis') .call(d3.axisBottom(x).tickSize(height).ticks(5)); // Add the Y Axis svg.append("g") .attr('class', 'y axis') .attr('transform', 'translate(' + width + ', 0)') .call(d3.axisLeft(y).tickSize(width).ticks(7)); // Add a container for the circles var circles = svg.append('g') .attr('id', 'circles'); // Add the line path circles.selectAll("circle") .data(data) .enter().append('circle') .attr('class', function(d) { return 'circle ' + d.source; }) .attr('r', 3) .attr('cx', function(d) { return x(parseTime(d.created_at)); }) .attr('cy', function(d) { return y(+d.favorite_count); }) // add annotations var annotation = svg.append('g') .attr('transform', 'translate(' + x(parseFormattedTime("2017-01-20")) + ',0)') annotation.append('line') .attr('class', 'annotation') .attr('y1', 0) .attr('y2', height) annotation.append('text') .text('Inauguration day') .attr('text-anchor', 'end') .attr('dx', -5) .attr('dy', 10) };