d3.js不支持attr和style像jquery一样传入多个对象,重写attr和style方法

import * as d3 from 'd3';
// 重写d3的attr和style支持传入对象
const oldAttr = d3.selection.prototype.attr;
d3.selection.prototype.attr = function () {
  if (arguments.length === 1) {
    if (typeof arguments[0] === 'object') {
      for (const item in arguments[0]) {
        oldAttr.call(this, item, arguments[0][item]);
      }
      return this;
    }
    return oldAttr.call(this, arguments[0]);
  } else if (arguments.length === 2) {
    return oldAttr.call(this, arguments[0], arguments[1]);
  }
};
const oldStyle = d3.selection.prototype.style;
d3.selection.prototype.style = function () {
  if (arguments.length === 1) {
    if (typeof arguments[0] === 'object') {
      for (const item in arguments[0]) {
        oldStyle.call(this, item, arguments[0][item]);
      }
      return this;
    }
    return oldStyle.call(this, arguments[0]);
  } else if (arguments.length === 2) {
    return oldStyle.call(this, arguments[0], arguments[1]);
  }
};

 

转载自:http://www.codezd.com/js/271.html

posted @ 2020-07-09 15:54  红苹果学园  阅读(482)  评论(0编辑  收藏  举报