我是 d3 的新手,我正在尝试在制作图表时在悬停/鼠标悬停时添加文本。任何人都可以请提出一种实现它的方法吗?我尝试了很多选项,比如创建一个 div 变量来选择 div id,将标题附加到圆圈并给出文本,但到目前为止没有任何效果。
这是我的js代码:
var width = 900,
height = 500;
var svg = d3.select("#chart")
.append("svg")
.attr("height", height)
.attr("width", width)
.append("g")
.attr("transform", "translate(" + width/2+"," + height/2 +")")
var defs = svg.append("defs");
defs.append("pattern")
.attr("id", "flag")
.attr("height", "100%")
.attr("width", "100%")
.attr("patternContentUnits", "objectBoundingBox")
.append("image")
.attr("height", 1)
.attr("width", 1)
.attr("preserveAspectRatio", "none")
.attr("xmlns:xlink", "http://www.w3.org/1999/xlink")
.attr("xlink:href", "images/usa.gif");
var radiusScale = d3.scaleSqrt().domain([0, 61000]).range([5, 40])
// the simulation is a collection of forces
// about where we want our circles to go
// and how we want our circles to interact
// STEP ONE: get them to the middle
// STEP TWO: don't have them collide!!!
var simulation = d3.forceSimulation()
.force("x", d3.forceX().strength(0.05))
.force("y", d3.forceY().strength(0.05))
.force("collide", d3.forceCollide(function(d) {
return radiusScale(d.casualities) + 1;
}))
d3.csv("sub_country.csv ").then(ready)
function ready (datapoints) {
defs.selectAll(".flag-pattern")
.data(datapoints)
.enter().append("pattern")
.attr("class", "flag-pattern")
.attr("id", function(d) {
return d.Country
})
.attr("height", "100%")
.attr("width", "100%")
.attr("patternContentUnits", "objectBoundingBox")
.append("image")
.attr("height", 1)
.attr("width", 1)
.attr("preserveAspectRatio", "none")
.attr("xmlns:xlink", "http://www.w3.org/1999/xlink")
.attr("xlink:href", function(d) {
return d.image_path
});
拉风的咖菲猫
相关分类