react-native-graph-plus
Version:
📈 Beautiful, high-performance Graphs and Charts for React Native +
65 lines (64 loc) • 2.5 kB
JavaScript
function _extends() { _extends = Object.assign || function (target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i]; for (var key in source) { if (Object.prototype.hasOwnProperty.call(source, key)) { target[key] = source[key]; } } } return target; }; return _extends.apply(this, arguments); }
import { Canvas, LinearGradient, Path, vec } from '@shopify/react-native-skia';
import { getSixDigitHex } from './utils/getSixDigitHex';
import React, { useCallback, useMemo, useState } from 'react';
import { View, StyleSheet } from 'react-native';
import { createGraphPath, getGraphPathRange, getPointsInRange } from './CreateGraphPath';
export function StaticLineGraph(_ref) {
let {
points: allPoints,
range,
color,
lineThickness = 3,
enableFadeInMask,
style,
...props
} = _ref;
const [width, setWidth] = useState(0);
const [height, setHeight] = useState(0);
const onLayout = useCallback(_ref2 => {
let {
nativeEvent: {
layout
}
} = _ref2;
setWidth(Math.round(layout.width));
setHeight(Math.round(layout.height));
}, []);
const pathRange = useMemo(() => getGraphPathRange(allPoints, range), [allPoints, range]);
const pointsInRange = useMemo(() => getPointsInRange(allPoints, pathRange), [allPoints, pathRange]);
const path = useMemo(() => createGraphPath({
pointsInRange: pointsInRange,
range: pathRange,
canvasHeight: height,
canvasWidth: width,
horizontalPadding: lineThickness,
verticalPadding: lineThickness
}), [height, lineThickness, pathRange, pointsInRange, width]);
const gradientColors = useMemo(() => [`${getSixDigitHex(color)}00`, `${getSixDigitHex(color)}ff`], [color]);
const gradientFrom = useMemo(() => vec(0, 0), []);
const gradientTo = useMemo(() => vec(width * 0.15, 0), [width]);
return /*#__PURE__*/React.createElement(View, _extends({}, props, {
style: style,
onLayout: onLayout
}), /*#__PURE__*/React.createElement(Canvas, {
style: styles.svg
}, /*#__PURE__*/React.createElement(Path, {
path: path,
strokeWidth: lineThickness,
color: enableFadeInMask ? undefined : color,
style: "stroke",
strokeJoin: "round",
strokeCap: "round"
}, enableFadeInMask && /*#__PURE__*/React.createElement(LinearGradient, {
start: gradientFrom,
end: gradientTo,
colors: gradientColors
}))));
}
const styles = StyleSheet.create({
svg: {
flex: 1
}
});
//# sourceMappingURL=StaticLineGraph.js.map