mirror of
https://github.com/apache/superset.git
synced 2026-04-18 23:55:00 +00:00
fix: Color consistency (#1406)
* Replace color in scheme statically * Set color statically and re-use instance * Fix tests and clean up * Improve comments * Refactor and simplify * Update tests * Remove unnecessary ColorMapControl * Remove unnecessary const * Remove control label_colors
This commit is contained in:
@@ -104,7 +104,7 @@ export const datasourceAndVizType: ControlPanelSectionConfig = {
|
||||
|
||||
export const colorScheme: ControlPanelSectionConfig = {
|
||||
label: t('Color Scheme'),
|
||||
controlSetRows: [['color_scheme', 'label_colors']],
|
||||
controlSetRows: [['color_scheme']],
|
||||
};
|
||||
|
||||
export const annotations: ControlPanelSectionConfig = {
|
||||
|
||||
@@ -478,19 +478,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 = {
|
||||
@@ -522,7 +509,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,
|
||||
|
||||
@@ -117,7 +117,6 @@ export type InternalControlType =
|
||||
| 'BoundsControl'
|
||||
| 'CheckboxControl'
|
||||
| 'CollectionControl'
|
||||
| 'ColorMapControl'
|
||||
| 'ColorPickerControl'
|
||||
| 'ColorSchemeControl'
|
||||
| 'DatasourceControl'
|
||||
|
||||
@@ -39,14 +39,8 @@ 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;
|
||||
|
||||
return newScale;
|
||||
}
|
||||
@@ -63,6 +57,10 @@ export default class CategoricalColorNamespace {
|
||||
|
||||
return this;
|
||||
}
|
||||
|
||||
resetColors() {
|
||||
this.forcedItems = {};
|
||||
}
|
||||
}
|
||||
|
||||
const namespaces: {
|
||||
@@ -86,6 +84,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);
|
||||
}
|
||||
|
||||
@@ -57,7 +57,6 @@ class CategoricalColorScale extends ExtensibleFunction {
|
||||
|
||||
getColor(value?: string) {
|
||||
const cleanedValue = stringifyAndTrim(value);
|
||||
|
||||
const parentColor = this.parentForcedColors && this.parentForcedColors[cleanedValue];
|
||||
if (parentColor) {
|
||||
return parentColor;
|
||||
@@ -78,7 +77,6 @@ class CategoricalColorScale extends ExtensibleFunction {
|
||||
*/
|
||||
setColor(value: string, forcedColor: string) {
|
||||
this.forcedColors[stringifyAndTrim(value)] = forcedColor;
|
||||
|
||||
return this;
|
||||
}
|
||||
|
||||
|
||||
@@ -81,15 +81,6 @@ describe('CategoricalColorNamespace', () => {
|
||||
expect(scale).toBeDefined();
|
||||
getCategoricalSchemeRegistry().setDefaultKey('testColors');
|
||||
});
|
||||
it('returns same scale if the scale with that name already exists in this namespace', () => {
|
||||
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);
|
||||
});
|
||||
});
|
||||
describe('.setColor()', () => {
|
||||
it('overwrites color for all CategoricalColorScales in this namespace', () => {
|
||||
@@ -127,19 +118,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()', () => {
|
||||
|
||||
@@ -46,10 +46,7 @@ const config: ControlPanelConfig = {
|
||||
{
|
||||
label: t('Chart Options'),
|
||||
expanded: true,
|
||||
controlSetRows: [
|
||||
['y_axis_format', null],
|
||||
['color_scheme', 'label_colors'],
|
||||
],
|
||||
controlSetRows: [['y_axis_format', null], ['color_scheme']],
|
||||
},
|
||||
],
|
||||
controlOverrides: {
|
||||
|
||||
@@ -70,7 +70,7 @@ const config: ControlPanelConfig = {
|
||||
label: t('Chart Options'),
|
||||
expanded: true,
|
||||
controlSetRows: [
|
||||
['color_scheme', 'label_colors'],
|
||||
['color_scheme'],
|
||||
[
|
||||
{
|
||||
name: 'link_length',
|
||||
|
||||
@@ -131,7 +131,7 @@ const config: ControlPanelConfig = {
|
||||
expanded: true,
|
||||
tabOverride: 'customize',
|
||||
controlSetRows: [
|
||||
['color_scheme', 'label_colors'],
|
||||
['color_scheme'],
|
||||
[
|
||||
{
|
||||
name: 'number_format',
|
||||
|
||||
@@ -65,7 +65,7 @@ const config: ControlPanelConfig = {
|
||||
label: t('Chart Options'),
|
||||
expanded: true,
|
||||
controlSetRows: [
|
||||
['color_scheme', 'label_colors'],
|
||||
['color_scheme'],
|
||||
[
|
||||
{
|
||||
name: 'number_format',
|
||||
|
||||
@@ -30,7 +30,7 @@ const config: ControlPanelConfig = {
|
||||
{
|
||||
label: t('Chart Options'),
|
||||
expanded: true,
|
||||
controlSetRows: [['color_scheme', 'label_colors']],
|
||||
controlSetRows: [['color_scheme']],
|
||||
},
|
||||
],
|
||||
controlOverrides: {
|
||||
|
||||
@@ -62,7 +62,7 @@ const config: ControlPanelConfig = {
|
||||
{
|
||||
label: t('Chart Options'),
|
||||
expanded: true,
|
||||
controlSetRows: [['color_scheme', 'label_colors']],
|
||||
controlSetRows: [['color_scheme']],
|
||||
},
|
||||
],
|
||||
};
|
||||
|
||||
@@ -54,7 +54,7 @@ const config: ControlPanelConfig = {
|
||||
expanded: true,
|
||||
tabOverride: 'customize',
|
||||
controlSetRows: [
|
||||
['color_scheme', 'label_colors'],
|
||||
['color_scheme'],
|
||||
[
|
||||
{
|
||||
name: 'treemap_ratio',
|
||||
|
||||
@@ -61,7 +61,7 @@ const config: ControlPanelConfig = {
|
||||
},
|
||||
},
|
||||
],
|
||||
['color_scheme', 'label_colors'],
|
||||
['color_scheme'],
|
||||
[richTooltip, showControls],
|
||||
],
|
||||
},
|
||||
|
||||
@@ -49,7 +49,6 @@ const config: ControlPanelConfig = {
|
||||
expanded: true,
|
||||
controlSetRows: [
|
||||
['color_scheme'],
|
||||
['label_colors'],
|
||||
[showBrush],
|
||||
[showLegend],
|
||||
[showBarValue],
|
||||
|
||||
@@ -69,10 +69,7 @@ const config: ControlPanelConfig = {
|
||||
label: t('Chart Options'),
|
||||
expanded: true,
|
||||
tabOverride: 'customize',
|
||||
controlSetRows: [
|
||||
['color_scheme', 'label_colors'],
|
||||
[showLegend, null],
|
||||
],
|
||||
controlSetRows: [['color_scheme'], [showLegend, null]],
|
||||
},
|
||||
{
|
||||
label: t('X Axis'),
|
||||
|
||||
@@ -39,7 +39,7 @@ const config: ControlPanelConfig = {
|
||||
{
|
||||
label: t('Chart Options'),
|
||||
expanded: true,
|
||||
controlSetRows: [['color_scheme', 'label_colors']],
|
||||
controlSetRows: [['color_scheme']],
|
||||
},
|
||||
{
|
||||
label: t('X Axis'),
|
||||
|
||||
@@ -79,7 +79,6 @@ const config: ControlPanelConfig = {
|
||||
expanded: true,
|
||||
controlSetRows: [
|
||||
['color_scheme'],
|
||||
['label_colors'],
|
||||
[showLegend],
|
||||
[showBarValue],
|
||||
[barStacked],
|
||||
|
||||
@@ -34,7 +34,7 @@ const config: ControlPanelConfig = {
|
||||
{
|
||||
label: t('Chart Options'),
|
||||
expanded: true,
|
||||
controlSetRows: [['color_scheme', 'label_colors'], [showLegend], [xAxisFormat]],
|
||||
controlSetRows: [['color_scheme'], [showLegend], [xAxisFormat]],
|
||||
},
|
||||
{
|
||||
label: t('Y Axis 1'),
|
||||
|
||||
@@ -46,7 +46,6 @@ const config: ControlPanelConfig = {
|
||||
expanded: true,
|
||||
controlSetRows: [
|
||||
['color_scheme'],
|
||||
['label_colors'],
|
||||
[showBrush],
|
||||
[
|
||||
{
|
||||
|
||||
@@ -55,7 +55,7 @@ const config: ControlPanelConfig = {
|
||||
tabOverride: 'customize',
|
||||
expanded: true,
|
||||
controlSetRows: [
|
||||
['color_scheme', 'label_colors'],
|
||||
['color_scheme'],
|
||||
[
|
||||
{
|
||||
name: 'prefix_metric_with_slice_name',
|
||||
|
||||
@@ -103,7 +103,7 @@ const config: ControlPanelConfig = {
|
||||
},
|
||||
},
|
||||
],
|
||||
['color_scheme', 'label_colors'],
|
||||
['color_scheme'],
|
||||
],
|
||||
},
|
||||
],
|
||||
|
||||
@@ -265,7 +265,7 @@ const config: ControlPanelConfig = {
|
||||
label: t('Chart Options'),
|
||||
expanded: true,
|
||||
controlSetRows: [
|
||||
['color_scheme', 'label_colors'],
|
||||
['color_scheme'],
|
||||
...createCustomizeSection(t('Query A'), ''),
|
||||
...createCustomizeSection(t('Query B'), 'B'),
|
||||
[
|
||||
|
||||
@@ -99,7 +99,7 @@ const config: ControlPanelConfig = {
|
||||
label: t('Chart Options'),
|
||||
expanded: true,
|
||||
controlSetRows: [
|
||||
['color_scheme', 'label_colors'],
|
||||
['color_scheme'],
|
||||
[
|
||||
{
|
||||
name: 'seriesType',
|
||||
|
||||
@@ -76,7 +76,7 @@ const config: ControlPanelConfig = {
|
||||
label: t('Chart Options'),
|
||||
expanded: true,
|
||||
controlSetRows: [
|
||||
['color_scheme', 'label_colors'],
|
||||
['color_scheme'],
|
||||
...showValueSection,
|
||||
[
|
||||
{
|
||||
|
||||
@@ -93,7 +93,7 @@ const config: ControlPanelConfig = {
|
||||
label: t('Chart Options'),
|
||||
expanded: true,
|
||||
controlSetRows: [
|
||||
['color_scheme', 'label_colors'],
|
||||
['color_scheme'],
|
||||
...showValueSection,
|
||||
[
|
||||
{
|
||||
|
||||
@@ -99,7 +99,7 @@ const config: ControlPanelConfig = {
|
||||
label: t('Chart Options'),
|
||||
expanded: true,
|
||||
controlSetRows: [
|
||||
['color_scheme', 'label_colors'],
|
||||
['color_scheme'],
|
||||
[
|
||||
{
|
||||
name: 'seriesType',
|
||||
|
||||
@@ -100,7 +100,7 @@ const config: ControlPanelConfig = {
|
||||
label: t('Chart Options'),
|
||||
expanded: true,
|
||||
controlSetRows: [
|
||||
['color_scheme', 'label_colors'],
|
||||
['color_scheme'],
|
||||
[
|
||||
{
|
||||
name: 'seriesType',
|
||||
|
||||
@@ -88,7 +88,7 @@ const config: ControlPanelConfig = {
|
||||
},
|
||||
},
|
||||
],
|
||||
['color_scheme', 'label_colors'],
|
||||
['color_scheme'],
|
||||
],
|
||||
},
|
||||
],
|
||||
|
||||
@@ -31,7 +31,7 @@ const config: ControlPanelConfig = {
|
||||
label: t('Chart Options'),
|
||||
expanded: true,
|
||||
controlSetRows: [
|
||||
['color_scheme', 'label_colors'],
|
||||
['color_scheme'],
|
||||
[
|
||||
{
|
||||
name: 'whisker_options',
|
||||
|
||||
Reference in New Issue
Block a user