Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Custom color schemes support #12210

Merged
merged 10 commits into from Jan 7, 2021
25 changes: 23 additions & 2 deletions superset-frontend/src/preamble.ts
Expand Up @@ -20,6 +20,7 @@ import { setConfig as setHotLoaderConfig } from 'react-hot-loader';
import 'abortcontroller-polyfill/dist/abortcontroller-polyfill-only';
import moment from 'moment';
import { configure } from '@superset-ui/core';
import ColorScheme from '@superset-ui/core/lib/color/ColorScheme';
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We should probably export this (and other related ones) in @superset-ui/core.

import setupClient from './setup/setupClient';
import setupColors from './setup/setupColors';
import setupFormatters from './setup/setupFormatters';
Expand All @@ -28,10 +29,12 @@ if (process.env.WEBPACK_MODE === 'development') {
setHotLoaderConfig({ logLevel: 'debug', trackTailUpdates: false });
}

let bootstrapData: any;
// Configure translation
if (typeof window !== 'undefined') {
const root = document.getElementById('app');
const bootstrapData = root

bootstrapData = root
? JSON.parse(root.getAttribute('data-bootstrap') || '{}')
: {};
if (bootstrapData.common && bootstrapData.common.language_pack) {
Expand All @@ -49,7 +52,25 @@ if (typeof window !== 'undefined') {
setupClient();

// Setup color palettes
setupColors();
let extraCategoricalColorSchemes: ColorScheme[] = [];
let extraSequentialColorSchemes: ColorScheme[] = [];

if (
bootstrapData.common &&
bootstrapData.common.extra_categorical_color_schemes
) {
extraCategoricalColorSchemes =
bootstrapData.common.extra_categorical_color_schemes;
}
if (
bootstrapData.common &&
bootstrapData.common.extra_sequential_color_schemes
) {
extraSequentialColorSchemes =
bootstrapData.common.extra_sequential_color_schemes;
}

setupColors(extraCategoricalColorSchemes, extraSequentialColorSchemes);

// Setup number formatters
setupFormatters();
19 changes: 18 additions & 1 deletion superset-frontend/src/setup/setupColors.js
Expand Up @@ -30,9 +30,19 @@ import {
} from '@superset-ui/core';
import superset from '@superset-ui/core/esm/color/colorSchemes/categorical/superset';

export default function setupColors() {
export default function setupColors(
extraCategoricalColorSchemas,
extraSequentialColorSchemes,
) {
// Register color schemes
const categoricalSchemeRegistry = getCategoricalSchemeRegistry();

if (extraCategoricalColorSchemas && extraCategoricalColorSchemas.length > 0) {
extraCategoricalColorSchemas.forEach(scheme => {
categoricalSchemeRegistry.registerValue(scheme.id, scheme);
});
}

[superset, airbnb, categoricalD3, echarts, google, lyft, preset].forEach(
group => {
group.forEach(scheme => {
Expand All @@ -43,6 +53,13 @@ export default function setupColors() {
categoricalSchemeRegistry.setDefaultKey('supersetColors');

const sequentialSchemeRegistry = getSequentialSchemeRegistry();

if (extraSequentialColorSchemes && extraSequentialColorSchemes.length > 0) {
extraSequentialColorSchemes.forEach(scheme => {
categoricalSchemeRegistry.registerValue(scheme.id, scheme);
});
}

[sequentialCommon, sequentialD3].forEach(group => {
group.forEach(scheme => {
sequentialSchemeRegistry.registerValue(scheme.id, scheme);
Expand Down
34 changes: 32 additions & 2 deletions superset/config.py
Expand Up @@ -368,6 +368,37 @@ def _try_json_readsha( # pylint: disable=unused-argument
# return feature_flags_dict
GET_FEATURE_FLAGS_FUNC: Optional[Callable[[Dict[str, bool]], Dict[str, bool]]] = None

# EXTRA_CATEGORICAL_COLOR_SCHEMES is used for adding custom categorical color schemes
# example code for "My custom warm to hot" color scheme
# EXTRA_CATEGORICAL_COLOR_SCHEMES = [
# {
# "id": 'myVisualizationColors',
# "description": '',
# "label": 'My Visualization Colors',
# "colors":
# ['#006699', '#009DD9', '#5AAA46', '#44AAAA', '#DDAA77', '#7799BB', '#88AA77',
# '#552288', '#5AAA46', '#CC7788', '#EEDD55', '#9977BB', '#BBAA44', '#DDCCDD']
# }]
#

# This is merely a default
EXTRA_CATEGORICAL_COLOR_SCHEMES: List[Dict[str, Any]] = []

# EXTRA_SEQUENTIAL_COLOR_SCHEMES is used for adding custom sequential color schemes
# EXTRA_SEQUENTIAL_COLOR_SCHEMES = [
# {
# "id": 'warmToHot',
# "description": '',
# "isDiverging": true
# "label": 'My custom warm to hot',
# "colors":
# ['#552288', '#5AAA46', '#CC7788', '#EEDD55', '#9977BB', '#BBAA44', '#DDCCDD',
# '#006699', '#009DD9', '#5AAA46', '#44AAAA', '#DDAA77', '#7799BB', '#88AA77']
# }]

# This is merely a default
EXTRA_SEQUENTIAL_COLOR_SCHEMES: List[Dict[str, Any]] = []

# ---------------------------------------------------
# Thumbnail config (behind feature flag)
# ---------------------------------------------------
Expand Down Expand Up @@ -545,6 +576,7 @@ def _try_json_readsha( # pylint: disable=unused-argument
SQLLAB_SAVE_WARNING_MESSAGE = None
SQLLAB_SCHEDULE_WARNING_MESSAGE = None


# Default celery config is to use SQLA as a broker, in a production setting
# you'll want to use a proper broker as specified here:
# http://docs.celeryproject.org/en/latest/getting-started/brokers/index.html
Expand Down Expand Up @@ -997,7 +1029,6 @@ class CeleryConfig: # pylint: disable=too-few-public-methods
'class="alert-link">here</a>.'
)


# SQLA table mutator, every time we fetch the metadata for a certain table
# (superset.connectors.sqla.models.SqlaTable), we call this hook
# to allow mutating the object with this callback.
Expand Down Expand Up @@ -1049,7 +1080,6 @@ class CeleryConfig: # pylint: disable=too-few-public-methods
logger.exception("Found but failed to import local superset_config")
raise


# It's possible to add a dataset health check logic which is specific to your system.
# It will get executed each time when user open a chart's explore view.
DATASET_HEALTH_CHECK = None
2 changes: 2 additions & 0 deletions superset/views/base.py
Expand Up @@ -318,6 +318,8 @@ def common_bootstrap_payload() -> Dict[str, Any]:
"locale": locale,
"language_pack": get_language_pack(locale),
"feature_flags": get_feature_flags(),
"extra_sequential_color_schemes": conf["EXTRA_SEQUENTIAL_COLOR_SCHEMES"],
"extra_categorical_color_schemes": conf["EXTRA_CATEGORICAL_COLOR_SCHEMES"],
"menu_data": menu_data(),
}

Expand Down