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 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 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 @@ -83,6 +83,7 @@ export type IFieldFormatId = FIELD_FORMAT_IDS | string;

export type IFieldFormatType = (new (params?: any, getConfig?: GetConfigFn) => FieldFormat) & {
id: IFieldFormatId;
title?: string;
Copy link
Contributor Author

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

Copy link
Contributor

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 the title is the same as id and fieldType always present, and could just be a mandatory string.

fieldType: string | string[];
};

Expand Down
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: {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

format and columnId should be marked as required: true in the definition here, otherwise there is a type mismatch with the FormatColumn interface on top (unfortunately something the typing of ExpressionFunction does not yet capture automatically.

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;
}),
};
},
};
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,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 All @@ -55,6 +56,7 @@ export class EditorFramePlugin {

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

return {
registerDatasource: datasource => {
Expand Down
Loading