-
Notifications
You must be signed in to change notification settings - Fork 2
feat: PivotTable component (cross-tabulation / pivot table) #592
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|
| @@ -0,0 +1,240 @@ | ||||||||||||
| /** | ||||||||||||
| * ObjectUI | ||||||||||||
| * Copyright (c) 2024-present ObjectStack Inc. | ||||||||||||
| * | ||||||||||||
| * This source code is licensed under the MIT license found in the | ||||||||||||
| * LICENSE file in the root directory of this source tree. | ||||||||||||
| */ | ||||||||||||
|
|
||||||||||||
| import React, { useMemo } from 'react'; | ||||||||||||
| import type { PivotTableSchema, PivotAggregation } from '@object-ui/types'; | ||||||||||||
| import { cn } from '@object-ui/components'; | ||||||||||||
|
|
||||||||||||
| export interface PivotTableProps { | ||||||||||||
| schema: PivotTableSchema; | ||||||||||||
| className?: string; | ||||||||||||
| } | ||||||||||||
|
|
||||||||||||
| /** Apply a simple format string to a number. Supports prefix/suffix like "$,.2f". */ | ||||||||||||
| function formatValue(value: number, format?: string): string { | ||||||||||||
| if (!format) return String(value); | ||||||||||||
|
|
||||||||||||
| let prefix = ''; | ||||||||||||
| let suffix = ''; | ||||||||||||
| let useGrouping = false; | ||||||||||||
| let decimals: number | undefined; | ||||||||||||
|
|
||||||||||||
| let fmt = format; | ||||||||||||
|
|
||||||||||||
| // Extract leading non-format characters as prefix (e.g. "$") | ||||||||||||
| const prefixMatch = fmt.match(/^([^0-9.,#]*)/); | ||||||||||||
| if (prefixMatch && prefixMatch[1]) { | ||||||||||||
| // comma inside the prefix-ish area means grouping, not a literal prefix | ||||||||||||
| const raw = prefixMatch[1]; | ||||||||||||
| prefix = raw.replace(',', ''); | ||||||||||||
| if (raw.includes(',')) useGrouping = true; | ||||||||||||
| fmt = fmt.slice(prefixMatch[1].length); | ||||||||||||
| } | ||||||||||||
|
|
||||||||||||
| // Grouping indicator anywhere remaining | ||||||||||||
| if (fmt.includes(',')) { | ||||||||||||
| useGrouping = true; | ||||||||||||
| fmt = fmt.replace(/,/g, ''); | ||||||||||||
| } | ||||||||||||
|
|
||||||||||||
| // Decimal specifier e.g. ".2f" | ||||||||||||
| const decMatch = fmt.match(/\.(\d+)f?/); | ||||||||||||
| if (decMatch) { | ||||||||||||
| decimals = Number(decMatch[1]); | ||||||||||||
| fmt = fmt.slice(decMatch[0].length); | ||||||||||||
| } | ||||||||||||
|
|
||||||||||||
| // Remaining characters become suffix | ||||||||||||
| suffix = fmt.replace(/[0-9#.f]/g, ''); | ||||||||||||
|
|
||||||||||||
| const formatted = decimals !== undefined ? value.toFixed(decimals) : String(value); | ||||||||||||
|
|
||||||||||||
| if (useGrouping) { | ||||||||||||
| const [intPart, decPart] = formatted.split('.'); | ||||||||||||
| const grouped = intPart.replace(/\B(?=(\d{3})+(?!\d))/g, ','); | ||||||||||||
| return prefix + (decPart !== undefined ? `${grouped}.${decPart}` : grouped) + suffix; | ||||||||||||
| } | ||||||||||||
|
|
||||||||||||
| return prefix + formatted + suffix; | ||||||||||||
| } | ||||||||||||
|
|
||||||||||||
| /** Aggregate an array of numbers with the given function. */ | ||||||||||||
| function aggregate(values: number[], fn: PivotAggregation): number { | ||||||||||||
| if (values.length === 0) return 0; | ||||||||||||
| switch (fn) { | ||||||||||||
| case 'sum': | ||||||||||||
| return values.reduce((a, b) => a + b, 0); | ||||||||||||
| case 'count': | ||||||||||||
| return values.length; | ||||||||||||
| case 'avg': | ||||||||||||
| return values.reduce((a, b) => a + b, 0) / values.length; | ||||||||||||
| case 'min': | ||||||||||||
| return Math.min(...values); | ||||||||||||
| case 'max': | ||||||||||||
| return Math.max(...values); | ||||||||||||
| default: | ||||||||||||
| return values.reduce((a, b) => a + b, 0); | ||||||||||||
| } | ||||||||||||
| } | ||||||||||||
|
|
||||||||||||
| /** | ||||||||||||
| * PivotTable – Cross-tabulation / Pivot Table component. | ||||||||||||
| * | ||||||||||||
| * Renders a matrix where rows correspond to `rowField`, columns to | ||||||||||||
| * `columnField`, and cells show the aggregated `valueField`. | ||||||||||||
| */ | ||||||||||||
| export const PivotTable: React.FC<PivotTableProps> = ({ schema, className }) => { | ||||||||||||
| const { | ||||||||||||
| title, | ||||||||||||
| rowField, | ||||||||||||
| columnField, | ||||||||||||
| valueField, | ||||||||||||
| aggregation = 'sum', | ||||||||||||
| data = [], | ||||||||||||
| showRowTotals = false, | ||||||||||||
| showColumnTotals = false, | ||||||||||||
| format, | ||||||||||||
| columnColors, | ||||||||||||
| } = schema; | ||||||||||||
|
|
||||||||||||
| const { rowKeys, colKeys, matrix, rowTotals, colTotals, grandTotal } = useMemo(() => { | ||||||||||||
| // Collect unique row/column values preserving insertion order | ||||||||||||
| const rowSet = new Map<string, true>(); | ||||||||||||
| const colSet = new Map<string, true>(); | ||||||||||||
| // Bucket raw values: bucket[row][col] = number[] | ||||||||||||
| const bucket: Record<string, Record<string, number[]>> = {}; | ||||||||||||
|
|
||||||||||||
| for (const item of data) { | ||||||||||||
| const r = String(item[rowField] ?? ''); | ||||||||||||
| const c = String(item[columnField] ?? ''); | ||||||||||||
| const v = Number(item[valueField]) || 0; | ||||||||||||
|
|
||||||||||||
| rowSet.set(r, true); | ||||||||||||
| colSet.set(c, true); | ||||||||||||
|
|
||||||||||||
| if (!bucket[r]) bucket[r] = {}; | ||||||||||||
| if (!bucket[r][c]) bucket[r][c] = []; | ||||||||||||
| bucket[r][c].push(v); | ||||||||||||
| } | ||||||||||||
|
|
||||||||||||
| const rKeys = Array.from(rowSet.keys()); | ||||||||||||
| const cKeys = Array.from(colSet.keys()); | ||||||||||||
|
|
||||||||||||
| // Build aggregated matrix | ||||||||||||
| const mat: Record<string, Record<string, number>> = {}; | ||||||||||||
| const rTotals: Record<string, number> = {}; | ||||||||||||
| const cTotals: Record<string, number> = {}; | ||||||||||||
|
|
||||||||||||
| for (const r of rKeys) { | ||||||||||||
| mat[r] = {}; | ||||||||||||
| const rowValues: number[] = []; | ||||||||||||
| for (const c of cKeys) { | ||||||||||||
| const cellValues = bucket[r]?.[c] ?? []; | ||||||||||||
| const cellAgg = aggregate(cellValues, aggregation); | ||||||||||||
| mat[r][c] = cellAgg; | ||||||||||||
| rowValues.push(...cellValues); | ||||||||||||
|
|
||||||||||||
| // Accumulate column bucket values for column totals | ||||||||||||
| if (!cTotals[c] && cTotals[c] !== 0) { | ||||||||||||
| // Will compute after | ||||||||||||
| } | ||||||||||||
|
Comment on lines
+141
to
+145
|
||||||||||||
| // Accumulate column bucket values for column totals | |
| if (!cTotals[c] && cTotals[c] !== 0) { | |
| // Will compute after | |
| } |
Copilot
AI
Feb 18, 2026
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The pivot computation has O(n*m) complexity where n is the number of data rows and m is the number of unique row/column combinations. While this is acceptable for typical dashboard use cases, consider documenting performance characteristics or adding a warning if the dataset is very large (e.g., > 10,000 rows). The computation is already memoized which helps, but users should be aware of the computational cost for large datasets.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The formatValue function's prefix extraction logic at line 34 removes commas from the prefix (e.g., "$," becomes "$"), which is correct. However, this logic is fragile because it treats any comma in the prefix area as a grouping indicator. For example, if a user wants a literal prefix containing a comma (unlikely but possible), this would fail. Consider documenting this behavior in a comment or adding a more explicit parsing strategy.