Skip to content

Commit

Permalink
Make CategoricalColorScale instance a function and remove .toFunction…
Browse files Browse the repository at this point in the history
…() (#33)

* Feat: Use the recently added ExtensibleFunction to make an instance of CategoricalColorScale be a function
* BREAKING CHANGE: Remove categoricalColorScale.toFunction().
* BREAKING CHANGE: The color scale no longer convert input to lowercase before finding color.
* Fix: Also transform input value before setting color.
  • Loading branch information
kristw authored and zhaoyongjie committed Nov 26, 2021
1 parent ffcc272 commit af725ce
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 20 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import CategoricalColorScale from './CategoricalColorScale';
import getCategoricalSchemeRegistry from './CategoricalSchemeRegistrySingleton';
import stringifyAndTrim from './stringifyAndTrim';

export default class CategoricalColorNamespace {
constructor(name) {
Expand Down Expand Up @@ -31,7 +32,7 @@ export default class CategoricalColorNamespace {
* @param {*} forcedColor color
*/
setColor(value, forcedColor) {
this.forcedItems[value] = forcedColor;
this.forcedItems[stringifyAndTrim(value)] = forcedColor;

return this;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,27 +1,23 @@
export function cleanValue(value) {
// for superset series that should have the same color
return String(value)
.trim()
.toLowerCase();
}
import { ExtensibleFunction } from '@superset-ui/core';
import stringifyAndTrim from './stringifyAndTrim';

export default class CategoricalColorScale {
export default class CategoricalColorScale extends ExtensibleFunction {
/**
* Constructor
* @param {*} colors an array of colors
* @param {*} parentForcedColors optional parameter that comes from parent
* (usually CategoricalColorNamespace) and supersede this.forcedColors
*/
constructor(colors, parentForcedColors) {
super((...args) => this.getColor(...args));
this.colors = colors;
this.parentForcedColors = parentForcedColors;
this.forcedColors = {};
this.seen = {};
this.fn = value => this.getColor(value);
}

getColor(value) {
const cleanedValue = cleanValue(value);
const cleanedValue = stringifyAndTrim(value);

const parentColor = this.parentForcedColors && this.parentForcedColors[cleanedValue];
if (parentColor) {
Expand Down Expand Up @@ -51,12 +47,8 @@ export default class CategoricalColorScale {
* @param {*} forcedColor forcedColor
*/
setColor(value, forcedColor) {
this.forcedColors[value] = forcedColor;
this.forcedColors[stringifyAndTrim(value)] = forcedColor;

return this;
}

toFunction() {
return this.fn;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
/**
* Ensure value is a string
* @param {any} value
*/
export default function stringifyAndTrim(value) {
return String(value).trim();
}
Original file line number Diff line number Diff line change
Expand Up @@ -83,12 +83,11 @@ describe('CategoricalColorScale', () => {
expect(scale).toBe(output);
});
});
describe('.toFunction()', () => {
it('returns a function that wraps getColor', () => {
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']);
const colorFn = scale.toFunction();
expect(scale.getColor('pig')).toBe(colorFn('pig'));
expect(scale.getColor('cat')).toBe(colorFn('cat'));
expect(scale.getColor('pig')).toBe(scale('pig'));
expect(scale.getColor('cat')).toBe(scale('cat'));
});
});
});

0 comments on commit af725ce

Please sign in to comment.