Skip to content

Commit 3e6e1dd

Browse files
committed
♻️ Make the value labels a global only setting + dimention shortcut
1 parent a11c646 commit 3e6e1dd

File tree

2 files changed

+90
-64
lines changed

2 files changed

+90
-64
lines changed

x-pack/plugins/lens/public/xy_visualization/to_expression.ts

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -219,9 +219,6 @@ export const buildExpression = (
219219
forAccessor: [yConfig.forAccessor],
220220
axisMode: yConfig.axisMode ? [yConfig.axisMode] : [],
221221
color: yConfig.color ? [yConfig.color] : [],
222-
showValueLabels: yConfig.showValueLabels
223-
? [yConfig.showValueLabels]
224-
: [],
225222
},
226223
},
227224
],

x-pack/plugins/lens/public/xy_visualization/xy_config_panel.tsx

Lines changed: 90 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,59 @@ const valueLabelsPositioningOptions: Array<{
106106
},
107107
];
108108

109+
function getValueLabelsConfiguration({
110+
state,
111+
frame,
112+
setState,
113+
}: VisualizationToolbarProps<State>): Required<State['displayValues']> & {
114+
shouldValueLabelsBeEnabled: boolean;
115+
disabledValueLabelsMessage: string;
116+
onValueDisplayVisibilitySettingsChange: (enabled: boolean) => void;
117+
} {
118+
const isBarChartType = state?.layers.some((layer) => layer.seriesType.includes('bar'));
119+
const isHistogram = state?.layers.some((layer) => {
120+
if (!layer.xAccessor) {
121+
return false;
122+
}
123+
const xAxisOperation = frame.datasourceLayers[layer.layerId]?.getOperationForColumnId(
124+
layer.xAccessor
125+
);
126+
return Boolean(
127+
xAxisOperation &&
128+
xAxisOperation.isBucketed &&
129+
xAxisOperation.scale &&
130+
xAxisOperation.scale !== 'ordinal'
131+
);
132+
});
133+
const shouldEnableValueLabels = isBarChartType && !isHistogram;
134+
const showLabels = Boolean(shouldEnableValueLabels && state?.displayValues?.showLabels);
135+
136+
const onValueDisplayVisibilitySettingsChange = (enabled: boolean): void => {
137+
setState({
138+
...state,
139+
displayValues: {
140+
...state.displayValues,
141+
showLabels: enabled,
142+
},
143+
});
144+
};
145+
146+
return {
147+
shouldValueLabelsBeEnabled: isBarChartType && !isHistogram,
148+
disabledValueLabelsMessage: isHistogram
149+
? i18n.translate('xpack.lens.xyChart.valueLabelsDisabledHelpText.histogram', {
150+
defaultMessage: 'This setting only applies to non-time related bar charts.',
151+
})
152+
: i18n.translate('xpack.lens.xyChart.valueLabelsDisabledHelpText.nonBarTypes', {
153+
defaultMessage: 'This setting only applies to bar type of charts.',
154+
}),
155+
showLabels,
156+
fontSize: state?.displayValues?.fontSize || 10,
157+
position: state?.displayValues?.position || 'inside',
158+
onValueDisplayVisibilitySettingsChange,
159+
};
160+
}
161+
109162
export function LayerContextMenu(props: VisualizationLayerWidgetProps<State>) {
110163
const { state, layerId } = props;
111164
const horizontalOnly = isHorizontalChart(state.layers);
@@ -258,42 +311,13 @@ export function XyToolbar(props: VisualizationToolbarProps<State>) {
258311
});
259312
};
260313

261-
const onValueDisplayVisibilitySettingsChange = (enabled: boolean): void => {
262-
setState({
263-
...state,
264-
displayValues: {
265-
...state.displayValues,
266-
showLabels: enabled,
267-
},
268-
});
269-
};
270-
271-
const isBarChartType = state?.layers.some((layer) => layer.seriesType.includes('bar'));
272-
const isHistogram = state?.layers.every((layer) => {
273-
if (!layer.xAccessor) {
274-
return false;
275-
}
276-
const xAxisOperation = frame.datasourceLayers[layer.layerId]?.getOperationForColumnId(
277-
layer.xAccessor
278-
);
279-
return Boolean(
280-
xAxisOperation &&
281-
xAxisOperation.isBucketed &&
282-
xAxisOperation.scale &&
283-
xAxisOperation.scale !== 'ordinal'
284-
);
285-
});
286-
const shouldEnableValueLabels = isBarChartType && !isHistogram;
287-
const showValueLabels = Boolean(shouldEnableValueLabels && state?.displayValues?.showLabels);
288-
const valueLabelsPositioning = state?.displayValues?.position || 'inside';
289-
290-
const valueLabelsDisabledMessage = isHistogram
291-
? i18n.translate('xpack.lens.xyChart.valueLabelsDisabledHelpText.histogram', {
292-
defaultMessage: 'This setting only applies to non-time related bar charts.',
293-
})
294-
: i18n.translate('xpack.lens.xyChart.valueLabelsDisabledHelpText.nonBarTypes', {
295-
defaultMessage: 'This setting only applies to bar type of charts.',
296-
});
314+
const {
315+
shouldValueLabelsBeEnabled,
316+
disabledValueLabelsMessage,
317+
showLabels,
318+
position: valueLabelsPosition,
319+
onValueDisplayVisibilitySettingsChange,
320+
} = getValueLabelsConfiguration(props);
297321

