Javascript 在散点图中使用d3.js在鼠标指针附近显示工具提示
我正在尝试使用d3.js绘制虹膜数据集的散点图。我在鼠标指针附近显示工具提示时遇到一些问题。使用以下代码,工具提示将显示在x轴的底部Javascript 在散点图中使用d3.js在鼠标指针附近显示工具提示,javascript,d3.js,scatter-plot,Javascript,D3.js,Scatter Plot,我正在尝试使用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
<!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>
正文{font:12px Arial;}
路径{
笔画:黑色;
笔画宽度:10;
填充:无;
}
.轴线路径,
.轴线{
填充:无;
笔画:黑色;
笔画宽度:1;
形状渲染:边缘清晰;
}
.区域{
填充:无;
剪辑路径:url(#剪辑);}
多特先生{
填写:#000;
不透明度:0.2;
}
div.tooltip{
位置:绝对位置;
文本对齐:居中;
宽度:60px;
高度:28px;
字体:12px无衬线;
背景:淡蓝色;
指针事件:无;
}
var margin={顶部:30,右侧:40,底部:30,左侧:50},
宽度=800-边距.左-边距.右,
高度=470-margin.top-margin.bottom;
var x=d3.scale.linear().range([0,宽度]),
y=d3.刻度.线性().范围([高度,0]);
变量标签=d3.scale.category10();
var xAxis=d3.svg.axis().scale(x).oriented(“底部”)
.滴答声(5),
yAxis=d3.svg.axis().scale(y).orient(“左”)
.蜱(5);
var svg=d3.选择(“主体”)
.append(“svg”)
.attr(“宽度”,宽度+边距。左侧+边距。右侧)
.attr(“高度”,高度+边距。顶部+边距。底部)
.附加(“g”)
.attr(“转换”,
“翻译(“+margin.left+”,“+margin.top+”);
var div=d3.选择(“主体”).追加(“div”)
.attr(“类”、“工具提示”)
.样式(“不透明度”,0);
d3.csv(“iris.csv”),函数(错误、数据){
x、 域(d3.extent(数据,函数(d){return+d.sepal_length;}));
y、 域([0,d3.max(数据,函数(d){return+d.sepal_width})];
//x轴
svg.append(“g”)
.attr(“类”、“x轴”)
.attr(“变换”、“平移(0)”、“高度+”)
.呼叫(xAxis)
.append(“文本”)
.attr(“类别”、“标签y”)
.attr(“x”,宽度)
.attr(“y”,-7)
.style(“文本锚定”、“结束”)
.文本(“萼片长度”);
//y轴
svg.append(“g”)
.attr(“类”、“y轴”)
.呼叫(yAxis)
.append(“文本”)
.attr(“类别”、“标签y”)
.attr(“y”,-26)
.attr(“变换”、“旋转(-90)”)
.style(“文本锚定”、“结束”)
.文本(“萼片宽度”);
//剪辑路径
svg.append(“defs”).append(“clipPath”)
.attr(“id”、“剪辑”)
.append(“rect”)
.attr(“x”,5)
.attr(“y”,5)
.attr(“宽度”,宽度-5)
.attr(“高度”,高度-5);
//加分
svg.append(“g”)
.attr(“id”、“圆圈”)
.attr(“剪辑路径”、“url(#剪辑)”)
.selectAll(“圆圈”)
.数据(数据)
.输入()
.附加(“圆圈”)
.attr(“类”、“点”)
.attr(“cx”,函数(d){returnx(+d.sepal_length);})
.attr(“cy”,函数(d){returny y(+d.sepal_width);})
.attr(“r”,函数(d){return+d.petal_width*2;})
.style(“fill”,函数(d){返回标签(d.species);})
.on(“鼠标悬停”,功能(d){
过渡部()
.持续时间(200)
.样式(“不透明度”,.9);
div.html(d.species)
.style(“左”(d3.event.pageX-50)+“px”)
.style(“右”),(d3.event.pageY-250)+“px”);
})
.开启(“鼠标出”,功能(d){
过渡部()
.持续时间(500)
.样式(“不透明度”,0);
});
var legend=svg.selectAll(“.legend”)
.data(labels.domain())
.enter().append(“g”)
.attr(“类”、“图例”)
.attr(“转换”,函数(d,i)
{返回“translate(0,+i*20+”);};
图例。追加(“rect”)
.attr(“x”,宽度-18)
.attr(“宽度”,18)
.attr(“高度”,18)
.样式(“填充”,标签);
图例。追加(“文本”)
.attr(“x”,宽度-24)
.attr(“y”,12)
.style(“文本锚定”、“结束”)
.text(函数(d){return d;});
});
你知道我怎样才能解决这个问题吗
非常感谢 在鼠标悬停功能内的代码中:
div.html(d.species)
.style("left", (d3.event.pageX - 50) + "px")
.style("right", (d3.event.pageY ) + "px");
应该是(请注意,右侧样式已替换为顶部样式)
完整工作代码
希望这有帮助 非常感谢您的回答。这是非常有益的。我还找到了另一个解决方案,我将d3.event放入“mousemove”函数中,如下所示:
.on("mousemove", function(d){
div.style('top', (d3.event.pageY + 10)+'px')
.style('left', (d3.event.pageX + 10)+'px');
它也工作得很好。再次感谢
.on("mousemove", function(d){
div.style('top', (d3.event.pageY + 10)+'px')
.style('left', (d3.event.pageX + 10)+'px');