@nivo/bullet
Version:
3 lines (2 loc) • 10.4 kB
JavaScript
import*as e from"react";import{useMemo as r,createElement as o,forwardRef as t}from"react";import{motionDefaultProps as n,defaultMargin as i,useMotionConfig as a,getColorScale as l,useDimensions as u,Container as s,SvgWrapper as c,ResponsiveWrapper as h}from"@nivo/core";import{animated as m,to as d,useTransition as f,useSpring as v}from"@react-spring/web";import{jsx as g,jsxs as p,Fragment as y}from"react/jsx-runtime";import{BasicTooltip as x,useTooltip as C}from"@nivo/tooltip";import{Axis as k}from"@nivo/axes";import{Text as M}from"@nivo/text";import{useTheme as z}from"@nivo/theming";import{useInheritedColor as w}from"@nivo/colors";import{createLinearScale as b}from"@nivo/scales";function W(){return W=Object.assign?Object.assign.bind():function(e){for(var r=1;r<arguments.length;r++){var o=arguments[r];for(var t in o)({}).hasOwnProperty.call(o,t)&&(e[t]=o[t])}return e},W.apply(null,arguments)}function B(e,r){if(null==e)return{};var o={};for(var t in e)if({}.hasOwnProperty.call(e,t)){if(-1!==r.indexOf(t))continue;o[t]=e[t]}return o}var P=function(e){var r=e.animatedProps,o=r.x,t=r.y,n=r.width,i=r.height,a=r.color,l=e.borderColor,u=e.borderWidth,s=e.data,c=e.onMouseEnter,h=e.onMouseMove,f=e.onMouseLeave,v=e.onClick;return g(m.rect,{x:o,y:t,width:d(n,(function(e){return Math.max(e,0)})),height:d(i,(function(e){return Math.max(e,0)})),fill:a,strokeWidth:u,stroke:l,onMouseMove:function(e){return h(s,e)},onMouseEnter:function(e){return c(s,e)},onMouseLeave:function(e){return f(s,e)},onClick:function(e){return v(s,e)}})},E={layout:"horizontal",reverse:!1,spacing:30,minValue:0,maxValue:"auto",axisPosition:"after",titlePosition:"before",titleAlign:"middle",titleRotation:0,titleOffsetX:0,titleOffsetY:0,rangeComponent:P,rangeColors:"seq:cool",measureComponent:P,measureColors:"seq:red_purple",markers:[],markerComponent:function(e){var r=e.animatedProps,o=r.color,t=r.transform,n=r.x,i=r.y1,a=r.y2,l=e.data,u=e.onMouseEnter,s=e.onMouseMove,c=e.onMouseLeave,h=e.onClick;return g(m.line,{transform:t,x1:n,x2:n,y1:i,y2:a,fill:"none",stroke:o,strokeWidth:"5",onMouseMove:function(e){return s(l,e)},onMouseEnter:function(e){return u(l,e)},onMouseLeave:function(e){return c(l,e)},onClick:function(e){return h(l,e)}})},markerColors:"seq:red_purple",rangeBorderWidth:0,rangeBorderColor:{from:"color"},measureSize:.4,measureBorderWidth:0,measureBorderColor:{from:"color"},markerSize:.6,isInteractive:!0,tooltip:function(e){var r=e.color,o=e.v0,t=e.v1;return g(x,{id:t?p("span",{children:[g("strong",{children:o})," to ",g("strong",{children:t})]}):g("strong",{children:o}),enableChip:!0,color:r})},animate:n.animate,motionConfig:n.config,margin:i,role:"img"},O=function(e,r,o,t,n){void 0===n&&(n=!1);var i=r.domain(),a=i[0],l=i[1];return[].concat(e,["measures"===t||e.includes(l)?0:l]).filter((function(e){return 0!==e})).sort((function(e,r){return e-r})).reduce((function(e,r,t){var i,l=e.slice(-1)[0],u=null!=(i=null==l?void 0:l.v1)?i:a,s=!0===n?u+(r-u)/2:r;return[].concat(e,[{index:t,v0:u,v1:r,color:o("sequential"===o.type?s:t)}])}),[])},R=["position"],L=function(o){var t=o.scale,n=o.layout,i=o.reverse,l=o.markers,u=o.height,s=o.markerSize,c=o.component,h=o.onMouseEnter,m=o.onMouseLeave,d=o.onClick,v=r((function(){return function(e){var r=e.layout,o=e.reverse,t=e.scale,n=e.height,i=e.markerSize;return"horizontal"===r?function(e){return{x:t(e.value),y:n/2,size:i,rotation:!0===o?180:0}}:function(e){return{x:n/2,y:t(e.value),size:i,rotation:!0===o?270:90}}}({layout:n,reverse:i,scale:t,height:u,markerSize:s})}),[n,i,t,u,s]),p=a(),x=p.animate,C=p.config,k=f(l.map((function(e){return W({},e,{position:v(e)})})),{keys:function(e){return""+e.index},enter:function(e){var r=e.color,o=e.position;return{color:r,transform:"rotate("+o.rotation+", "+o.x+", "+o.y+")",x:o.x,y1:o.y-o.size/2,y2:o.y+o.size/2}},update:function(e){var r=e.color,o=e.position;return{color:r,transform:"rotate("+o.rotation+", "+o.x+", "+o.y+")",x:o.x,y1:o.y-o.size/2,y2:o.y+o.size/2}},config:C,immediate:!x});return g(y,{children:k((function(r,o){var t=o.position,n=B(o,R);return e.createElement(c,W({key:n.index},n,t,{animatedProps:r,data:n,onMouseEnter:h,onMouseMove:h,onMouseLeave:m,onClick:d}))}))})},H=function(e){var t=e.animatedProps,n=e.data,i=e.layout,l=e.y,u=e.borderColor,s=e.borderWidth,c=e.component,h=e.reverse,v=e.scale,p=e.height,y=e.onMouseEnter,x=e.onMouseLeave,C=e.onClick,k=r((function(){return function(e){var r=e.data,o=function(e){var r=e.layout,o=e.reverse,t=e.scale,n=e.height;return"horizontal"===r?!0===o?function(e){var r=t(e.v1);return{x:r,y:0,width:t(e.v0)-r,height:n}}:function(e){var r=t(e.v0);return{x:r,y:0,width:t(e.v1)-r,height:n}}:!0===o?function(e){var r=t(e.v0),o=t(e.v1)-r;return{x:0,y:r,width:n,height:o}}:function(e){var r=t(e.v1),o=t(e.v0)-r;return{x:0,y:r,width:n,height:o}}}({layout:e.layout,reverse:e.reverse,scale:e.scale,height:e.height});return r.map((function(e){return W({data:e},o(e))}))}({data:n,layout:i,reverse:h,scale:v,height:p})}),[n,i,h,v,p]),M=z(),b=w(u,M),B=function(e){return"translate("+("horizontal"===i?0:e)+","+("horizontal"===i?e:0)+")"},P=t?d(t.measuresY,B):B(l),E=a(),O=E.animate,R=E.config,L=f(k,{keys:function(e){return""+e.data.index},enter:function(e){return{x:e.x,y:e.y,width:e.width,height:e.height,color:e.data.color}},update:function(e){return{x:e.x,y:e.y,width:e.width,height:e.height,color:e.data.color}},config:R,immediate:!O});return g(m.g,{transform:P,children:L((function(e,r){return o(c,{key:r.data.index,index:r.data.index,animatedProps:e,data:r.data,x:e.x.get(),y:e.y.get(),borderColor:b(r.data),borderWidth:s,width:d(e.width,(function(e){return Math.max(e,0)})).get(),height:d(e.height,(function(e){return Math.max(e,0)})).get(),color:e.color.get(),onMouseEnter:y,onMouseMove:y,onMouseLeave:x,onClick:C})}))})},S=function(e){var o=e.id,t=e.scale,n=e.layout,i=e.reverse,u=e.axisPosition,s=e.x,c=e.y,h=e.width,d=e.height,f=e.title,y=void 0===f?o:f,x=e.titlePosition,w=e.titleAlign,b=e.titleOffsetX,B=e.titleOffsetY,P=e.titleRotation,E=e.tooltip,R=e.rangeBorderColor,S=e.rangeBorderWidth,V=e.rangeComponent,Y=e.rangeColors,q=e.ranges,A=e.measureBorderColor,X=e.measureBorderWidth,j=e.measureComponent,I=e.measureHeight,T=e.measureColors,_=e.measures,F=e.markerComponent,D=e.markerColors,G=e.markerHeight,J=e.markers,K=void 0===J?[]:J,N=e.onRangeClick,Q=e.onMeasureClick,U=e.onMarkerClick,Z=z(),$=C(),ee=$.showTooltipFromEvent,re=$.hideTooltip,oe=r((function(){var e=l(Y,t,!0);return O(q,t,e,"range")}),[Y,q,t]),te=r((function(){var e=l(T,t);return O(_,t,e,"measures")}),[T,_,t]),ne=r((function(){var e=l(D,t);return K.map((function(r,o){return{value:r,index:o,color:e("sequential"===e.type?r:o)}}))}),[D,K,t]),ie=E,ae=g(H,{data:oe,scale:t,layout:n,reverse:i,x:0,y:0,width:h,height:d,component:V,borderColor:R,borderWidth:S,onMouseEnter:function(e,r){ee(g(ie,{color:e.color,v0:e.v0,v1:e.v1}),r)},onMouseLeave:re,onClick:function(e,r){null==N||N(W({id:o},e),r)}}),le=g(L,{markers:ne,scale:t,layout:n,reverse:i,height:d,markerSize:G,component:F,onMouseEnter:function(e,r){ee(g(ie,{color:e.color,v0:e.value}),r)},onMouseLeave:re,onClick:function(e,r){null==U||U(W({id:o},e),r)}}),ue=g("g",{transform:"translate("+("vertical"===n&&"after"===u?d:0)+","+("horizontal"===n&&"after"===u?d:0)+")",children:g(k,{axis:"horizontal"===n?"x":"y",length:"horizontal"===n?h:d,scale:t,ticksPosition:u})}),se=g("g",{transform:"translate("+("horizontal"===n?"before"===x?b:h+b:d/2+b)+","+("horizontal"===n?d/2+B:"before"===x?B:h+B)+") rotate("+P+")",children:"string"==typeof y?g(M,{style:W({},Z.labels.text,{dominantBaseline:"central",textAnchor:w}),children:y}):y}),ce=a(),he=ce.animate,me=ce.config,de=v({measuresY:(d-I)/2,transform:"translate("+s+","+c+")",config:me,immediate:!he});return p(m.g,{transform:de.transform,children:[ae,g(H,{animatedProps:de,data:te,scale:t,layout:n,reverse:i,x:0,y:0,width:h,height:I,component:j,borderColor:A,borderWidth:X,onMouseEnter:function(e,r){ee(g(ie,{color:e.color,v0:e.v1}),r)},onMouseLeave:re,onClick:function(e,r){null==Q||Q(W({id:o},e),r)}}),ue,le,se]})},V=t((function(e,o){var t=W({},E,e),n=t.data,i=t.layout,a=t.spacing,l=t.measureSize,h=t.markerSize,m=t.reverse,d=t.axisPosition,f=t.minValue,v=t.maxValue,p=t.margin,y=t.width,x=t.height,C=t.titlePosition,k=t.titleAlign,M=t.titleOffsetX,z=t.titleOffsetY,w=t.titleRotation,B=t.rangeBorderColor,P=t.rangeBorderWidth,O=t.rangeComponent,R=t.rangeColors,L=t.measureBorderColor,H=t.measureBorderWidth,V=t.measureComponent,Y=t.measureColors,q=t.markerComponent,A=t.markerColors,X=t.theme,j=t.tooltip,I=void 0===j?E.tooltip:j,T=t.animate,_=t.motionConfig,F=t.isInteractive,D=t.onRangeClick,G=t.onMeasureClick,J=t.onMarkerClick,K=t.role,N=u(y,x,p),Q=N.margin,U=N.innerWidth,Z=N.innerHeight,$="horizontal"===i?(Z-a*(n.length-1))/n.length:(U-a*(n.length-1))/n.length,ee=$*l,re=$*h,oe=function(e,o){var t=o.layout,n=o.maxValue,i=o.minValue,a=o.reverse,l=o.height,u=o.width;return r((function(){return e.map((function(e){var r,o=[].concat(e.ranges,e.measures,null!=(r=e.markers)?r:[]),s=null!=n?n:Math.max.apply(Math,o),c=null!=i?i:Math.min.apply(Math,o);return W({},e,{scale:b({clamp:!0,min:c,max:s,type:"linear",round:!0},{all:o,max:s,min:c},"horizontal"===t?u:l,"horizontal"===t?a?"y":"x":a?"x":"y")})}))}),[e,l,t,n,i,a,u])}(n,{height:Z,layout:i,maxValue:"auto"===v?void 0:v,minValue:"auto"===f?void 0:f,reverse:m,width:U});return g(s,{isInteractive:F,theme:X,animate:T,motionConfig:_,children:g(c,{width:y,height:x,margin:Q,role:K,ref:o,children:oe.map((function(e,r){return g(S,W({},e,{layout:i,reverse:m,x:"vertical"===i?$*r+a*r:0,y:"horizontal"===i?$*r+a*r:0,width:U,height:$,titlePosition:C,titleAlign:k,titleOffsetX:M,titleOffsetY:z,titleRotation:w,measureHeight:ee,markerHeight:re,rangeBorderColor:B,rangeBorderWidth:P,rangeComponent:O,rangeColors:R,measureBorderColor:L,measureBorderWidth:H,measureComponent:V,measureColors:Y,markerComponent:q,markerColors:A,axisPosition:d,onRangeClick:D,onMeasureClick:G,onMarkerClick:J,tooltip:I}),e.id)}))})})})),Y=["defaultWidth","defaultHeight","onResize","debounceResize"],q=t((function(e,r){var o=e.defaultWidth,t=e.defaultHeight,n=e.onResize,i=e.debounceResize,a=B(e,Y);return g(h,{defaultWidth:o,defaultHeight:t,onResize:n,debounceResize:i,children:function(e){var o=e.width,t=e.height;return g(V,W({width:o,height:t},a,{ref:r}))}})}));export{V as Bullet,S as BulletItem,q as ResponsiveBullet,E as defaultProps};
//# sourceMappingURL=nivo-bullet.mjs.map