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

[TSVB] Integrates the color service #93749

Merged
merged 22 commits into from
Mar 23, 2021
Merged
Changes from 1 commit
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
Prev Previous commit
Next Next commit
Fix unit tests
stratoula committed Mar 8, 2021

Verified

This commit was created on GitHub.com and signed with GitHub’s verified signature. The key has expired.
commit 1ca740ee97745e72a2aea2994fc0fd8dd138cd2d
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
/*
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
* or more contributor license agreements. Licensed under the Elastic License
* 2.0 and the Server Side Public License, v 1; you may not use this file except
* in compliance with, at your election, the Elastic License 2.0 or the Server
* Side Public License, v 1.
*/
import React from 'react';
import { mountWithIntl } from '@kbn/test/jest';
import { ReactWrapper } from 'enzyme';
import { PalettePicker, PalettePickerProps } from './palette_picker';
import { chartPluginMock } from '../../../../charts/public/mocks';
import { EuiColorPalettePicker } from '@elastic/eui';
import { PALETTES } from '../../../common/types';

describe('PalettePicker', function () {
let props: PalettePickerProps;
let component: ReactWrapper<PalettePickerProps>;

beforeAll(() => {
props = {
palettes: chartPluginMock.createPaletteRegistry(),
activePalette: {
type: 'palette',
name: 'kibana_palette',
},
setPalette: jest.fn(),
color: '#68BC00',
};
});

it('renders the EuiPalettePicker', () => {
component = mountWithIntl(<PalettePicker {...props} />);
expect(component.find(EuiColorPalettePicker).length).toBe(1);
});

it('renders the default palette if not activePalette is given', function () {
const { activePalette, ...newProps } = props;
component = mountWithIntl(<PalettePicker {...newProps} />);
const palettePicker = component.find(EuiColorPalettePicker);
expect(palettePicker.props().valueOfSelected).toBe('default');
});

it('renders the activePalette palette if given', function () {
component = mountWithIntl(<PalettePicker {...props} />);
const palettePicker = component.find(EuiColorPalettePicker);
expect(palettePicker.props().valueOfSelected).toBe('kibana_palette');
});

it('renders two additional palettes, rainbow and gradient', function () {
component = mountWithIntl(<PalettePicker {...props} />);
const palettePicker = component.find(EuiColorPalettePicker);
expect(palettePicker.props().palettes).toEqual(
expect.arrayContaining([
expect.objectContaining({
value: PALETTES.RAINBOW,
}),
expect.objectContaining({
value: PALETTES.GRADIENT,
}),
])
);
});
});
Original file line number Diff line number Diff line change
@@ -22,7 +22,7 @@ export interface PalettePickerProps {
}

