2016-04-10 12 views
1

次の例では、ユーザーが国を選択すると、地球儀が回転してその国を中心とすることがわかります。国をクリックしたときに地球がその国に集中するように、どのようにコード化することができますか?D3 - クリックした国の中心に

D3 Globe

私はマウスオーバーイベントの前にクリックハンドラの追加を行うことを試みたが、動作するようには思えません。何か案は?

.on("click", function(d) { 
    var rotate = projection.rotate(), 
    focusedCountry = country(countries, this), 
    p = d3.geo.centroid(focusedCountry); 
    svg.selectAll(".focused").classed("focused", focused = false); 

    //Globe rotating 

    (function transition() { 
    d3.transition() 
    .duration(2500) 
    .tween("rotate", function() { 
      var r = d3.interpolate(projection.rotate(), [-p[0], -p[1]]); 
      return function(t) { 
      projection.rotate(r(t)); 
      svg.selectAll("path").attr("d", path) 
      .classed("focused", function(d, i) { return d.id == focusedCountry.id ? focused = d : false; }); 
      }; 
      }) 
    })(); 
}) 

答えて

3

まず、このようなパスにクリックを作成します。クリックしたデータのための

.on("click", function(d){rotateMe(d);}) 

変更コード。クリックしたパスのIDを取得し、その国のデータを取得します。

var rotateMe = function(d) { 
    var rotate = projection.rotate(), 
    focusedCountry = country(countries, d.id),//get the clicked country's details 
    p = d3.geo.centroid(focusedCountry); 
    console.log(focusedCountry, "hello") 
    svg.selectAll(".focused").classed("focused", focused = false); 

//Globe rotating 

(function transition() { 
    d3.transition() 
    .duration(2500) 
    .tween("rotate", function() { 
    var r = d3.interpolate(projection.rotate(), [-p[0], -p[1]]); 
    return function(t) { 
     projection.rotate(r(t)); 
     svg.selectAll("path").attr("d", path) 
     .classed("focused", function(d, i) { return d.id == focusedCountry.id ? focused = d : false; }); 
    }; 
    }) 
    })(); 
}; 

作業コードhere

+0

あなたが面白いものを知っていますか?国際日付ラインに合格すると、地球儀は元に戻ります!たとえば、地球を2〜3回回転させ、特定の国を中心付近に配置します。あなたがその国をクリックすると、地球がちょっと回転して国を中心に置くことになるでしょうか?しかし、代わりに、それはそれらを2〜3回戻して、国を中心にします! –

+0

それを何度も試してみましたが、珍しい何も見つかりませんでした... – Cyril

+0

本当に?多分私のブラウザですか?これを試してみましょう:あなたの投影は、西アフリカで始まります。プロジェクションの中心にオーストラリアを得るまで、プロジェクションを西に向かって動かします(グローブを西から東に向ける)。オーストラリアをクリックします。私のブラウザでは、地球儀は東への完全な回りを回し、その後オーストラリアを中心にします。 –

関連する問題