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