Frage

I am working on a visulaization http://bost.ocks.org/mike/nations/:

enter image description here

I have a query as to the interpolation function. Initially the mousemove is called which calls the displayYear, where the interapolate data is called.

    function mousemove() {
      displayYear(yearScale.invert(d3.mouse(this)[0]));
    }
  }

  // Tweens the entire chart by first tweening the year, and then the data.
  // For the interpolated data, the dots and label are redrawn.
  function tweenYear() {
    var year = d3.interpolateNumber(thisyear, 2009);
    return function(t) { displayYear(year(t)); };
  }

  // Updates the display to show the specified year.
  function displayYear(year) {
    //alert(year);
    thisyear=year;

    dot.data(interpolateData(year), key).call(position).sort(order);
    label.text(Math.round(year));
  }

  // Interpolates the dataset for the given (fractional) year.
  function interpolateData(year) {
    return nations.map(function(d) {
      return {
        name: d.name,
        region: d.region,
        checkins: interpolateValues(d.checkins, year),
        teamsize: interpolateValues(d.teamsize, year),
        Checkintimes: interpolateValues(d.Checkintimes, year)
      };


// Finds (and possibly interpolates) the value for the specified year.
  function interpolateValues(values, year) {
    var i = bisect.left(values, year, 0, values.length - 1),
        a = values[i];
    if (i > 0) {
      var b = values[i - 1],
          t = (year - a[0]) / (b[0] - a[0]);
      return a[1] * (1 - t) + b[1] * t;
    }
    return a[1];
  }

what does this function do? i tried debugging and found that it compares 2 years and does a calculation and then returns the values. can anyone elaborate this?

The json file contains

[
{

    "name":"India",
    "region":"South Asia",
    "income":[[2000,225],[2001,225],[2002,226],[2003,227],[2004,229],[2005,230],[2006,231],[2007,244],[2008,254],[2009,253]],
    "population":[[2000,41542812],[2001,41623424],[2002,41866106],[2003,42186202],[2004,41521432],[2005,41827315],[2006,42127071],[2007,42420476],[2008,42707546],[2009,42707546]],
    "lifeExpectancy":[[2000,43.56],[2001,43.86],[2002,44.22],[2003,64.61],[2004,56.05],[2005,56.52],[2006,66.02],[2007,68.54],[2008,67.06],[2009,73.58]]
}

]
War es hilfreich?

Lösung

What this code is meant for is the case where a year (or more) of data is missing, which doesn't seem to be the case in the example data that you've posted. First, it finds the closest data point to the given year using bisect.left and then computes an extrapolation between the previous data point and the current data point for the requested year.

Note in particular that year - a[0] in your case will always be 0, as all the years are present (this computes the difference between the requested and found year). Hence, the whole term will be 0 and the return value corresponds to a[1].

If the year that's requested is not present, t denotes the factor that describes the change between the previous year b and the current year a. This change factor is used to extrapolate past a to get the value for the requested year.

Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top