UNPKG

@nivo/bullet

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