block by tophtucker f3395144c2d2a2dd695e

Mock polygraph illustration

Full Screen

Based on the chart at the bottom of http://bost.ocks.org/mike/path/, a mock polygraph illustration driven by mouse and scroll events, with some sine noise. Used in this story: http://www.bloomberg.com/graphics/2015-doug-williams-war-on-lie-detector/

index.html

<!DOCTYPE html>
<meta charset="utf-8">

<style>

body {
  height: 5000px;
  margin: 0;
  padding: 0;
}

svg {
  font: 10px sans-serif;
}

.axis path, .axis line {
  fill: none;
  stroke: #000;
  shape-rendering: crispEdges;
}

.x.axis {
  display: none;
}

.x.axis line {
  shape-rendering: auto;
}

.line {
  fill: none;
  stroke: #e55c47;
  stroke-width: 4px;
}

</style>

<body></body>

<script src="d3.min.js"></script>
<script>(function() {

var n = 80,
    duration = 100,
    now = new Date(Date.now() - duration),
    count = 0,
    data = d3.range(n).map(function() { return 0; });

var margin = {top: 6, right: 0, bottom: 20, left: 40},
    width = innerWidth - margin.right,
    height = 220 - margin.top - margin.bottom;

var x = d3.time.scale()
    .domain([now - (n - 2) * duration, now - duration])
    .range([0, width]);

var y = d3.scale.linear()
    .range([height, 0]);

var line = d3.svg.line()
    .interpolate("basis")
    .x(function(d, i) { return x(now - (n - 1 - i) * duration); })
    .y(function(d, i) { return y(d); });

var svg = d3.select("body").append("p").append("svg")
    .attr("width", width + margin.left + margin.right)
    .attr("height", height + margin.top + margin.bottom)
    .style("margin-left", -margin.left + "px")
  .append("g")
    .attr("transform", "translate(" + margin.left + "," + margin.top + ")");

svg.append("defs").append("clipPath")
    .attr("id", "clip")
  .append("rect")
    .attr("width", width)
    .attr("height", height);

var axis = svg.append("g")
    .attr("class", "x axis")
    .attr("transform", "translate(0," + height + ")")
    .call(x.axis = d3.svg.axis().scale(x).orient("bottom"));

var path = svg.append("g")
    .attr("clip-path", "url(#clip)")
  .append("path")
    .datum(data)
    .attr("class", "line");

var transition = d3.select({}).transition()
    .duration(100)
    .ease("linear");

d3.select(window)
    .on("scroll", function() { ++count; })
    .on("mousemove", function() { ++count; });

(function tick() {
  transition = transition.each(function() {

    // update the domains
    now = new Date();
    x.domain([now - (n - 2) * duration, now - duration]);
    y.domain([-5, d3.max(data)]);

    // push the accumulated count onto the back, and reset the count

    count += 3 * Math.sin(+new Date() / 10);

    data.push(Math.min(60, count));
    count = 0;

    // redraw the line
    svg.select(".line")
        .attr("d", line)
        .attr("transform", null);

    // slide the x-axis left
    axis.call(x.axis);

    // slide the line left
    path.transition()
        .attr("transform", "translate(" + x(now - (n - 1) * duration) + ")");

    // pop the old data point off the front
    data.shift();

  }).transition().each("start", tick);
})();

})()</script>