2020-05-05 21:59:32 +02:00
|
|
|
/*
|
|
|
|
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
|
2021-02-04 03:12:39 +01:00
|
|
|
* or more contributor license agreements. Licensed under the Elastic License
|
|
|
|
* 2.0; you may not use this file except in compliance with the Elastic License
|
|
|
|
* 2.0.
|
2020-05-05 21:59:32 +02:00
|
|
|
*/
|
|
|
|
|
|
|
|
import { Ast } from '@kbn/interpreter/common';
|
2020-11-04 11:27:52 +01:00
|
|
|
import { PaletteRegistry } from 'src/plugins/charts/public';
|
2020-08-21 18:08:25 +02:00
|
|
|
import { Operation, DatasourcePublicAPI } from '../types';
|
2020-05-05 21:59:32 +02:00
|
|
|
import { DEFAULT_PERCENT_DECIMALS } from './constants';
|
|
|
|
import { PieVisualizationState } from './types';
|
|
|
|
|
2020-08-21 18:08:25 +02:00
|
|
|
export function toExpression(
|
|
|
|
state: PieVisualizationState,
|
2020-10-01 18:02:37 +02:00
|
|
|
datasourceLayers: Record<string, DatasourcePublicAPI>,
|
2020-11-04 11:27:52 +01:00
|
|
|
paletteService: PaletteRegistry,
|
2020-10-01 18:02:37 +02:00
|
|
|
attributes: Partial<{ title: string; description: string }> = {}
|
2020-08-21 18:08:25 +02:00
|
|
|
) {
|
2020-11-04 11:27:52 +01:00
|
|
|
return expressionHelper(state, datasourceLayers, paletteService, {
|
|
|
|
...attributes,
|
|
|
|
isPreview: false,
|
|
|
|
});
|
2020-05-05 21:59:32 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
function expressionHelper(
|
|
|
|
state: PieVisualizationState,
|
2020-08-21 18:08:25 +02:00
|
|
|
datasourceLayers: Record<string, DatasourcePublicAPI>,
|
2020-11-04 11:27:52 +01:00
|
|
|
paletteService: PaletteRegistry,
|
2020-10-01 18:02:37 +02:00
|
|
|
attributes: { isPreview: boolean; title?: string; description?: string } = { isPreview: false }
|
2020-05-05 21:59:32 +02:00
|
|
|
): Ast | null {
|
|
|
|
const layer = state.layers[0];
|
2020-08-21 18:08:25 +02:00
|
|
|
const datasource = datasourceLayers[layer.layerId];
|
2020-05-05 21:59:32 +02:00
|
|
|
const operations = layer.groups
|
2020-05-22 09:08:58 +02:00
|
|
|
.map((columnId) => ({ columnId, operation: datasource.getOperationForColumnId(columnId) }))
|
2020-05-05 21:59:32 +02:00
|
|
|
.filter((o): o is { columnId: string; operation: Operation } => !!o.operation);
|
|
|
|
if (!layer.metric || !operations.length) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
return {
|
|
|
|
type: 'expression',
|
|
|
|
chain: [
|
|
|
|
{
|
|
|
|
type: 'function',
|
|
|
|
function: 'lens_pie',
|
|
|
|
arguments: {
|
2020-10-01 18:02:37 +02:00
|
|
|
title: [attributes.title || ''],
|
|
|
|
description: [attributes.description || ''],
|
2020-05-05 21:59:32 +02:00
|
|
|
shape: [state.shape],
|
2020-10-01 18:02:37 +02:00
|
|
|
hideLabels: [attributes.isPreview],
|
2020-05-22 09:08:58 +02:00
|
|
|
groups: operations.map((o) => o.columnId),
|
2020-05-05 21:59:32 +02:00
|
|
|
metric: [layer.metric],
|
|
|
|
numberDisplay: [layer.numberDisplay],
|
|
|
|
categoryDisplay: [layer.categoryDisplay],
|
|
|
|
legendDisplay: [layer.legendDisplay],
|
2020-07-22 12:14:59 +02:00
|
|
|
legendPosition: [layer.legendPosition || 'right'],
|
2020-05-05 21:59:32 +02:00
|
|
|
percentDecimals: [layer.percentDecimals ?? DEFAULT_PERCENT_DECIMALS],
|
|
|
|
nestedLegend: [!!layer.nestedLegend],
|
2020-11-04 11:27:52 +01:00
|
|
|
...(state.palette
|
|
|
|
? {
|
|
|
|
palette: [
|
|
|
|
{
|
|
|
|
type: 'expression',
|
|
|
|
chain: [
|
|
|
|
{
|
|
|
|
type: 'function',
|
|
|
|
function: 'theme',
|
|
|
|
arguments: {
|
|
|
|
variable: ['palette'],
|
|
|
|
default: [
|
|
|
|
paletteService
|
|
|
|
.get(state.palette.name)
|
|
|
|
.toExpression(state.palette.params),
|
|
|
|
],
|
|
|
|
},
|
|
|
|
},
|
|
|
|
],
|
|
|
|
},
|
|
|
|
],
|
|
|
|
}
|
|
|
|
: {}),
|
2020-05-05 21:59:32 +02:00
|
|
|
},
|
|
|
|
},
|
|
|
|
],
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2020-08-21 18:08:25 +02:00
|
|
|
export function toPreviewExpression(
|
|
|
|
state: PieVisualizationState,
|
2020-11-04 11:27:52 +01:00
|
|
|
datasourceLayers: Record<string, DatasourcePublicAPI>,
|
|
|
|
paletteService: PaletteRegistry
|
2020-08-21 18:08:25 +02:00
|
|
|
) {
|
2020-11-04 11:27:52 +01:00
|
|
|
return expressionHelper(state, datasourceLayers, paletteService, { isPreview: true });
|
2020-05-05 21:59:32 +02:00
|
|
|
}
|