export function PalettePicker({ activePalette, palettes, setPalette, color }: PalettePickerProps) {
const finalGradientColor = computeGradientFinalColor(color, 10);
const finalGradientColor = computeGradientFinalColor(color);

return (
<EuiColorPalettePicker
Original file line number Diff line number Diff line change
@@ -119,28 +119,8 @@ const TimeseriesSeriesUI = injectI18n(function (props) {
);
}

const handleColorChange = (val) => {
props.onChange(val);
// if (model.palette.name === 'gradient') {
// props.onChange({
// palette: {
// ...model.palette,
// params: {
// colors: [val.color, 'black'],
// gradient: true,
// },
// },
// });
// }
};

const colorPicker = (
<ColorPicker
disableTrash={true}
onChange={handleColorChange}
name="color"
value={model.color}
/>
<ColorPicker disableTrash={true} onChange={props.onChange} name="color" value={model.color} />
);

return (
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
/*
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
* or more contributor license agreements. Licensed under the Elastic License
* 2.0 and the Server Side Public License, v 1; you may not use this file except
* in compliance with, at your election, the Elastic License 2.0 or the Server
* Side Public License, v 1.
*/

import { computeGradientFinalColor } from './compute_gradient_final_color';

describe('computeGradientFinalColor Function', () => {
it('Should compute the gradient final color correctly for rgb color', () => {
const color = computeGradientFinalColor('rgba(211,96,134,1)');
expect(color).toEqual('rgb(145, 40, 75)');
});

it('Should compute the gradient final color correctly for hex color', () => {
const color = computeGradientFinalColor('#6092C0');
expect(color).toEqual('rgb(43, 77, 108)');
});
});
Original file line number Diff line number Diff line change
@@ -7,12 +7,10 @@
*/
import Color from 'color';

export const computeGradientFinalColor = (color: string, size: number): string => {
const inputColor = new Color(color);
let workingColor = Color.hsl(inputColor.hsl().object());
const rotateBy = inputColor.luminosity() / (size - 1);
const hsl = workingColor.hsl().object();
hsl.l -= rotateBy * 100 * (size - 1);
workingColor = Color.hsl(hsl);
return workingColor.rgb().toString();
export const computeGradientFinalColor = (color: string): string => {
let inputColor = new Color(color);
const hsl = inputColor.hsl().object();
hsl.l -= inputColor.luminosity() * 100;
inputColor = Color.hsl(hsl);
return inputColor.rgb().toString();
};
Original file line number Diff line number Diff line change
@@ -124,10 +124,7 @@ export const TimeSeries = ({
? 'custom'
: seriesById[0].palette.name;

const gradientFinalColor = computeGradientFinalColor(
seriesById[0].baseColor,
seriesById.length
);
const gradientFinalColor = computeGradientFinalColor(seriesById[0].baseColor);
const paletteParams =
seriesById[0].palette.name === PALETTES.GRADIENT
? {
Original file line number Diff line number Diff line change
@@ -223,118 +223,6 @@ describe('getSplits(resp, panel, series)', () => {
]);
});

describe('terms group bys', () => {
const resp = {
aggregations: {
SERIES: {
buckets: [
{
key: 'example-01',
timeseries: { buckets: [] },
SIBAGG: { value: 1 },
},
{
key: 'example-02',
timeseries: { buckets: [] },
SIBAGG: { value: 2 },
},
],
meta: { bucketSize: 10 },
},
},
};

test('should return a splits with no color', async () => {
const series = {
id: 'SERIES',
color: '#F00',
split_mode: 'terms',
terms_field: 'beat.hostname',
terms_size: 10,
metrics: [
{ id: 'AVG', type: 'avg', field: 'cpu' },
{ id: 'SIBAGG', type: 'avg_bucket', field: 'AVG' },
],
};
const panel = { type: 'timeseries' };

expect(await getSplits(resp, panel, series)).toEqual([
{
id: 'SERIES:example-01',
key: 'example-01',
label: 'example-01',
labelFormatted: '',
meta: { bucketSize: 10 },
color: undefined,
splitByLabel: 'Overall Average of Average of cpu',
timeseries: { buckets: [] },
SIBAGG: { value: 1 },
},
{
id: 'SERIES:example-02',
key: 'example-02',
label: 'example-02',
labelFormatted: '',
meta: { bucketSize: 10 },
color: undefined,
splitByLabel: 'Overall Average of Average of cpu',
timeseries: { buckets: [] },
SIBAGG: { value: 2 },
},
]);
});

test('should return gradient color', async () => {
const series = {
id: 'SERIES',
color: '#F00',
split_mode: 'terms',
split_color_mode: 'gradient',
terms_field: 'beat.hostname',
terms_size: 10,
metrics: [
{ id: 'AVG', type: 'avg', field: 'cpu' },
{ id: 'SIBAGG', type: 'avg_bucket', field: 'AVG' },
],
};
const panel = { type: 'timeseries' };

expect(await getSplits(resp, panel, series)).toEqual([
expect.objectContaining({
color: 'rgb(255, 0, 0)',
}),
expect.objectContaining({
color: 'rgb(147, 0, 0)',
}),
]);
});

test('should return rainbow color', async () => {
const series = {
id: 'SERIES',
color: '#F00',
split_mode: 'terms',
split_color_mode: 'rainbow',
terms_field: 'beat.hostname',
terms_size: 10,
metrics: [
{ id: 'AVG', type: 'avg', field: 'cpu' },
{ id: 'SIBAGG', type: 'avg_bucket', field: 'AVG' },
],
};
const panel = { type: 'timeseries' };

expect(await getSplits(resp, panel, series)).toEqual([
expect.objectContaining({
color: '#68BC00',
}),
expect.objectContaining({
color: '#009CE0',
}),
]);
});
});

test('should return a splits for filters group bys', async () => {
const resp = {
aggregations: {