react-native-chart-kit
Version:
Beautiful React Native charts for dashboards, reports, and data-rich mobile apps.
76 lines (75 loc) • 2.66 kB
JavaScript
const defaultCellHitSlop = 3;
const defaultPointerOffset = { x: 0, y: 0 };
const normalizeHitSlop = (hitSlop) => typeof hitSlop === "number" && Number.isFinite(hitSlop) && hitSlop >= 0
? hitSlop
: defaultCellHitSlop;
const normalizePointerOffset = (pointerOffset) => ({
x: typeof pointerOffset?.x === "number" && Number.isFinite(pointerOffset.x)
? pointerOffset.x
: defaultPointerOffset.x,
y: typeof pointerOffset?.y === "number" && Number.isFinite(pointerOffset.y)
? pointerOffset.y
: defaultPointerOffset.y
});
export const getContributionGraphInteractionConfig = (interaction) => {
if (!interaction) {
return {
mode: "tap",
hitSlop: defaultCellHitSlop,
pointerOffset: defaultPointerOffset,
onSelect: undefined
};
}
if (interaction === "none" ||
interaction === "tap" ||
interaction === "pressAndDrag") {
return {
mode: interaction,
hitSlop: defaultCellHitSlop,
pointerOffset: defaultPointerOffset,
onSelect: undefined
};
}
return {
mode: interaction.mode ?? "tap",
hitSlop: normalizeHitSlop(interaction.hitSlop),
pointerOffset: normalizePointerOffset(interaction.pointerOffset),
onSelect: interaction.onSelect
};
};
export const getContributionGraphCellKey = (cell) => `${cell.index}:${cell.date.toISOString()}`;
export const buildContributionGraphDayPressEvent = (cell) => ({
index: cell.index,
date: cell.date,
value: cell.value,
...(cell.raw !== undefined ? { raw: cell.raw } : {})
});
export const getContributionGraphCellAtPoint = ({ cells, hitSlop, locationX, locationY }) => {
let nearestCell;
let nearestDistance = Number.POSITIVE_INFINITY;
cells.forEach((cell) => {
if (!Number.isFinite(cell.size) || cell.size <= 0) {
return;
}
const minX = cell.x - hitSlop;
const maxX = cell.x + cell.size + hitSlop;
const minY = cell.y - hitSlop;
const maxY = cell.y + cell.size + hitSlop;
if (locationX < minX ||
locationX > maxX ||
locationY < minY ||
locationY > maxY) {
return;
}
const centerX = cell.x + cell.size / 2;
const centerY = cell.y + cell.size / 2;
const dx = locationX - centerX;
const dy = locationY - centerY;
const distance = dx * dx + dy * dy;
if (distance < nearestDistance) {
nearestCell = cell;
nearestDistance = distance;
}
});
return nearestCell;
};