victory-native
Version:
A charting library for React Native with a focus on performance and customization.
45 lines (44 loc) • 2.29 kB
JavaScript
;
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.BarGraphLabels = void 0;
const react_1 = __importDefault(require("react"));
const react_native_skia_1 = require("@shopify/react-native-skia");
const getFontGlyphWidth_1 = require("../../utils/getFontGlyphWidth");
const CartesianChartContext_1 = require("../contexts/CartesianChartContext");
const getBarLabelPosition_1 = require("../utils/getBarLabelPosition");
const getBarLabelText_1 = require("../utils/getBarLabelText");
const BarGraphLabels = ({ points, chartBounds, barWidth = 0, options, }) => {
const { position, font, color, formatLabel, rotate } = options;
const { orientation, xScale } = (0, CartesianChartContext_1.useCartesianChartContext)();
// Loop over the data points and position each label
return points.map(({ x, y = 0, yValue }) => {
var _a;
const yText = (0, getBarLabelText_1.getBarLabelText)(yValue, formatLabel);
const labelWidth = (0, getFontGlyphWidth_1.getFontGlyphWidth)(yText, font);
const fontSize = (_a = font === null || font === void 0 ? void 0 : font.getSize()) !== null && _a !== void 0 ? _a : 0;
const labelRotate = rotate !== null && rotate !== void 0 ? rotate : 0;
const labelPosition = (0, getBarLabelPosition_1.getBarLabelPosition)({
orientation,
position,
x,
y: Number(y),
labelWidth,
fontSize,
labelRotate,
barWidth,
chartBounds,
baselineX: xScale(0),
});
const labelOrigin = labelRotate
? (0, react_native_skia_1.vec)(labelPosition.x + labelWidth / 2, labelPosition.y - fontSize / 2)
: undefined;
const labelTransform = labelRotate
? [{ rotate: (Math.PI / 180) * labelRotate }]
: undefined;
return (<react_native_skia_1.Text key={`${labelPosition.x}-${labelPosition.y}-${yText}`} x={labelPosition.x} y={labelPosition.y} text={yText} font={font} color={color} origin={labelOrigin} transform={labelTransform}/>);
});
};
exports.BarGraphLabels = BarGraphLabels;