Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[MetricVis] Add possibility to configure label font and position in metric unified renderer #125251

Merged
Show file tree
Hide file tree
Changes from all commits
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
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,8 @@
*/

export const EXPRESSION_METRIC_NAME = 'metricVis';

export const LabelPosition = {
BOTTOM: 'bottom',
TOP: 'top',
} as const;

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import { metricVisFunction } from './metric_vis_function';
import type { MetricArguments } from '../../common';
import { functionWrapper } from '../../../../expressions/common/expression_functions/specs/tests/utils';
import { Datatable } from '../../../../expressions/common/expression_types/specs';
import { LabelPosition } from '../constants';

describe('interpreter/functions#metric', () => {
const fn = functionWrapper(metricVisFunction());
Expand All @@ -35,6 +36,8 @@ describe('interpreter/functions#metric', () => {
},
showLabels: true,
font: { spec: { fontSize: '60px' }, type: 'style', css: '' },
labelFont: { spec: { fontSize: '24px' }, type: 'style', css: '' },
labelPosition: LabelPosition.BOTTOM,
metric: [
{
type: 'vis_dimension',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,31 @@ import { visType } from '../types';
import { prepareLogTable, Dimension } from '../../../../visualizations/common/prepare_log_table';
import { ColorMode } from '../../../../charts/common';
import { MetricVisExpressionFunctionDefinition } from '../types';
import { EXPRESSION_METRIC_NAME } from '../constants';
import { EXPRESSION_METRIC_NAME, LabelPosition } from '../constants';

const validateOptions = (
value: string,
availableOptions: Record<string, string>,
getErrorMessage: () => string
) => {
if (!Object.values(availableOptions).includes(value)) {
throw new Error(getErrorMessage());
}
};

const errors = {
invalidColorModeError: () =>
i18n.translate('expressionMetricVis.function.errors.invalidColorModeError', {
defaultMessage: 'Invalid color mode is specified. Supported color modes: {colorModes}',
values: { colorModes: Object.values(ColorMode).join(', ') },
}),
invalidLabelPositionError: () =>
i18n.translate('expressionMetricVis.function.errors.invalidLabelPositionError', {
defaultMessage:
'Invalid label position is specified. Supported label positions: {labelPosition}',
values: { labelPosition: Object.values(LabelPosition).join(', ') },
}),
};

export const metricVisFunction = (): MetricVisExpressionFunctionDefinition => ({
name: EXPRESSION_METRIC_NAME,
Expand Down Expand Up @@ -57,6 +81,21 @@ export const metricVisFunction = (): MetricVisExpressionFunctionDefinition => ({
}),
default: `{font size=60 align="center"}`,
},
labelFont: {
types: ['style'],
help: i18n.translate('expressionMetricVis.function.labelFont.help', {
defaultMessage: 'Label font settings.',
}),
default: `{font size=24 align="center"}`,
},
labelPosition: {
types: ['string'],
options: [LabelPosition.BOTTOM, LabelPosition.TOP],
help: i18n.translate('expressionMetricVis.function.labelPosition.help', {
defaultMessage: 'Label position',
}),
default: LabelPosition.BOTTOM,
},
metric: {
types: ['vis_dimension'],
help: i18n.translate('expressionMetricVis.function.metric.help', {
Expand Down Expand Up @@ -84,6 +123,9 @@ export const metricVisFunction = (): MetricVisExpressionFunctionDefinition => ({
throw new Error('Palette must be provided when using percentageMode');
}

validateOptions(args.colorMode, ColorMode, errors.invalidColorModeError);
validateOptions(args.labelPosition, LabelPosition, errors.invalidLabelPositionError);

if (handlers?.inspectorAdapters?.tables) {
const argsTable: Dimension[] = [
[
Expand Down Expand Up @@ -118,6 +160,10 @@ export const metricVisFunction = (): MetricVisExpressionFunctionDefinition => ({
metricColorMode: args.colorMode,
labels: {
show: args.showLabels,
position: args.labelPosition,
style: {
...args.labelFont,
},
},
style: {
bgColor: args.colorMode === ColorMode.Background,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import {
} from '../../../../expressions';
import { ExpressionValueVisDimension } from '../../../../visualizations/common';
import { ColorMode, CustomPaletteState, PaletteOutput } from '../../../../charts/common';
import { VisParams, visType } from './expression_renderers';
import { VisParams, visType, LabelPositionType } from './expression_renderers';
import { EXPRESSION_METRIC_NAME } from '../constants';

export interface MetricArguments {
Expand All @@ -23,6 +23,8 @@ export interface MetricArguments {
showLabels: boolean;
palette?: PaletteOutput<CustomPaletteState>;
font: Style;
labelFont: Style;
labelPosition: LabelPositionType;
metric: ExpressionValueVisDimension[];
bucket?: ExpressionValueVisDimension;
autoScale?: boolean;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
* Side Public License, v 1.
*/

import { $Values } from '@kbn/utility-types';
import { ExpressionValueVisDimension } from '../../../../visualizations/common';
import {
ColorMode,
Expand All @@ -14,6 +15,7 @@ import {
Style as ChartStyle,
} from '../../../../charts/common';
import { Style } from '../../../../expressions/common';
import { LabelPosition } from '../constants';

export const visType = 'metric';

Expand All @@ -22,13 +24,17 @@ export interface DimensionsVisParam {
bucket?: ExpressionValueVisDimension;
}

export type LabelPositionType = $Values<typeof LabelPosition>;

export type MetricStyle = Style & Pick<ChartStyle, 'bgColor' | 'labelColor'>;

export type LabelsConfig = Labels & { style: Style; position: LabelPositionType };
export interface MetricVisParam {
percentageMode: boolean;
percentageFormatPattern?: string;
metricColorMode: ColorMode;
palette?: CustomPaletteState;
labels: Labels;
labels: LabelsConfig;
style: MetricStyle;
autoScale?: boolean;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import { Render } from '../../../../presentation_util/public/__stories__';
import { ColorMode, CustomPaletteState } from '../../../../charts/common';
import { getMetricVisRenderer } from '../expression_renderers';
import { MetricStyle, MetricVisRenderConfig, visType } from '../../common/types';
import { LabelPosition } from '../../common/constants';

const palette: CustomPaletteState = {
colors: ['rgb(219 231 38)', 'rgb(112 38 231)', 'rgb(38 124 231)'],
Expand Down Expand Up @@ -57,7 +58,11 @@ const config: MetricVisRenderConfig = {
visConfig: {
metric: {
metricColorMode: ColorMode.None,
labels: { show: true },
labels: {
show: true,
style: { spec: {}, type: 'style', css: '' },
position: LabelPosition.BOTTOM,
},
percentageMode: false,
style,
},
Expand Down Expand Up @@ -132,7 +137,14 @@ storiesOf('renderers/visMetric', module)
...config,
visConfig: {
...config.visConfig,
metric: { ...config.visConfig.metric, labels: { show: false } },
metric: {
...config.visConfig.metric,
labels: {
show: false,
style: { spec: {}, type: 'style', css: '' },
position: LabelPosition.BOTTOM,
},
},
},
}}
{...containerSize}
Expand Down Expand Up @@ -160,6 +172,32 @@ storiesOf('renderers/visMetric', module)
/>
);
})
.add('With label position is top and custom font for label', () => {
return (
<Render
renderer={metricVisRenderer}
config={{
...config,
visConfig: {
...config.visConfig,
metric: {
...config.visConfig.metric,
style: {
...config.visConfig.metric.style,
spec: { ...config.visConfig.metric.style.spec, fontSize: '80px' },
},
labels: {
show: false,
style: { spec: { fontSize: '60px', align: 'left' }, type: 'style', css: '' },
position: LabelPosition.TOP,
},
},
},
}}
{...containerSize}
/>
);
})
.add('With color ranges, background color mode', () => {
return (
<Render
Expand Down

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@
.mtrVis__container {
text-align: center;
padding: $euiSize;
display: flex;
flex-direction: column;
}

.mtrVis__container--light {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import React from 'react';
import { shallow } from 'enzyme';
import { Datatable } from '../../../../expressions/common';
import MetricVisComponent, { MetricVisComponentProps } from './metric_component';
import { LabelPosition } from '../../common/constants';

jest.mock('../../../expression_metric/public/services', () => ({
getFormatService: () => {
Expand Down Expand Up @@ -54,6 +55,8 @@ describe('MetricVisComponent', function () {
},
labels: {
show: true,
style: { spec: {}, type: 'style', css: '' },
position: LabelPosition.BOTTOM,
},
},
dimensions: {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ class MetricVisComponent extends Component<MetricVisComponentProps> {
onFilter={
this.props.visParams.dimensions.bucket ? () => this.filterBucket(index) : undefined
}
showLabel={this.props.visParams.metric.labels.show}
labelConfig={this.props.visParams.metric.labels}
/>
);
};
Expand Down
Loading