Skip to content
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
merged 26 commits into from
Feb 28, 2020
Merged
Show file tree
Hide file tree
Changes from all 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 Jan 28, 2020
3483bae
Merge remote-tracking branch 'origin/master' into lens/format-selection
wylieconlon Jan 28, 2020
d60ed38
Fix merge issues
wylieconlon Jan 28, 2020
dfba40b
Merge branch 'master' into lens/format-selection
elasticmachine Jan 29, 2020
ab5898f
Merge remote-tracking branch 'origin/master' into lens/format-selection
wylieconlon Jan 31, 2020
0edfa93
Text and decimal changes from review
wylieconlon Jan 31, 2020
1213880
Merge remote-tracking branch 'origin/master' into lens/format-selection
wylieconlon Feb 5, 2020
cdd7654
Persist number format across operations
wylieconlon Feb 5, 2020
c3ec868
Merge branch 'master' into lens/format-selection
elasticmachine Feb 6, 2020
8240214
Merge branch 'master' into lens/format-selection
elasticmachine Feb 10, 2020
b01b81c
Merge remote-tracking branch 'origin/master' into lens/format-selection
wylieconlon Feb 10, 2020
0b41134
Respond to review comments
wylieconlon Feb 11, 2020
c1d4f3a
Merge remote-tracking branch 'origin/master' into lens/format-selection
wylieconlon Feb 11, 2020
0b3b0c4
Change label
wylieconlon Feb 11, 2020
59d7d66
Merge remote-tracking branch 'origin/master' into lens/format-selection
wylieconlon Feb 12, 2020
6a394cb
Add persistence
wylieconlon Feb 12, 2020
7b66f45
Merge remote-tracking branch 'origin/master' into lens/format-selection
wylieconlon Feb 14, 2020
6a2f438
Fix import
wylieconlon Feb 14, 2020
0a4e8cb
Merge remote-tracking branch 'origin/master' into lens/format-selection
wylieconlon Feb 20, 2020
575fa9d
2 decimals
wylieconlon Feb 20, 2020
fe4c842
Merge remote-tracking branch 'origin/master' into lens/format-selection
wylieconlon Feb 21, 2020
e002e9d
Persist number formatting on drop too
wylieconlon Feb 21, 2020
17a6992
Merge branch 'master' into lens/format-selection
elasticmachine Feb 24, 2020
5c1ea81
Merge branch 'master' into lens/format-selection
elasticmachine Feb 26, 2020
40f5a2f
Merge branch 'master' into lens/format-selection
elasticmachine Feb 27, 2020
655700d
Merge branch 'master' into lens/format-selection
elasticmachine Feb 28, 2020
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
1 change: 1 addition & 0 deletions src/plugins/data/common/field_formats/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,7 @@ export type IFieldFormatType = (new (
getConfig?: FieldFormatsGetConfigFn
) => FieldFormat) & {
id: FieldFormatId;
title: string;
fieldType: string | string[];
};

Expand Down
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;
}),
};
},
};
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ import {
} from '../types';
import { EditorFrame } from './editor_frame';
import { mergeTables } from './merge_tables';
import { formatColumn } from './format_column';
import { EmbeddableFactory } from './embeddable/embeddable_factory';
import { getActiveDatasourceIdFromDoc } from './editor_frame/state_management';

Expand Down Expand Up @@ -64,6 +65,7 @@ export class EditorFrameService {

public setup(core: CoreSetup, plugins: EditorFrameSetupPlugins): EditorFrameSetup {
plugins.expressions.registerFunction(() => mergeTables);
plugins.expressions.registerFunction(() => formatColumn);

return {
registerDatasource: datasource => {
Expand Down
Loading