Skip to content
This repository was archived by the owner on Dec 10, 2021. It is now read-only.
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
18 changes: 18 additions & 0 deletions packages/superset-ui-color/src/CategoricalColorScale.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,4 +57,22 @@ export default class CategoricalColorScale extends ExtensibleFunction {

return this;
}

/**
* Get a mapping of data values to colors
* @returns an object where the key is the data value and the value is the hex color code
*/
getColorMap() {
const colorMap: { [key: string]: string } = {};
const { length } = this.colors;
Object.keys(this.seen).forEach(value => {
colorMap[value] = this.colors[this.seen[value] % length];
});

return {
...colorMap,
...this.forcedColors,
...this.parentForcedColors,
};
}
}
16 changes: 16 additions & 0 deletions packages/superset-ui-color/test/CategoricalColorScale.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,22 @@ describe('CategoricalColorScale', () => {
expect(scale).toBe(output);
});
});
describe('.getColorMap()', () => {
it('returns correct mapping and parentForcedColors and forcedColors are specified', () => {
const scale1 = new CategoricalColorScale(['blue', 'red', 'green']);
scale1.setColor('cow', 'black');
const scale2 = new CategoricalColorScale(['blue', 'red', 'green'], scale1.forcedColors);
scale2.setColor('pig', 'pink');
scale2.getColor('cow');
scale2.getColor('pig');
scale2.getColor('horse');
expect(scale2.getColorMap()).toEqual({
cow: 'black',
pig: 'pink',
horse: 'blue',
});
});
});
describe('a CategoricalColorScale instance is also a color function itself', () => {
it('scale(value) returns color similar to calling scale.getColor(value)', () => {
const scale = new CategoricalColorScale(['blue', 'red', 'green']);
Expand Down