react-native-chart-kit
Version:
Beautiful React Native charts for dashboards, reports, and data-rich mobile apps.
51 lines (50 loc) • 2.37 kB
JavaScript
import { normalizeLegacyContributionData } from "../../../core/index";
const defaultFormatDate = (date) => date.toISOString().slice(0, 10);
const defaultFormatValue = (value) => String(value);
const pluralize = (count, singular, plural = `${singular}s`) => `${count} ${count === 1 ? singular : plural}`;
const getMinRow = (rows) => rows.reduce((min, row) => typeof row.value === "number" && (!min || row.value < (min.value ?? 0))
? row
: min, undefined);
const getMaxRow = (rows) => rows.reduce((max, row) => typeof row.value === "number" && (!max || row.value > (max.value ?? 0))
? row
: max, undefined);
export const getContributionGraphDataTable = ({ accessor, endDate, formatDate = defaultFormatDate, formatValue = defaultFormatValue, numDays, values }) => {
const normalized = normalizeLegacyContributionData(values, {
endDate,
numDays,
...(accessor !== undefined ? { accessor } : {})
});
return {
rows: normalized.days.map((day) => {
const row = {
date: day.date,
dateLabel: formatDate(day.date, day.index),
formattedValue: day.value === null ? "No value" : formatValue(day.value),
index: day.index,
value: day.value
};
if (day.raw !== undefined) {
row.raw = day.raw;
}
return row;
})
};
};
export const getContributionGraphAccessibilitySummary = (input) => {
const table = getContributionGraphDataTable(input);
if (table.rows.length === 0) {
return "Contribution graph with no days.";
}
const definedRows = table.rows.filter((row) => row.value !== null);
const activeRows = definedRows.filter((row) => (row.value ?? 0) > 0);
if (definedRows.length === 0) {
return `Contribution graph with ${pluralize(table.rows.length, "day")} and no defined values.`;
}
const minRow = getMinRow(definedRows);
const maxRow = getMaxRow(definedRows);
const intro = `Contribution graph with ${pluralize(table.rows.length, "day")}. ${pluralize(activeRows.length, "day")} with activity.`;
if (!minRow || !maxRow) {
return intro;
}
return `${intro} Highest value is ${maxRow.formattedValue} on ${maxRow.dateLabel}. Lowest value is ${minRow.formattedValue} on ${minRow.dateLabel}.`;
};