UNPKG

victory-native

Version:

A charting library for React Native with a focus on performance and customization.

85 lines (84 loc) 4.26 kB
"use strict"; var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) { if (k2 === undefined) k2 = k; var desc = Object.getOwnPropertyDescriptor(m, k); if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) { desc = { enumerable: true, get: function() { return m[k]; } }; } Object.defineProperty(o, k2, desc); }) : (function(o, m, k, k2) { if (k2 === undefined) k2 = k; o[k2] = m[k]; })); var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) { Object.defineProperty(o, "default", { enumerable: true, value: v }); }) : function(o, v) { o["default"] = v; }); var __importStar = (this && this.__importStar) || function (mod) { if (mod && mod.__esModule) return mod; var result = {}; if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k); __setModuleDefault(result, mod); return result; }; 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; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.useHorizontalStackedBarPaths = void 0; const React = __importStar(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 getBarThickness_1 = require("../utils/getBarThickness"); const getHorizontalStackedBarRect_1 = require("../utils/getHorizontalStackedBarRect"); const DEFAULT_COLORS = ["red", "orange", "blue", "green", "blue", "purple"]; const DEFAULT_BAR_OPTIONS = () => ({}); const useHorizontalStackedBarPaths = ({ points, chartBounds, innerPadding = 0.25, barWidth: customBarWidth, barCount, barOptions = DEFAULT_BAR_OPTIONS, colors = DEFAULT_COLORS, }) => { const { xScale } = (0, CartesianChartContext_1.useCartesianChartContext)(); const barWidth = (0, getBarThickness_1.getBarThickness)({ points, axisStart: chartBounds.top, axisEnd: chartBounds.bottom, innerPadding, customBarThickness: customBarWidth, barCount, }); const paths = React.useMemo(() => { const bars = []; const segments = (0, getStackedBarSegments_1.getStackedBarSegments)(points); segments.forEach((segment) => { const rect = (0, getHorizontalStackedBarRect_1.getHorizontalStackedBarRect)({ segment, xScale, barWidth, }); if (!rect) return; const options = barOptions((0, getStackedBarSegments_1.getHorizontalStackedBarOptionsContext)(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, "horizontal"); 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, xScale]); return paths; }; exports.useHorizontalStackedBarPaths = useHorizontalStackedBarPaths;