react-native-chart-kit
Version:
Beautiful React Native charts for dashboards, reports, and data-rich mobile apps.
78 lines (77 loc) • 2.3 kB
JavaScript
import { buildLayoutDebugModel } from "../../../core/index";
const getXLabelRect = (label) => {
const left = label.textAnchor === "start"
? label.x
: label.textAnchor === "end"
? label.x - label.size.width
: label.x - label.size.width / 2;
return {
id: `x-label-${label.index}`,
text: label.text,
x: left,
y: label.y - label.size.height,
width: label.size.width,
height: label.size.height
};
};
const getYLabelRect = ({ label, measureYLabel, plotX }) => {
const size = measureYLabel(label.text);
return {
id: `y-label-${label.key}`,
text: label.text,
x: plotX - 8 - size.width,
y: label.y - size.height,
width: size.width,
height: size.height
};
};
const defaultMeasureYLabel = (fontSize) => {
return (text) => ({
height: fontSize + 2,
width: text.length * fontSize * 0.58
});
};
const getBaseDebugLayout = ({ boxes, labels, tooltip }) => {
const options = {
boxes,
labels
};
return buildLayoutDebugModel(tooltip
? {
...options,
tooltip: {
placement: "top",
rect: {
x: tooltip.x,
y: tooltip.y,
width: tooltip.width,
height: tooltip.height
}
}
}
: options);
};
export const buildLineChartDebugLayoutModel = ({ boxes, legendItems, measureYLabel, tooltip, xLabels, yAxisLabelFontSize, yLabels }) => {
const resolvedMeasureYLabel = measureYLabel ?? defaultMeasureYLabel(yAxisLabelFontSize);
const labels = [
...xLabels.map(getXLabelRect),
...yLabels.map((label) => getYLabelRect({
label,
measureYLabel: resolvedMeasureYLabel,
plotX: boxes.plot.x
}))
];
const base = getBaseDebugLayout({ boxes, labels, tooltip });
const legendRects = legendItems?.map((item) => ({
id: `legend-${item.key}`,
kind: "legend",
text: item.label,
x: item.x,
y: item.y,
width: item.width,
height: item.height
})) ?? [];
return {
rects: [...base.rects, ...legendRects]
};
};