Skip to content

Commit

Permalink
chore: Add option to set a custom color scheme as default (apache#16540)
Browse files Browse the repository at this point in the history
* upgrade superset-ui for new flag

* configurable default categorical scheme

* dry refactor

* doc example

* Update superset-frontend/src/setup/setupColors.ts

Co-authored-by: Geido <[email protected]>

* re-adding # type: ignore (suggestion from ville)

* ugh... missed a space.

Co-authored-by: Geido <[email protected]>
Co-authored-by: Evan Rusackas <[email protected]>
(cherry picked from commit 540277e)
  • Loading branch information
suddjian authored and Steven Uray committed Sep 16, 2021
1 parent 5cf4d5b commit 3187c66
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 35 deletions.
69 changes: 34 additions & 35 deletions superset-frontend/src/setup/setupColors.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,44 +31,43 @@ import {
SequentialScheme,
} from '@superset-ui/core';
import superset from '@superset-ui/core/lib/color/colorSchemes/categorical/superset';
import ColorSchemeRegistry from '@superset-ui/core/lib/color/ColorSchemeRegistry';

export default function setupColors(
extraCategoricalColorSchemas: CategoricalScheme[] = [],
extraSequentialColorSchemes: SequentialScheme[] = [],
function registerColorSchemes(
registry: ColorSchemeRegistry<unknown>,
colorSchemes: (CategoricalScheme | SequentialScheme)[],
standardDefaultKey: string,
) {
// Register color schemes
const categoricalSchemeRegistry = getCategoricalSchemeRegistry();
colorSchemes.forEach(scheme => {
registry.registerValue(scheme.id, scheme);
});

if (extraCategoricalColorSchemas?.length > 0) {
extraCategoricalColorSchemas.forEach(scheme => {
categoricalSchemeRegistry.registerValue(scheme.id, scheme);
});
}
const defaultKey =
colorSchemes.find(scheme => scheme.isDefault)?.id || standardDefaultKey;
registry.setDefaultKey(defaultKey);
}

[superset, airbnb, categoricalD3, echarts, google, lyft, preset].forEach(
group => {
group.forEach(scheme => {
categoricalSchemeRegistry.registerValue(scheme.id, scheme);
});
},
export default function setupColors(
extraCategoricalColorSchemes: CategoricalScheme[] = [],
extraSequentialColorSchemes: SequentialScheme[] = [],
) {
registerColorSchemes(
getCategoricalSchemeRegistry(),
[
...superset,
...airbnb,
...categoricalD3,
...echarts,
...google,
...lyft,
...preset,
...extraCategoricalColorSchemes,
],
'supersetColors',
);
registerColorSchemes(
getSequentialSchemeRegistry(),
[...sequentialCommon, ...sequentialD3, ...extraSequentialColorSchemes],
'superset_seq_1',
);
categoricalSchemeRegistry.setDefaultKey('supersetColors');

const sequentialSchemeRegistry = getSequentialSchemeRegistry();

if (extraSequentialColorSchemes?.length > 0) {
extraSequentialColorSchemes.forEach(scheme => {
sequentialSchemeRegistry.registerValue(
scheme.id,
new SequentialScheme(scheme),
);
});
}

[sequentialCommon, sequentialD3].forEach(group => {
group.forEach(scheme => {
sequentialSchemeRegistry.registerValue(scheme.id, scheme);
});
});
sequentialSchemeRegistry.setDefaultKey('superset_seq_1');
}
2 changes: 2 additions & 0 deletions superset/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -456,6 +456,7 @@ def _try_json_readsha(filepath: str, length: int) -> Optional[str]:
# "id": 'myVisualizationColors',
# "description": '',
# "label": 'My Visualization Colors',
# "isDefault": True,
# "colors":
# ['#006699', '#009DD9', '#5AAA46', '#44AAAA', '#DDAA77', '#7799BB', '#88AA77',
# '#552288', '#5AAA46', '#CC7788', '#EEDD55', '#9977BB', '#BBAA44', '#DDCCDD']
Expand Down Expand Up @@ -490,6 +491,7 @@ def _try_json_readsha(filepath: str, length: int) -> Optional[str]:
# "description": '',
# "isDiverging": True,
# "label": 'My custom warm to hot',
# "isDefault": True,
# "colors":
# ['#552288', '#5AAA46', '#CC7788', '#EEDD55', '#9977BB', '#BBAA44', '#DDCCDD',
# '#006699', '#009DD9', '#5AAA46', '#44AAAA', '#DDAA77', '#7799BB', '#88AA77']
Expand Down

0 comments on commit 3187c66

Please sign in to comment.