d3-axes
Version:
D3 plugin to manage pairs of axes.
1 lines • 5.63 kB
JavaScript
!function(t,n){"object"==typeof exports&&"undefined"!=typeof module?n(exports,require("d3-selection"),require("d3-gup")):"function"==typeof define&&define.amd?define(["exports","d3-selection","d3-gup"],n):n(t.d3={},t.d3,t.d3)}(this,function(t,r,c){"use strict";function v(t,n){for(var e=t;!e.classed(n);){if(!(e=e.node().parentElement))return;e=r.select(e)}return e}function i(t,n){var e=t.selection?t.selection():t,r=0,i=0;v(e,"axis");if(e.classed("x"))i=+n;else{if(!e.classed("y"))return;r=+n}t.attr("transform","translate("+r+" "+i+")")}function n(t,n){i(t,n.scale().range()[0])}function e(t,n){i(t,n.scale().range()[1])}var a=function(t,n){i(t,n.scale()(0))},d=c.gup().enter(function(t,n){return t.append("g").attr("id",n).attr("class","axes")}).post(function(t,n,e,r){return t.attr("transform","translate("+e+" "+r+")")})([null]),g=c.gup().enter(function(t,n){return t.append("clipPath").attr("id",n+"-clip-path").call(function(t){return t.append("rect")})}).post(function(t,n,e,r){return t.select("rect").attr("width",e).attr("height",r)})([null]),x=c.gup().enter(function(t,n){return t.append("g").attr("class",n+" axis")}).post(function(t,n,e,r){return t.call(e,r)})([null]),y=0;function u(n,e,r,i,a,u){return function(t){return t.attr("opacity",a).attr(n+"2",r).call(u?function(t){return t.each(function(){i.set(this,u)})}:function(t){return t}).attr(e+"1",function(t){return i.get(this)(t)}).attr(e+"2",function(t){return i.get(this)(t)})}}function s(e,r){if(r.scale)return r.scale(e);var t=function(){for(var t=[],n=arguments.length;n--;)t[n]=arguments[n];return e.call(this,r.apply(this,t))};return t.scale=function(t){return arguments.length?(e=t,this):e},t}var o=0,f=c.gup().select(function(t,n){return t.select(".axes").selectAll("g#"+n)}).enter(function(t,n){return t.append("g").attr("id",n).attr("class","shapes")})([null]);function l(a){var n=o++,e=null,r=c.gup().select(function(t){return t.selectAll("path.shape")}).exit(function(t){return t.remove()}).enter(function(t){return t.append("path").attr("d",a).attr("class","shape")}).pre(function(t){return t.attr("d",a)}),u=r([]);function l(t,n){for(var e,r=[],i=arguments.length-2;0<i--;)r[i]=arguments[i+2];a.x&&a.x(s(n.x().scale(),a.x())),a.y&&a.y(s(n.y().scale(),a.y())),n.apply(this,[t].concat(r)),(e=f(t,l.id())).call.apply(e,[u].concat(r))}return l.path=function(t){return arguments.length?(a=t,this):a},l.data=function(){for(var t=[],n=arguments.length;n--;)t[n]=arguments[n];return arguments.length?(u=r.apply(void 0,t),this):u.data?u.data():u},l.id=function(t){return arguments.length?(e=t,this):e||"axes-shape-"+n},l.gup=function(t){return arguments.length?(r=t,this):r},l}t.axes=function(a,u){var l=20,c=20,s=20,o=20,f=0,p=0,n=y++,e=null;function h(t){t.selection&&t.selection();var n=f-l-s,e=p-c-o;a.scale().range([0,e]),u.scale().range([n,0]);var r=h.id();t.selectAll("g#"+r).call(d,r,o,l);var i=t.select("g#"+r);i.selectAll("#"+r+"-clip-path").call(g,r,e,n),i.selectAll(".x.axis").call(x,"x",a,u),i.selectAll(".y.axis").call(x,"y",u,a)}return h.x=function(t){return arguments.length?(a=t,this):a},h.y=function(t){return arguments.length?(u=t,this):u},h.padding=function(t,n,e,r){var i,a;switch(arguments.length){case 0:return{top:l,right:c,bottom:s,left:o};case 1:n=e=r=t;break;case 2:e=(i=[t,n])[0],r=i[1];break;case 3:r=n}return l=(a=[t,n,e,r])[0],c=a[1],s=a[2],o=a[3],this},h.height=function(t){return arguments.length?(f=t,this):f},h.width=function(t){return arguments.length?(p=t,this):p},h.domain=function(t,n){return arguments.length?(t&&a.scale().domain(t),n&&u.scale().domain(n),this):[a.scale().domain(),u.scale().domain()]},h.id=function(t){return arguments.length?(e=t,this):e||"axes-"+n},h},t.axesPositionBottom=n,t.axesPositionLeft=n,t.axesPositionTop=e,t.axesPositionRight=e,t.axesPositionDefault=a,t.axesPosition=function(t){var e=+t;return isNaN(e)?a:function(t,n){i(t,n.scale()(e))}},t.axesEmpty=function(n){function t(t){}return t.scale=function(t){return arguments.length?(n=t,this):n},t},t.axesGrid=function(h){var d=r.local(),g=null,x=null,y=c.gup().select(function(t){return t.selectAll(".grid")}).pre(function(t){return(t.selection?t.selection():t).order()}).exit(function(t,n,e,r,i,a){return t.call(u(n,e,r,i,0,a)).remove()}).enter(function(t,n,e,r,i,a){return t.append("line").attr("class","grid").call(u(n,e,r,i,0))}).post(function(t,n,e,r,i,a){return t.call(u(n,e,r,i,1,a))});function t(t,n){var e=t.selection?t.selection():t,r=h.scale(),i=null==g?r.ticks?r.ticks.apply(r,x||h.tickArguments()):r.domain():g,a=n.scale().range(),u=a[1]-a[0],l=v(e,"axis").classed("y")?["x","y"]:["y","x"],c=l[0],s=l[1];a=h.scale().range();var o,f,p=r.copy().range([a[0]+.5,a[1]+.5]);r.bandwidth&&(f=(o=p).bandwidth()/2,o.round()&&(f=Math.round(f)),p=function(t){return o(t)+f}),d.get(t.node())||d.set(t.node(),p),t.call(y(i,r),c,s,u,d,p),d.set(t.node(),p)}return t.ticks=function(){for(var t=[],n=arguments.length;n--;)t[n]=arguments[n];return x=t,this},t.tickArguments=function(t){return arguments.length?(x=null==t?[]:slice.call(t),this):x.slice()},t.tickValues=function(t){return arguments.length?(g=null==t?null:slice.call(t),this):g&&g.slice()},t.gup=function(t){return arguments.length?(y=t,this):y},t},t.axesShape=l,t.axesSymbol=function(n){var e=function(t){return t[0]},r=function(t){return t[1]};n.x=function(t){return arguments.length?(e=t,this):e},n.y=function(t){return arguments.length?(r=t,this):r};var t=l(n);return t.gup(c.gupCompose(c.gup().pre(i).enter(i),t.gup())),t;function i(t){return t.attr("transform",function(t){return"translate("+n.x()(t)+" "+n.y()(t)+")"})}},Object.defineProperty(t,"__esModule",{value:!0})});