我正在尝试使用d3.js制作虹膜数据集的散点图。我有一些问题显示工具提示靠近鼠标指针。使用以下代码,工具提示将显示在x轴的底部。
<!DOCTYPE html>
<meta charset="utf-8">
<style>
body { font: 12px Arial;}
path {
stroke: black;
stroke-width: 10;
fill: none;
}
.axis path,
.axis line {
fill: none;
stroke: black;
stroke-width: 1;
shape-rendering: crispEdges;
}
.area {
fill: none;
clip-path: url(#clip);}
.dot{
fill: #000;
opacity: 0.2;
}
div.tooltip {
position: absolute;
text-align: center;
width: 60px;
height: 28px;
font: 12px sans-serif;
background: lightsteelblue;
pointer-events: none;
}
</style>
<body>
<script src = "http://d3js.org/d3.v3.js"> </script>
<script>
var margin = {top:30,right:40,bottom:30,left:50},
width = 800 - margin.left - margin.right,
height = 470 - margin.top - margin.bottom;
var x = d3.scale.linear().range([0,width]),
y = d3.scale.linear().range([height,0]);
var labels = d3.scale.category10();
var xAxis = d3.svg.axis().scale(x).orient("bottom")
.ticks(5),
yAxis = d3.svg.axis().scale(y).orient("left")
.ticks(5);
var svg = d3.select("body")
.append("svg")
.attr("width", width + margin.left + margin.right)
.attr("height", height + margin.top + margin.bottom)
.append("g")
.attr("transform",
"translate(" + margin.left + "," + margin.top + ")");
var div = d3.select("body").append("div")
.attr("class", "tooltip")
.style("opacity",0);
d3.csv("iris.csv", function(error, data){
x.domain(d3.extent(data,function(d){return +d.sepal_length;}));
y.domain([0,d3.max(data,function(d){return +d.sepal_width})]);
// x axis
svg.append("g")
.attr("class","x axis")
.attr("transform", "translate(0," + height + ")")
.call(xAxis)
.append("text")
.attr("class", "label y")
.attr("x", width)
.attr("y", -7)
.style("text-anchor", "end")
.text("sepal_length");
//y axis
svg.append("g")
.attr("class", "y axis")
.call(yAxis)
.append("text")
.attr("class", "label y")
.attr("y",-26)
.attr("transform", "rotate(-90)")
.style("text-anchor", "end")
.text("sepal_width");
//clip path
svg.append("defs").append("clipPath")
.attr("id","clip")
.append("rect")
.attr("x",5)
.attr("y",5)
.attr("width", width -5)
.attr("height",height-5);
// add points
svg.append("g")
.attr("id","circles")
.attr("clip-path","url(#clip)")
.selectAll("circle")
.data(data)
.enter()
.append("circle")
.attr("class", "dot")
.attr("cx", function(d){return x(+d.sepal_length);})
.attr("cy", function(d){return y(+d.sepal_width);})
.attr("r", function(d){return +d.petal_width*2;})
.style("fill", function(d){return labels(d.species);})
.on("mouseover", function(d){
div.transition()
.duration(200)
.style("opacity", .9);
div.html(d.species)
.style("left", (d3.event.pageX - 50)+"px")
.style("right",(d3.event.pageY - 250)+"px");
})
.on("mouseout",function(d){
div.transition()
.duration(500)
.style("opacity",0);
});
var legend = svg.selectAll(".legend")
.data(labels.domain())
.enter().append("g")
.attr("class", "legend")
.attr("transform", function(d,i)
{return "translate(0," + i * 20 + ")";});
legend.append("rect")
.attr("x", width -18)
.attr("width",18)
.attr("height",18)
.style("fill", labels);
legend.append("text")
.attr("x",width - 24)
.attr("y", 12)
.style("text-anchor", "end")
.text(function(d) { return d; });
});
</script>
</body>
您知道我该如何解决这个问题?
非常感谢!!
答案 0 :(得分:2)
鼠标悬停功能中的代码:
div.html(d.species)
.style("left", (d3.event.pageX - 50) + "px")
.style("right", (d3.event.pageY ) + "px");
应该是(注意样式权利被顶部替换)
div.html(d.species)
.style("left", (d3.event.pageX - 50) + "px")
.style("top", (d3.event.pageY ) + "px");
完整的工作代码here
希望这有帮助!
答案 1 :(得分:2)
非常感谢你的回答。这非常有帮助。我还找到了另一个解决方案,我将d3.event放入了#34; mousemove&#34;功能如下:
.on("mousemove", function(d){
div.style('top', (d3.event.pageY + 10)+'px')
.style('left', (d3.event.pageX + 10)+'px');
它也可以正常工作。再次感谢。