react-native-chart-kit
Version:
Beautiful React Native charts for dashboards, reports, and data-rich mobile apps.
56 lines (55 loc) • 1.88 kB
JavaScript
export const defaultLineChartAxisLabelAnimationDuration = 160;
const getMaxWidth = (sizes) => sizes.reduce((max, size) => Math.max(max, size.width), 0);
export const resolveLineChartYAxisLabelSizes = ({ sizes, stableSizes, width }) => {
const fixedWidth = typeof width === "number" && Number.isFinite(width)
? Math.max(0, width)
: width === "stable"
? getMaxWidth(stableSizes ?? sizes)
: undefined;
if (fixedWidth === undefined) {
return sizes;
}
return sizes.map((size) => ({
...size,
width: fixedWidth
}));
};
export const resolveLineChartAxisLabelAnimationConfig = (animation) => {
if (!animation) {
return {
enabled: false,
duration: 0,
strategy: "crossfade"
};
}
if (animation === true) {
return {
enabled: true,
duration: defaultLineChartAxisLabelAnimationDuration,
strategy: "crossfade"
};
}
return {
enabled: true,
duration: typeof animation.duration === "number" &&
Number.isFinite(animation.duration)
? Math.max(0, animation.duration)
: defaultLineChartAxisLabelAnimationDuration,
strategy: animation.strategy ?? "crossfade"
};
};
export const buildLineChartYAxisLabels = ({ formatYLabel, labelOffset, ticks, yScale }) => {
const keyCounts = new Map();
return ticks.map((tick) => {
const text = formatYLabel(tick);
const baseKey = `${tick}:${text}`;
const duplicateCount = keyCounts.get(baseKey) ?? 0;
keyCounts.set(baseKey, duplicateCount + 1);
return {
key: duplicateCount === 0 ? baseKey : `${baseKey}:${duplicateCount}`,
text,
y: yScale.scale(tick) + labelOffset,
opacity: 1
};
});
};