2015-07-03 66 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