我正在努力进行视觉化 http://bost.ocks.org/mike/nations/:

enter image description here

我对插值功能有一个查询。最初称为mousemove,称为displayyear,在该示例中调用了Interaplate数据。

    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];
  }

这个功能有什么作用?我尝试调试,发现它比较了2年并进行计算,然后返回值。谁能阐述这个?

JSON文件包含

[
{

    "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]]
}

]
有帮助吗?

解决方案

该代码的目的是缺少一年(或更多)数据的情况,在您发布的示例数据中似乎并非如此。首先,它发现使用的最接近的数据点使用 bisect.left 然后计算上一个数据点和当前数据点之间的推断。

特别注意 year - a[0] 在您的情况下,所有年份都存在(这计算了要求的年度和发现年之间的差异)。因此,整个期限为0,返回值对应于 a[1].

如果要求的一年不存在, t 表示描述上一年变化的因素 b 和当年 a. 。这种变化因素用于推断过去 a 获得所需年的价值。

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top