-
Notifications
You must be signed in to change notification settings - Fork 8.3k
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
[Lens] Allow number formatting within Lens #56253
Merged
wylieconlon
merged 26 commits into
elastic:master
from
wylieconlon:lens/format-selection
Feb 28, 2020
Merged
Changes from 4 commits
Commits
Show all changes
26 commits
Select commit
Hold shift + click to select a range
7ba8962
[Lens] Allow custom number formats on dimensions
wylieconlon 3483bae
Merge remote-tracking branch 'origin/master' into lens/format-selection
wylieconlon d60ed38
Fix merge issues
wylieconlon dfba40b
Merge branch 'master' into lens/format-selection
elasticmachine ab5898f
Merge remote-tracking branch 'origin/master' into lens/format-selection
wylieconlon 0edfa93
Text and decimal changes from review
wylieconlon 1213880
Merge remote-tracking branch 'origin/master' into lens/format-selection
wylieconlon cdd7654
Persist number format across operations
wylieconlon c3ec868
Merge branch 'master' into lens/format-selection
elasticmachine 8240214
Merge branch 'master' into lens/format-selection
elasticmachine b01b81c
Merge remote-tracking branch 'origin/master' into lens/format-selection
wylieconlon 0b41134
Respond to review comments
wylieconlon c1d4f3a
Merge remote-tracking branch 'origin/master' into lens/format-selection
wylieconlon 0b3b0c4
Change label
wylieconlon 59d7d66
Merge remote-tracking branch 'origin/master' into lens/format-selection
wylieconlon 6a394cb
Add persistence
wylieconlon 7b66f45
Merge remote-tracking branch 'origin/master' into lens/format-selection
wylieconlon 6a2f438
Fix import
wylieconlon 0a4e8cb
Merge remote-tracking branch 'origin/master' into lens/format-selection
wylieconlon 575fa9d
2 decimals
wylieconlon fe4c842
Merge remote-tracking branch 'origin/master' into lens/format-selection
wylieconlon e002e9d
Persist number formatting on drop too
wylieconlon 17a6992
Merge branch 'master' into lens/format-selection
elasticmachine 5c1ea81
Merge branch 'master' into lens/format-selection
elasticmachine 40f5a2f
Merge branch 'master' into lens/format-selection
elasticmachine 655700d
Merge branch 'master' into lens/format-selection
elasticmachine File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
95 changes: 95 additions & 0 deletions
95
x-pack/legacy/plugins/lens/public/editor_frame_plugin/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,95 @@ | ||
/* | ||
* 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 { i18n } from '@kbn/i18n'; | ||
import { ExpressionFunction, KibanaDatatable } from 'src/plugins/expressions/public'; | ||
|
||
interface FormatColumn { | ||
format: string; | ||
columnId: string; | ||
maxDecimals?: number; | ||
} | ||
|
||
const supportedFormats: Record<string, { decimalsToPattern: (decimals?: number) => string }> = { | ||
number: { | ||
decimalsToPattern: (decimals = 3) => { | ||
if (decimals === 0) { | ||
return `0,0`; | ||
} | ||
return `0,0.[${'0'.repeat(decimals)}]`; | ||
}, | ||
}, | ||
percent: { | ||
decimalsToPattern: (decimals = 3) => { | ||
if (decimals === 0) { | ||
return `0,0%`; | ||
} | ||
return `0,0.[${'0'.repeat(decimals)}]%`; | ||
}, | ||
}, | ||
bytes: { | ||
decimalsToPattern: (decimals = 3) => { | ||
if (decimals === 0) { | ||
return `0,0b`; | ||
} | ||
return `0,0.[${'0'.repeat(decimals)}]b`; | ||
}, | ||
}, | ||
}; | ||
|
||
export const formatColumn: ExpressionFunction< | ||
'lens_format_column', | ||
KibanaDatatable, | ||
FormatColumn, | ||
KibanaDatatable | ||
> = { | ||
name: 'lens_format_column', | ||
type: 'kibana_datatable', | ||
help: i18n.translate('xpack.lens.functions.mergeTables.help', { | ||
defaultMessage: 'A helper to merge any number of kibana tables into a single table', | ||
}), | ||
args: { | ||
format: { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
types: ['string'], | ||
help: '', | ||
}, | ||
columnId: { | ||
types: ['string'], | ||
help: '', | ||
}, | ||
maxDecimals: { | ||
types: ['number'], | ||
help: '', | ||
}, | ||
}, | ||
context: { | ||
types: ['kibana_datatable'], | ||
}, | ||
fn(ctx, { format, columnId, maxDecimals }: FormatColumn) { | ||
return { | ||
...ctx, | ||
columns: ctx.columns.map(col => { | ||
if (col.id === columnId) { | ||
if (supportedFormats[format]) { | ||
return { | ||
...col, | ||
formatHint: { | ||
id: format, | ||
params: { pattern: supportedFormats[format].decimalsToPattern(maxDecimals) }, | ||
}, | ||
}; | ||
} 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.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
Title is already present on formatters, but the type was not set
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.
Is there a reason this needs to be optional? It seems from the
FieldFormat
class, that thetitle
is the same asid
andfieldType
always present, and could just be a mandatorystring
.