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

[Lens] Make metric color fill the entire space #122091

Merged
merged 13 commits into from
Jan 18, 2022
Original file line number Diff line number Diff line change
Expand Up @@ -16,5 +16,6 @@

.lnsMetricExpression__title {
font-size: $euiSizeXL;
color: $euiTextColor;
}
}
46 changes: 26 additions & 20 deletions x-pack/plugins/lens/public/metric_visualization/expression.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
import { MetricChart } from './expression';
import { MetricConfig, metricChart } from '../../common/expressions';
import React from 'react';
import { shallow } from 'enzyme';
import { shallow, mount } from 'enzyme';
import { createMockExecutionContext } from '../../../../../src/plugins/expressions/common/mocks';
import type { IFieldFormat } from '../../../../../src/plugins/field_formats/common';
import { layerTypes } from '../../common';
Expand Down Expand Up @@ -94,14 +94,14 @@ describe('metric_expression', () => {
).toMatchInlineSnapshot(`
<VisualizationContainer
className="lnsMetricExpression__container"
style={Object {}}
>
<AutoScale
key="3"
>
<div
className="lnsMetricExpression__value"
data-test-subj="lns_metric_value"
style={Object {}}
>
3
</div>
Expand Down Expand Up @@ -132,14 +132,14 @@ describe('metric_expression', () => {
).toMatchInlineSnapshot(`
<VisualizationContainer
className="lnsMetricExpression__container"
style={Object {}}
>
<AutoScale
key="last"
>
<div
className="lnsMetricExpression__value"
data-test-subj="lns_metric_value"
style={Object {}}
>
last
</div>
Expand Down Expand Up @@ -169,14 +169,14 @@ describe('metric_expression', () => {
).toMatchInlineSnapshot(`
<VisualizationContainer
className="lnsMetricExpression__container"
style={Object {}}
>
<AutoScale
key="3"
>
<div
className="lnsMetricExpression__value"
data-test-subj="lns_metric_value"
style={Object {}}
>
3
</div>
Expand Down Expand Up @@ -206,14 +206,14 @@ describe('metric_expression', () => {
).toMatchInlineSnapshot(`
<VisualizationContainer
className="lnsMetricExpression__container"
style={Object {}}
>
<AutoScale
key="3"
>
<div
className="lnsMetricExpression__value"
data-test-subj="lns_metric_value"
style={Object {}}
>
3
</div>
Expand Down Expand Up @@ -287,14 +287,14 @@ describe('metric_expression', () => {
).toMatchInlineSnapshot(`
<VisualizationContainer
className="lnsMetricExpression__container"
style={Object {}}
>
<AutoScale
key="0"
>
<div
className="lnsMetricExpression__value"
data-test-subj="lns_metric_value"
style={Object {}}
>
0
</div>
Expand Down Expand Up @@ -337,7 +337,7 @@ describe('metric_expression', () => {
colors: ['red', 'yellow', 'green'],
};

const instance = shallow(
const instance = mount(
<MetricChart
data={data}
args={args}
Expand All @@ -346,7 +346,9 @@ describe('metric_expression', () => {
/>
);

expect(instance.find('[data-test-subj="lns_metric_value"]').first().prop('style')).toEqual(
expect(
instance.find('[data-test-subj="lnsVisualizationContainer"]').first().prop('style')
).toEqual(
expect.objectContaining({
color: 'red',
})
Expand All @@ -368,7 +370,7 @@ describe('metric_expression', () => {
continuity: 'above',
};

const instance = shallow(
const instance = mount(
<MetricChart
data={data}
args={args}
Expand All @@ -378,7 +380,7 @@ describe('metric_expression', () => {
);

expect(
instance.find('[data-test-subj="lns_metric_value"]').first().prop('style')
instance.find('[data-test-subj="lnsVisualizationContainer"]').first().prop('style')
).not.toEqual(
expect.objectContaining({
color: expect.any(String),
Expand All @@ -400,7 +402,7 @@ describe('metric_expression', () => {
continuity: 'below',
};

const instance = shallow(
const instance = mount(
<MetricChart
data={data}
args={args}
Expand All @@ -410,7 +412,7 @@ describe('metric_expression', () => {
);

expect(
instance.find('[data-test-subj="lns_metric_value"]').first().prop('style')
instance.find('[data-test-subj="lnsVisualizationContainer"]').first().prop('style')
).not.toEqual(
expect.objectContaining({
color: expect.any(String),
Expand All @@ -433,7 +435,7 @@ describe('metric_expression', () => {
continuity: 'none',
};

const instance = shallow(
const instance = mount(
<MetricChart
data={data}
args={args}
Expand All @@ -443,7 +445,7 @@ describe('metric_expression', () => {
);

expect(
instance.find('[data-test-subj="lns_metric_value"]').first().prop('style')
instance.find('[data-test-subj="lnsVisualizationContainer"]').first().prop('style')
).not.toEqual(
expect.objectContaining({
color: expect.any(String),
Expand All @@ -466,7 +468,7 @@ describe('metric_expression', () => {
continuity: 'none',
};

const instance = shallow(
const instance = mount(
<MetricChart
data={data}
args={args}
Expand All @@ -476,7 +478,7 @@ describe('metric_expression', () => {
);

expect(
instance.find('[data-test-subj="lns_metric_value"]').first().prop('style')
instance.find('[data-test-subj="lnsVisualizationContainer"]').first().prop('style')
).not.toEqual(
expect.objectContaining({
color: expect.any(String),
Expand All @@ -499,7 +501,7 @@ describe('metric_expression', () => {
continuity: 'all',
};

const instance = shallow(
const instance = mount(
<MetricChart
data={data}
args={args}
Expand All @@ -508,7 +510,9 @@ describe('metric_expression', () => {
/>
);

expect(instance.find('[data-test-subj="lns_metric_value"]').first().prop('style')).toEqual(
expect(
instance.find('[data-test-subj="lnsVisualizationContainer"]').first().prop('style')
).toEqual(
expect.objectContaining({
color: 'green',
})
Expand All @@ -530,7 +534,7 @@ describe('metric_expression', () => {
continuity: 'all',
};

const instance = shallow(
const instance = mount(
<MetricChart
data={data}
args={args}
Expand All @@ -539,7 +543,9 @@ describe('metric_expression', () => {
/>
);

expect(instance.find('[data-test-subj="lns_metric_value"]').first().prop('style')).toEqual(
expect(
instance.find('[data-test-subj="lnsVisualizationContainer"]').first().prop('style')
).toEqual(
expect.objectContaining({
color: 'red',
})
Expand Down
13 changes: 9 additions & 4 deletions x-pack/plugins/lens/public/metric_visualization/expression.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,8 @@ function getColorStyling(
[cssProp]: color,
};
if (colorMode === ColorMode.Background && color) {
styling.color = getContrastColor(color, isDarkTheme);
// set to "euiTextColor" for both light and dark color, depending on the theme
styling.color = getContrastColor(color, isDarkTheme, 'euiTextColor', 'euiTextColor');
}
return styling;
}
Expand Down Expand Up @@ -143,13 +144,17 @@ export function MetricChart({
const color = getColorStyling(rawValue, colorMode, palette, uiSettings.get('theme:darkMode'));

return (
<VisualizationContainer className="lnsMetricExpression__container">
<VisualizationContainer className="lnsMetricExpression__container" style={color}>
<AutoScale key={value}>
<div data-test-subj="lns_metric_value" className="lnsMetricExpression__value" style={color}>
<div data-test-subj="lns_metric_value" className="lnsMetricExpression__value">
{value}
</div>
{mode === 'full' && (
<div data-test-subj="lns_metric_title" className="lnsMetricExpression__title">
<div
data-test-subj="lns_metric_title"
className="lnsMetricExpression__title"
style={colorMode === ColorMode.Background ? color : undefined}
>
{metricTitle}
</div>
)}
Expand Down
14 changes: 11 additions & 3 deletions x-pack/plugins/lens/public/shared_components/coloring/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -293,9 +293,17 @@ export function getColorStops(
return freshColorStops;
}

export function getContrastColor(color: string, isDarkTheme: boolean) {
const darkColor = isDarkTheme ? euiDarkVars.euiColorInk : euiLightVars.euiColorInk;
const lightColor = isDarkTheme ? euiDarkVars.euiColorGhost : euiLightVars.euiColorGhost;
export function getContrastColor(
color: string,
isDarkTheme: boolean,
darkTextProp: 'euiColorInk' | 'euiTextColor' = 'euiColorInk',
lightTextProp: 'euiColorGhost' | 'euiTextColor' = 'euiColorGhost'
) {
// when in light theme both text color and colorInk are dark and the choice
// may depends on the specific context.
const darkColor = isDarkTheme ? euiDarkVars.euiColorInk : euiLightVars[darkTextProp];
// Same thing for light color in dark theme
const lightColor = isDarkTheme ? euiDarkVars[lightTextProp] : euiLightVars.euiColorGhost;
const backgroundColor = isDarkTheme
? euiDarkVars.euiPageBackgroundColor
: euiLightVars.euiPageBackgroundColor;
Expand Down
2 changes: 1 addition & 1 deletion x-pack/test/functional/page_objects/lens_page.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1020,7 +1020,7 @@ export function LensPageProvider({ getService, getPageObjects }: FtrProviderCont
},

async getMetricStyle() {
const el = await testSubjects.find('lns_metric_value');
const el = await testSubjects.find('lnsVisualizationContainer');
const styleString = await el.getAttribute('style');
return styleString.split(';').reduce<Record<string, string>>((memo, cssLine) => {
const [prop, value] = cssLine.split(':');
Expand Down