@nivo/voronoi
Version:
3 lines (2 loc) • 10.5 kB
JavaScript
"use strict";var e=require("react"),o=require("@nivo/core"),n=require("d3-scale"),i=require("d3-delaunay"),t=require("@nivo/tooltip"),r=require("react/jsx-runtime");function l(){return l=Object.assign?Object.assign.bind():function(e){for(var o=1;o<arguments.length;o++){var n=arguments[o];for(var i in n)({}).hasOwnProperty.call(n,i)&&(e[i]=n[i])}return e},l.apply(null,arguments)}function u(e,o){if(null==e)return{};var n={};for(var i in e)if({}.hasOwnProperty.call(e,i)){if(-1!==o.indexOf(i))continue;n[i]=e[i]}return n}var a={xDomain:[0,1],yDomain:[0,1],layers:["links","cells","points","bounds"],enableLinks:!1,linkLineWidth:1,linkLineColor:"#bbbbbb",enableCells:!0,cellLineWidth:2,cellLineColor:"#000000",enablePoints:!0,pointSize:4,pointColor:"#666666",role:"img"},s=function(e){return[e.x,e.y]},d=o.defaultMargin,c="cursor",h="top",v=function(e){var o=e.points,n=e.getNodePosition,i=void 0===n?s:n,t=e.margin,r=void 0===t?d:t;return o.map((function(e){var o=i(e),n=o[0],t=o[1];return[n+r.left,t+r.top]}))},p=function(e){var o=e.points,n=e.width,t=e.height,r=e.margin,l=void 0===r?d:r,u=e.debug,a=i.Delaunay.from(o),s=u?a.voronoi([0,0,l.left+n+l.right,l.top+t+l.bottom]):void 0;return{points:o,delaunay:a,voronoi:s}},f=function(o){var n=o.points,i=o.getNodePosition,t=void 0===i?s:i,r=o.width,l=o.height,u=o.margin,a=void 0===u?d:u,c=o.debug;return e.useMemo((function(){return p({points:v({points:n,margin:a,getNodePosition:t}),width:r,height:l,margin:a,debug:c})}),[t,n,r,l,a,c])},M=function(o){var t=o.data,r=o.width,l=o.height,u=o.xDomain,a=o.yDomain,s=e.useMemo((function(){return n.scaleLinear().domain(u).range([0,r])}),[u,r]),d=e.useMemo((function(){return n.scaleLinear().domain(a).range([0,l])}),[a,l]),c=e.useMemo((function(){return t.map((function(e){return{x:s(e.x),y:d(e.y),data:e}}))}),[t,s,d]);return e.useMemo((function(){var e=i.Delaunay.from(c.map((function(e){return[e.x,e.y]}))),o=e.voronoi([0,0,r,l]);return{points:c,delaunay:e,voronoi:o}}),[c,r,l])},g=function(o){var n=o.points,i=o.delaunay,t=o.voronoi;return e.useMemo((function(){return{points:n,delaunay:i,voronoi:t}}),[n,i,t])},b=function(n){var i=n.elementRef,r=n.nodes,l=n.getNodePosition,u=void 0===l?s:l,a=n.delaunay,v=n.setCurrent,p=n.margin,f=void 0===p?d:p,M=n.detectionRadius,g=void 0===M?1/0:M,b=n.isInteractive,k=void 0===b||b,m=n.onMouseEnter,C=n.onMouseMove,x=n.onMouseLeave,y=n.onMouseDown,D=n.onMouseUp,w=n.onClick,T=n.onDoubleClick,L=n.onTouchStart,P=n.onTouchMove,R=n.onTouchEnd,E=n.enableTouchCrosshair,W=void 0!==E&&E,j=n.tooltip,S=n.tooltipPosition,U=void 0===S?c:S,z=n.tooltipAnchor,A=void 0===z?h:z,N=e.useState(null),V=N[0],q=N[1],H=e.useRef(null);e.useEffect((function(){H.current=V}),[H,V]);var O=e.useCallback((function(e){if(!i.current||0===r.length)return null;var n=o.getRelativeCursor(i.current,e),t=n[0],l=n[1],s=a.find(t,l),d=void 0!==s?r[s]:null;if(d&&g!==1/0){var c=u(d),h=c[0],v=c[1];o.getDistance(t,l,h+f.left,v+f.top)>g&&(s=null,d=null)}return null===s||null===d?null:[s,d]}),[i,a,r,u,f,g]),I=t.useTooltip(),F=I.showTooltipAt,B=I.showTooltipFromEvent,G=I.hideTooltip,J=e.useMemo((function(){if(j)return"cursor"===U?function(e,o){B(j(e),o,A)}:function(e){var o=u(e),n=o[0],i=o[1];F(j(e),[n+f.left,i+f.top],A)}}),[F,B,j,U,A,u,f]),K=e.useCallback((function(e){var o=O(e);if(q(o),null==v||v(o?o[1]:null),o){var n=o[1];null==J||J(n,e),null==m||m(o[1],e)}}),[O,q,v,J,m]),Q=e.useCallback((function(e){var o=O(e);if(q(o),o){var n=o[0],i=o[1];if(null==v||v(i),null==J||J(i,e),H.current){var t=H.current,r=t[0],l=t[1];n!==r?null==x||x(l,e):null==C||C(i,e)}else null==m||m(i,e)}else null==v||v(null),null==G||G(),H.current&&(null==x||x(H.current[1],e))}),[O,q,v,H,m,C,x,J,G]),X=e.useCallback((function(e){q(null),null==v||v(null),G(),x&&H.current&&x(H.current[1],e)}),[q,v,H,G,x]),Y=e.useCallback((function(e){var o=O(e);q(o),o&&(null==y||y(o[1],e))}),[O,q,y]),Z=e.useCallback((function(e){var o=O(e);q(o),o&&(null==D||D(o[1],e))}),[O,q,D]),$=e.useCallback((function(e){var o=O(e);q(o),o&&(null==w||w(o[1],e))}),[O,q,w]),_=e.useCallback((function(e){var o=O(e);q(o),o&&(null==T||T(o[1],e))}),[O,q,T]),ee=e.useCallback((function(e){var o=O(e);W&&(q(o),null==v||v(o?o[1]:null)),o&&(null==L||L(o[1],e))}),[O,q,v,W,L]),oe=e.useCallback((function(e){var o=O(e);W&&(q(o),null==v||v(o?o[1]:null)),o&&(null==P||P(o[1],e))}),[O,q,v,W,P]),ne=e.useCallback((function(e){W&&(q(null),null==v||v(null)),R&&H.current&&R(H.current[1],e)}),[W,q,v,R,H]);return{current:V,handleMouseEnter:k?K:void 0,handleMouseMove:k?Q:void 0,handleMouseLeave:k?X:void 0,handleMouseDown:k?Y:void 0,handleMouseUp:k?Z:void 0,handleClick:k?$:void 0,handleDoubleClick:k?_:void 0,handleTouchStart:k?ee:void 0,handleTouchMove:k?oe:void 0,handleTouchEnd:k?ne:void 0}},k=["theme"],m=function(n){var i=n.data,t=n.width,l=n.height,u=n.margin,s=n.layers,d=void 0===s?a.layers:s,c=n.xDomain,h=void 0===c?a.xDomain:c,v=n.yDomain,p=void 0===v?a.yDomain:v,f=n.enableLinks,b=void 0===f?a.enableLinks:f,k=n.linkLineWidth,m=void 0===k?a.linkLineWidth:k,C=n.linkLineColor,x=void 0===C?a.linkLineColor:C,y=n.enableCells,D=void 0===y?a.enableCells:y,w=n.cellLineWidth,T=void 0===w?a.cellLineWidth:w,L=n.cellLineColor,P=void 0===L?a.cellLineColor:L,R=n.enablePoints,E=void 0===R?a.enableCells:R,W=n.pointSize,j=void 0===W?a.pointSize:W,S=n.pointColor,U=void 0===S?a.pointColor:S,z=n.role,A=void 0===z?a.role:z,N=n.forwardedRef,V=o.useDimensions(t,l,u),q=V.outerWidth,H=V.outerHeight,O=V.margin,I=V.innerWidth,F=V.innerHeight,B=M({data:i,width:I,height:F,xDomain:h,yDomain:p}),G=B.points,J=B.delaunay,K=B.voronoi,Q={links:null,cells:null,points:null,bounds:null};b&&d.includes("links")&&(Q.links=r.jsx("path",{stroke:x,strokeWidth:m,fill:"none",d:J.render()},"links")),D&&d.includes("cells")&&(Q.cells=r.jsx("path",{d:K.render(),fill:"none",stroke:P,strokeWidth:T},"cells")),E&&d.includes("points")&&(Q.points=r.jsx("path",{stroke:"none",fill:U,d:J.renderPoints(void 0,j/2)},"points")),d.includes("bounds")&&(Q.bounds=r.jsx("path",{fill:"none",stroke:P,strokeWidth:T,d:K.renderBounds()},"bounds"));var X=g({points:G,delaunay:J,voronoi:K});return r.jsx(o.SvgWrapper,{width:q,height:H,margin:O,role:A,ref:N,children:d.map((function(o,n){return void 0!==Q[o]?Q[o]:"function"==typeof o?r.jsx(e.Fragment,{children:e.createElement(o,X)},n):null}))})},C=e.forwardRef((function(e,n){var i=e.theme,t=u(e,k);return r.jsx(o.Container,{isInteractive:!1,animate:!1,theme:i,children:r.jsx(m,l({},t,{forwardedRef:n}))})})),x=["defaultWidth","defaultHeight","onResize","debounceResize"],y=e.forwardRef((function(e,n){var i=e.defaultWidth,t=e.defaultHeight,a=e.onResize,s=e.debounceResize,d=u(e,x);return r.jsx(o.ResponsiveWrapper,{defaultWidth:i,defaultHeight:t,onResize:a,debounceResize:s,children:function(e){var o=e.width,i=e.height;return r.jsx(C,l({},d,{width:o,height:i,ref:n}))}})})),D=function(e,o){e.save(),e.globalAlpha=.75,e.beginPath(),o.render(e),e.strokeStyle="red",e.lineWidth=1,e.stroke(),e.restore()},w=function(e,o,n){e.save(),e.globalAlpha=.15,e.beginPath(),o.renderPoints(e,n),e.strokeStyle="red",e.lineWidth=1,e.stroke(),e.restore()},T=function(e,o,n){e.save(),e.globalAlpha=.35,e.beginPath(),o.renderCell(n,e),e.fillStyle="pink",e.fill(),e.restore()};exports.Mesh=function(o){var n=o.nodes,i=o.width,t=o.height,l=o.margin,u=void 0===l?d:l,a=o.getNodePosition,s=o.setCurrent,v=o.onMouseEnter,p=o.onMouseMove,M=o.onMouseLeave,g=o.onMouseDown,k=o.onMouseUp,m=o.onClick,C=o.onDoubleClick,x=o.onTouchStart,y=o.onTouchMove,D=o.onTouchEnd,w=o.enableTouchCrosshair,T=void 0!==w&&w,L=o.detectionRadius,P=void 0===L?1/0:L,R=o.tooltip,E=o.tooltipPosition,W=void 0===E?c:E,j=o.tooltipAnchor,S=void 0===j?h:j,U=o.debug,z=e.useRef(null),A=f({points:n,getNodePosition:a,width:i,height:t,margin:u,debug:U}),N=A.delaunay,V=A.voronoi,q=b({elementRef:z,nodes:n,delaunay:N,margin:u,detectionRadius:P,setCurrent:s,onMouseEnter:v,onMouseMove:p,onMouseLeave:M,onMouseDown:g,onMouseUp:k,onClick:m,onDoubleClick:C,onTouchStart:x,onTouchMove:y,onTouchEnd:D,enableTouchCrosshair:T,tooltip:R,tooltipPosition:W,tooltipAnchor:S}),H=q.current,O=q.handleMouseEnter,I=q.handleMouseMove,F=q.handleMouseLeave,B=q.handleMouseDown,G=q.handleMouseUp,J=q.handleClick,K=q.handleDoubleClick,Q=q.handleTouchStart,X=q.handleTouchMove,Y=q.handleTouchEnd,Z=e.useMemo((function(){if(U&&V)return V.render()}),[U,V]);return r.jsxs("g",{ref:z,transform:"translate("+-u.left+","+-u.top+")",children:[U&&V&&r.jsxs(r.Fragment,{children:[r.jsx("path",{d:Z,stroke:"red",strokeWidth:1,opacity:.75}),P<1/0&&r.jsx("path",{stroke:"red",strokeWidth:.35,fill:"none",d:N.renderPoints(void 0,P)}),H&&r.jsx("path",{fill:"pink",opacity:.35,d:V.renderCell(H[0])})]}),r.jsx("rect",{"data-ref":"mesh-interceptor",width:u.left+i+u.right,height:u.top+t+u.bottom,fill:"red",opacity:0,style:{cursor:"auto"},onMouseEnter:O,onMouseMove:I,onMouseLeave:F,onMouseDown:B,onMouseUp:G,onTouchStart:Q,onTouchMove:X,onTouchEnd:Y,onClick:J,onDoubleClick:K})]})},exports.ResponsiveVoronoi=y,exports.Voronoi=C,exports.computeMesh=p,exports.computeMeshPoints=v,exports.defaultVoronoiProps=a,exports.renderDebugToCanvas=function(e,o){var n=o.delaunay,i=o.voronoi,t=o.detectionRadius,r=o.index;D(e,i),t<1/0&&w(e,n,t),null!==r&&T(e,i,r)},exports.renderDelaunayPointsToCanvas=w,exports.renderVoronoiCellToCanvas=T,exports.renderVoronoiToCanvas=D,exports.useMesh=function(e){var o=e.elementRef,n=e.nodes,i=e.getNodePosition,t=e.width,r=e.height,l=e.margin,u=void 0===l?d:l,a=e.isInteractive,s=void 0===a||a,v=e.detectionRadius,p=void 0===v?1/0:v,M=e.setCurrent,g=e.onMouseEnter,k=e.onMouseMove,m=e.onMouseLeave,C=e.onMouseDown,x=e.onMouseUp,y=e.onClick,D=e.onDoubleClick,w=e.tooltip,T=e.tooltipPosition,L=void 0===T?c:T,P=e.tooltipAnchor,R=void 0===P?h:P,E=e.debug,W=f({points:n,getNodePosition:i,width:t,height:r,margin:u,debug:void 0!==E&&E}),j=W.delaunay,S=W.voronoi,U=b({elementRef:o,nodes:n,margin:u,setCurrent:M,delaunay:j,detectionRadius:p,isInteractive:s,onMouseEnter:g,onMouseMove:k,onMouseLeave:m,onMouseDown:C,onMouseUp:x,onClick:y,onDoubleClick:D,tooltip:w,tooltipPosition:L,tooltipAnchor:R}),z=U.handleMouseEnter,A=U.handleMouseMove,N=U.handleMouseLeave,V=U.handleMouseDown,q=U.handleMouseUp,H=U.handleClick,O=U.handleDoubleClick;return{delaunay:j,voronoi:S,current:U.current,handleMouseEnter:z,handleMouseMove:A,handleMouseLeave:N,handleMouseDown:V,handleMouseUp:q,handleClick:H,handleDoubleClick:O}},exports.useMeshEvents=b,exports.useVoronoi=M,exports.useVoronoiLayerContext=g,exports.useVoronoiMesh=f;
//# sourceMappingURL=nivo-voronoi.cjs.js.map