Skip to content

The easy-to-use open source Business Intelligence and Embedded Analytics tool that lets everyone work with data :bar_chart:

Make sunburst charts reflect colors set in the appearance tab (#68641)

kevinkelleher12b27e93965dba
diff --git a/.loki/reference/chrome_laptop_Viz_Static_Viz_SmartScalar_Big_Numbers.png b/.loki/reference/chrome_laptop_Viz_Static_Viz_SmartScalar_Big_Numbers.png
index a4a6e4223f3..e02c627f06a 100644
Binary files a/.loki/reference/chrome_laptop_Viz_Static_Viz_SmartScalar_Big_Numbers.png and b/.loki/reference/chrome_laptop_Viz_Static_Viz_SmartScalar_Big_Numbers.png differ
diff --git a/.loki/reference/chrome_laptop_Viz_Static_Viz_SmartScalar_Comparisons.png b/.loki/reference/chrome_laptop_Viz_Static_Viz_SmartScalar_Comparisons.png
index 815e383a322..32b0eea836e 100644
Binary files a/.loki/reference/chrome_laptop_Viz_Static_Viz_SmartScalar_Comparisons.png and b/.loki/reference/chrome_laptop_Viz_Static_Viz_SmartScalar_Comparisons.png differ
diff --git a/.loki/reference/chrome_laptop_Viz_Static_Viz_SmartScalar_Default.png b/.loki/reference/chrome_laptop_Viz_Static_Viz_SmartScalar_Default.png
index 63de5e5ac6c..a5d3b9b9bf3 100644
Binary files a/.loki/reference/chrome_laptop_Viz_Static_Viz_SmartScalar_Default.png and b/.loki/reference/chrome_laptop_Viz_Static_Viz_SmartScalar_Default.png differ
diff --git a/.loki/reference/chrome_laptop_Viz_Static_Viz_SmartScalar_Missing_Interval.png b/.loki/reference/chrome_laptop_Viz_Static_Viz_SmartScalar_Missing_Interval.png
index 819465543f8..537cd3b5097 100644
Binary files a/.loki/reference/chrome_laptop_Viz_Static_Viz_SmartScalar_Missing_Interval.png and b/.loki/reference/chrome_laptop_Viz_Static_Viz_SmartScalar_Missing_Interval.png differ
diff --git a/.loki/reference/chrome_laptop_Viz_Static_Viz_SmartScalar_Null_Value.png b/.loki/reference/chrome_laptop_Viz_Static_Viz_SmartScalar_Null_Value.png
index 1600076a506..3676da0848b 100644
Binary files a/.loki/reference/chrome_laptop_Viz_Static_Viz_SmartScalar_Null_Value.png and b/.loki/reference/chrome_laptop_Viz_Static_Viz_SmartScalar_Null_Value.png differ
diff --git a/enterprise/frontend/src/metabase-enterprise/whitelabel/components/ColorSettings/ColorSettings.tsx b/enterprise/frontend/src/metabase-enterprise/whitelabel/components/ColorSettings/ColorSettings.tsx
index e4ce9388141..0cf60087314 100644
--- a/enterprise/frontend/src/metabase-enterprise/whitelabel/components/ColorSettings/ColorSettings.tsx
+++ b/enterprise/frontend/src/metabase-enterprise/whitelabel/components/ColorSettings/ColorSettings.tsx
@@ -11,20 +11,20 @@ import ChartColorSettings from "../ChartColorSettings";
export interface ColorSettingsProps {
initialColors: ColorSettingsType | null;
- originalColors: ColorSettingsType;
+ themeColors: ColorSettingsType;
onChange?: (colors: ColorSettingsType) => void;
}
export const ColorSettings = ({
initialColors,
- originalColors,
+ themeColors,
onChange,
}: ColorSettingsProps): JSX.Element => {
const [colors, setColors] = useState(initialColors ?? {});
const colorPalette = useMemo(() => {
- return { ...originalColors, ...colors };
- }, [colors, originalColors]);
+ return { ...themeColors, ...colors };
+ }, [colors, themeColors]);
const handleChange = useCallback(
(colors: ColorSettingsType) => {
diff --git a/enterprise/frontend/src/metabase-enterprise/whitelabel/components/ColorSettings/ColorSettings.unit.spec.tsx b/enterprise/frontend/src/metabase-enterprise/whitelabel/components/ColorSettings/ColorSettings.unit.spec.tsx
index 11b065b8e21..88982670fb3 100644
--- a/enterprise/frontend/src/metabase-enterprise/whitelabel/components/ColorSettings/ColorSettings.unit.spec.tsx
+++ b/enterprise/frontend/src/metabase-enterprise/whitelabel/components/ColorSettings/ColorSettings.unit.spec.tsx
@@ -24,7 +24,7 @@ describe("ColorSettings", () => {
render(
<ColorSettings
initialColors={initialColors}
- originalColors={colors}
+ themeColors={colors}
onChange={onChange}
/>,
);
@@ -48,7 +48,7 @@ describe("ColorSettings", () => {
render(
<ColorSettings
initialColors={initialColors}
- originalColors={colors}
+ themeColors={colors}
onChange={onChange}
/>,
);
@@ -69,7 +69,7 @@ describe("ColorSettings", () => {
render(
<ColorSettings
initialColors={initialColors}
- originalColors={colors}
+ themeColors={colors}
onChange={onChange}
/>,
);
@@ -88,7 +88,7 @@ describe("ColorSettings", () => {
render(
<ColorSettings
initialColors={initialColors}
- originalColors={colors}
+ themeColors={colors}
onChange={onChange}
/>,
);
diff --git a/enterprise/frontend/src/metabase-enterprise/whitelabel/components/ColorSettingsWidget/ColorSettingsWidget.tsx b/enterprise/frontend/src/metabase-enterprise/whitelabel/components/ColorSettingsWidget/ColorSettingsWidget.tsx
index 8193bab53c1..5017beff57d 100644
--- a/enterprise/frontend/src/metabase-enterprise/whitelabel/components/ColorSettingsWidget/ColorSettingsWidget.tsx
+++ b/enterprise/frontend/src/metabase-enterprise/whitelabel/components/ColorSettingsWidget/ColorSettingsWidget.tsx
@@ -2,7 +2,6 @@ import { useDebouncedCallback } from "@mantine/hooks";
import { SetByEnvVar } from "metabase/admin/settings/components/widgets/AdminSettingInput";
import { useAdminSetting } from "metabase/api/utils";
-import { originalColors } from "metabase/lib/colors/palette";
import { useMantineTheme } from "metabase/ui";
import type { ColorSettings as ColorSettingsType } from "metabase-types/api";
@@ -16,6 +15,13 @@ export const ColorSettingsWidget = () => {
} = useAdminSetting("application-colors");
const theme = useMantineTheme();
+ const themeColors = Object.fromEntries(
+ Object.entries(theme.colors).map(([colorName, color]) => [
+ colorName,
+ color[theme.primaryShade as number],
+ ]),
+ );
+
const handleChange = async (newValue: ColorSettingsType) => {
await updateSetting({
key: "application-colors",
@@ -37,7 +43,7 @@ export const ColorSettingsWidget = () => {
return (
<ColorSettings
initialColors={colorSettings}
- originalColors={originalColors}
+ themeColors={themeColors}
onChange={onChangeDebounced}
/>
);
diff --git a/enterprise/frontend/src/metabase-enterprise/whitelabel/components/ColorSettingsWidget/ColorSettingsWidget.unit.spec.tsx b/enterprise/frontend/src/metabase-enterprise/whitelabel/components/ColorSettingsWidget/ColorSettingsWidget.unit.spec.tsx
new file mode 100644
index 00000000000..0889cdf6b38
--- /dev/null
+++ b/enterprise/frontend/src/metabase-enterprise/whitelabel/components/ColorSettingsWidget/ColorSettingsWidget.unit.spec.tsx
@@ -0,0 +1,60 @@
+import fetchMock from "fetch-mock";
+
+import { renderWithProviders, screen } from "__support__/ui";
+import {
+ deriveChartShadeColor,
+ deriveChartTintColor,
+} from "metabase/lib/colors/accents";
+import MetabaseSettings from "metabase/lib/settings";
+import type { SettingKey } from "metabase-types/api";
+
+import { ColorSettingsWidget } from "./ColorSettingsWidget";
+
+function setup(colors: Record<string, string>) {
+ MetabaseSettings.set("application-colors" as SettingKey, colors);
+ fetchMock.get("path:/api/session/properties", {
+ "application-colors": colors,
+ });
+ fetchMock.get("path:/api/setting", {});
+ renderWithProviders(<ColorSettingsWidget />);
+}
+
+async function expectColorsToExist(colors: Record<string, string>) {
+ await Promise.all(
+ Object.values(colors).map(async (color) => {
+ expect(
+ await screen.findByRole("button", { name: color }),
+ ).toBeInTheDocument();
+ }),
+ );
+}
+
+describe("ColorSettingsWidget", () => {
+ it("should respect whitelabel colors", async () => {
+ const colors = {
+ accent0: "#abcdef",
+ "accent0-light": "#123456",
+ "accent0-dark": "#789012",
+ };
+ setup(colors);
+ await expectColorsToExist(colors);
+ });
+
+ it("should derive light and dark variants of whitelabel colors", async () => {
+ const colors = {
+ accent0: "#abcdef",
+ "accent1-light": "#123456",
+ };
+ setup(colors);
+ const expectedColors = {
+ ...colors,
+ "accent0-light": deriveChartTintColor(colors.accent0),
+ "accent0-dark": deriveChartShadeColor(colors.accent0),
+ accent1: deriveChartShadeColor(colors["accent1-light"]),
+ "accent1-dark": deriveChartShadeColor(
+ deriveChartShadeColor(colors["accent1-light"]),
+ ),
+ };
+ await expectColorsToExist(expectedColors);
+ });
+});
diff --git a/enterprise/frontend/src/metabase-enterprise/whitelabel/lib/whitelabel.js b/enterprise/frontend/src/metabase-enterprise/whitelabel/lib/whitelabel.js
index c52beb52001..3581d2a7448 100644
--- a/enterprise/frontend/src/metabase-enterprise/whitelabel/lib/whitelabel.js
+++ b/enterprise/frontend/src/metabase-enterprise/whitelabel/lib/whitelabel.js
@@ -1,11 +1,9 @@
-import { colors } from "metabase/lib/colors/colors";
+import { mutateColors } from "metabase/lib/colors/colors";
import MetabaseSettings from "metabase/lib/settings";
export function updateColors() {
const scheme = MetabaseSettings.get("application-colors") || {};
- for (const [colorName, themeColor] of Object.entries(scheme)) {
- colors[colorName] = themeColor;
- }
+ mutateColors(scheme);
}
// Update the JS colors to ensure components that use a color statically get the
diff --git a/frontend/src/metabase/lib/colors/accents.ts b/frontend/src/metabase/lib/colors/accents.ts
index 0716a7273e2..1a35137d82e 100644
--- a/frontend/src/metabase/lib/colors/accents.ts
+++ b/frontend/src/metabase/lib/colors/accents.ts
@@ -1,5 +1,7 @@
import Color from "color";
+import type { ColorSettings } from "metabase-types/api";
+
import {
ACCENT_COLOR_NAMES_MAP,
CHART_TINT_SHADE_FACTOR,
@@ -78,3 +80,53 @@ export function deriveChartShadeColor(hexColor: string): string {
.lightness(value.lightness() - CHART_TINT_SHADE_FACTOR * 100)
.hex();
}
+
+/**
+ * Given color settings, adds any missing accent variants, like "accent0-light", "accent1-dark", or even "accent2".
+ * Similar to mapChartColorsToAccents, but more flexible:
+ * - The base color is not required; can derive "accent0" and "accent0-dark" if only "accent0-light is provided"
+ * - Can skip an accent entirely; can provide "accent0" and "accent2" but skip "accent1"
+ * This flexibility is required for the ColorSettingsWidget UX
+ */
+export function deriveAllAccentColors(colors: ColorSettings): ColorSettings {
+ const accentColors = Object.fromEntries(
+ Object.entries(colors).filter(([key]) => key.startsWith("accent")),
+ );
+ const otherColors = Object.fromEntries(
+ Object.entries(colors).filter(([key]) => !key.startsWith("accent")),
+ );
+ const allAccentColors = Object.entries(accentColors).reduce(
+ (acc, [key, value]) => {
+ const [accent, variant] = key.split("-");
+ let colorsToMaybeSet;
+ if (variant === "light") {
+ const base = deriveChartShadeColor(value);
+ colorsToMaybeSet = {
+ [accent]: base,
+ [`${accent}-dark`]: deriveChartShadeColor(base),
+ };
+ } else if (variant === "dark") {
+ const base = deriveChartTintColor(value);
+ colorsToMaybeSet = {
+ [accent]: base,
+ [`${accent}-light`]: deriveChartTintColor(base),
+ };
+ } else {
+ colorsToMaybeSet = {
+ [`${accent}-light`]: deriveChartTintColor(value),
+ [`${accent}-dark`]: deriveChartShadeColor(value),
+ };
+ }
+ return {
+ // acc takes priority because it's initialized with provided accentColors
+ ...colorsToMaybeSet,
+ ...acc,
+ };
+ },
+ accentColors,
+ );
+ return {
+ ...allAccentColors,
+ ...otherColors,
+ };
+}
diff --git a/frontend/src/metabase/lib/colors/derive-theme.ts b/frontend/src/metabase/lib/colors/derive-theme.ts
index 0656df7d03d..0bc00698e39 100644
--- a/frontend/src/metabase/lib/colors/derive-theme.ts
+++ b/frontend/src/metabase/lib/colors/derive-theme.ts
@@ -5,7 +5,7 @@ import type { ColorSettings } from "metabase-types/api";
import type { ResolvedColorScheme } from "../color-scheme";
-import { mapChartColorsToAccents } from "./accents";
+import { deriveAllAccentColors, mapChartColorsToAccents } from "./accents";
import { PROTECTED_COLORS } from "./constants/protected-colors";
import { getThemeFromColorScheme } from "./theme-from-color-scheme";
import type { MetabaseColorKey, MetabaseDerivedThemeV2 } from "./types";
@@ -43,7 +43,7 @@ export function deriveFullMetabaseTheme({
colors: {
...baseTheme.colors,
...mapChartColorsToAccents(baseTheme.chartColors),
- ...whitelabelColors,
+ ...deriveAllAccentColors(whitelabelColors ?? {}),
...filteredEmbeddingColors,
...mapChartColorsToAccents(embeddingThemeOverride?.chartColors ?? []),
} as Record<MetabaseColorKey, string>,
diff --git a/frontend/src/metabase/lib/colors/derive-theme.unit.spec.ts b/frontend/src/metabase/lib/colors/derive-theme.unit.spec.ts
index fd7ab06473c..60516e3bb61 100644
--- a/frontend/src/metabase/lib/colors/derive-theme.unit.spec.ts
+++ b/frontend/src/metabase/lib/colors/derive-theme.unit.spec.ts
@@ -1,3 +1,4 @@
+import { deriveChartShadeColor, deriveChartTintColor } from "./accents";
import { PROTECTED_COLORS } from "./constants/protected-colors";
import { METABASE_LIGHT_THEME } from "./constants/themes/light";
import { deriveFullMetabaseTheme } from "./derive-theme";
@@ -84,4 +85,46 @@ describe("deriveFullMetabaseTheme", () => {
expect(colors["accent0-light"]).toBe("#ff8888");
expect(colors["accent0-dark"]).toBe("#880000");
});
+
+ it("applies whitelabel accent colors over the base theme", () => {
+ const derived = deriveFullMetabaseTheme({
+ colorScheme: "light",
+ whitelabelColors: {
+ accent0: "#ff0000",
+ "accent0-light": "#00ff00",
+ "accent0-dark": "#0000ff",
+ accent2: "#bada55",
+ "accent3-light": "#4B9CD3",
+ },
+ });
+
+ //accent 0 should use provided colors
+ expect(derived.colors["accent0"]).toBe("#ff0000");
+ expect(derived.colors["accent0-light"]).toBe("#00ff00");
+ expect(derived.colors["accent0-dark"]).toBe("#0000ff");
+
+ //accent 1 should be default
+ expect(derived.colors["accent1"]).toBe(METABASE_LIGHT_THEME.chartColors[1]);
+ expect(derived.colors["accent1-light"]).toBe(
+ deriveChartTintColor(METABASE_LIGHT_THEME.chartColors[1] as string),
+ );
+ expect(derived.colors["accent1-dark"]).toBe(
+ deriveChartShadeColor(METABASE_LIGHT_THEME.chartColors[1] as string),
+ );
+
+ //accent 2 should calculate light and dark from provided color
+ expect(derived.colors["accent2"]).toBe("#bada55");
+ expect(derived.colors["accent2-light"]).toBe(
+ deriveChartTintColor("#bada55"),
+ );
+ expect(derived.colors["accent2-dark"]).toBe(
+ deriveChartShadeColor("#bada55"),
+ );
+
+ //accent 3 should calculate base and dark from provided color
+ const base = deriveChartShadeColor("#4B9CD3");
+ expect(derived.colors["accent3"]).toBe(base);
+ expect(derived.colors["accent3-light"]).toBe("#4B9CD3");
+ expect(derived.colors["accent3-dark"]).toBe(deriveChartShadeColor(base));
+ });
});
diff --git a/frontend/src/metabase/static-viz/lib/colors.ts b/frontend/src/metabase/static-viz/lib/colors.ts
index 6124be55c30..ab0b489883b 100644
--- a/frontend/src/metabase/static-viz/lib/colors.ts
+++ b/frontend/src/metabase/static-viz/lib/colors.ts
@@ -1,6 +1,6 @@
import Color from "color";
-import { colors, staticVizOverrides } from "metabase/lib/colors/colors";
+import { getColors, staticVizOverrides } from "metabase/lib/colors/colors";
import { color } from "metabase/lib/colors/palette";
import type { ColorPalette } from "metabase/lib/colors/types";
import type { ColorGetter } from "metabase/visualizations/types";
@@ -8,7 +8,7 @@ import type { ColorGetter } from "metabase/visualizations/types";
export const createColorGetter = (
instanceColors: ColorPalette = {},
): ColorGetter => {
- const palette = { ...colors, ...staticVizOverrides, ...instanceColors };
+ const palette = { ...getColors(instanceColors), ...staticVizOverrides };
return (colorName: string) => {
const value = color(colorName, palette);
diff --git a/frontend/src/metabase/visualizations/echarts/pie/model/index.ts b/frontend/src/metabase/visualizations/echarts/pie/model/index.ts
index 20008ea2e9f..418982c13e2 100644
--- a/frontend/src/metabase/visualizations/echarts/pie/model/index.ts
+++ b/frontend/src/metabase/visualizations/echarts/pie/model/index.ts
@@ -334,7 +334,7 @@ export function getPieChartModel(
if (!hasMultipleRings) {
return hexColor;
}
- const accentKey = hexToAccentColorMap.get(hexColor);
+ const accentKey = hexToAccentColorMap.get(hexColor.toUpperCase());
if (accentKey == null) {
return hexColor;
}
diff --git a/frontend/src/metabase/visualizations/echarts/pie/util/colors.ts b/frontend/src/metabase/visualizations/echarts/pie/util/colors.ts
index e2f0605cd98..4fedebb8155 100644
--- a/frontend/src/metabase/visualizations/echarts/pie/util/colors.ts
+++ b/frontend/src/metabase/visualizations/echarts/pie/util/colors.ts
@@ -25,7 +25,7 @@ export function createHexToAccentNumberMap() {
}
const accentKey = extractAccentKey(colorKey);
if (accentKey) {
- hexToAccentNumber.set(hex, accentKey);
+ hexToAccentNumber.set(hex.toUpperCase(), accentKey);
}
}
@@ -36,7 +36,7 @@ export function createHexToAccentNumberMap() {
const accentKey = extractAccentKey(colorKey);
if (accentKey) {
- hexToAccentNumber.set(hexGetter(colors), accentKey);
+ hexToAccentNumber.set(hexGetter(colors).toUpperCase(), accentKey);
}
}