-
Notifications
You must be signed in to change notification settings - Fork 8.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[Lens] Allow number formatting within Lens (#56253)
* [Lens] Allow custom number formats on dimensions * Fix merge issues * Text and decimal changes from review * Persist number format across operations * Respond to review comments * Change label * Add persistence * Fix import * 2 decimals * Persist number formatting on drop too Co-authored-by: Elastic Machine <elasticmachine@users.noreply.github.com>
- Loading branch information
1 parent
8620f43
commit 6c6bc1f
Showing
18 changed files
with
600 additions
and
48 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
92 changes: 92 additions & 0 deletions
92
x-pack/legacy/plugins/lens/public/editor_frame_service/format_column.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,92 @@ | ||
/* | ||
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
* or more contributor license agreements. Licensed under the Elastic License; | ||
* you may not use this file except in compliance with the Elastic License. | ||
*/ | ||
|
||
import { ExpressionFunctionDefinition, KibanaDatatable } from 'src/plugins/expressions/public'; | ||
|
||
interface FormatColumn { | ||
format: string; | ||
columnId: string; | ||
decimals?: number; | ||
} | ||
|
||
const supportedFormats: Record<string, { decimalsToPattern: (decimals?: number) => string }> = { | ||
number: { | ||
decimalsToPattern: (decimals = 2) => { | ||
if (decimals === 0) { | ||
return `0,0`; | ||
} | ||
return `0,0.${'0'.repeat(decimals)}`; | ||
}, | ||
}, | ||
percent: { | ||
decimalsToPattern: (decimals = 2) => { | ||
if (decimals === 0) { | ||
return `0,0%`; | ||
} | ||
return `0,0.${'0'.repeat(decimals)}%`; | ||
}, | ||
}, | ||
bytes: { | ||
decimalsToPattern: (decimals = 2) => { | ||
if (decimals === 0) { | ||
return `0,0b`; | ||
} | ||
return `0,0.${'0'.repeat(decimals)}b`; | ||
}, | ||
}, | ||
}; | ||
|
||
export const formatColumn: ExpressionFunctionDefinition< | ||
'lens_format_column', | ||
KibanaDatatable, | ||
FormatColumn, | ||
KibanaDatatable | ||
> = { | ||
name: 'lens_format_column', | ||
type: 'kibana_datatable', | ||
help: '', | ||
args: { | ||
format: { | ||
types: ['string'], | ||
help: '', | ||
required: true, | ||
}, | ||
columnId: { | ||
types: ['string'], | ||
help: '', | ||
required: true, | ||
}, | ||
decimals: { | ||
types: ['number'], | ||
help: '', | ||
}, | ||
}, | ||
inputTypes: ['kibana_datatable'], | ||
fn(input, { format, columnId, decimals }: FormatColumn) { | ||
return { | ||
...input, | ||
columns: input.columns.map(col => { | ||
if (col.id === columnId) { | ||
if (supportedFormats[format]) { | ||
return { | ||
...col, | ||
formatHint: { | ||
id: format, | ||
params: { pattern: supportedFormats[format].decimalsToPattern(decimals) }, | ||
}, | ||
}; | ||
} else { | ||
return { | ||
...col, | ||
formatHint: { id: format, params: {} }, | ||
}; | ||
} | ||
} | ||
return col; | ||
}), | ||
}; | ||
}, | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.