1

私は視覚化に取り組んでいますhttp://bost.ocks.org/mike/nations/ :

ここに画像の説明を入力

補間関数について質問があります。最初に、補間データが呼び出される displayYear を呼び出す mousemove が呼び出されます。

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

]
4

1 に答える 1

1

このコードが意味するのは、1 年 (またはそれ以上) のデータが欠落している場合です。これは、投稿したサンプル データには当てはまらないようです。最初に、 を使用して特定の年に最も近いデータ ポイントを見つけ、bisect.left要求された年の前のデータ ポイントと現在のデータ ポイントの間の外挿を計算します。

特に、すべての年が存在するため、この場合は常に 0 になることに注意しyear - a[0]てください (これにより、要求された年と見つかった年の差が計算されます)。したがって、項全体が 0 になり、戻り値は に対応しa[1]ます。

要求された年が存在しない場合は、前年とt今年の間の変化を表す要因を示します。この変更係数は、要求された年の値を取得するために過去を推定するために使用されます。baa

于 2013-11-08T09:35:43.757 に答える