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 4 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

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 @@ -35,6 +35,8 @@ describe('interpreter/functions#metric', () => {
},
showLabels: true,
font: { spec: { fontSize: '60px' }, type: 'style', css: '' },
labelFont: { spec: { fontSize: '24px' }, type: 'style', css: '' },
labelPosition: 'bottom',
metric: [
{
type: 'vis_dimension',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,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: ['bottom', 'top'],
help: i18n.translate('expressionMetricVis.function.labelPosition.help', {
defaultMessage: 'Label position',
}),
default: `bottom`,
},
metric: {
types: ['vis_dimension'],
help: i18n.translate('expressionMetricVis.function.metric.help', {
Expand Down Expand Up @@ -118,6 +133,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 @@ -23,6 +23,8 @@ export interface MetricArguments {
showLabels: boolean;
palette?: PaletteOutput<CustomPaletteState>;
font: Style;
labelFont: Style;
labelPosition: 'bottom' | 'top';
VladLasitsa marked this conversation as resolved.
Show resolved Hide resolved
metric: ExpressionValueVisDimension[];
bucket?: ExpressionValueVisDimension;
autoScale?: boolean;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ export interface MetricVisParam {
percentageFormatPattern?: string;
metricColorMode: ColorMode;
palette?: CustomPaletteState;
labels: Labels;
labels: Labels & { style: Style; position: 'bottom' | 'top' };
VladLasitsa marked this conversation as resolved.
Show resolved Hide resolved
style: MetricStyle;
autoScale?: boolean;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ const config: MetricVisRenderConfig = {
visConfig: {
metric: {
metricColorMode: ColorMode.None,
labels: { show: true },
labels: { show: true, style: { spec: {}, type: 'style', css: '' }, position: 'bottom' },
percentageMode: false,
style,
},
Expand Down Expand Up @@ -132,7 +132,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: 'bottom',
},
},
},
}}
{...containerSize}
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 @@ -54,6 +54,8 @@ describe('MetricVisComponent', function () {
},
labels: {
show: true,
style: { spec: {}, type: 'style', css: '' },
position: '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
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import React from 'react';
import { shallow } from 'enzyme';

import { MetricVisValue } from './metric_value';
import { MetricOptions, MetricStyle } from '../../common/types';
import { MetricOptions, MetricStyle, VisParams } from '../../common/types';

const baseMetric: MetricOptions = { label: 'Foo', value: 'foo', lightText: false };
const font: MetricStyle = {
Expand All @@ -24,38 +24,63 @@ const font: MetricStyle = {
/* stylelint-enable */
};

const labelConfig: VisParams['metric']['labels'] = {
show: true,
position: 'bottom',
style: { spec: {}, type: 'style', css: '' },
};

describe('MetricVisValue', () => {
it('should be wrapped in button if having a click listener', () => {
const component = shallow(
<MetricVisValue style={font} metric={baseMetric} onFilter={() => {}} />
<MetricVisValue
style={font}
metric={baseMetric}
onFilter={() => {}}
labelConfig={labelConfig}
/>
);
expect(component.find('button').exists()).toBe(true);
});

it('should not be wrapped in button without having a click listener', () => {
const component = shallow(<MetricVisValue style={font} metric={baseMetric} />);
const component = shallow(
<MetricVisValue style={font} metric={baseMetric} labelConfig={labelConfig} />
);
expect(component.find('button').exists()).toBe(false);
});

it('should add -isfilterable class if onFilter is provided', () => {
const onFilter = jest.fn();
const component = shallow(
<MetricVisValue style={font} metric={baseMetric} onFilter={onFilter} />
<MetricVisValue
style={font}
metric={baseMetric}
onFilter={onFilter}
labelConfig={labelConfig}
/>
);
component.simulate('click');
expect(component.find('.mtrVis__container-isfilterable')).toHaveLength(1);
});

it('should not add -isfilterable class if onFilter is not provided', () => {
const component = shallow(<MetricVisValue style={font} metric={baseMetric} />);
const component = shallow(
<MetricVisValue style={font} metric={baseMetric} labelConfig={labelConfig} />
);
component.simulate('click');
expect(component.find('.mtrVis__container-isfilterable')).toHaveLength(0);
});

it('should call onFilter callback if provided', () => {
const onFilter = jest.fn();
const component = shallow(
<MetricVisValue style={font} metric={baseMetric} onFilter={onFilter} />
<MetricVisValue
style={font}
metric={baseMetric}
onFilter={onFilter}
labelConfig={labelConfig}
/>
);
component.simulate('click');
expect(onFilter).toHaveBeenCalled();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,16 +8,16 @@

import React, { CSSProperties } from 'react';
import classNames from 'classnames';
import type { MetricOptions, MetricStyle } from '../../common/types';
import type { MetricOptions, MetricStyle, MetricVisParam } from '../../common/types';

interface MetricVisValueProps {
metric: MetricOptions;
onFilter?: () => void;
showLabel?: boolean;
style: MetricStyle;
labelConfig: MetricVisParam['labels'];
}

export const MetricVisValue = ({ style, metric, onFilter, showLabel }: MetricVisValueProps) => {
export const MetricVisValue = ({ style, metric, onFilter, labelConfig }: MetricVisValueProps) => {
const containerClassName = classNames('mtrVis__container', {
// eslint-disable-next-line @typescript-eslint/naming-convention
'mtrVis__container--light': metric.lightText,
Expand All @@ -43,7 +43,16 @@ export const MetricVisValue = ({ style, metric, onFilter, showLabel }: MetricVis
*/
dangerouslySetInnerHTML={{ __html: metric.value }} // eslint-disable-line react/no-danger
/>
{showLabel && <div>{metric.label}</div>}
{labelConfig.show && (
<div
style={{
...(labelConfig.style.spec as CSSProperties),
order: labelConfig.position === 'top' ? -1 : 2,
}}
>
{metric.label}
</div>
)}
</div>
);

Expand Down
Original file line number Diff line number Diff line change
@@ -1 +1 @@
{"as":"metricVis","type":"render","value":{"visConfig":{"dimensions":{"bucket":{"accessor":0,"format":{"id":"string","params":{}},"type":"vis_dimension"},"metrics":[{"accessor":1,"format":{"id":"number","params":{}},"type":"vis_dimension"}]},"metric":{"autoScale":null,"labels":{"show":true},"metricColorMode":"None","palette":null,"percentageMode":false,"style":{"bgColor":false,"css":"font-family:'Open Sans', Helvetica, Arial, sans-serif;font-weight:normal;font-style:normal;text-decoration:none;text-align:center;font-size:60px;line-height:1","labelColor":false,"spec":{"fontFamily":"'Open Sans', Helvetica, Arial, sans-serif","fontSize":"60px","fontStyle":"normal","fontWeight":"normal","lineHeight":"1","textAlign":"center","textDecoration":"none"},"type":"style"}}},"visData":{"columns":[{"id":"col-0-2","meta":{"field":"response.raw","index":"logstash-*","params":{"id":"terms","params":{"id":"string","missingBucketLabel":"Missing","otherBucketLabel":"Other"}},"source":"esaggs","sourceParams":{"appliedTimeRange":null,"enabled":true,"hasPrecisionError":false,"id":"2","indexPatternId":"logstash-*","params":{"field":"response.raw","missingBucket":false,"missingBucketLabel":"Missing","order":"desc","orderBy":"1","otherBucket":false,"otherBucketLabel":"Other","size":4},"schema":"segment","type":"terms"},"type":"string"},"name":"response.raw: Descending"},{"id":"col-1-1","meta":{"field":null,"index":"logstash-*","params":{"id":"number"},"source":"esaggs","sourceParams":{"appliedTimeRange":null,"enabled":true,"hasPrecisionError":false,"id":"1","indexPatternId":"logstash-*","params":{},"schema":"metric","type":"count"},"type":"number"},"name":"Count"}],"rows":[{"col-0-2":"200","col-1-1":12891},{"col-0-2":"404","col-1-1":696},{"col-0-2":"503","col-1-1":417}],"type":"datatable"},"visType":"metric"}}
{"as":"metricVis","type":"render","value":{"visConfig":{"dimensions":{"bucket":{"accessor":0,"format":{"id":"string","params":{}},"type":"vis_dimension"},"metrics":[{"accessor":1,"format":{"id":"number","params":{}},"type":"vis_dimension"}]},"metric":{"autoScale":null,"labels":{"position":"bottom","show":true,"style":{"css":"font-family:'Open Sans', Helvetica, Arial, sans-serif;font-weight:normal;font-style:normal;text-decoration:none;text-align:center;font-size:24px;line-height:1","spec":{"fontFamily":"'Open Sans', Helvetica, Arial, sans-serif","fontSize":"24px","fontStyle":"normal","fontWeight":"normal","lineHeight":"1","textAlign":"center","textDecoration":"none"},"type":"style"}},"metricColorMode":"None","palette":null,"percentageMode":false,"style":{"bgColor":false,"css":"font-family:'Open Sans', Helvetica, Arial, sans-serif;font-weight:normal;font-style:normal;text-decoration:none;text-align:center;font-size:60px;line-height:1","labelColor":false,"spec":{"fontFamily":"'Open Sans', Helvetica, Arial, sans-serif","fontSize":"60px","fontStyle":"normal","fontWeight":"normal","lineHeight":"1","textAlign":"center","textDecoration":"none"},"type":"style"}}},"visData":{"columns":[{"id":"col-0-2","meta":{"field":"response.raw","index":"logstash-*","params":{"id":"terms","params":{"id":"string","missingBucketLabel":"Missing","otherBucketLabel":"Other"}},"source":"esaggs","sourceParams":{"appliedTimeRange":null,"enabled":true,"hasPrecisionError":false,"id":"2","indexPatternId":"logstash-*","params":{"field":"response.raw","missingBucket":false,"missingBucketLabel":"Missing","order":"desc","orderBy":"1","otherBucket":false,"otherBucketLabel":"Other","size":4},"schema":"segment","type":"terms"},"type":"string"},"name":"response.raw: Descending"},{"id":"col-1-1","meta":{"field":null,"index":"logstash-*","params":{"id":"number"},"source":"esaggs","sourceParams":{"appliedTimeRange":null,"enabled":true,"hasPrecisionError":false,"id":"1","indexPatternId":"logstash-*","params":{},"schema":"metric","type":"count"},"type":"number"},"name":"Count"}],"rows":[{"col-0-2":"200","col-1-1":12891},{"col-0-2":"404","col-1-1":696},{"col-0-2":"503","col-1-1":417}],"type":"datatable"},"visType":"metric"}}
Original file line number Diff line number Diff line change
@@ -1 +1 @@
{"as":"metricVis","type":"render","value":{"visConfig":{"dimensions":{"bucket":{"accessor":0,"format":{"id":"string","params":{}},"type":"vis_dimension"},"metrics":[{"accessor":1,"format":{"id":"number","params":{}},"type":"vis_dimension"}]},"metric":{"autoScale":null,"labels":{"show":true},"metricColorMode":"None","palette":null,"percentageMode":false,"style":{"bgColor":false,"css":"font-family:'Open Sans', Helvetica, Arial, sans-serif;font-weight:normal;font-style:normal;text-decoration:none;text-align:center;font-size:60px;line-height:1","labelColor":false,"spec":{"fontFamily":"'Open Sans', Helvetica, Arial, sans-serif","fontSize":"60px","fontStyle":"normal","fontWeight":"normal","lineHeight":"1","textAlign":"center","textDecoration":"none"},"type":"style"}}},"visData":{"columns":[{"id":"col-0-2","meta":{"field":"response.raw","index":"logstash-*","params":{"id":"terms","params":{"id":"string","missingBucketLabel":"Missing","otherBucketLabel":"Other"}},"source":"esaggs","sourceParams":{"appliedTimeRange":null,"enabled":true,"hasPrecisionError":false,"id":"2","indexPatternId":"logstash-*","params":{"field":"response.raw","missingBucket":false,"missingBucketLabel":"Missing","order":"desc","orderBy":"1","otherBucket":false,"otherBucketLabel":"Other","size":4},"schema":"segment","type":"terms"},"type":"string"},"name":"response.raw: Descending"},{"id":"col-1-1","meta":{"field":null,"index":"logstash-*","params":{"id":"number"},"source":"esaggs","sourceParams":{"appliedTimeRange":null,"enabled":true,"hasPrecisionError":false,"id":"1","indexPatternId":"logstash-*","params":{},"schema":"metric","type":"count"},"type":"number"},"name":"Count"}],"rows":[{"col-0-2":"200","col-1-1":12891},{"col-0-2":"404","col-1-1":696},{"col-0-2":"503","col-1-1":417}],"type":"datatable"},"visType":"metric"}}
{"as":"metricVis","type":"render","value":{"visConfig":{"dimensions":{"bucket":{"accessor":0,"format":{"id":"string","params":{}},"type":"vis_dimension"},"metrics":[{"accessor":1,"format":{"id":"number","params":{}},"type":"vis_dimension"}]},"metric":{"autoScale":null,"labels":{"position":"bottom","show":true,"style":{"css":"font-family:'Open Sans', Helvetica, Arial, sans-serif;font-weight:normal;font-style:normal;text-decoration:none;text-align:center;font-size:24px;line-height:1","spec":{"fontFamily":"'Open Sans', Helvetica, Arial, sans-serif","fontSize":"24px","fontStyle":"normal","fontWeight":"normal","lineHeight":"1","textAlign":"center","textDecoration":"none"},"type":"style"}},"metricColorMode":"None","palette":null,"percentageMode":false,"style":{"bgColor":false,"css":"font-family:'Open Sans', Helvetica, Arial, sans-serif;font-weight:normal;font-style:normal;text-decoration:none;text-align:center;font-size:60px;line-height:1","labelColor":false,"spec":{"fontFamily":"'Open Sans', Helvetica, Arial, sans-serif","fontSize":"60px","fontStyle":"normal","fontWeight":"normal","lineHeight":"1","textAlign":"center","textDecoration":"none"},"type":"style"}}},"visData":{"columns":[{"id":"col-0-2","meta":{"field":"response.raw","index":"logstash-*","params":{"id":"terms","params":{"id":"string","missingBucketLabel":"Missing","otherBucketLabel":"Other"}},"source":"esaggs","sourceParams":{"appliedTimeRange":null,"enabled":true,"hasPrecisionError":false,"id":"2","indexPatternId":"logstash-*","params":{"field":"response.raw","missingBucket":false,"missingBucketLabel":"Missing","order":"desc","orderBy":"1","otherBucket":false,"otherBucketLabel":"Other","size":4},"schema":"segment","type":"terms"},"type":"string"},"name":"response.raw: Descending"},{"id":"col-1-1","meta":{"field":null,"index":"logstash-*","params":{"id":"number"},"source":"esaggs","sourceParams":{"appliedTimeRange":null,"enabled":true,"hasPrecisionError":false,"id":"1","indexPatternId":"logstash-*","params":{},"schema":"metric","type":"count"},"type":"number"},"name":"Count"}],"rows":[{"col-0-2":"200","col-1-1":12891},{"col-0-2":"404","col-1-1":696},{"col-0-2":"503","col-1-1":417}],"type":"datatable"},"visType":"metric"}}
Loading