react-native-wagmi-charts
Version:
A sweet candlestick chart for React Native
143 lines (141 loc) • 3.48 kB
JavaScript
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)
);
}
import React from 'react';
import * as d3Shape from 'd3-shape';
import { Dimensions, StyleSheet, View } from 'react-native';
import { LineChartIdProvider, useLineChartData } from './Data';
import { parse } from 'react-native-redash';
import { getArea, getPath } from './utils';
import { LineChartContext } from './Context';
export const LineChartDimensionsContext = /*#__PURE__*/ React.createContext({
width: 0,
height: 0,
pointWidth: 0,
parsedPath: {},
path: '',
area: '',
shape: d3Shape.curveBumpX,
gutter: 0,
pathWidth: 0,
});
const { width: screenWidth } = Dimensions.get('window');
LineChart.displayName = 'LineChart';
export function LineChart({
children,
yGutter = 16,
width = screenWidth,
height = screenWidth,
shape = d3Shape.curveBumpX,
id,
absolute,
...props
}) {
const { yDomain, xLength, xDomain } = React.useContext(LineChartContext);
const { 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.useMemo(() => {
let allowedWidth = width;
if (data && xLength > data.length) {
allowedWidth = (width * data.length) / xLength;
}
return allowedWidth;
}, [data, width, xLength]);
const path = React.useMemo(() => {
if (data && data.length > 0) {
return getPath({
data,
width: pathWidth,
height: chartDrawingHeight,
gutter: yGutter,
shape,
yDomain,
xDomain,
});
}
return '';
}, [data, pathWidth, chartDrawingHeight, yGutter, shape, yDomain, xDomain]);
const area = React.useMemo(() => {
if (data && data.length > 0) {
return getArea({
data,
width: pathWidth,
height: chartDrawingHeight,
gutter: yGutter,
shape,
yDomain,
xDomain,
});
}
return '';
}, [data, pathWidth, chartDrawingHeight, yGutter, shape, yDomain, xDomain]);
const parsedPath = React.useMemo(() => parse(path), [path]);
const pointWidth = React.useMemo(
() => width / (data ? data.length - 1 : 1),
[data, width]
);
const contextValue = React.useMemo(
() => ({
gutter: yGutter,
parsedPath,
pointWidth,
area,
path,
width,
height,
pathWidth,
shape: shape,
}),
[
yGutter,
parsedPath,
pointWidth,
area,
path,
width,
height,
pathWidth,
shape,
]
);
return /*#__PURE__*/ React.createElement(
LineChartIdProvider,
{
id: id,
},
/*#__PURE__*/ React.createElement(
LineChartDimensionsContext.Provider,
{
value: contextValue,
},
/*#__PURE__*/ React.createElement(
View,
_extends({}, props, {
style: [absolute && styles.absolute, props.style],
}),
children
)
)
);
}
const styles = StyleSheet.create({
absolute: {
position: 'absolute',
},
});
//# sourceMappingURL=Chart.js.map