Skip to content

Commit b687378

Browse files
committed
⚗️ Integrating various feedback from review
1 parent e4f0a22 commit b687378

File tree

5 files changed

+231
-49
lines changed

5 files changed

+231
-49
lines changed

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

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,14 @@ import { ExpressionsSetup } from '../../../../../src/plugins/expressions/public'
1010
import { UI_SETTINGS } from '../../../../../src/plugins/data/public';
1111
import { xyVisualization } from './xy_visualization';
1212
import { xyChart, getXyChartRenderer } from './xy_expression';
13-
import { legendConfig, layerConfig, yAxisConfig, tickLabelsConfig, gridlinesConfig } from './types';
13+
import {
14+
legendConfig,
15+
layerConfig,
16+
yAxisConfig,
17+
tickLabelsConfig,
18+
gridlinesConfig,
19+
valueLabelsConfig,
20+
} from './types';
1421
import { EditorFrameSetup, FormatFactory } from '../types';
1522
import { ChartsPluginSetup } from '../../../../../src/plugins/charts/public';
1623

@@ -41,6 +48,7 @@ export class XyVisualization {
4148
expressions.registerFunction(() => yAxisConfig);
4249
expressions.registerFunction(() => tickLabelsConfig);
4350
expressions.registerFunction(() => gridlinesConfig);
51+
expressions.registerFunction(() => valueLabelsConfig);
4452
expressions.registerFunction(() => layerConfig);
4553
expressions.registerFunction(() => xyChart);
4654

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

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -146,6 +146,22 @@ export const buildExpression = (
146146
],
147147
},
148148
],
149+
displayValues: [
150+
{
151+
type: 'expression',
152+
chain: [
153+
{
154+
type: 'function',
155+
function: 'lens_xy_displayValuesConfig',
156+
arguments: {
157+
showLabels: [state?.displayValues?.showLabels ?? false],
158+
fontSize: [state?.displayValues?.fontSize ?? 10],
159+
position: [state?.displayValues?.position ?? 'inside'],
160+
},
161+
},
162+
],
163+
},
164+
],
149165
layers: validLayers.map((layer) => {
150166
const columnToLabel: Record<string, string> = {};
151167

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

Lines changed: 46 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -169,6 +169,43 @@ const axisConfig: { [key in keyof AxisConfig]: ArgumentType<AxisConfig[key]> } =
169169
},
170170
};
171171

172+
type ValueLabelsConfigResult = ValueLabelConfig & {
173+
type: 'lens_xy_displayValuesConfig';
174+
};
175+
176+
export const valueLabelsConfig: ExpressionFunctionDefinition<
177+
'lens_xy_displayValuesConfig',
178+
null,
179+
ValueLabelConfig,
180+
ValueLabelsConfigResult
181+
> = {
182+
name: 'lens_xy_displayValuesConfig',
183+
aliases: [],
184+
type: 'lens_xy_displayValuesConfig',
185+
help: ``,
186+
inputTypes: ['null'],
187+
args: {
188+
showLabels: {
189+
types: ['boolean'],
190+
help: '',
191+
},
192+
fontSize: {
193+
types: ['number'],
194+
help: '',
195+
},
196+
position: {
197+
types: ['string'],
198+
help: '',
199+
},
200+
},
201+
fn: function fn(input: unknown, args: ValueLabelConfig) {
202+
return {
203+
type: 'lens_xy_displayValuesConfig',
204+
...args,
205+
};
206+
},
207+
};
208+
172209
type YConfigResult = YConfig & { type: 'lens_xy_yConfig' };
173210

