d3-geo-scale-bar
Version:
Displays automatic scale bars for projected geospatial data.
1 lines • 6.36 kB
JavaScript
!function(t,n){"object"==typeof exports&&"undefined"!=typeof module?n(exports):"function"==typeof define&&define.amd?define(["exports"],n):n(t.d3=t.d3||{})}(this,function(t){"use strict";function n(){this.reset()}function e(t,n,e){var r=t.s=n+e,o=r-n,i=r-o;t.t=n-i+(e-o)}function r(t,n){t&&S.hasOwnProperty(t.type)&&S[t.type](t,n)}function o(t,n,e){var r,o=-1,i=t.length-e;for(n.lineStart();++o<i;)r=t[o],n.point(r[0],r[1],r[2]);n.lineEnd()}function i(t,n){var e=-1,r=t.length;for(n.polygonStart();++e<r;)o(t[e],n,1);n.polygonEnd()}function a(t,n){t&&M.hasOwnProperty(t.type)?M[t.type](t,n):r(t,n)}function c(){P.point=l,P.lineEnd=u}function u(){P.point=P.lineEnd=function(t){}}function l(t,n){t*=Math.PI/180,n*=Math.PI/180,m=t,y=Math.sin(n),v=Math.cos(n),P.point=s}function s(t,n){t*=Math.PI/180,n*=Math.PI/180;var e=Math.sin(n),r=Math.cos(n),o=Math.abs(t-m),i=Math.cos(o),a=Math.sin(o),c=r*a,u=v*e-y*r*i,l=y*e+v*r*i;k.add(Math.atan2(Math.sqrt(c*c+u*u),l)),m=t,y=e,v=r}function f(t){return k.reset(),a(t,P),+k}function h(t,n){return C[0]=t,C[1]=n,f(b)}function d(){return 1}function g(){function t(t){i=null===i?null:i||f.charAt(0).toUpperCase()+f.slice(1);var d=a[1][0]-a[0][0],M=a[1][1]-a[0][1],S=a[0][0]+d*c,k=a[0][1]+M*u,P=n.invert([S,k]),C=0,b=0;if(e)C=e,b=C/(h(P,n.invert([S+1,k]))*s);else{for(var w=.01,z=80/(x?1:v),E=0,F=[1,2,4,5];b<z&&E<100;){for(var L=0,A=F.length;L<A&&(C=w*F[L],!((b=C/(h(P,n.invert([S+1,k]))*s))>=z));L++);w*=10,E++}r=C}var I=C/(x?v:1),O=null===o?[]:o||[0,I/4,I/2,I],j=function(t){return t*b/(C/v)},B=t.selection?t.selection():t,_=B.selectAll(".label").data([i]),q=B.selectAll(".domain").data([null]),G=B.selectAll(".tick").data(O,j).order(),K=G.exit(),T=G.enter().append("g").attr("class","tick"),U=G.select("line"),V=G.select("text"),D=G.select("rect");B.attr("font-family","sans-serif").attr("transform","translate(".concat([S,k],")")),q=q.merge(q.enter().insert("path",".tick").attr("class","domain").attr("fill","none").attr("stroke","currentColor")),G=G.merge(T),U=U.merge(T.append("line").attr("stroke","currentColor").attr("y2",m*l)),V=V.merge(T.append("text").attr("fill","currentColor").attr("y",m*l+p*l).attr("font-size",10).attr("text-anchor","middle").attr("dy","".concat(1===l?.71:0,"em"))),D=D.merge(T.append("rect").attr("fill",function(t,n){return n%2==0?"currentColor":"#fff"}).attr("stroke","currentColor").attr("stroke-width",.5).attr("width",function(t,n,e){return n===e.length-1?0:j(O[n+1]-t)}).attr("y",1===l?0:-m).attr("height",m)),t!==B&&(G=G.transition(t),q=q.transition(t),D=D.transition(t),K=K.transition(t).attr("opacity",1e-6).attr("transform",function(t){return"translate(".concat(j(t),")")}),T.attr("opacity",1e-6).attr("transform",function(t){return"translate(".concat(j(t),")")})),K.remove(),q.attr("d","M".concat(j(0),",").concat(m*l," L").concat(j(0),",0 L").concat(j(I),",0 L").concat(j(I),",").concat(m*l)),G.attr("transform",function(t){return"translate(".concat(j(t),")")}).attr("opacity",1),U.attr("y2",m*l),V.attr("y",m*l+p*l).text(g),D.attr("fill",function(t,n){return n%2==0?"currentColor":"#fff"}).attr("width",function(t,n,e){return n===e.length-1?0:j(O[n+1]-t)}).attr("y",1===l?0:-m).attr("height",m),null===_?_.remove():(t!==B?_.transition(t).attr("x","start"===y?0:j("middle"===y?I/2:I)).attr("y",1===l?0:"1.3em").attr("text-anchor",y).text(function(t){return t}):_.attr("x","start"===y?0:j("middle"===y?I/2:I)).attr("y",1===l?0:"1.3em").attr("text-anchor",y).text(function(t){return t}),_.enter().append("text").attr("class","label").attr("fill","currentColor").attr("font-size",12).attr("dy","-0.32em").attr("x","start"===y?0:j("middle"===y?I/2:I)).attr("y",1===l?0:"1.3em").attr("text-anchor",y).text(function(t){return t}))}var n,e,r,o,i,a=null,c=0,u=0,l=d(),s=w.radius,f=w.units,g=function(t){return+t.toFixed(2)},p=2,m=4,y="start",v=1,x=!0;return t.distance=function(n){return arguments.length?(e=+n,t):e||r},t.extent=function(n){return arguments.length?(a=n,t):a},t.label=function(n){return arguments.length?(i=n,t):i},t.labelAnchor=function(n){return arguments.length?(y=n,t):y},t.left=function(n){return arguments.length?(c=+n>1?1:+n<0?0:+n,t):c},t.orient=function(n){return arguments.length?(l=n(),t):1===l?"bottom":"top"},t.projection=function(e){return arguments.length?(n=e,t):n},t.radius=function(n){return arguments.length?(s=+n,t):s},t.size=function(n){return arguments.length?(a=[[0,0],n],t):a[1]},t.top=function(n){return arguments.length?(u=+n>1?1:+n<0?0:+n,t):u},t.tickFormat=function(n){return arguments.length?(g=n,t):g},t.tickPadding=function(n){return arguments.length?(p=+n,t):p},t.tickSize=function(n){return arguments.length?(m=+n,t):m},t.tickValues=function(n){return arguments.length?(o=n,t):o},t.units=function(n){var e;return arguments.length?(e=n,f=e.units,s=e.radius,t):f},t.zoomClamp=function(n){return arguments.length?(x=!!n,t):x},t.zoomFactor=function(n){return arguments.length?(v=+n,t):v},t}function p(){return-1}n.prototype={constructor:n,reset:function(){this.s=this.t=0},add:function(t){e(x,t,this.t),e(this,x.s,this.s),this.s?this.t+=x.t:this.s=x.t},valueOf:function(){return this.s}};var m,y,v,x=new n,M={Feature:function(t,n){r(t.geometry,n)},FeatureCollection:function(t,n){for(var e=t.features,o=-1,i=e.length;++o<i;)r(e[o].geometry,n)}},S={Sphere:function(t,n){n.sphere()},Point:function(t,n){t=t.coordinates,n.point(t[0],t[1],t[2])},MultiPoint:function(t,n){for(var e=t.coordinates,r=-1,o=e.length;++r<o;)t=e[r],n.point(t[0],t[1],t[2])},LineString:function(t,n){o(t.coordinates,n,0)},MultiLineString:function(t,n){for(var e=t.coordinates,r=-1,i=e.length;++r<i;)o(e[r],n,0)},Polygon:function(t,n){i(t.coordinates,n)},MultiPolygon:function(t,n){for(var e=t.coordinates,r=-1,o=e.length;++r<o;)i(e[r],n)},GeometryCollection:function(t,n){for(var e=t.geometries,o=-1,i=e.length;++o<i;)r(e[o],n)}},k=function(){return new n}(),P={sphere:function(t){},point:function(t){},lineStart:c,lineEnd:function(t){},polygonStart:function(t){},polygonEnd:function(t){}},C=[null,null],b={type:"LineString",coordinates:C},w={units:"kilometers",radius:6371.0088},z={units:"feet",radius:20902259.664},E={units:"meters",radius:6371008.8},F={units:"miles",radius:3958.7613};t.geoScaleBar=g,t.geoScaleBottom=d,t.geoScaleTop=p,t.geoScaleFeet=z,t.geoScaleKilometers=w,t.geoScaleMeters=E,t.geoScaleMiles=F,Object.defineProperty(t,"__esModule",{value:!0})});