-
Notifications
You must be signed in to change notification settings - Fork 8.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[Lens] Allow setting custom colours to collapsed by slices pie's mult…
…iple metrics (#160592) Fixes #159808 It was working before, the only missing point was you couldn't color when all groups were collaped by. 1. Moved dimension editor code to separate file. 2. Removed StaticColorValue component and used `ColorPicker` that we use for other places in the code 3. Fixed the issue for pie (hopefully tested everything now) 4. Fixed the issue for xy chart. To reproduce - create a date histogram xy chart, assign color to the Count of records. Then create a breakdown dimension and collapse it. Open dimension panel for count of records - the displayed color is the default one, not the one we assigned.
- Loading branch information
Showing
11 changed files
with
245 additions
and
224 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
155 changes: 155 additions & 0 deletions
155
x-pack/plugins/lens/public/visualizations/partition/dimension_editor.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,155 @@ | ||
/* | ||
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
* 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. | ||
*/ | ||
|
||
import './toolbar.scss'; | ||
import React from 'react'; | ||
import { i18n } from '@kbn/i18n'; | ||
import type { PaletteRegistry } from '@kbn/coloring'; | ||
import { ColorPicker, useDebouncedValue } from '@kbn/visualization-ui-components/public'; | ||
import { PieVisualizationState } from '../../../common/types'; | ||
import { VisualizationDimensionEditorProps } from '../../types'; | ||
import { PalettePicker } from '../../shared_components'; | ||
import { CollapseSetting } from '../../shared_components/collapse_setting'; | ||
import { | ||
getDefaultColorForMultiMetricDimension, | ||
hasNonCollapsedSliceBy, | ||
isCollapsed, | ||
} from './visualization'; | ||
|
||
type DimensionEditorProps = VisualizationDimensionEditorProps<PieVisualizationState> & { | ||
paletteService: PaletteRegistry; | ||
}; | ||
|
||
export function DimensionEditor(props: DimensionEditorProps) { | ||
const { inputValue: localState, handleInputChange: setLocalState } = | ||
useDebouncedValue<PieVisualizationState>({ | ||
value: props.state, | ||
onChange: props.setState, | ||
}); | ||
|
||
const currentLayer = localState.layers.find((layer) => layer.layerId === props.layerId); | ||
|
||
const setConfig = React.useCallback( | ||
({ color }) => { | ||
if (!currentLayer) { | ||
return; | ||
} | ||
const newColorsByDimension = { ...currentLayer.colorsByDimension }; | ||
|
||
if (color) { | ||
newColorsByDimension[props.accessor] = color; | ||
} else { | ||
delete newColorsByDimension[props.accessor]; | ||
} | ||
|
||
setLocalState({ | ||
...localState, | ||
layers: localState.layers.map((layer) => | ||
layer.layerId === currentLayer.layerId | ||
? { | ||
...layer, | ||
colorsByDimension: newColorsByDimension, | ||
} | ||
: layer | ||
), | ||
}); | ||
}, | ||
[currentLayer, localState, props.accessor, setLocalState] | ||
); | ||
|
||
if (!currentLayer) { | ||
return null; | ||
} | ||
|
||
const firstNonCollapsedColumnId = currentLayer.primaryGroups.find( | ||
(id) => !isCollapsed(id, currentLayer) | ||
); | ||
|
||
const showColorPicker = | ||
currentLayer.metrics.includes(props.accessor) && currentLayer.allowMultipleMetrics; | ||
|
||
const colorPickerDisabledMessage = hasNonCollapsedSliceBy(currentLayer) | ||
? ['pie', 'donut'].includes(props.state.shape) | ||
? i18n.translate('xpack.lens.pieChart.colorPicker.disabledBecauseSliceBy', { | ||
defaultMessage: | ||
'You are unable to apply custom colors to individual slices when the layer includes one or more "Slice by" dimensions.', | ||
}) | ||
: i18n.translate('xpack.lens.pieChart.colorPicker.disabledBecauseGroupBy', { | ||
defaultMessage: | ||
'You are unable to apply custom colors to individual slices when the layer includes one or more "Group by" dimensions.', | ||
}) | ||
: undefined; | ||
|
||
return ( | ||
<> | ||
{props.accessor === firstNonCollapsedColumnId && ( | ||
<PalettePicker | ||
palettes={props.paletteService} | ||
activePalette={props.state.palette} | ||
setPalette={(newPalette) => { | ||
setLocalState({ ...props.state, palette: newPalette }); | ||
}} | ||
/> | ||
)} | ||
{showColorPicker && ( | ||
<ColorPicker | ||
{...props} | ||
overwriteColor={currentLayer.colorsByDimension?.[props.accessor]} | ||
defaultColor={getDefaultColorForMultiMetricDimension({ | ||
layer: currentLayer, | ||
columnId: props.accessor, | ||
paletteService: props.paletteService, | ||
datasource: props.datasource, | ||
palette: props.state.palette, | ||
})} | ||
disabledMessage={colorPickerDisabledMessage} | ||
setConfig={setConfig} | ||
/> | ||
)} | ||
</> | ||
); | ||
} | ||
|
||
export function DimensionDataExtraEditor( | ||
props: VisualizationDimensionEditorProps<PieVisualizationState> & { | ||
paletteService: PaletteRegistry; | ||
} | ||
) { | ||
const currentLayer = props.state.layers.find((layer) => layer.layerId === props.layerId); | ||
|
||
if (!currentLayer) { | ||
return null; | ||
} | ||
|
||
return ( | ||
<> | ||
{[...currentLayer.primaryGroups, ...(currentLayer.secondaryGroups ?? [])].includes( | ||
props.accessor | ||
) && ( | ||
<CollapseSetting | ||
value={currentLayer?.collapseFns?.[props.accessor] || ''} | ||
onChange={(collapseFn) => { | ||
props.setState({ | ||
...props.state, | ||
layers: props.state.layers.map((layer) => | ||
layer.layerId !== props.layerId | ||
? layer | ||
: { | ||
...layer, | ||
collapseFns: { | ||
...layer.collapseFns, | ||
[props.accessor]: collapseFn, | ||
}, | ||
} | ||
), | ||
}); | ||
}} | ||
/> | ||
)} | ||
</> | ||
); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.