UNPKG

pagamio-frontend-commons-lib

Version:

Pagamio library for Frontend reusable components like the form engine and table container

117 lines (116 loc) 5.28 kB
import { type MRT_ColumnDef } from 'mantine-react-table'; import type { MetricData, TableColumn } from '../types'; import type { DataPoint } from '../visuals/DistributionChart'; interface SeriesItem { name: string; type: 'bar'; stack: 'total'; data: number[]; } interface ChartDataResult { xAxisValues: string[]; seriesData: SeriesItem[]; } /** * A generic utility to build x-axis values and series data for a stacked bar chart in ECharts. * * @param data - An array of objects of any shape (T). * @param xAxisDataValueKey - The string key in each object representing x-axis data (e.g. "channel", "status"). * @param seriesDataNameKey - The string key in each object representing the series name (e.g. "productName"). * @param seriesDataValueKey - The string key in each object representing the numeric value (e.g. "amount"). * * @returns An object containing: * - xAxisValues: A string[] of unique x-axis values (in order). * - seriesData: An array of SeriesItem (with "name", "type", "stack", and the numeric data array). */ export declare function getBarChartData<T extends Record<string, any>>(data: { [key: string]: any; }[], xAxisDataValueKey: string, seriesDataNameKey: string, seriesDataValueKey: string): ChartDataResult; /** * Flattens your data (if it happens to be an array of arrays) * and maps it into an xAxis array of names and a seriesData * array of { name, value } objects. * * @param data The raw data from the backend, e.g. [ [ { amount: 60, productName: 'Electronics' }, ... ] ] * If data is just a single array (e.g. [ { amount: 60, productName: 'Electronics' }, ... ]), * this function will still work (no harm in calling `flat()`). * @param nameKey The key in each object that maps to the category name (used in xAxis and the `name` field). * @param valueKey The key in each object that maps to the numeric value (used in `value`). * * @returns An object containing: * - xAxis: string[] (e.g. ["Electronics", "Fashion", "Home & Garden"]) * - seriesData: Array<{ name: string; value: number }> * (e.g. [{ name: "Electronics", value: 60 }, ...]) */ export declare function getPieChartData<T extends Record<PropertyKey, any>>(data: T[] | T[][], // can be an array of objects OR an array of arrays of objects nameKey: keyof T, // e.g. 'productName' valueKey: keyof T, // e.g. 'amount' otherKey?: keyof T): { xAxis: string[]; seriesData: { name: string; value: number; otherValue: string | number; }[]; }; /** * Flattens your data (if it happens to be an array of arrays) * and maps it into an xAxis array of names and a seriesData * array of { name, value } objects. * * @param data The raw data from the backend, e.g. [ [ { amount: 60, productName: 'Electronics' }, ... ] ] * If data is just a single array (e.g. [ { amount: 60, productName: 'Electronics' }, ... ]), * this function will still work (no harm in calling `flat()`). * @param nameKey The key in each object that maps to the category name (used in xAxis and the `name` field). * @param valueKey The key in each object that maps to the numeric value (used in `value`). * * @returns An object containing: * - seriesData: Array<{ name: string; value: number }> * (e.g. [{ name: "Electronics", value: 60 }, ...]) */ export declare function getSeriesData<T extends Record<PropertyKey, any>>(data: T[] | T[][], // can be an array of objects OR an array of arrays of objects nameKey: keyof T, // e.g. 'productName' valueKey: keyof T): { seriesData: { name: string; value: number; }[]; }; /** * Flattens your data (if it happens to be an array of arrays) * and maps it into a select options array. Handles null/undefined values safely. */ export declare function getFilterOptionsData<T extends Record<PropertyKey, any>>(data: T[] | T[][], filterKey: keyof T): { optionsData: { label: string; value: string; }[]; }; /** * Flattens your data (if it happens to be an array of arrays) * and maps it into a select options array. */ export declare function generateSelectOptions<T extends Record<PropertyKey, any>>(data: T[] | T[][], labelKey: keyof T, valueKey: keyof T): { optionsData: { label: string; value: string; }[]; }; export declare const formatValue: (value: number, format?: string, options?: { currency?: string; locale?: string; }) => string; export declare const transformColumns: <TData extends Record<string, any>>(columns: TableColumn<TData>[]) => MRT_ColumnDef<TData>[]; /** * Formats an array of objects into DataPoint format required for Distribution Chart * @param data Array of objects to format * @param valueKey Key in the object to use as the value * @param nameKey Key in the object to use as the name * @returns Array of DataPoint objects */ export declare function formatToDataPoints<T extends Record<string, any>>(data: T[], valueKey: keyof T, nameKey: keyof T): DataPoint[]; export declare const formatDonutChartData: (data: MetricData[], nameKey: string, valueKey: string) => { legendData: any[]; seriesData: Record<string, any>[]; }; export {};