UNPKG

@groww-tech/react-charts

Version:
2 lines (1 loc) 10.8 kB
import{_ as e,a as n}from"./tslib.es6-fef9fe46.js";import t,{useState as o,useMemo as i}from"react";import{i as r}from"./index-aa089771.js";import{m as l,a,l as s,b as u}from"./localPoint-1ddfe424.js";import{a as d}from"./D3ShapeFactories-c73bea6f.js";export default function(c){var v,f=o(null),m=f[0],p=f[1],g=o(null),h=g[0],y=g[1],E=c.paddingVert,x=c.paddingHorz,P=c.height,k=c.width,M=c.linePaths,S=c.onMouseLeave,T=i(function(){if(r(M))return null;var e=function(e){return null==e?void 0:e[0]},n=function(e){return null==e?void 0:e[1]},t=Number.MAX_SAFE_INTEGER,o=Number.MIN_SAFE_INTEGER,i=Number.MAX_SAFE_INTEGER,s=Number.MIN_SAFE_INTEGER;return M.forEach(function(r){if(r.isSeriesToScale){var u=l(r.series,n),d=a(r.series,n),c=l(r.series,e),v=a(r.series,e);void 0!==u&&(t=Math.min(t,u)),void 0!==d&&(o=Math.max(o,d)),void 0!==c&&(i=Math.min(i,c)),void 0!==v&&(s=Math.max(s,v))}}),{scaleX:{domain:[i,s],range:[0+x,k-x]},scaleY:{domain:[t,o],range:[P-E,E]}}},[M,k,P,x,E]),b=i(function(){var e;if(!T||r(M)||!(null===(e=M[0])||void 0===e?void 0:e.series)||0===M[0].series.length)return{x:k/2,y:P/2};var n=T.scaleX,t=T.scaleY,o=M[0].series,i=0,l=0,a=0;return o.forEach(function(e){var o=e[0],r=e[1];if("number"==typeof o&&"number"==typeof r){var s=M[0].isSeriesToScale?function(e){return n.range[0]+(e-n.domain[0])*(n.range[1]-n.range[0])/(n.domain[1]-n.domain[0])}(o):o,u=M[0].isSeriesToScale?function(e){return t.range[0]+(e-t.domain[0])*(t.range[1]-t.range[0])/(t.domain[1]-t.domain[0])}(r):r;i+=s,l+=u,a++}}),a>0?{x:i/a,y:l/a}:{x:k/2,y:P/2}},[T,M,k,P]);if(r(M)||!T)return null;var D=T.scaleX,w=T.scaleY;function L(e){return w.range[0]+(e-w.domain[0])*(w.range[1]-w.range[0])/(w.domain[1]-w.domain[0])}function O(e){return D.range[0]+(e-D.domain[0])*(D.range[1]-D.range[0])/(D.domain[1]-D.domain[0])}var W,C,F,N,R,I,_,A,G,U,X,Y,j,B,V,H,z=function(e){return null==e?void 0:e[0]},q=function(e){return null==e?void 0:e[1]},J=function(e){return D.domain[0]+(e-D.range[0])*(D.domain[1]-D.domain[0])/(D.range[1]-D.range[0])},K=function(e,n){return(0,u(z).left)(n,J(e),0)},Q=function(e){var n,t,o,i,r,l,a=null!=h?h:{},s=a.startPoint,u=a.endPoint,d=null!==(n=null==e?void 0:e.draggableConfig)&&void 0!==n?n:{},c=d.fill,v=void 0===c?null==e?void 0:e.color:c,f=d.lineColor,m=d.negativeLineColor;if(void 0===s||void 0===u)return null==e?void 0:e.color;var p=null===(o=null===(t=null==e?void 0:e.series)||void 0===t?void 0:t[K(s,e.series)])||void 0===o?void 0:o[1],g=null===(r=null===(i=null==e?void 0:e.series)||void 0===i?void 0:i[K(u,e.series)])||void 0===r?void 0:r[1],y=null!=f?f:v,E=null!==(l=null!=m?m:f)&&void 0!==l?l:v;return s<u?p<g?y:E:p>g?y:E},Z=function(){null==S||S(),p(null)},$=function(t){t.preventDefault();var o=c.onMouseEnter,i=s(t)||{x:0,y:0},l=i.x,a=i.y,u=J(l),d=function(e){return w.domain[0]+(e-w.range[0])*(w.domain[1]-w.domain[0])/(w.range[1]-w.range[0])}(a),v=[];null==M||M.map(function(t){var o,i,a=t.isDraggable,s=n([],t.series,!0),d=s.length,c=K(l,s),f=0!==c&&!r(null===(o=null==s?void 0:s[c])||void 0===o?void 0:o[0])&&(null===(i=null==s?void 0:s[c])||void 0===i?void 0:i[0])>=u;c===d&&(c=d-1);var m=s[c],p=s[c>0?c-1:c],g=(m[1]-p[1])/(m[0]-p[0])*(u-p[0])+p[1],E={point:m,tooltipLeft:(null==t?void 0:t.isSeriesToScale)?O(z(m)):z(m),tooltipTop:(null==t?void 0:t.isSeriesToScale)?L(q(m)):q(m),isPerfectIntersection:f,intersectionPointOnLine:{coords:{x:O(u),y:L(g)},invertedValues:{x:u,y:g}}};if(a&&h){var x=s[K(null==h?void 0:h.startPoint,s)];v.push(e(e({},E),{dragStartPoint:x,dragTooltipLeft:null==h?void 0:h.startPoint})),x||y(null),u<d-1&&void 0!==h.startPoint&&y(e(e({},h),{endPoint:l}))}else v.push(E)});var f={seriesData:v,otherData:{coords:{x:l,y:a},invertedValues:{x:u,y:d}}};null==o||o(f),p(f)},ee=function(e,n,o,i,r){var l=null!=r?r:1.5;return t.createElement(t.Fragment,null,t.createElement("circle",{cx:e,cy:n,r:i*l,fill:o,stroke:"var(--dangerouslySetPrimaryBg)",strokeWidth:i,pointerEvents:"none"}),t.createElement("circle",{cx:e,cy:n,r:i*l,fill:"rgba(0, 0, 0, 0)",stroke:o,strokeWidth:i*l/2,pointerEvents:"none",opacity:"0.3"}))};return t.createElement(t.Fragment,null,t.createElement("svg",{width:k,height:P,onTouchMove:$,onMouseMove:$,onTouchStart:$,onTouchEnd:Z,onMouseLeave:Z,onMouseUp:function(){var e;null===(e=c.onMouseUp)||void 0===e||e.call(c),y(null)},onMouseDown:function(e){var n;if(null===(n=c.onMouseDown)||void 0===n||n.call(c),c.isDragAllowed){var t=(s(e)||{x:0}).x;y({startPoint:t})}}},t.createElement("defs",null,null===(v=c.getDefs)||void 0===v?void 0:v.call(c),(I=M[0],_=function(){var e,n,t;return void 0!==(null==h?void 0:h.startPoint)&&void 0!==(null==h?void 0:h.endPoint)?{x:(Math.min(h.startPoint,h.endPoint)+Math.max(h.startPoint,h.endPoint))/2,y:P/8}:(null===(t=null===(n=null===(e=null==m?void 0:m.seriesData)||void 0===e?void 0:e[0])||void 0===n?void 0:n.intersectionPointOnLine)||void 0===t?void 0:t.coords)?{x:m.seriesData[0].intersectionPointOnLine.coords.x,y:m.seriesData[0].intersectionPointOnLine.coords.y}:b}(),A=I.series.map(function(e){return I.isSeriesToScale?O(e[0]):e[0]}),G=Math.min.apply(Math,A),U=Math.max.apply(Math,A),X=I.series.map(function(e){return I.isSeriesToScale?L(e[1]):e[1]}),Y=Math.min.apply(Math,X),j=Math.max.apply(Math,X),B="dotPattern-".concat(I.key),V="dotGradient-".concat(I.key),H="dotMask-".concat(I.key),t.createElement(t.Fragment,{key:"patterns-".concat(I.key)},t.createElement("pattern",{id:B,x:"0",y:"0",width:"6",height:"6",patternUnits:"userSpaceOnUse"},t.createElement("rect",{width:"6",height:"6",fill:"black"})," ",t.createElement("circle",{cx:"6",cy:"6",r:"1.5",fill:"white"})),t.createElement("mask",{id:H},t.createElement("rect",{width:"100%",height:"100%",fill:"url(#".concat(B,")")})),t.createElement("radialGradient",{id:V,cx:U-G===0?.5:Math.max(0,Math.min(1,(_.x-G)/(1.75*(U-G)))),cy:j-Y===0?.5:Math.max(0,Math.min(1,(_.y-Y)/(j-Y)/4)),r:j-Y===0?.3:Math.max(.1,Math.min(.8,(j-_.y)/(j-Y)/3.5)),gradientUnits:"objectBoundingBox",gradientTransform:"scale(1.75,4)"},t.createElement("stop",{offset:"0%",stopColor:I.color,stopOpacity:"1"}),t.createElement("stop",{offset:"40%",stopColor:I.color,stopOpacity:"0.8"}),t.createElement("stop",{offset:"70%",stopColor:I.color,stopOpacity:"0.4"}),t.createElement("stop",{offset:"100%",stopColor:I.color,stopOpacity:"0"}))))),t.createElement("g",null,M.map(function(n,o){var i,l,a,s,u,c=function(e){return n.isSeriesToScale?O(z(e)):z(e)},v=function(e){return n.isSeriesToScale?L(q(e)):q(e)},f=(n||{}).areaProps,m=f&&(!r(null==f?void 0:f.toX)||!r(null==f?void 0:f.toY)),p=d({}),g=d({x:c,y:v}),y=f||{},E=y.toX,x=y.toY;r(E)?r(x)||(p.x(c),p.y0(L(null!=x?x:0)),p.y1(v)):(p.y(v),p.x0(O(null!=E?E:0)),p.x1(c));var M=n.isDraggable&&(null==h?void 0:h.endPoint),S=M?function(e){var n,t=null!=h?h:{},o=t.startPoint,i=t.endPoint;if(void 0===o||void 0===i)return[];o>i&&(o=(n=[i,o])[0],i=n[1]),i+=1;var r=K(o,e),l=K(i,e);return null==e?void 0:e.slice(r,l)}(null==n?void 0:n.series):[],T=Q(n),b=d({}),D=null!==(i=null==n?void 0:n.labelData)&&void 0!==i?i:{},w=D.showLabel,W=D.labelText,C=D.labelRefs,F=D.rectSvgProps,N=D.textSvgProps,R=D.labelPosition,I=null!=C?C:{},_=I.underlyinglabelTextRef,A=I.underlyinglabelRectRef;(null===(l=null==n?void 0:n.draggableConfig)||void 0===l?void 0:l.toY)?(b.y(c),b.x0(k),b.x1(v)):(b.x(c),b.y0(P),b.y1(v));var G="dotGradient-".concat(n.key),U=0===o?function(e){return d({x:function(n){return e.isSeriesToScale?O(z(n)):z(n)},y0:P,y1:function(n){return e.isSeriesToScale?L(q(n)):q(n)}})(e.series)||""}(n):"";return t.createElement(t.Fragment,{key:n.key},0===o&&t.createElement("path",{d:U,fill:"url(#".concat(G,")"),mask:"url(#dotMask-".concat(n.key,")"),opacity:"0.6"}),m&&t.createElement("path",{d:p(n.series)||"",fill:null==f?void 0:f.fill,opacity:null==f?void 0:f.opacity,style:e({},f.style)}),t.createElement("path",{d:g(n.series)||"",opacity:null==f?void 0:f.opacity,stroke:0===o?"url(#lineGradient)":n.color,style:e({},n.style),strokeOpacity:n.strokeOpacity,shapeRendering:"geometricPrecision",strokeWidth:n.strokeWidth,fill:"none"}),w&&t.createElement("g",null,t.createElement("rect",e({ref:A},F)),t.createElement("text",e({x:O(null!==(a=null==R?void 0:R.x)&&void 0!==a?a:0),y:L(null!==(s=null==R?void 0:R.y)&&void 0!==s?s:0),ref:_},N),W)),M&&t.createElement(t.Fragment,null,t.createElement("path",{d:b(S)||"",fill:null===(u=null==n?void 0:n.draggableConfig)||void 0===u?void 0:u.fill}),t.createElement("path",{d:g(S)||"",stroke:T,style:e({},n.style),strokeOpacity:n.strokeOpacity,shapeRendering:"geometricPrecision",strokeWidth:n.strokeWidth,fill:"none"})))}),M.map(function(e,n){return t.createElement(t.Fragment,{key:"points-".concat(e.key)},function(e){var n,o=null!==(n=e.hoverPointStrokeMultiplier)&&void 0!==n?n:1.5,i=e.series[e.series.length-1],r=L(q(i)),l=O(z(i));return e.showLastPointBlinking?t.createElement("g",null,t.createElement("circle",{cx:l,cy:r,r:e.strokeWidth*o,fill:e.color,stroke:"var(--dangerouslySetPrimaryBg)",strokeWidth:e.strokeWidth*o/2,pointerEvents:"none",style:e.style},t.createElement("animate",{attributeName:"opacity",values:"1;0.3;1",dur:"2s",repeatCount:"indefinite"}))):null}(e),function(e){var n;if(e.hasHighlightedPoints){var o=null!==(n=e.highlightPointStrokeMultiplier)&&void 0!==n?n:1.5;return t.createElement("g",null,e.series.map(function(n,i){var r;return(null===(r=null==n?void 0:n[3])||void 0===r?void 0:r.highlightPoint)?t.createElement("circle",{key:"highlight-".concat(i),cx:O(z(n)),cy:L(q(n)),r:e.strokeWidth*o,fill:e.color,stroke:e.color,strokeWidth:e.strokeWidth*o/2,pointerEvents:"none"}):null}))}}(e),function(e,n){var t;if(!e.allowToolTip||r(m))return null;var o=e.hoverPointStrokeMultiplier,i=e.strokeWidth,l=e.hoverExactPoint,a=null===(t=null==m?void 0:m.seriesData)||void 0===t?void 0:t[n],s=0,u=0;if(l){if(!(null==a?void 0:a.isPerfectIntersection))return null;s=a.intersectionPointOnLine.coords.x,u=a.intersectionPointOnLine.coords.y}else s=null==a?void 0:a.tooltipLeft,u=null==a?void 0:a.tooltipTop;var d=(null==h?void 0:h.endPoint)?Q(e):null==e?void 0:e.color;return D.range[0]<=s&&s<=D.range[1]&&w.range[1]<=u&&u<=w.range[0]?ee(s,u,d,i,o):void 0}(e,n),function(e){var n,t;if(!e.allowToolTip||!(null==h?void 0:h.endPoint))return null;var o=e.hoverPointStrokeMultiplier,i=e.strokeWidth,r=null==h?void 0:h.startPoint,l=L(null===(t=null===(n=e.series)||void 0===n?void 0:n[K(h.startPoint,null==e?void 0:e.series)])||void 0===t?void 0:t[1]),a=Q(e);return D.range[0]<=r&&r<=D.range[1]&&w.range[1]<=l&&l<=w.range[0]?ee(r,l,a,i,o):void 0}(e))}))),(W=c.getTooltipUI,C=c.toolTipLeftUpdated,F=c.toolTipTopUpdated,N=c.isDragAllowed,R={top:0,left:0,boxShadow:"none",padding:0,backgroundColor:"transparent",position:"absolute",borderRadius:"3px",transform:"translate(".concat(C,"px, ").concat(F,"px)")},m&&W?N&&(null==h?void 0:h.endPoint)?W(m):t.createElement("div",{style:R},W(m)):null))}