我想顯示與voronoi區域相關的城市名稱和人口懸停。然而,在我如何製作voronoi區域時,我只能發送座標數據並且所有的圖紙都工作,或者發送更多的數據,並且沒有繪製voronoi區域(b/c它不能讀取非voronoi區域)座標數據,我不知道如何在數組或對象中指定,至少在創建voronois時)。我可以爲工具提示輸入靜態或不相關的數據(正如我在下面所做的那樣),但不是來自實際數據集的任何數據。當懸停在voronoi多邊形上時顯示信息(在D3.js中)
var tooltip = d3.select("body")
.append("div")
.style("position", "absolute")
.style("z-index", "10")
.style("visibility", "hidden")
.text("a simple tooltip");
var voronoi = d3.geom.voronoi()
.clipExtent([[0, 0], [w, h]]);
d3.csv("us-cities1.csv", function(d) {
return [projection([+d.lon, +d.lat])[0], projection([+d.lon, +d.lat])[1]];
}, function(error, rows) {
vertices = rows;
drawV(vertices);
}
);
function polygon(d) {
return "M" + d.join("L") + "Z";
}
function drawV(d) {
svg.append("g")
.selectAll("path")
.data(voronoi(d), polygon)
.enter().append("path")
.attr("class", "test")
.attr("d", polygon)
.attr("id", function(d, i){return i;})
.on("mouseover", function(){return tooltip.style("visibility", "visible");})
.on("mousemove", function(){return tooltip.style("top", (event.pageY-10)+"px").style("left",(event.pageX+10)+"px").text((this).id);})
.on("mouseout", function(){return tooltip.style("visibility", "hidden");});
svg.selectAll("circle")
.data(d)
.enter().append("circle")
.attr("class", "city")
.attr("transform", function(d) { return "translate(" + d + ")"; })
.attr("r", 2);
}
可以爲x和y的維諾指定訪問函數(見[這裏](https://github.com/mbostock/d3/wiki/Voronoi- GEOM#維基-X))。這允許您指定如何提取座標,並且可以有其他數據。 –
我顯然不理解如何使用該語法。你能提供一個快速的例子嗎?我的數據有標題「排名」,「地點」,「人口」,「拉特」和「lon」。我嘗試了很多變化,但我認爲最有意義的是: d3.csv(「us- cities1.csv」,function(d){ \t svg.append(「g」) .selectAll (「路徑」) .data(voronoi.x(+ d.lon).y(+ d.lat),多邊形) .... – NickSilhacek
這應該可行,但是在您的鼠標懸停功能中,您必須添加訪問數據的代碼(它將自動作爲鼠標懸停功能的第一個參數傳遞)並設置工具提示文本。[教程] [http://chimera.labs.oreilly.com/books/1230000000345/ch10。 html#_binding_event_listeners)來自Scott Murray的* Interactive Data Visualization for the Web *。 – AmeliaBR