zoukankan      html  css  js  c++  java
  • [D3] Add hovercard

    The way to add hovercard is 

    • Append a div with class 'hovercard'
    • in the tick function, positioning the hovercard with 'd3.event.pageX and pageY'
    .hovercard {
      position: absolute;
      max-width: 400px;
      height: auto;
      padding: 5px;
      background-color: white;
      -webkit-border-radius: 5px;
      -moz-border-radius: 5px;
      border-radius: 5px;
      -webkit-box-shadow: 4px 4px 10px rgba(0, 0, 0, 0.4);
      -moz-box-shadow: 4px 4px 10px rgba(0, 0, 0, 0.4);
      box-shadow: 4px 4px 10px rgba(0, 0, 0, 0.4);
      pointer-events: none;
      font: 12px sans-serif;
    }
                const hovercard = d3.select('body')
                    .append('div')
                    .attr('class', 'hovercard')
                    .style('opacity', 0)
                    .style('width', 400);
                function ticked() {
    
                    // adjust nodes containers position
                    svgNodes
                        .attr('transform', d =>`translate(${d.x},${d.y})`)
                        .call(d3.drag()
                            .on('start', dragstarted)
                            .on('drag', dragged)
                            .on('end', dragended));
    
    
                    // Curve paths
                    path
                        .attr('d', (d) => {
                            const curve = d.battle_number * .5;
                            const dx = d.target.x - d.source.x;
                            const dy = d.target.y - d.source.y;
                            const dr = Math.sqrt(dx * dx * curve + dy * dy * curve);
                            return `M${d.source.x},${d.source.y}A${dr},${dr} 0 0,1 ${d.target.x},${d.target.y}`;
                        });
    
                    path.on('mouseover', d => {
    
                        hovercard
                            .transition()
                            .duration(300)
                            .delay(20)
                            .style('opacity', 1);
    
                        const tip =
                            '<h2>' + d.name + '</h2>' +
                            '<h4>' + d.source.name + ' attacked ' + d.target.name + ' and the outcome was a ' + d.attacker_outcome + '</h4>' +
                            '<h3>Details</h3>' +
                            '<strong> Attacker King: </strong>'+d.attacker_king + '<br/>'+
                            '<strong> Battle Type: </strong>'+d.battle_type + '<br/>'+
                            '<strong> Major Death?: </strong>'+d.major_death + '<br/>'+
                            '<strong> Major Capture?: </strong>'+d.major_capture + '<br/>'+
                            '<strong> Attacker Size: </strong>'+d.value + '<br/>'+
                            '<strong> Defender Size: </strong>'+d.defender_size + '<br/>'+
                            '<strong> Region / Location: </strong>'+d.region+ ' / ' + d.location + '<br/>'+
                            '<strong> Attacking Commander: </strong>'+d.attacker_commander + '<br/>'+
                            '<strong> Defending Commander: </strong>'+d.defender_commander;
    
                        hovercard
                            .html(tip)
                            .style('left', d3.event.pageX + 'px')
                            .style('top', d3.event.pageY + 'px');
                    });
    
                    path.on('mouseout', d => {
                        hovercard
                            .transition()
                            .duration(500)
                            .style('opacity', 0);
                    });
    
    
                }
            });
  • 相关阅读:
    Python学习笔记捌——面向对象高级编程
    Python学习笔记五,函数及其参数
    Python学习笔记四,dict和set
    Python学习笔记三,数组list和tuple
    Python学习笔记一,输入输出
    Linux 环境下自动化测试工具,Redhat dogtail的安装
    Testlink接口使用方法-python语言远程调用
    Python入门学习之input()与raw_input()的区别
    从客户端(&)中检测到有潜在危险的 Request.Path 值解决方案
    树莓派嵌入式开发第一课笔记
  • 原文地址:https://www.cnblogs.com/Answer1215/p/7492281.html
Copyright © 2011-2022 走看看