block by jmuyskens df89fd5d8a5ac0e2890d030f4db5d6cb

Scatterplot by time of day (annotation)

Full Screen

Demonstration of transforming CSV data using D3

For NICAR Beginning D3, 2017

Charts forked from d3indepth‘s block: DOM Manipulation

forked from darlacameron‘s block: 2. Transforming data

forked from darlacameron‘s block: 3. Drawing shapes

forked from anonymous‘s block: Time series chart with real data

forked from jmuyskens‘s block: Multiple time series chart with real data

forked from jmuyskens‘s block: Scatterplot with real data

forked from jmuyskens‘s block: Scatterplot with real data (playing with axis formatting)

forked from jmuyskens‘s block: Scatterplot with real data (annotation)

forked from jmuyskens‘s block: Scatterplot by time of day (annotation)

script.js

var parseTime = d3.timeParse('%a %b %d %H:%M:%S +0000 %Y');

// this function creates an SVG line chart
var lineChart = function(data){
  
  data = data.map(function(d) {
    var date = parseTime(d.created_at);
		date.setMonth(0);
		date.setDate(0);
    date.setFullYear(2017);
		d.date = date
    return d;
  });
  
  // set the dimensions and margins of the graph
  var margin = {top: 20, right: 20, bottom: 60, left: 50},
      width = 950 - 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]);
  //var rScale = d3.scaleSqrt().range([0, 30]);

  //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 + ")");
  
  var tweetTextDiv = d3.select("body").append('div')
  	.attr('id', 'tweettext');

  // Scale the range of the data
  x.domain(d3.extent(data, function(d) { return d.date; }));
  y.domain(d3.extent(data, function(d) { return +d.favorite_count; }));
  //rScale.domain([0, d3.max(data, function(d) { return +d.retweet_count; })]);
  
  // Add the X Axis
  svg.append("g")
  	.attr('class', 'x axis')
    .call(d3.axisBottom(x).tickSize(height).ticks(6));

  // 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('r', function(d) { return rScale(+d.retweet_count); })
    .attr('cx', function(d) { return x(d.date); })
    .attr('cy', function(d) { return y(+d.favorite_count); })
    .on('mouseenter', function(d) {
    	console.log(d.created_at, d.text);
      tweetTextDiv.text(d.text);
  	})
};

index.html

<!DOCTYPE html>
<meta charset="utf-8">
<head>
	<title>Transforming data</title>
</head>

<style>
  body {
    font-family: Helvetica Neue, sans-serif;
    
  }
  
  svg {
    overflow: visible;
  }
  
	path {
    fill: none;
    stroke: #aaa;
  }
  
  circle {
    opacity: 0.26;
  }
  
  .axis path {
    stroke: none;
  }
  
  .axis line {
    stroke: #ccc;
  }
  
  .axis.x line {
    stroke-dasharray: 3px 2px;
  }
  
  .circle.Android {
    fill: red;
  }
  
  line.annotation {
    stroke: black;
  }

</style>

<body>

	<script src="//d3js.org/d3.v4.min.js"></script>
  
	<script src="script.js"></script>
  
  <script>
    d3.csv("realdonaldtrumpsinceelection.csv", function(error, data) {
      if (error) throw error;
      lineChart(data);
    });
  </script>
</body>
</html>