174211
export const yAxisConfig: ExpressionFunctionDefinition<
@@ -199,7 +236,7 @@ export const yAxisConfig: ExpressionFunctionDefinition<
199236
showValueLabels: {
200237
types: ['boolean'],
201238
default: false,
202-
help: 'Whether to show value labels on the chart bars (horizontal only)',
239+
help: '',
203240
},
204241
},
205242
fn: function fn(input: unknown, args: YConfig) {
@@ -293,6 +330,12 @@ export type SeriesType =
293330

294331
export type YAxisMode = 'auto' | 'left' | 'right';
295332

333+
export interface ValueLabelConfig {
334+
showLabels: boolean;
335+
fontSize?: number;
336+
position?: 'inside' | 'outside';
337+
}
338+
296339
export interface YConfig {
297340
forAccessor: string;
298341
axisMode?: YAxisMode;
@@ -328,6 +371,7 @@ export interface XYArgs {
328371
showYAxisTitle?: boolean;
329372
tickLabelsVisibilitySettings?: AxesSettingsConfig & { type: 'lens_xy_tickLabelsConfig' };
330373
gridlinesVisibilitySettings?: AxesSettingsConfig & { type: 'lens_xy_gridlinesConfig' };
374+
displayValues: ValueLabelConfig & { type: 'lens_xy_displayValuesConfig' };
331375
}
332376

333377
// Persisted parts of the state
@@ -342,6 +386,7 @@ export interface XYState {
342386
showYAxisTitle?: boolean;
343387
tickLabelsVisibilitySettings?: AxesSettingsConfig;
344388
gridlinesVisibilitySettings?: AxesSettingsConfig;
389+
displayValues?: ValueLabelConfig;
345390
}
346391

347392
export type State = XYState;

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

Lines changed: 136 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ import {
2929
EuiHorizontalRule,
3030
EuiTitle,
3131
} from '@elastic/eui';
32+
import { EuiFieldNumber } from '@elastic/eui';
3233
import {
3334
VisualizationLayerWidgetProps,
3435
VisualizationDimensionEditorProps,
@@ -83,6 +84,27 @@ const legendOptions: Array<{ id: string; value: 'auto' | 'show' | 'hide'; label:
8384
},
8485
];
8586

87+
const valueLabelsPositioningOptions: Array<{
88+
id: string;
89+
value: 'inside' | 'outside';
90+
label: string;
91+
}> = [
92+
{
93+
id: 'xy_valueLabels_inside',
94+
value: 'inside',
95+
label: i18n.translate('xpack.lens.xyChart.valueLabels.labelsPositioning.inside', {
96+
defaultMessage: 'inside',
97+
}),
98+
},
99+
{
100+
id: 'xy_valueLabels_outside',
101+
value: 'outside',
102+
label: i18n.translate('xpack.lens.xyChart.valueLabels.labelsPositioning.outside', {
103+
defaultMessage: 'outside',
104+
}),
105+
},
106+
];
107+
86108
export function LayerContextMenu(props: VisualizationLayerWidgetProps<State>) {
87109
const { state, layerId } = props;
88110
const horizontalOnly = isHorizontalChart(state.layers);
@@ -235,6 +257,20 @@ export function XyToolbar(props: VisualizationToolbarProps<State>) {
235257
});
236258
};
237259

260+
const onValueDisplayVisibilitySettingsChange = (enabled: boolean): void => {
261+
setState({
262+
...state,
263+
displayValues: {
264+
...state.displayValues,
265+
showLabels: enabled,
266+
},
267+
});
268+
};
269+
270+
const shouldEnableValueLabels = state?.layers.some((layer) => layer.seriesType.includes('bar'));
271+
const showValueLabels = Boolean(shouldEnableValueLabels && state?.displayValues?.showLabels);
272+
const valueLabelsPositioning = state?.displayValues?.position || 'inside';
273+
238274
const legendMode =
239275
state?.legend.isVisible && !state?.legend.showSingleSeries
240276
? 'auto'
@@ -365,6 +401,80 @@ export function XyToolbar(props: VisualizationToolbarProps<State>) {
365401
/>
366402
</EuiFormRow>
367403
<EuiHorizontalRule margin="s" />
404+
<EuiFormRow
405+
display="columnCompressedSwitch"
406+
fullWidth
407+
label={i18n.translate('xpack.lens.xyChart.valueLabels.showlabels.label', {
408+
defaultMessage: 'Value Labels Display',
409+
})}
410+
>
411+
<EuiSwitch
412+
data-test-subj="lnsshowShowValueLabelsSwitch"
413+
showLabel={false}
414+
label={i18n.translate('xpack.lens.xyChart.valueLabels.showlabels', {
415+
defaultMessage: 'Value Labels Display',
416+
})}
417+
onChange={({ target }) => onValueDisplayVisibilitySettingsChange(target.checked)}
418+
checked={showValueLabels}
419+
disabled={!shouldEnableValueLabels}
420+
/>
421+
</EuiFormRow>
422+
<EuiFormRow
423+
display="columnCompressed"
424+
fullWidth
425+
label={i18n.translate('xpack.lens.xyChart.valueLabels.labelsFontSize.label', {
426+
defaultMessage: 'Value Labels size',
427+
})}
428+
>
429+
<EuiFieldNumber
430+
value={state?.displayValues?.fontSize || 10}
431+
onChange={({ target }) =>
432+
setState({
433+
...state,
434+
displayValues: {
435+
...state.displayValues,
436+
showLabels: true,
437+
fontSize: Number(target.value),
438+
},
439+
})
440+
}
441+
disabled={!shouldEnableValueLabels}
442+
/>
443+
</EuiFormRow>
444+
<EuiFormRow
445+
display="columnCompressed"
446+
label={i18n.translate('xpack.lens.xyChart.valueLabels.labelsPositioning.label', {
447+
defaultMessage: 'Value labels position',
448+
})}
449+
>
450+
<EuiButtonGroup
451+
isFullWidth
452+
legend={i18n.translate('xpack.lens.xyChart.legendVisibilityLabel', {
453+
defaultMessage: 'Value labels position',
454+
})}
455+
name="valueLabelsPositioning"
456+
buttonSize="compressed"
457+
options={valueLabelsPositioningOptions}
458+
idSelected={
459+
valueLabelsPositioningOptions.find(({ value }) => value === valueLabelsPositioning)!
460+
.id
461+
}
462+
isDisabled={!shouldEnableValueLabels || !showValueLabels}
463+
onChange={(optionId) => {
464+
const newMode = valueLabelsPositioningOptions.find(({ id }) => id === optionId)!
465+
.value;
466+
setState({
467+
...state,
468+
displayValues: {
469+
...state.displayValues,
470+
showLabels: true,
471+
position: newMode,
472+
},
473+
});
474+
}}
475+
/>
476+
</EuiFormRow>
477+
<EuiHorizontalRule margin="s" />
368478
<EuiFormRow
369479
display="columnCompressed"
370480
label={i18n.translate('xpack.lens.xyChart.TickLabels', {
@@ -488,11 +598,10 @@ const idPrefix = htmlIdGenerator()();
488598

489599
export function DimensionEditor(props: VisualizationDimensionEditorProps<State>) {
490600
const { state, setState, layerId, accessor } = props;
491-
const horizontalOnly = isHorizontalChart(state.layers);
492-
const chartHasMoreThanOneSeries =
493-
state.layers.length > 1 ||
494-
state.layers.some(({ accessors }) => accessors.length > 1) ||
495-
state.layers.some(({ splitAccessor }) => splitAccessor);
601+
// const horizontalOnly = isHorizontalChart(state.layers);
602+
// const chartHasMoreThanOneSeries =
603+
// state.layers.length > 1 || state.layers.some(({ splitAccessor }) => splitAccessor);
604+
const shouldEnableValueLabels = state?.layers.some((layer) => layer.seriesType.includes('bar'));
496605

497606
const index = state.layers.findIndex((l) => l.layerId === layerId);
498607
const layer = state.layers[index];
@@ -501,9 +610,8 @@ export function DimensionEditor(props: VisualizationDimensionEditorProps<State>)
501610
forAccessor: accessor,
502611
};
503612
const axisMode = layerConfig?.axisMode || 'auto';
504-
const shouldShowValueLabels = horizontalOnly && !chartHasMoreThanOneSeries;
505613

506-
const showValueLabels = (shouldShowValueLabels && layerConfig?.showValueLabels) ?? false;
614+
const showValueLabels = (shouldEnableValueLabels && layerConfig?.showValueLabels) ?? false;
507615

508616
const createNewYAxisConfigsWithValue = useCallback(
509617
<K extends keyof YConfig, V extends YConfig[K]>(prop: K, newValue: V) => {
@@ -570,31 +678,30 @@ export function DimensionEditor(props: VisualizationDimensionEditorProps<State>)
570678
}}
571679
/>
572680
</EuiFormRow>
573-
{shouldShowValueLabels && (
574-
<EuiFormRow
575-
display="columnCompressedSwitch"
576-
fullWidth
681+
<EuiFormRow
682+
display="columnCompressedSwitch"
683+
fullWidth
684+
label={i18n.translate('xpack.lens.xyChart.showValueLabels.label', {
685+
defaultMessage: 'Show Value Labels',
686+
})}
687+
>
688+
<EuiSwitch
689+
data-test-subj="lnsshowShowValueLabelsSwitch"
690+
showLabel={false}
577691
label={i18n.translate('xpack.lens.xyChart.showValueLabels.label', {
578692
defaultMessage: 'Show Value Labels',
579693
})}
580-
>
581-
<EuiSwitch
582-
data-test-subj="lnsshowShowValueLabelsSwitch"
583-
showLabel={false}
584-
label={i18n.translate('xpack.lens.xyChart.showValueLabels.label', {
585-
defaultMessage: 'Show Value Labels',
586-
})}
587-
onChange={({ target }) => {
588-
const newYAxisConfigs = createNewYAxisConfigsWithValue(
589-
'showValueLabels',
590-
target.checked
591-
);
592-
setState(updateLayer(state, { ...layer, yConfig: newYAxisConfigs }, index));
593-
}}
594-
checked={showValueLabels}
595-
/>
596-
</EuiFormRow>
597-
)}
694+
onChange={({ target }) => {
695+
const newYAxisConfigs = createNewYAxisConfigsWithValue(
696+
'showValueLabels',
697+
target.checked
698+
);
699+
setState(updateLayer(state, { ...layer, yConfig: newYAxisConfigs }, index));
700+
}}
701+
checked={showValueLabels}
702+
disabled={!shouldEnableValueLabels}
703+
/>
704+
</EuiFormRow>
598705
</EuiForm>
599706
);
600707
}

0 commit comments

Comments
 (0)