D3 selection对象扩展

D3是一个非常好的大数据前台展示技术。工作中把EXTJS和D3JS一起用,在用D3 selection对象的attr方法设置SVG的rect的ext:qtip时出了一点小问题。D3会检查attribute的名称里面有没有包含冒号,有的话会把冒号前的作为namespace另行处理。这样造成的效果就是页面只有qtip属性,EXT不认识。为达到设置ext:qtip的目的,我扩展了D3的selection对象。具体代码如下:


	function d3_selection_qtip(value){
			var name = "ext:qtip";
			function attrNull() {
		      this.removeAttribute(name);
		    }
		    function attrConstant() {
		      this.setAttribute(name, value);
		    }				 
		    function attrFunction() {
		      var x = value.apply(this, arguments);
		      if (x == null) this.removeAttribute(name); else this.setAttribute(name, x);
		    }			  
		    return value == null ?  attrNull : typeof value === "function" ?  attrFunction :  attrConstant;	
		}
		
		d3.selection.prototype.qtip = function(value) {
			if(Ext!="undefined"){
				return this.each(d3_selection_qtip(value));
			}else{
				//do nothing if no Ext.
			}			
		};


转载请务必指出原出本博客。

你可能感兴趣的:(d3,D3JS)