298322
const legendMode =
299323
state?.legend.isVisible && !state?.legend.showSingleSeries
@@ -434,7 +458,7 @@ export function XyToolbar(props: VisualizationToolbarProps<State>) {
434458
>
435459
<EuiToolTip
436460
anchorClassName="eui-displayBlock"
437-
content={!shouldEnableValueLabels && valueLabelsDisabledMessage}
461+
content={!shouldValueLabelsBeEnabled && disabledValueLabelsMessage}
438462
>
439463
<EuiSwitch
440464
data-test-subj="lnsshowShowValueLabelsSwitch"
@@ -443,8 +467,8 @@ export function XyToolbar(props: VisualizationToolbarProps<State>) {
443467
defaultMessage: 'Value Labels Display',
444468
})}
445469
onChange={({ target }) => onValueDisplayVisibilitySettingsChange(target.checked)}
446-
checked={showValueLabels}
447-
disabled={!shouldEnableValueLabels}
470+
checked={showLabels}
471+
disabled={!shouldValueLabelsBeEnabled}
448472
/>
449473
</EuiToolTip>
450474
</EuiFormRow>
@@ -455,7 +479,10 @@ export function XyToolbar(props: VisualizationToolbarProps<State>) {
455479
defaultMessage: 'Value Labels size',
456480
})}
457481
>
458-
<ValueLabelFontSizeInput {...props} disabled={!shouldEnableValueLabels} />
482+
<ValueLabelFontSizeInput
483+
{...props}
484+
disabled={!shouldValueLabelsBeEnabled || !showLabels}
485+
/>
459486
</EuiFormRow>
460487
<EuiFormRow
461488
display="columnCompressed"
@@ -472,10 +499,9 @@ export function XyToolbar(props: VisualizationToolbarProps<State>) {
472499
buttonSize="compressed"
473500
options={valueLabelsPositioningOptions}
474501
idSelected={
475-
valueLabelsPositioningOptions.find(({ value }) => value === valueLabelsPositioning)!
476-
.id
502+
valueLabelsPositioningOptions.find(({ value }) => value === valueLabelsPosition)!.id
477503
}
478-
isDisabled={!shouldEnableValueLabels || !showValueLabels}
504+
isDisabled={!shouldValueLabelsBeEnabled || !showLabels}
479505
onChange={(optionId) => {
480506
const newMode = valueLabelsPositioningOptions.find(({ id }) => id === optionId)!
481507
.value;
@@ -614,7 +640,6 @@ const idPrefix = htmlIdGenerator()();
614640

615641
export function DimensionEditor(props: VisualizationDimensionEditorProps<State>) {
616642
const { state, setState, layerId, accessor } = props;
617-
const shouldEnableValueLabels = state?.layers.some((layer) => layer.seriesType.includes('bar'));
618643

619644
const index = state.layers.findIndex((l) => l.layerId === layerId);
620645
const layer = state.layers[index];
@@ -624,7 +649,12 @@ export function DimensionEditor(props: VisualizationDimensionEditorProps<State>)
624649
};
625650
const axisMode = layerConfig?.axisMode || 'auto';
626651

627-
const showValueLabels = (shouldEnableValueLabels && layerConfig?.showValueLabels) ?? false;
652+
const {
653+
shouldValueLabelsBeEnabled,
654+
showLabels,
655+
disabledValueLabelsMessage,
656+
onValueDisplayVisibilitySettingsChange,
657+
} = getValueLabelsConfiguration(props);
628658

629659
const createNewYAxisConfigsWithValue = useCallback(
630660
<K extends keyof YConfig, V extends YConfig[K]>(prop: K, newValue: V) => {
@@ -698,22 +728,21 @@ export function DimensionEditor(props: VisualizationDimensionEditorProps<State>)
698728
defaultMessage: 'Show Value Labels',
699729
})}
700730
>
701-
<EuiSwitch
702-
data-test-subj="lnsshowShowValueLabelsSwitch"
703-
showLabel={false}
704-
label={i18n.translate('xpack.lens.xyChart.showValueLabels.label', {
705-
defaultMessage: 'Show Value Labels',
706-
})}
707-
onChange={({ target }) => {
708-
const newYAxisConfigs = createNewYAxisConfigsWithValue(
709-
'showValueLabels',
710-
target.checked
711-
);
712-
setState(updateLayer(state, { ...layer, yConfig: newYAxisConfigs }, index));
713-
}}
714-
checked={showValueLabels}
715-
disabled={!shouldEnableValueLabels}
716-
/>
731+
<EuiToolTip
732+
anchorClassName="eui-displayBlock"
733+
content={!shouldValueLabelsBeEnabled && disabledValueLabelsMessage}
734+
>
735+
<EuiSwitch
736+
data-test-subj="lnsshowShowValueLabelsSwitch"
737+
showLabel={false}
738+
label={i18n.translate('xpack.lens.xyChart.showValueLabels.label', {
739+
defaultMessage: 'Show Value Labels',
740+
})}
741+
onChange={({ target }) => onValueDisplayVisibilitySettingsChange(target.checked)}
742+
checked={showLabels}
743+
disabled={!shouldValueLabelsBeEnabled}
744+
/>
745+
</EuiToolTip>
717746
</EuiFormRow>
718747
</EuiForm>
719748
);

0 commit comments

Comments
 (0)