Skip to content

Commit

Permalink
[Lens] Allow number formatting within Lens (#56253)
Browse files Browse the repository at this point in the history
* [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
Wylie Conlon and elasticmachine authored Feb 28, 2020
1 parent 8620f43 commit 6c6bc1f
Show file tree
Hide file tree
Showing 18 changed files with 600 additions and 48 deletions.
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

0 comments on commit 6c6bc1f

Please sign in to comment.