javascript - 使用 d3.js 在散点图中显示靠近鼠标指针的工具提示

标签 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 {
    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>

您知道如何解决这个问题吗?

非常感谢!!

最佳答案

在鼠标悬停功能内的代码中:

 div.html(d.species)
            .style("left", (d3.event.pageX - 50) + "px")
            .style("right", (d3.event.pageY ) + "px");

应该是(注意样式 right 被 top 替换了)

 div.html(d.species)
            .style("left", (d3.event.pageX - 50) + "px")
            .style("top", (d3.event.pageY ) + "px");

完整工作代码here

希望这有帮助!

关于javascript - 使用 d3.js 在散点图中显示靠近鼠标指针的工具提示,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33063374/

相关文章:

javascript - FullCalendar RenderEvent 在版本 3.9.0 中不起作用

javascript - 从数据构建特定图标

javascript - D3 和​​ jQuery 有什么区别?

Python Matplotlib 散点图添加 x 轴标签

javascript - 通过事件指针/引用取消绑定(bind)特定的 Jquery 事件

javascript - Angular JS/Dojo 小部件解析

php - 在php中自动执行链接

javascript - D3.js 剪辑路径切断了我的图表的边缘

r - 确定哪些点位于 R 中不规则形状的数据足迹之外?

python-3.x - 如何删除图例的特定部分(seaborn,散点图)