Skip to content
This repository has been archived by the owner on Dec 10, 2021. It is now read-only.

fix: Color consistency #1406

Merged
merged 12 commits into from
Nov 2, 2021
Original file line number Diff line number Diff line change
Expand Up @@ -464,19 +464,6 @@ const color_scheme: SharedControlConfig<'ColorSchemeControl'> = {
schemes: () => categoricalSchemeRegistry.getMap(),
};

const label_colors: SharedControlConfig<'ColorMapControl'> = {
type: 'ColorMapControl',
label: t('Color Map'),
default: {},
renderTrigger: true,
mapStateToProps: ({
form_data: { color_namespace: colorNamespace, color_scheme: colorScheme },
}) => ({
colorNamespace,
colorScheme,
}),
};

const enableExploreDnd = isFeatureEnabled(FeatureFlag.ENABLE_EXPLORE_DRAG_AND_DROP);

const sharedControls = {
Expand Down Expand Up @@ -508,7 +495,6 @@ const sharedControls = {
x_axis_time_format,
adhoc_filters: enableExploreDnd ? dnd_adhoc_filters : adhoc_filters,
color_scheme,
label_colors,
series_columns: enableExploreDnd ? dndColumnsControl : columnsControl,
series_limit,
series_limit_metric: enableExploreDnd ? dnd_sort_by : sort_by,
Expand Down
1 change: 0 additions & 1 deletion packages/superset-ui-chart-controls/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,6 @@ export type InternalControlType =
| 'BoundsControl'
| 'CheckboxControl'
| 'CollectionControl'
| 'ColorMapControl'
| 'ColorPickerControl'
| 'ColorSchemeControl'
| 'DatasourceControl'
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,14 +20,9 @@ export default class CategoricalColorNamespace {

getScale(schemeId?: string) {
const id = schemeId ?? getCategoricalSchemeRegistry().getDefaultKey() ?? '';
const scale = this.scales[id];
if (scale) {
return scale;
}
const scheme = getCategoricalSchemeRegistry().get(id);

const newScale = new CategoricalColorScale(scheme?.colors ?? [], this.forcedItems);
this.scales[id] = newScale;
const forcedColors = this.forcedItems;
const newScale = new CategoricalColorScale(scheme?.colors ?? [], forcedColors);
geido marked this conversation as resolved.
Show resolved Hide resolved

return newScale;
}
Expand All @@ -44,6 +39,10 @@ export default class CategoricalColorNamespace {

return this;
}

resetColors() {
this.forcedItems = {};
}
}

const namespaces: {
Expand All @@ -67,6 +66,10 @@ export function getColor(value?: string, schemeId?: string, namespace?: string)
return getNamespace(namespace).getScale(schemeId).getColor(value);
}

/*
Returns a new scale instance within the same namespace.
Especially useful when a chart is booting for the first time
*/
export function getScale(scheme?: string, namespace?: string) {
return getNamespace(namespace).getScale(scheme);
}
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,6 @@ class CategoricalColorScale extends ExtensibleFunction {

getColor(value?: string) {
const cleanedValue = stringifyAndTrim(value);

const parentColor = this.parentForcedColors && this.parentForcedColors[cleanedValue];
if (parentColor) {
return parentColor;
Expand All @@ -59,7 +58,6 @@ class CategoricalColorScale extends ExtensibleFunction {
*/
setColor(value: string, forcedColor: string) {
this.forcedColors[stringifyAndTrim(value)] = forcedColor;

return this;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,15 +62,6 @@ describe('CategoricalColorNamespace', () => {
expect(scale).toBeDefined();
getCategoricalSchemeRegistry().setDefaultKey('testColors');
});
it('returns same scale if the scale with that name already exists in this namespace', () => {
geido marked this conversation as resolved.
Show resolved Hide resolved
const namespace = getNamespace('test-get-scale2');
const scale1 = namespace.getScale('testColors');
const scale2 = namespace.getScale('testColors2');
const scale3 = namespace.getScale('testColors2');
const scale4 = namespace.getScale('testColors');
expect(scale1).toBe(scale4);
expect(scale2).toBe(scale3);
geido marked this conversation as resolved.
Show resolved Hide resolved
});
});
describe('.setColor()', () => {
it('overwrites color for all CategoricalColorScales in this namespace', () => {
Expand Down Expand Up @@ -108,19 +99,15 @@ describe('CategoricalColorNamespace', () => {
const scale = getScale();
expect(scale).toBeDefined();
const scale2 = getNamespace().getScale();
expect(scale).toBe(scale2);
expect(scale2).toBeDefined();
});
it('getScale(scheme) returns a CategoricalColorScale with specified scheme in default namespace', () => {
const scale = getScale('testColors');
const scale = getNamespace().getScale('testColors');
expect(scale).toBeDefined();
const scale2 = getNamespace().getScale('testColors');
expect(scale).toBe(scale2);
});
it('getScale(scheme, namespace) returns a CategoricalColorScale with specified scheme in specified namespace', () => {
const scale = getScale('testColors', 'test-getScale');
const scale = getNamespace('test-getScale').getScale('testColors');
expect(scale).toBeDefined();
const scale2 = getNamespace('test-getScale').getScale('testColors');
expect(scale).toBe(scale2);
});
});
describe('static getColor()', () => {
Expand Down