victory-native
Version:
A charting library for React Native with a focus on performance and customization.
64 lines (63 loc) • 3.23 kB
JavaScript
;
var __rest = (this && this.__rest) || function (s, e) {
var t = {};
for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p) && e.indexOf(p) < 0)
t[p] = s[p];
if (s != null && typeof Object.getOwnPropertySymbols === "function")
for (var i = 0, p = Object.getOwnPropertySymbols(s); i < p.length; i++) {
if (e.indexOf(p[i]) < 0 && Object.prototype.propertyIsEnumerable.call(s, p[i]))
t[p[i]] = s[p[i]];
}
return t;
};
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.useStackedBarPaths = void 0;
const react_1 = __importDefault(require("react"));
const react_native_skia_1 = require("@shopify/react-native-skia");
const createRoundedRectPath_1 = require("../../utils/createRoundedRectPath");
const CartesianChartContext_1 = require("../contexts/CartesianChartContext");
const getStackedBarSegments_1 = require("../utils/getStackedBarSegments");
const getVerticalStackedBarRect_1 = require("../utils/getVerticalStackedBarRect");
const useBarWidth_1 = require("./useBarWidth");
const DEFAULT_COLORS = ["red", "orange", "blue", "green", "blue", "purple"];
const DEFAULT_BAR_OPTIONS = () => ({});
const useStackedBarPaths = ({ points, chartBounds, innerPadding = 0.25, barWidth: customBarWidth, barCount, barOptions = DEFAULT_BAR_OPTIONS, colors = DEFAULT_COLORS, }) => {
const { yScale } = (0, CartesianChartContext_1.useCartesianChartContext)();
const barWidth = (0, useBarWidth_1.useBarWidth)({
points,
chartBounds,
innerPadding,
customBarWidth,
barCount,
});
const paths = react_1.default.useMemo(() => {
const bars = [];
const segments = (0, getStackedBarSegments_1.getStackedBarSegments)(points);
segments.forEach((segment) => {
const rect = (0, getVerticalStackedBarRect_1.getVerticalStackedBarRect)({
segment,
yScale,
barWidth,
});
if (!rect)
return;
const options = barOptions((0, getStackedBarSegments_1.getVerticalStackedBarOptionsContext)(segment));
const { roundedCorners, color } = options, ops = __rest(options, ["roundedCorners", "color"]);
const path = react_native_skia_1.Skia.Path.Make();
if (roundedCorners) {
const nonUniformRoundedRect = (0, createRoundedRectPath_1.createRoundedRectPath)(rect.x, rect.y, rect.width, rect.height, roundedCorners, segment.value);
path.addRRect(nonUniformRoundedRect);
}
else {
path.addRect(react_native_skia_1.Skia.XYWHRect(rect.x, rect.y, rect.width, rect.height));
}
bars.push(Object.assign({ path, key: `${segment.seriesIndex}-${segment.datumIndex}`, color: color !== null && color !== void 0 ? color : colors[segment.seriesIndex] }, ops));
});
return bars;
}, [barOptions, barWidth, colors, points, yScale]);
return paths;
};
exports.useStackedBarPaths = useStackedBarPaths;