react-native-chart-kit
Version:
Beautiful React Native charts for dashboards, reports, and data-rich mobile apps.
47 lines (46 loc) • 1.7 kB
JavaScript
const tooltipLineHeight = 18;
const clamp = (value, min, max) => {
if (max < min) {
return min;
}
return Math.min(Math.max(value, min), max);
};
export const getBarChartTooltipModel = ({ bar, boxes, config, pointer }) => {
if (!bar || !config.visible) {
return undefined;
}
const hasLabel = bar.xLabel.trim().length > 0;
const contentHeight = hasLabel
? config.labelFontSize + tooltipLineHeight
: config.fontSize;
const height = config.padding * 2 + contentHeight + 2;
const pointerAnchor = config.anchor === "pointer" && pointer?.key === bar.key
? pointer
: undefined;
const anchorX = pointerAnchor?.x ?? bar.x + bar.width / 2;
const anchorY = pointerAnchor?.y ?? bar.y;
const belowAnchorY = pointerAnchor?.y ?? bar.y + bar.height;
const minX = boxes.plot.x + config.edgePadding;
const maxX = boxes.plot.x + boxes.plot.width - config.width - config.edgePadding;
const x = clamp(anchorX - config.width / 2, minX, maxX);
const minY = boxes.outer.y + config.edgePadding;
const maxY = boxes.outer.y + boxes.outer.height - height - config.edgePadding;
const aboveY = anchorY - height - config.offset;
const belowY = belowAnchorY + config.offset;
const y = config.placement === "top"
? minY
: config.placement === "above"
? clamp(aboveY, minY, maxY)
: config.placement === "below"
? clamp(belowY, minY, maxY)
: aboveY >= minY
? aboveY
: clamp(belowY, minY, maxY);
return {
bar,
height,
width: config.width,
x,
y
};
};