text: // Global vars
  var pymChild = null;
  var isMobile = false;
  var dataSeries = [];
  var obamaDates = [ d3.time.format('%m/%d/%Y').parse('01/20/2009'),
                     d3.time.format('%m/%d/%Y').parse('07/17/2016') ];
  
  /*
   * Initialize graphic
   */
  var onWindowLoaded = function() {
      if (Modernizr.svg) {
          formatData();
  
          pymChild = new pym.Child({
              renderCallback: render
          });
      } else {
          pymChild = new pym.Child({});
      }
  
      pymChild.onMessage('on-screen', function(bucket) {
          ANALYTICS.trackEvent('on-screen', bucket);
      });
      pymChild.onMessage('scroll-depth', function(data) {
          data = JSON.parse(data);
          ANALYTICS.trackEvent('scroll-depth', data.percent, data.seconds);
      });
  }
  
  /*
   * Format graphic data for processing by D3.
   */
  var formatData = function() {
      DATA.forEach(function(d) {
          d['date'] = d3.time.format('%m/%d/%Y').parse(d['date']);
  
          delete d['week'];
          delete d['week_fmt'];
          delete d['date2'];
  
          for (var key in d) {
              if (key != 'date' && d[key] != null && d[key].length > 0) {
                  d[key] = +d[key];
              }
          }
      });
  
      /*
       * Restructure tabular data for easier charting.
       */
      for (var column in DATA[0]) {
          if (column == 'date') {
              continue;
          }
  
          dataSeries.push({
              'name': column,
              'values': DATA.map(function(d) {
                  return {
                      'date': d['date'],
                      'amt': d[column]
                  };
      // filter out empty data. uncomment this if you have inconsistent data.
      //        }).filter(function(d) {
      //            return d['amt'].length > 0;
              })
          });
      }
  }
  
  /*
   * Render the graphic(s). Called by pym with the container width.
   */
  var render = function(containerWidth) {
      if (!containerWidth) {
          containerWidth = DEFAULT_WIDTH;
      }
  
      if (containerWidth <= MOBILE_THRESHOLD) {
          isMobile = true;
      } else {
          isMobile = false;
      }
  
      // Render the chart!
      renderLineChart({
          container: '#line-chart',
          width: containerWidth,
          data: dataSeries
      });
  
      // Update iframe
      if (pymChild) {
          pymChild.sendHeight();
      }
  }
  
  /*
   * Render a line chart.
   */
  var renderLineChart = function(config) {
      /*
       * Setup
       */
      var dateColumn = 'date';
      var valueColumn = 'amt';
  
      var aspectWidth = isMobile ? 4 : 16;
      var aspectHeight = isMobile ? 3 : 9;
  
      var margins = {
          top: 20,
          right: 65,
          bottom: 20,
          left: 2
      };
  
      var ticksX = 10;
      var ticksY = 10;
      var roundTicksFactor = 5;
  
      // Mobile
      if (isMobile) {
          ticksX = 5;
          ticksY = 5;
          margins['right'] = 45;
      }
  
      // Calculate actual chart dimensions
      var chartWidth = config['width'] - margins['left'] - margins['right'];
      var chartHeight = Math.ceil((config['width'] * aspectHeight) / aspectWidth) - margins['top'] - margins['bottom'];
  
      // Clear existing graphic (for redraw)
      var containerElement = d3.select(config['container']);
      containerElement.html('');
  
      /*
       * Create D3 scale objects.
       */
      var xScale = d3.time.scale()
          .domain(d3.extent(config['data'][0]['values'], function(d) {
              return d['date'];
          }))
          .range([ 0, chartWidth ])
  
      var min = d3.min(config['data'], function(d) {
          return d3.min(d['values'], function(v) {
              return Math.floor(v[valueColumn] / roundTicksFactor) * roundTicksFactor;
          })
      });
  
      if (min > 0) {
          min = 0;
      }
  
      var max = d3.max(config['data'], function(d) {
          return d3.max(d['values'], function(v) {
              return Math.ceil(v[valueColumn] / roundTicksFactor) * roundTicksFactor;
          })
      });
  
      var yScale = d3.scale.linear()
          .domain([min, max])
          .range([chartHeight, 0]);
  
      var colorScale = d3.scale.ordinal()
          .domain(_.pluck(config['data'], 'name'))
          .range([COLORS['red3'], COLORS['yellow3'], COLORS['blue3'], COLORS['orange3'], COLORS['teal3']]);
  
      /*
       * Create the root SVG element.
       */
      var chartWrapper = containerElement.append('div')
          .attr('class', 'graphic-wrapper');
  
      var chartElement = chartWrapper.append('svg')
          .attr('width', chartWidth + margins['left'] + margins['right'])
          .attr('height', chartHeight + margins['top'] + margins['bottom'])
          .append('g')
          .attr('transform', 'translate(' + margins['left'] + ',' + margins['top'] + ')');
  
      /*
       * Create D3 axes.
       */
      var xAxis = d3.svg.axis()
          .scale(xScale)
          .orient('bottom')
          .ticks(ticksX)
          .tickFormat(function(d, i) {
              if (isMobile) {
                  return '\u2019' + fmtYearAbbrev(d);
              } else {
                  return fmtYearFull(d);
              }
          });
  
      var yAxis = d3.svg.axis()
          .scale(yScale)
          .orient('left')
          .ticks(ticksY);
  
      /*
       * Render axes to chart.
       */
      chartElement.append('g')
          .attr('class', 'x axis')
          .attr('transform', makeTranslate(0, chartHeight))
          .call(xAxis);
  
      // chartElement.append('g')
      //     .attr('class', 'y axis')
      //     .call(yAxis);
  
      /*
       * Render grid to chart.
       */
      var xAxisGrid = function() {
          return xAxis;
      }
  
      var yAxisGrid = function() {
          return yAxis;
      }
  
      chartElement.append('g')
          .attr('class', 'x grid')
          .attr('transform', makeTranslate(0, chartHeight))
          .call(xAxisGrid()
              .tickSize(-chartHeight, 0, 0)
              .tickFormat('')
          );
  
      chartElement.append('g')
          .attr('class', 'y grid')
          .call(yAxisGrid()
              .tickSize(-chartWidth, 0, 0)
              .tickFormat('')
          );
  
      /*
       * Render lines to chart.
       */
      var line = d3.svg.line()
          .interpolate('monotone')
          .x(function(d) {
              return xScale(d[dateColumn]);
          })
          .y(function(d) {
              return yScale(d[valueColumn]);
          });
  
      chartElement.append('g')
          .attr('class', 'lines')
          .selectAll('path')
          .data(config['data'])
          .enter()
          .append('path')
              .attr('class', function(d, i) {
                  return 'line ' + classify(d['name']);
              })
              .attr('stroke', function(d) {
                  return colorScale(d['name']);
              })
              .attr('d', function(d) {
                  return line(d['values']);
              });
  
      chartElement.append('g')
          .attr('class', 'value')
          .selectAll('text')
          .data(config['data'])
          .enter().append('text')
              .attr('x', function(d, i) {
                  var last = d['values'][d['values'].length - 1];
  
                  return xScale(last[dateColumn]) + 5;
              })
              .attr('y', function(d) {
                  var last = d['values'][d['values'].length - 1];
  
                  return yScale(last[valueColumn]) + 3;
              })
              .text(function(d) {
                  var last = d['values'][d['values'].length - 1];
                  var value = last[valueColumn];
                  return label = 'Week of July 17';
              })
              .call(wrapText, margins['right'] - 5, 13);
  
      var annotations = chartElement.append('g')
          .attr('class', 'annotations');
      annotations.append('line')
          .attr('x1', xScale(obamaDates[0]))
          .attr('x2', xScale(obamaDates[0]))
          .attr('y1', yScale(yScale.domain()[0]))
          .attr('y2', yScale(yScale.domain()[1]) - 3);
      annotations.append('text')
          .text('Obama takes office')
          .attr('x', xScale(obamaDates[0]))
          .attr('y', -10)
          .attr('dx', -3);
  }
  
  /*
   * Initially load the graphic
   * (NB: Use window.load to ensure all images have loaded)
   */
  window.onload = onWindowLoaded;
  