Skip to content

Commit fa7ca30

Browse files
author
Tim Roes
authored
Optimize charts plugin (#78922) (#78962)
* Optimize charts plugin * Fix issues to keep same behavior * Remove dead import * Revert name change
1 parent b498778 commit fa7ca30

4 files changed

Lines changed: 15 additions & 9 deletions

File tree

src/plugins/charts/public/services/colors/color_palette.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,8 @@
1717
* under the License.
1818
*/
1919

20-
import d3 from 'd3';
2120
import _ from 'lodash';
21+
import { hsl } from 'color';
2222

2323
import { seedColors } from './seed_colors';
2424

@@ -49,7 +49,7 @@ const fraction = function (goal: number) {
4949
* If the number is greater than the length of seed colors available,
5050
* new colors are generated up to the value of the input number.
5151
*/
52-
export function createColorPalette(num?: any): string[] {
52+
export function createColorPalette(num: number): string[] {
5353
if (!_.isNumber(num)) {
5454
throw new TypeError('ColorPaletteUtilService expects a number');
5555
}
@@ -58,7 +58,7 @@ export function createColorPalette(num?: any): string[] {
5858
const seedLength = seedColors.length;
5959

6060
_.times(num - seedLength, function (i) {
61-
colors.push(d3.hsl((fraction(i + seedLength + 1) * 360 + offset) % 360, 0.5, 0.5).toString());
61+
colors.push(hsl((fraction(i + seedLength + 1) * 360 + offset) % 360, 0.5, 0.5).hex());
6262
});
6363

6464
return colors;

src/plugins/charts/public/services/colors/colors_palette.test.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,26 +37,32 @@ describe('Color Palette', () => {
3737

3838
it('should throw an error if input is not a number', () => {
3939
expect(() => {
40+
// @ts-expect-error
4041
createColorPalette(string);
4142
}).toThrowError();
4243

4344
expect(() => {
45+
// @ts-expect-error
4446
createColorPalette(bool);
4547
}).toThrowError();
4648

4749
expect(() => {
50+
// @ts-expect-error
4851
createColorPalette(nullValue);
4952
}).toThrowError();
5053

5154
expect(() => {
55+
// @ts-expect-error
5256
createColorPalette(emptyArr);
5357
}).toThrowError();
5458

5559
expect(() => {
60+
// @ts-expect-error
5661
createColorPalette(emptyObject);
5762
}).toThrowError();
5863

5964
expect(() => {
65+
// @ts-expect-error
6066
createColorPalette();
6167
}).toThrowError();
6268
});

src/plugins/charts/public/services/colors/mapped_colors.test.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
*/
1919

2020
import _ from 'lodash';
21-
import d3 from 'd3';
21+
import Color from 'color';
2222

2323
import { coreMock } from '../../../../../core/public/mocks';
2424
import { COLOR_MAPPING_SETTING } from '../../../common';
@@ -61,7 +61,7 @@ describe('Mapped Colors', () => {
6161
mappedColors.mapKeys(arr);
6262

6363
const colorValues = _(mappedColors.mapping).values();
64-
expect(colorValues.includes(seedColors[0])).toBe(false);
64+
expect(colorValues).not.toContain(seedColors[0]);
6565
expect(colorValues.uniq().size()).toBe(arr.length);
6666
});
6767

@@ -78,8 +78,8 @@ describe('Mapped Colors', () => {
7878
});
7979

8080
it('should treat different formats of colors as equal', () => {
81-
const color = d3.rgb(seedColors[0]);
82-
const rgb = `rgb(${color.r}, ${color.g}, ${color.b})`;
81+
const color = new Color(seedColors[0]);
82+
const rgb = `rgb(${color.red()}, ${color.green()}, ${color.blue()})`;
8383
const newConfig = { bar: rgb };
8484
config.set(COLOR_MAPPING_SETTING, newConfig);
8585

src/plugins/charts/public/services/colors/mapped_colors.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,14 +18,14 @@
1818
*/
1919

2020
import _ from 'lodash';
21-
import d3 from 'd3';
21+
import Color from 'color';
2222

2323
import { CoreSetup } from 'kibana/public';
2424

2525
import { COLOR_MAPPING_SETTING } from '../../../common';
2626
import { createColorPalette } from './color_palette';
2727

28-
const standardizeColor = (color: string) => d3.rgb(color).toString();
28+
const standardizeColor = (color: string) => new Color(color).hex().toLowerCase();
2929

3030
/**
3131
* Maintains a lookup table that associates the value (key) with a hex color (value)

0 commit comments

Comments
 (0)