react-native-chart-kit
Version:
Beautiful React Native charts for dashboards, reports, and data-rich mobile apps.
63 lines (62 loc) • 2.08 kB
JavaScript
export const getBarChartBarKey = (selectedBar) => selectedBar ? `${selectedBar.seriesKey}-${selectedBar.dataIndex}` : undefined;
export const getBarChartInteractionConfig = (interaction) => {
if (!interaction) {
return {
mode: "none",
deselectOnOutsidePress: false
};
}
if (typeof interaction === "string") {
return {
mode: interaction,
deselectOnOutsidePress: interaction !== "none"
};
}
return {
mode: interaction.mode ?? "tap",
deselectOnOutsidePress: interaction.deselectOnOutsidePress ?? true,
...(interaction.onSelect ? { onSelect: interaction.onSelect } : {}),
...(interaction.onDeselect ? { onDeselect: interaction.onDeselect } : {})
};
};
export const isBarChartInteractionEnabled = (config) => config.mode !== "none";
export const getBarChartBarAtPoint = ({ bars, hitSlop = 6, locationX, locationY }) => {
const candidates = bars.filter((bar) => {
const top = bar.y;
const bottom = bar.y + bar.height;
return (locationX >= bar.x - hitSlop &&
locationX <= bar.x + bar.width + hitSlop &&
locationY >= top - hitSlop &&
locationY <= bottom + hitSlop);
});
if (candidates.length <= 1) {
return candidates[0];
}
return candidates
.map((bar) => ({
bar,
distance: Math.abs(locationX - (bar.x + bar.width / 2)) +
Math.abs(locationY - (bar.y + bar.height / 2))
}))
.sort((a, b) => a.distance - b.distance)[0]?.bar;
};
export const buildBarChartSelectEvent = (bar) => {
if (!bar) {
return undefined;
}
return {
color: bar.color,
dataIndex: bar.dataIndex,
formattedValue: bar.formattedValue,
position: {
x: bar.x + bar.width / 2,
y: bar.y
},
raw: bar.raw,
seriesKey: bar.seriesKey,
seriesLabel: bar.seriesLabel,
value: bar.value,
x: bar.xValue,
xLabel: bar.xLabel
};
};