zoukankan      html  css  js  c++  java
  • d3-tip中show在自己调用时需要改变this值

    <script src="https://d3js.org/d3.v4.min.js"></script>
    <script src="https://cdn.bootcss.com/d3-tip/0.9.1/d3-tip.js"></script>


    const svg = d3.select('body').append('svg')
    .attr('width', 500)
    .attr('height', 500);
    let dataSymbol = [d3.symbolCircle, d3.symbolCross, d3.symbolDiamond, d3.symbolSquare,
    d3.symbolStar, d3.symbolTriangle, d3.symbolWye];
    let color = d3.scaleOrdinal(d3.schemeCategory10);
    const symbol = d3.symbol().size(500).type(d => d);
    let tip = d3.tip() // 设置tip
    .attr('class', 'd3-tip')
    .offset([-10, 0])
    .html(function (d, i) {
    return "<strong>颜色" + color(i) + "<br></strong> <span style='color:#ffeb3b'>" + color(i) + "</span>";
    });

    svg.call(tip);
    svg.append('g')
    .attr('transform','translate(100, 100)')
    .selectAll('path').data(dataSymbol).enter().append('path')
    .attr('d', d => symbol(d))
    .attr('stroke', 'black')
    .attr('stroke-width', 5)
    .attr('transform', (d,i) => 'translate('+ i * 60+','+ 200 +')')
    .attr('fill', (d,i) => color(i))
    .on('mouseover', function () {
    let args = Array.prototype.slice.call(arguments);
    d3.select(this).attr('fill', 'red');
    tip.show.apply(this, [...args]); //.on('mouseover',tip.show) 没有其他事件程序时这样写,tip.show函数的this值为发生鼠标移入事件的元素。如果事件程序多,写在一个function中时,需要将tip.show的this值指定为同样的元素,即事件函数的this,并将参数传入
        })
    .on('mouseout', function (a,b,c) {
    d3.select(this).attr('fill', color(b)).selectAll('text').remove();
    tip.hide();
    })
  • 相关阅读:
    Make a web page as screensaver
    python写入sqlserver中文乱码问题
    单片机中的ROM,RAM和FLASH的作用
    单片机STM32F4系统内部中断和外部中断优先级问题
    单片机AHB和APB
    嵌入式编程中一些常用的转换函数
    'AVStream::codec': 被声明为已否
    Python安装Python_snappy安装失败
    .net core 数据库问题
    .net 5.0 中 CrystalQuartz 增加授权校验
  • 原文地址:https://www.cnblogs.com/sgqwjr/p/10314770.html
Copyright © 2011-2022 走看看