UNPKG

react-native-pie-chart

Version:
1 lines 3.76 kB
var _interopRequireDefault=require("@babel/runtime/helpers/interopRequireDefault");Object.defineProperty(exports,"__esModule",{value:true});exports.default=void 0;var _slicedToArray2=_interopRequireDefault(require("@babel/runtime/helpers/slicedToArray"));var _react=require("react");var _reactNativeSvg=require("react-native-svg");var d3=_interopRequireWildcard(require("d3-shape"));var _jsxRuntime=require("react/jsx-runtime");var _this=this,_jsxFileName="/Users/Aidin.Gharibnavaz/workspace/aidin36/react-native-pie-chart/src/index.tsx";function _interopRequireWildcard(e,t){if("function"==typeof WeakMap)var r=new WeakMap(),n=new WeakMap();return(_interopRequireWildcard=function _interopRequireWildcard(e,t){if(!t&&e&&e.__esModule)return e;var o,i,f={__proto__:null,default:e};if(null===e||"object"!=typeof e&&"function"!=typeof e)return f;if(o=t?n:r){if(o.has(e))return o.get(e);o.set(e,f);}for(var _t in e)"default"!==_t&&{}.hasOwnProperty.call(e,_t)&&((i=(o=Object.defineProperty)&&Object.getOwnPropertyDescriptor(e,_t))&&(i.get||i.set)?o(f,_t,i):f[_t]=e[_t]);return f;})(e,t);}var PieChart=function PieChart(_ref){var widthAndHeight=_ref.widthAndHeight,series=_ref.series,cover=_ref.cover,_ref$style=_ref.style,style=_ref$style===void 0?{}:_ref$style,padAngle=_ref.padAngle;series.forEach(function(s){if(s.value<0){throw Error(`Invalid series: all numbers should be positive. The invalid slice: ${JSON.stringify(s)}`);}if(!s.color){throw Error(`'color' is mandatory in the series. The invalid slice: ${JSON.stringify(s)}`);}});var sum=series.reduce(function(acc,current){return acc+current.value;},0);if(sum<=0){throw Error('Invalid series: sum of series is zero');}var coverRadius=typeof cover==='object'?cover.radius:cover;var coverColor=typeof cover==='object'?cover.color:undefined;if(coverRadius!=null&&(coverRadius<=0||coverRadius>=1)){throw Error(`Invalid "coverRadius": It should be between zero and one. But it's ${coverRadius}`);}var radius=widthAndHeight/2;var arcs=d3.pie().sort(null).value(function(d){return d.value;})(series);return(0,_jsxRuntime.jsx)(_reactNativeSvg.Svg,{style:style,width:widthAndHeight,height:widthAndHeight,children:(0,_jsxRuntime.jsxs)(_reactNativeSvg.G,{transform:`translate(${widthAndHeight/2}, ${widthAndHeight/2})`,children:[arcs.map(function(arc){var _sliceLabel$offsetX,_sliceLabel$offsetY;var arcGenerator=d3.arc().outerRadius(radius).startAngle(arc.startAngle).endAngle(arc.endAngle);if(padAngle){arcGenerator=arcGenerator.padAngle(padAngle);}if(!coverRadius){arcGenerator=arcGenerator.innerRadius(0);}else{arcGenerator=arcGenerator.innerRadius(coverRadius*radius);}var _arcGenerator$centroi=arcGenerator.centroid(arc),_arcGenerator$centroi2=(0,_slicedToArray2.default)(_arcGenerator$centroi,2),sliceCenterX=_arcGenerator$centroi2[0],sliceCenterY=_arcGenerator$centroi2[1];var sliceColor=arc.data.color;var sliceLabel=arc.data.label;return(0,_jsxRuntime.jsxs)(_react.Fragment,{children:[(0,_jsxRuntime.jsx)(_reactNativeSvg.Path,{fill:sliceColor,d:arcGenerator()},`p-${arc.index}`),sliceLabel&&(0,_jsxRuntime.jsx)(_reactNativeSvg.Text,{x:sliceCenterX+((_sliceLabel$offsetX=sliceLabel.offsetX)!=null?_sliceLabel$offsetX:0),y:sliceCenterY+((_sliceLabel$offsetY=sliceLabel.offsetY)!=null?_sliceLabel$offsetY:0),textAnchor:"middle",fill:sliceLabel.fill,stroke:sliceLabel.stroke,fontSize:sliceLabel.fontSize,fontWeight:sliceLabel.fontWeight,fontFamily:sliceLabel.fontFamily,fontStyle:sliceLabel.fontStyle,children:sliceLabel.text},`t-${arc.index}`)]},`f-${arc.index}`);}),coverRadius&&coverRadius>0&&coverColor&&(0,_jsxRuntime.jsx)(_reactNativeSvg.Path,{fill:coverColor,d:d3.arc().outerRadius(coverRadius*radius).innerRadius(0).startAngle(0).endAngle(360)()},'cover')]})});};var _default=exports.default=PieChart;