【问题标题】:Add hover event on intersecting sections of SVG graph在 SVG 图的相交部分添加悬停事件
【发布时间】:2016-07-02 00:45:56
【问题描述】:

我已经创建了一个小提琴http://jsfiddle.net/bhyt7L5z/

我有 5 个椭圆相交。我想在相交区域捕获悬停事件并在工具提示中显示相应的数据。 A类似:

    $('paths').hover(function(e) {
        console.log('show content related to intersection');
    });

谁能指点我如何做到这一点。

在 JavaScript 中是否有任何图表可以提供交互式椭圆维恩图

问候

【问题讨论】:

    标签: javascript jquery css svg


    【解决方案1】:

    只需为每个椭圆附加.attr('id','ellipse1'),现在就可以使用了

     $('#ellipse1').hover(function(e) {
            console.log('show content related to intersection');
        });
    

    来自你 jsfiddle 的代码:

    var width = 450,
       height = 400
    
    
        var svg = d3.select("body").append("svg")
            .attr("width", width)
            .attr("height", height);
    
    
    svg.append("svg:ellipse")
    .attr("cx", 200)
    .attr("cy", 200)
    .attr("rx", 50)
    .attr("ry",100)
    .style("fill", 'rgba(108,255,108,0.4)')
    .style("stroke", "#777")
    .style("stroke-width", '1px')
    .style("transform", 'rotate(30deg)')
    .style("transform-origin", "50% 50%")
    .attr("id","ellipse1")
    .attr("data-toggle","tooltip")
    .attr("title","ellipse1")
    
    
    svg.append("svg:ellipse")
    .attr("cx", 200)
    .attr("cy", 200)
    .attr("rx", 50)
    .attr("ry",100)
    .style("fill", 'rgba(168,255,168,0.4)')
    .style("stroke", "#777")
    .style("stroke-width", '1px')
    .style("transform", 'rotate(70deg)')
    .style("transform-origin", "50% 50%")
    .attr("id","ellipse2")
    .attr("data-toggle","tooltip")
    .attr("title","ellipse2")
    
    svg.append("svg:ellipse")
    .attr("cx", 200)
    .attr("cy", 200)
    .attr("rx", 50)
    .attr("ry",100)
    .style("fill", 'rgba(148,255,148,0.4)')
    .style("stroke", "#777")
    .style("stroke-width", '1px')
    .style("transform", 'rotate(110deg)')
    .style("transform-origin", "50% 50%")
    .attr("id","ellipse3")
    .attr("data-toggle","tooltip")
    .attr("title","ellipse3")
    
    svg.append("svg:ellipse")
    .attr("cx", 200)
    .attr("cy", 200)
    .attr("rx", 50)
    .attr("ry",100)
    .style("fill", 'rgba(128,255,128,0.4)')
    .style("stroke", "#777")
    .style("stroke-width", '1px')
    .style("transform", 'rotate(140deg)')
    .style("transform-origin", "50% 50%")
    .attr("id","ellipse4")
    .attr("data-toggle","tooltip")
    .attr("title","ellipse4")
     $('[data-toggle="tooltip"]').tooltip({container: 'body'});   
    svg { 
        border:1px dotted #000; 
    }
    <script src="http://d3js.org/d3.v3.js"></script>
    <link rel="stylesheet" href="http://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css">
      <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.0/jquery.min.js"></script>
      <script src="http://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/js/bootstrap.min.js"></script>
    <div id="content"></div>

    【讨论】:

    • 感谢回复 我需要将鼠标悬停在路口区域 一旦悬停在路口我需要显示相关数据
    • 我添加了一个引导工具提示只是为了显示可能
    • 只是为了简化我的问题,如果两个椭圆相交,它们会有一些公共区域。我需要强调这个领域。例如:第一次日食:日食1和第二次:日食2如果用户将鼠标悬停在交叉点上,我需要突出显示该区域并显示哪些日食相交。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2022-08-13
    • 2018-07-11
    • 1970-01-01
    • 1970-01-01
    • 2011-06-18
    • 2020-05-23
    • 1970-01-01
    相关资源
    最近更新 更多