Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
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
16 changes: 13 additions & 3 deletions x-pack/plugins/lens/public/pie_visualization/to_expression.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,18 @@ import type { PaletteRegistry } from 'src/plugins/charts/public';
import type { Operation, DatasourcePublicAPI } from '../types';
import { DEFAULT_PERCENT_DECIMALS, EMPTY_SIZE_RATIOS } from './constants';
import { shouldShowValuesInLegend } from './render_helpers';
import type { PieVisualizationState } from '../../common/expressions';
import type { PieLayerState, PieVisualizationState } from '../../common/expressions';
import { getDefaultVisualValuesForLayer } from '../shared_components/datasource_default_values';

export const getSortedAccessors = (datasource: DatasourcePublicAPI, layer: PieLayerState) => {
Copy link
Contributor

Choose a reason for hiding this comment

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

nit: getSortedGroups?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yes, makes sense. I'll update the code.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Done.

const originalOrder = datasource
.getTableSpec()
.map(({ columnId }: { columnId: string }) => columnId)
.filter((columnId: string) => layer.groups.includes(columnId));
// When we add a column it could be empty, and therefore have no order
return Array.from(new Set(originalOrder.concat(layer.groups)));
};

export function toExpression(
state: PieVisualizationState,
datasourceLayers: Record<string, DatasourcePublicAPI>,
Expand All @@ -33,14 +42,15 @@ function expressionHelper(
): Ast | null {
const layer = state.layers[0];
const datasource = datasourceLayers[layer.layerId];
const operations = layer.groups
const groups = getSortedAccessors(datasource, layer);

const operations = groups
.map((columnId) => ({ columnId, operation: datasource.getOperationForColumnId(columnId) }))
.filter((o): o is { columnId: string; operation: Operation } => !!o.operation);

if (!layer.metric || !operations.length) {
return null;
}

return {
type: 'expression',
chain: [
Expand Down
15 changes: 6 additions & 9 deletions x-pack/plugins/lens/public/pie_visualization/visualization.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ import type {
AccessorConfig,
VisualizationDimensionGroupConfig,
} from '../types';
import { toExpression, toPreviewExpression } from './to_expression';
import { getSortedAccessors, toExpression, toPreviewExpression } from './to_expression';
import type { PieLayerState, PieVisualizationState } from '../../common/expressions';
import { layerTypes } from '../../common';
import { suggestions } from './suggestions';
Expand Down Expand Up @@ -126,14 +126,11 @@ export const getPieVisualization = ({
}

const datasource = frame.datasourceLayers[layer.layerId];
const originalOrder = datasource
.getTableSpec()
.map(({ columnId }) => columnId)
.filter((columnId) => columnId !== layer.metric);
const originalOrder = getSortedAccessors(datasource, layer);
// When we add a column it could be empty, and therefore have no order
const sortedColumns: AccessorConfig[] = Array.from(
new Set(originalOrder.concat(layer.groups))
).map((accessor) => ({ columnId: accessor }));
const sortedColumns: AccessorConfig[] = originalOrder.map((accessor) => ({
columnId: accessor,
}));

if (sortedColumns.length) {
applyPaletteToColumnConfig(sortedColumns, state, paletteService);
Expand Down Expand Up @@ -197,7 +194,7 @@ export const getPieVisualization = ({
return l;
}
if (groupId === 'groups') {
return { ...l, groups: [...l.groups, columnId] };
return { ...l, groups: [...l.groups.filter((group) => group !== columnId), columnId] };
}
return { ...l, metric: columnId };
}),
Expand Down