2015-07-03 65 views
4

我與D3世界地圖實驗和用這個例子來建立在:http://techslides.com/demos/d3/worldmap-template.htmlD3地圖提示

現在我想實現一個類似於在地方的國家(即加亮和顯示工具提示名稱)繪製到地圖上的城市。

到目前爲止,我已粘貼並略微更改了國家/地區工具提示的代碼,並試圖將其與csv的城市日期連接起來。 這與原來的註釋代碼和我的複製粘貼的後半部分:

//function to add points and text to the map (used in plotting capitals) 
function addpoint(lat,lon,text) { 
    var gpoint = g.append("g").attr("class", "gpoint"); 
    var x = projection([lat,lon])[0]; 
    var y = projection([lat,lon])[1]; 

    gpoint.append("svg:circle") 
     .attr("cx", x) 
     .attr("cy", y) 
     .attr("class","point") 
     .attr("r", 1.5); 

    //conditional in case a point has no associated text 
    if(text.length>0){ 
     gpoint.append("text")  
      .attr("x", x+2) 
      .attr("y", y+2) 
      .attr("class","text")  
      .text(text); 
    } 


gpoint 

    .on("mousemove", function(d,i) { 


     var mouses = d3.mouse(svg.node()) 
      .map(function(d) { return parseInt(d); }); 

     tooltip.classed("hidden", false) 
      .attr("style", "left:"+(mouses[0])+"px;top:"+(mouses[1])+"px") 
      .html(d.CapitalName);              
    }) 


    .on("mouseout", function(d,i) { 
     tooltip.classed("hidden", true); 
    }); 

現在當我將鼠標懸停在首都之一它給了我'不能讀取屬性未定義「CapitalName」。

任何人都可以幫助我嗎?

+0

你能不能給我們一個plunker或小提琴來看待? –

+0

很想,但我是noob,不能讓外部文件正確加載... – luoar

+0

好吧,大家都是小菜鳥。試着設置它,我相信你可以做到這一點。或者上傳到你自己的網站空間? – kwoxer

回答

2

正如我在我的評論說,

Have you bound any data to gpoint? It doesn't look like it, so d3 isn't going to pass a datum (the d in your mousemove function). Hence the error: Cannot read property 'CapitalName' of undefined

這是監守您不使用d3數據綁定。如果我正確地讀你的代碼,你正在做這樣的事情:

var myDat = [{lat: 34, lon: 39, CapitalName: "akdjf"}, etc...] 
for (var i = 0; i < myDat.length; i++){ 
    addpoint(myDat[i].lat,myDat[i].lon,myDat[i].CapitalName); 
} 

d3不過,希望你的數據綁定,然後在內部循環。像這樣的東西(沒有經過測試的,但希望你能明白我的意思):

d3.csv("data/country-capitals.csv", function(err, capitals) { 

    var gpoint = g.selectAll('.gpoint') 
     .data(capitals) //<-- bind your data 
     .enter() //<-- enter selection 
     .append("g") 
     .attr("class", "gpoint"); 

    gpoint.append("circle") 
     .attr("cx", function(d, i){ 
     return projection([d.lat,d.lon])[0]; //<-- bound data and d is passed in... 
     }).attr("cy", function(d, i){ 
     return projection([d.lat,d.lon])[1]; 
     }); 

    gpoint.on("mousemove", function(d,i) { 
     var coors = d3.mouse(this); 
     tooltip.classed("hidden", false) 
      .attr("style", "left:"+(coors.x)+"px;top:"+(coors.y)+"px") //<- use d3.mosue to get position 
      .html(d.CapitalName); //<-- bound data d is passed...             
     }); 
} 

編輯註釋

是的,你需要轉換爲數字。 d3提供了一個handy callback吧:

d3.csv("data/country-capitals.csv", function(d) { 
    return { 
    CapitalLongitude = +d.CapitalLongitude, 
    CapitalLatitude = +d.CapitalLatitude, 
    CapitalName = d.CapitalName 
    }; 
}, function(error, capitals) { 
    // rest of code here 
}); 
+0

謝謝,這似乎是工作可能只需要轉經/緯度數據轉換成數字,也許它會工作 – luoar

+0

@luoar ,請參閱數字轉換的更新答案 – Mark

+0

非常感謝@Mark,設法使其工作! – luoar