UNPKG

react-native-wagmi-charts

Version:

A sweet candlestick chart for React Native

186 lines (183 loc) 4.76 kB
'use strict'; Object.defineProperty(exports, '__esModule', { value: true, }); exports.LineChart = LineChart; exports.LineChartDimensionsContext = void 0; var _react = _interopRequireDefault(require('react')); var d3Shape = _interopRequireWildcard(require('d3-shape')); var _reactNative = require('react-native'); var _Data = require('./Data'); var _reactNativeRedash = require('react-native-redash'); var _utils = require('./utils'); var _Context = require('./Context'); function _interopRequireWildcard(e, t) { if ('function' == typeof WeakMap) var r = new WeakMap(), n = new WeakMap(); return (_interopRequireWildcard = function (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 (const 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); } function _interopRequireDefault(e) { return e && e.__esModule ? e : { default: e }; } function _extends() { return ( (_extends = Object.assign ? Object.assign.bind() : function (n) { for (var e = 1; e < arguments.length; e++) { var t = arguments[e]; for (var r in t) ({}).hasOwnProperty.call(t, r) && (n[r] = t[r]); } return n; }), _extends.apply(null, arguments) ); } const LineChartDimensionsContext = (exports.LineChartDimensionsContext = /*#__PURE__*/ _react.default.createContext({ width: 0, height: 0, pointWidth: 0, parsedPath: {}, path: '', area: '', shape: d3Shape.curveBumpX, gutter: 0, pathWidth: 0, })); const { width: screenWidth } = _reactNative.Dimensions.get('window'); LineChart.displayName = 'LineChart'; function LineChart({ children, yGutter = 16, width = screenWidth, height = screenWidth, shape = d3Shape.curveBumpX, id, absolute, ...props }) { const { yDomain, xLength, xDomain } = _react.default.useContext( _Context.LineChartContext ); const { data } = (0, _Data.useLineChartData)({ id, }); // Reserve space at the bottom for x-axis cursor labels const X_AXIS_LABEL_RESERVED_HEIGHT = 40; const chartDrawingHeight = height - X_AXIS_LABEL_RESERVED_HEIGHT; const pathWidth = _react.default.useMemo(() => { let allowedWidth = width; if (data && xLength > data.length) { allowedWidth = (width * data.length) / xLength; } return allowedWidth; }, [data, width, xLength]); const path = _react.default.useMemo(() => { if (data && data.length > 0) { return (0, _utils.getPath)({ data, width: pathWidth, height: chartDrawingHeight, gutter: yGutter, shape, yDomain, xDomain, }); } return ''; }, [data, pathWidth, chartDrawingHeight, yGutter, shape, yDomain, xDomain]); const area = _react.default.useMemo(() => { if (data && data.length > 0) { return (0, _utils.getArea)({ data, width: pathWidth, height: chartDrawingHeight, gutter: yGutter, shape, yDomain, xDomain, }); } return ''; }, [data, pathWidth, chartDrawingHeight, yGutter, shape, yDomain, xDomain]); const parsedPath = _react.default.useMemo( () => (0, _reactNativeRedash.parse)(path), [path] ); const pointWidth = _react.default.useMemo( () => width / (data ? data.length - 1 : 1), [data, width] ); const contextValue = _react.default.useMemo( () => ({ gutter: yGutter, parsedPath, pointWidth, area, path, width, height, pathWidth, shape: shape, }), [ yGutter, parsedPath, pointWidth, area, path, width, height, pathWidth, shape, ] ); return /*#__PURE__*/ _react.default.createElement( _Data.LineChartIdProvider, { id: id, }, /*#__PURE__*/ _react.default.createElement( LineChartDimensionsContext.Provider, { value: contextValue, }, /*#__PURE__*/ _react.default.createElement( _reactNative.View, _extends({}, props, { style: [absolute && styles.absolute, props.style], }), children ) ) ); } const styles = _reactNative.StyleSheet.create({ absolute: { position: 'absolute', }, }); //# sourceMappingURL=Chart.js.map