Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import type { ColDef, GridOptions, ValueFormatterParams } from 'ag-grid-community';
import type { ColDef, GridOptions, ValueFormatterLiteParams } from 'ag-grid-community';
import {
ClientSideRowModelModule,
ModuleRegistry,
Expand Down Expand Up @@ -28,7 +28,7 @@ type SalesRow = {
cost: number;
};

const formatter = (params: ValueFormatterParams<SalesRow, number>, formattedString: string): string => {
const formatter = (params: ValueFormatterLiteParams<SalesRow, number>, format: (value: number) => string): string => {
const { value } = params;
if (value == null) {
return '';
Expand All @@ -38,42 +38,40 @@ const formatter = (params: ValueFormatterParams<SalesRow, number>, formattedStri
return String(value);
}

return formattedString;
return format(value);
};

const currencyFormatter = (params: ValueFormatterParams<SalesRow, number>) =>
formatter(params, `$${(params.value ?? '').toLocaleString()}`);
const currencyFormatter = (params: ValueFormatterLiteParams<SalesRow, number>) =>
formatter(params, (value) => `$${value.toLocaleString()}`);

const percentageFormatter = (params: ValueFormatterParams<SalesRow, number>) =>
formatter(params, `${Math.round((params.value ?? 0) * 100)}%`);
const percentageFormatter = (params: ValueFormatterLiteParams<SalesRow, number>) =>
formatter(params, (value) => `${Math.round(value * 100)}%`);

const columnDefs: ColDef<SalesRow>[] = [
{ field: 'account', flex: 1.4 },
{
field: 'revenue',
valueFormatter: currencyFormatter,
cellDataType: 'currency',
},
{
field: 'cost',
valueFormatter: currencyFormatter,
cellDataType: 'currency',
},
{
colId: 'profit',
headerName: 'Profit',
calculatedExpression: '[revenue] - [cost]',
cellDataType: 'number',
cellDataType: 'currency',
sortable: true,
filter: 'agNumberColumnFilter',
valueFormatter: currencyFormatter,
},
{
colId: 'margin',
headerName: 'Margin',
calculatedExpression: '[profit] / [revenue]',
cellDataType: 'number',
cellDataType: 'percentage',
sortable: true,
filter: 'agNumberColumnFilter',
valueFormatter: percentageFormatter,
},
{
colId: 'status',
Expand Down Expand Up @@ -111,7 +109,21 @@ const rowData: SalesRow[] = [
const gridOptions: GridOptions<SalesRow> = {
columnDefs,
rowData,
calculatedColumns: true,
dataTypeDefinitions: {
currency: {
baseDataType: 'number',
extendsDataType: 'number',
valueFormatter: currencyFormatter,
},
percentage: {
baseDataType: 'number',
extendsDataType: 'number',
valueFormatter: percentageFormatter,
},
},
calculatedColumns: {
dataTypes: ['currency', 'percentage', 'number', 'text', 'boolean'],
},
defaultColDef: {
flex: 1,
minWidth: 130,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import type { ColDef, GridApi, GridOptions, ValueFormatterParams } from 'ag-grid-community';
import type { ColDef, GridApi, GridOptions, ValueFormatterLiteParams } from 'ag-grid-community';
import {
ClientSideRowModelModule,
ColumnApiModule,
Expand Down Expand Up @@ -27,30 +27,28 @@ type SalesRow = {
cost: number;
};

const currencyFormatter = (params: ValueFormatterParams<SalesRow, number>) =>
const currencyFormatter = (params: ValueFormatterLiteParams<SalesRow, number>) =>
params.value == null ? '' : `$${params.value.toLocaleString()}`;

const percentFormatter = (params: ValueFormatterParams<SalesRow, number>) =>
const percentFormatter = (params: ValueFormatterLiteParams<SalesRow, number>) =>
params.value == null ? '' : `${(params.value * 100).toFixed(1)}%`;

const marginColumn: ColDef<SalesRow> = {
colId: 'profitMargin',
headerName: 'Profit Margin',
calculatedExpression: '([revenue] - [cost]) / [revenue]',
cellDataType: 'number',
valueFormatter: percentFormatter,
cellDataType: 'percentage',
};

const columnDefs: ColDef<SalesRow>[] = [
{ field: 'product', flex: 1 },
{ field: 'revenue', valueFormatter: currencyFormatter },
{ field: 'cost', valueFormatter: currencyFormatter },
{ field: 'revenue', cellDataType: 'currency' },
{ field: 'cost', cellDataType: 'currency' },
{
colId: 'profit',
headerName: 'Profit',
calculatedExpression: '[revenue] - [cost]',
cellDataType: 'number',
valueFormatter: currencyFormatter,
cellDataType: 'currency',
},
];

Expand Down Expand Up @@ -82,6 +80,18 @@ let gridApi: GridApi<SalesRow>;
const gridOptions: GridOptions<SalesRow> = {
columnDefs,
rowData,
dataTypeDefinitions: {
currency: {
baseDataType: 'number',
extendsDataType: 'number',
valueFormatter: currencyFormatter,
},
percentage: {
baseDataType: 'number',
extendsDataType: 'number',
valueFormatter: percentFormatter,
},
},
calculatedColumns: true,
defaultColDef: {
flex: 1,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import type { ColDef, GridOptions, ValueFormatterParams } from 'ag-grid-community';
import type { ColDef, GridOptions, ValueFormatterLiteParams } from 'ag-grid-community';
import {
ClientSideRowModelModule,
ModuleRegistry,
Expand Down Expand Up @@ -28,7 +28,7 @@ type SalesRow = {
cost: number;
};

const formatter = (params: ValueFormatterParams<SalesRow, number>, formattedString: string): string => {
const currencyFormatter = (params: ValueFormatterLiteParams<SalesRow, number>): string => {
const { value } = params;
if (value == null) {
return '';
Expand All @@ -38,32 +38,28 @@ const formatter = (params: ValueFormatterParams<SalesRow, number>, formattedStri
return String(value);
}

return formattedString;
return `$${value.toLocaleString()}`;
};

const currencyFormatter = (params: ValueFormatterParams<SalesRow, number>) =>
formatter(params, `$${(params.value ?? '').toLocaleString()}`);

const columnDefs: ColDef<SalesRow>[] = [
{ field: 'product', flex: 1 },
{
field: 'revenue',
editable: true,
valueFormatter: currencyFormatter,
cellDataType: 'currency',
},
{
field: 'cost',
editable: true,
valueFormatter: currencyFormatter,
cellDataType: 'currency',
},
{
colId: 'profit',
headerName: 'Profit',
calculatedExpression: '[revenue] - [cost]',
cellDataType: 'number',
cellDataType: 'currency',
sortable: true,
filter: 'agNumberColumnFilter',
valueFormatter: currencyFormatter,
},
];

Expand Down Expand Up @@ -93,8 +89,16 @@ const rowData: SalesRow[] = [
const gridOptions: GridOptions<SalesRow> = {
columnDefs,
rowData,
dataTypeDefinitions: {
currency: {
baseDataType: 'number',
extendsDataType: 'number',
valueFormatter: currencyFormatter,
},
},
calculatedColumns: {
applyMode: 'deferred',
dataTypes: ['currency', 'number', 'text', 'boolean'],
},
defaultColDef: {
flex: 1,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import type { ColDef, ColGroupDef, GridOptions, ValueFormatterParams } from 'ag-grid-community';
import type { ColDef, ColGroupDef, GridOptions, ValueFormatterLiteParams } from 'ag-grid-community';
import {
ClientSideRowModelModule,
ModuleRegistry,
Expand Down Expand Up @@ -34,7 +34,10 @@ type QuarterlyRevenueRow = {

type QuarterField = Exclude<keyof QuarterlyRevenueRow, 'product'>;

const formatter = (params: ValueFormatterParams<QuarterlyRevenueRow, number>, formattedString: string): string => {
const formatter = (
params: ValueFormatterLiteParams<QuarterlyRevenueRow, number>,
format: (value: number) => string
): string => {
const { value } = params;
if (value == null) {
return '';
Expand All @@ -44,22 +47,21 @@ const formatter = (params: ValueFormatterParams<QuarterlyRevenueRow, number>, fo
return String(value);
}

return formattedString;
return format(value);
};

const currencyFormatter = (params: ValueFormatterParams<QuarterlyRevenueRow, number>) =>
formatter(params, `$${(params.value ?? '').toLocaleString()}`);
const currencyFormatter = (params: ValueFormatterLiteParams<QuarterlyRevenueRow, number>) =>
formatter(params, (value) => `$${value.toLocaleString()}`);

const percentageFormatter = (params: ValueFormatterParams<QuarterlyRevenueRow, number>) =>
formatter(params, `${(params.value ?? 0).toLocaleString(undefined, { maximumFractionDigits: 1 })}%`);
const percentageFormatter = (params: ValueFormatterLiteParams<QuarterlyRevenueRow, number>) =>
formatter(params, (value) => `${(value * 100).toLocaleString(undefined, { maximumFractionDigits: 1 })}%`);

const quarterColumn = (field: QuarterField): ColDef<QuarterlyRevenueRow, number> => ({
field,
colId: field,
headerName: field.slice(0, 2).toUpperCase(),
columnGroupShow: 'open',
cellDataType: 'number',
valueFormatter: currencyFormatter,
cellDataType: 'currency',
});

const columnDefs: (ColDef<QuarterlyRevenueRow> | ColGroupDef<QuarterlyRevenueRow>)[] = [
Expand All @@ -77,8 +79,7 @@ const columnDefs: (ColDef<QuarterlyRevenueRow> | ColGroupDef<QuarterlyRevenueRow
headerName: 'Total',
columnGroupShow: 'closed',
calculatedExpression: '[q1_2025] + [q2_2025] + [q3_2025] + [q4_2025]',
cellDataType: 'number',
valueFormatter: currencyFormatter,
cellDataType: 'currency',
},
],
},
Expand All @@ -95,8 +96,7 @@ const columnDefs: (ColDef<QuarterlyRevenueRow> | ColGroupDef<QuarterlyRevenueRow
headerName: 'Total',
columnGroupShow: 'closed',
calculatedExpression: '[q1_2026] + [q2_2026] + [q3_2026] + [q4_2026]',
cellDataType: 'number',
valueFormatter: currencyFormatter,
cellDataType: 'currency',
},
],
},
Expand All @@ -106,21 +106,19 @@ const columnDefs: (ColDef<QuarterlyRevenueRow> | ColGroupDef<QuarterlyRevenueRow
{
colId: 'q4Change',
headerName: 'Q4 Change',
calculatedExpression: 'ROUND((([q4_2026] - [q4_2025]) / [q4_2025]) * 100, 1)',
cellDataType: 'number',
calculatedExpression: '([q4_2026] - [q4_2025]) / [q4_2025]',
cellDataType: 'percentage',
sortable: true,
filter: 'agNumberColumnFilter',
valueFormatter: percentageFormatter,
},
{
colId: 'yearChange',
headerName: 'Year Change',
calculatedExpression:
'([q1_2026] + [q2_2026] + [q3_2026] + [q4_2026]) - ([q1_2025] + [q2_2025] + [q3_2025] + [q4_2025])',
cellDataType: 'number',
cellDataType: 'currency',
sortable: true,
filter: 'agNumberColumnFilter',
valueFormatter: currencyFormatter,
},
],
},
Expand Down Expand Up @@ -352,7 +350,21 @@ const rowData: QuarterlyRevenueRow[] = [
const gridOptions: GridOptions<QuarterlyRevenueRow> = {
columnDefs,
rowData,
calculatedColumns: true,
dataTypeDefinitions: {
currency: {
baseDataType: 'number',
extendsDataType: 'number',
valueFormatter: currencyFormatter,
},
percentage: {
baseDataType: 'number',
extendsDataType: 'number',
valueFormatter: percentageFormatter,
},
},
calculatedColumns: {
dataTypes: ['currency', 'percentage', 'number', 'text', 'boolean'],
},
defaultColDef: {
minWidth: 120,
flex: 1,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import type { ColDef, GridOptions, ValueFormatterParams } from 'ag-grid-community';
import type { ColDef, GridOptions, ValueFormatterLiteParams } from 'ag-grid-community';
import {
ClientSideRowModelModule,
ModuleRegistry,
Expand Down Expand Up @@ -26,7 +26,7 @@ type SalesRow = {
cost: number;
};

const currencyFormatter = (params: ValueFormatterParams<SalesRow, number>): string => {
const currencyFormatter = (params: ValueFormatterLiteParams<SalesRow, number>): string => {
const { value } = params;
if (value == null) {
return '';
Expand Down
Loading
Loading