Skip to content

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

Gdgt 910 rework look and feel of color settings in gauge (#65552)

Nick Fitzpatrickd3efd21c50ee
diff --git a/frontend/src/metabase/common/components/ColorPill/ColorPill.module.css b/frontend/src/metabase/common/components/ColorPill/ColorPill.module.css
index 2a5e62f3d18..721487c7eff 100644
--- a/frontend/src/metabase/common/components/ColorPill/ColorPill.module.css
+++ b/frontend/src/metabase/common/components/ColorPill/ColorPill.module.css
@@ -33,3 +33,9 @@
height: 2rem;
padding: 0.1875rem;
}
+
+.Large {
+ width: 2.5rem;
+ height: 2.5rem;
+ padding: 0.25rem;
+}
diff --git a/frontend/src/metabase/common/components/ColorPill/ColorPill.tsx b/frontend/src/metabase/common/components/ColorPill/ColorPill.tsx
index 7aec0a10fef..0d38f760862 100644
--- a/frontend/src/metabase/common/components/ColorPill/ColorPill.tsx
+++ b/frontend/src/metabase/common/components/ColorPill/ColorPill.tsx
@@ -20,6 +20,7 @@ export interface ColorPillProps extends ColorPillAttributes {
onSelect?: (newColor: string) => void;
pillSize?: PillSize;
"data-testid"?: string;
+ className?: string;
}
export const ColorPill = ({
@@ -31,6 +32,7 @@ export const ColorPill = ({
onClick,
onSelect,
"data-testid": dataTestId,
+ className,
}: ColorPillProps) => {
const handleClick = useCallback(
(event: MouseEvent<HTMLDivElement>) => {
@@ -46,12 +48,18 @@ export const ColorPill = ({
aria-label={ariaLabel}
role="button"
onClick={handleClick}
- className={cx(ColorPillS.ColorPill, CS.flexNoShrink, {
- [ColorPillS.Small]: pillSize === "small",
- [ColorPillS.Medium]: pillSize === "medium",
- [ColorPillS.Selected]: isSelected,
- [ColorPillS.Auto]: isAuto,
- })}
+ className={cx(
+ ColorPillS.ColorPill,
+ CS.flexNoShrink,
+ {
+ [ColorPillS.Small]: pillSize === "small",
+ [ColorPillS.Medium]: pillSize === "medium",
+ [ColorPillS.Large]: pillSize === "large",
+ [ColorPillS.Selected]: isSelected,
+ [ColorPillS.Auto]: isAuto,
+ },
+ className,
+ )}
>
<Box bg={color} w="100%" h="100%" style={{ borderRadius: "50%" }}></Box>
</Center>
diff --git a/frontend/src/metabase/common/components/ColorPill/types.ts b/frontend/src/metabase/common/components/ColorPill/types.ts
index 901639b4519..4186c4a9772 100644
--- a/frontend/src/metabase/common/components/ColorPill/types.ts
+++ b/frontend/src/metabase/common/components/ColorPill/types.ts
@@ -1 +1 @@
-export type PillSize = "small" | "medium";
+export type PillSize = "small" | "medium" | "large";
diff --git a/frontend/src/metabase/lib/colors/colors.ts b/frontend/src/metabase/lib/colors/colors.ts
index 3e6b1af3b15..9443a3340af 100644
--- a/frontend/src/metabase/lib/colors/colors.ts
+++ b/frontend/src/metabase/lib/colors/colors.ts
@@ -493,6 +493,13 @@ const getColorConfig = (settings: ColorSettings = {}) => ({
light: baseColors.orionAlpha[60],
dark: baseColors.orionAlphaInverse[60],
},
+
+ //Used in gauge viz... there should be a better way to do this
+ "text-medium-opaque": {
+ //should be text-secondary
+ light: baseColors.orion[60],
+ dark: baseColors.orion[20],
+ },
"text-primary": {
light: baseColors.orionAlpha[80],
dark: baseColors.orionAlphaInverse[80],
diff --git a/frontend/src/metabase/query_builder/components/view/sidebars/ChartSettingsSidebar.unit.spec.js b/frontend/src/metabase/query_builder/components/view/sidebars/ChartSettingsSidebar.unit.spec.js
index 1a19d0cba15..c6d45a13c09 100644
--- a/frontend/src/metabase/query_builder/components/view/sidebars/ChartSettingsSidebar.unit.spec.js
+++ b/frontend/src/metabase/query_builder/components/view/sidebars/ChartSettingsSidebar.unit.spec.js
@@ -38,7 +38,7 @@ describe("ChartSettingsSidebar", () => {
// see options header with sections
expect(screen.queryByText("Gauge options")).not.toBeInTheDocument();
expect(screen.getByText("Formatting")).toBeInTheDocument();
- expect(screen.getByText("Display")).toBeInTheDocument();
+ expect(screen.getByText("Ranges")).toBeInTheDocument();
// click on formatting section
fireEvent.click(screen.getByText("Formatting"));
@@ -49,7 +49,7 @@ describe("ChartSettingsSidebar", () => {
// but the sections and back title are unchanged
expect(screen.queryByText("Gauge options")).not.toBeInTheDocument();
expect(screen.getByText("Formatting")).toBeInTheDocument();
- expect(screen.getByText("Display")).toBeInTheDocument();
+ expect(screen.getByText("Ranges")).toBeInTheDocument();
});
it("should not hide the title when showSidebarTitle is false", () => {
diff --git a/frontend/src/metabase/visualizations/components/ChartSettings/BaseChartSettings/BaseChartSettings.styled.tsx b/frontend/src/metabase/visualizations/components/ChartSettings/BaseChartSettings/BaseChartSettings.styled.tsx
index 621094f28ca..23e2e773b77 100644
--- a/frontend/src/metabase/visualizations/components/ChartSettings/BaseChartSettings/BaseChartSettings.styled.tsx
+++ b/frontend/src/metabase/visualizations/components/ChartSettings/BaseChartSettings/BaseChartSettings.styled.tsx
@@ -7,16 +7,18 @@ export const SectionContainer = styled.div`
width: 100%;
${Radio.RadioGroupVariants.join(", ")} {
+ padding-left: 0.5rem;
+ padding-right: 0.5rem;
border-bottom: 1px solid var(--mb-color-border);
}
${Radio.RadioContainerVariants.join(", ")} {
- padding-left: 0.5rem;
- padding-right: 0.5rem;
+ padding-left: 1rem;
+ padding-right: 1rem;
}
${Radio.RadioLabelVariants.join(", ")} {
- flex-grow: 1;
+ /* flex-grow: 1; */
margin-right: 0;
display: flex;
justify-content: center;
diff --git a/frontend/src/metabase/visualizations/components/ChartSettings/BaseChartSettings/hooks.ts b/frontend/src/metabase/visualizations/components/ChartSettings/BaseChartSettings/hooks.ts
index d4bcf3fab0b..ba4a32b0e89 100644
--- a/frontend/src/metabase/visualizations/components/ChartSettings/BaseChartSettings/hooks.ts
+++ b/frontend/src/metabase/visualizations/components/ChartSettings/BaseChartSettings/hooks.ts
@@ -44,6 +44,7 @@ export const useChartSettingsSections = ({
"data",
"display",
"axes",
+ "ranges",
// include all section names so any forgotten sections are sorted to the end
...names.map((x) => x.toLowerCase()),
];
diff --git a/frontend/src/metabase/visualizations/components/settings/ChartSettingGaugeSegments.jsx b/frontend/src/metabase/visualizations/components/settings/ChartSettingGaugeSegments.jsx
deleted file mode 100644
index 1b19fc94d06..00000000000
--- a/frontend/src/metabase/visualizations/components/settings/ChartSettingGaugeSegments.jsx
+++ /dev/null
@@ -1,133 +0,0 @@
-/* eslint-disable react/prop-types */
-import cx from "classnames";
-import { Fragment } from "react";
-import { t } from "ttag";
-import _ from "underscore";
-
-import Button from "metabase/common/components/Button";
-import { ColorSelector } from "metabase/common/components/ColorSelector";
-import NumericInput from "metabase/common/components/NumericInput/OldNumericInput";
-import CS from "metabase/css/core/index.css";
-import { color } from "metabase/lib/colors";
-import { getAccentColors } from "metabase/lib/colors/groups";
-import { Icon } from "metabase/ui";
-
-const ChartSettingGaugeSegments = ({ value: segments, onChange }) => {
- const onChangeProperty = (index, property, value) =>
- onChange([
- ...segments.slice(0, index),
- { ...segments[index], [property]: value },
- ...segments.slice(index + 1),
- ]);
- return (
- <div>
- <table>
- <thead>
- <tr>
- <th>{t`Color`}</th>
- <th>{t`Min`}</th>
- <th>{t`Max`}</th>
- </tr>
- </thead>
- <tbody>
- {segments.map((segment, index) => (
- <Fragment key={segment.index}>
- <tr>
- <td>
- <ColorSelector
- className={CS.mr1}
- value={segment.color}
- colors={getColorPalette()}
- onChange={(color) =>
- onChangeProperty(index, "color", color)
- }
- />
- </td>
- <td>
- <NumericInput
- type="number"
- className={CS.full}
- value={segment.min}
- onChange={(value) => onChangeProperty(index, "min", value)}
- placeholder={t`Min`}
- />
- </td>
- <td>
- <NumericInput
- type="number"
- className={CS.full}
- value={segment.max}
- onChange={(value) => onChangeProperty(index, "max", value)}
- placeholder={t`Max`}
- />
- </td>
- <td>
- {segments.length > 1 && (
- <Icon
- name="close"
- className={cx(CS.cursorPointer, CS.ml2)}
- onClick={() =>
- onChange(segments.filter((v, i) => i !== index))
- }
- />
- )}
- </td>
- </tr>
- <tr>
- <td colSpan={3} className={CS.pb2}>
- <input
- type="text"
- className={cx(CS.full, CS.input)}
- value={segment.label}
- onChange={(e) =>
- onChangeProperty(index, "label", e.target.value)
- }
- placeholder={t`Label for this range (optional)`}
- />
- </td>
- </tr>
- </Fragment>
- ))}
- </tbody>
- </table>
- <Button
- borderless
- icon="add"
- onClick={() => onChange(segments.concat(newSegment(segments)))}
- >
- {t`Add a range`}
- </Button>
- </div>
- );
-};
-
-function getColorPalette() {
- return [
- ...getAccentColors(),
- color("error"),
- color("warning"),
- color("success"),
- color("bg-medium"),
- ];
-}
-
-function newSegment(segments) {
- const palette = getColorPalette();
- const lastSegment = segments[segments.length - 1];
- const lastColorIndex = lastSegment
- ? _.findIndex(palette, (color) => color === lastSegment.color)
- : -1;
- const nextColor =
- lastColorIndex >= 0
- ? palette[(lastColorIndex + 1) % palette.length]
- : palette[0];
-
- return {
- min: lastSegment ? lastSegment.max : 0,
- max: lastSegment ? lastSegment.max * 2 : 1,
- color: nextColor,
- label: "",
- };
-}
-
-export default ChartSettingGaugeSegments;
diff --git a/frontend/src/metabase/visualizations/components/settings/ChartSettingSegmentsEditor/ChartSettingSegmentsEditor.module.css b/frontend/src/metabase/visualizations/components/settings/ChartSettingSegmentsEditor/ChartSettingSegmentsEditor.module.css
new file mode 100644
index 00000000000..e36994aea6b
--- /dev/null
+++ b/frontend/src/metabase/visualizations/components/settings/ChartSettingSegmentsEditor/ChartSettingSegmentsEditor.module.css
@@ -0,0 +1,15 @@
+.ColorPill {
+ border-radius: 0.5rem;
+
+ & > div {
+ border-radius: 0.5rem !important;
+ }
+}
+
+.Table {
+ border-spacing: 6px;
+
+ tr > td {
+ padding-bottom: 1.125rem;
+ }
+}
diff --git a/frontend/src/metabase/visualizations/components/settings/ChartSettingSegmentsEditor/ChartSettingSegmentsEditor.tsx b/frontend/src/metabase/visualizations/components/settings/ChartSettingSegmentsEditor/ChartSettingSegmentsEditor.tsx
new file mode 100644
index 00000000000..0202c7572c4
--- /dev/null
+++ b/frontend/src/metabase/visualizations/components/settings/ChartSettingSegmentsEditor/ChartSettingSegmentsEditor.tsx
@@ -0,0 +1,147 @@
+import { t } from "ttag";
+import _ from "underscore";
+
+// import Button from "metabase/common/components/Button";
+import { ColorSelector } from "metabase/common/components/ColorSelector";
+import CS from "metabase/css/core/index.css";
+import { color } from "metabase/lib/colors";
+import { getAccentColors } from "metabase/lib/colors/groups";
+import { Box, Button, Icon, NumberInput, TextInput } from "metabase/ui";
+
+import S from "./ChartSettingSegmentsEditor.module.css";
+
+export type Segment = {
+ min: number;
+ max: number;
+ color: string;
+ label?: string;
+};
+
+export const ChartSettingSegmentsEditor = ({
+ value: segments,
+ onChange,
+}: {
+ value: Segment[];
+ onChange: (value: Segment[]) => void;
+}) => {
+ const onChangeProperty = (
+ index: number,
+ property: keyof Segment,
+ value: number | string,
+ ) =>
+ onChange([
+ ...segments.slice(0, index),
+ { ...segments[index], [property]: value },
+ ...segments.slice(index + 1),
+ ]);
+
+ return (
+ <Box px="1.5rem">
+ <table className={S.Table}>
+ <thead>
+ <tr>
+ <th />
+ <th>{t`Label`}</th>
+ <th>{t`Min`}</th>
+ <th>{t`Max`}</th>
+ </tr>
+ </thead>
+ <tbody>
+ {segments.map((segment, index) => (
+ <tr key={index}>
+ <td>
+ <ColorSelector
+ pillSize="large"
+ className={S.ColorPill}
+ value={segment.color}
+ colors={getColorPalette()}
+ onChange={(color) => onChangeProperty(index, "color", color)}
+ />
+ </td>
+ <td>
+ <TextInput
+ value={segment.label}
+ placeholder={t`Label for this range (optional)`}
+ onChange={(e) =>
+ onChangeProperty(index, "label", e.target.value)
+ }
+ />
+ </td>
+ <td>
+ <NumberInput
+ className={CS.full}
+ value={segment.min}
+ onBlur={(e) =>
+ onChangeProperty(index, "min", parseInt(e.target.value))
+ }
+ placeholder={t`Min`}
+ w="4rem"
+ />
+ </td>
+ <td>
+ <NumberInput
+ className={CS.full}
+ value={segment.max}
+ onBlur={(e) =>
+ onChangeProperty(index, "max", parseInt(e.target.value))
+ }
+ placeholder={t`Max`}
+ w="4rem"
+ />
+ </td>
+ <td>
+ {segments.length > 1 && (
+ <Button
+ leftSection={<Icon name="trash" c="text-light" />}
+ onClick={() =>
+ onChange(segments.filter((v, i) => i !== index))
+ }
+ />
+ )}
+ </td>
+ </tr>
+ ))}
+ </tbody>
+ </table>
+ <Button
+ leftSection={<Icon name="add" />}
+ onClick={() => onChange(segments.concat(newSegment(segments)))}
+ w="100%"
+ >
+ {t`Add a range`}
+ </Button>
+ </Box>
+ );
+};
+
+function getColorPalette() {
+ return [
+ ...getAccentColors(),
+ color("error"),
+ color("warning"),
+ color("success"),
+ color("bg-medium"),
+ ];
+}
+
+function newSegment(segments: Segment[]) {
+ const palette = getColorPalette();
+ const lastSegment = segments[segments.length - 1];
+ const lastColorIndex = lastSegment
+ ? _.findIndex(palette, (color) => color === lastSegment.color)
+ : -1;
+ const nextColor =
+ lastColorIndex >= 0
+ ? palette[(lastColorIndex + 1) % palette.length]
+ : palette[0];
+
+ return {
+ min: lastSegment ? lastSegment.max : 0,
+ max: lastSegment ? lastSegment.max * 2 : 1,
+ color: nextColor,
+ label: "",
+ key: newSegmentKey(),
+ };
+}
+
+const newSegmentKey = () => `gauge-segment-${_.uniqueId()}`;
diff --git a/frontend/src/metabase/visualizations/components/settings/ChartSettingSegmentsEditor/ChartSettingSegmentsEditor.unit.spec.tsx b/frontend/src/metabase/visualizations/components/settings/ChartSettingSegmentsEditor/ChartSettingSegmentsEditor.unit.spec.tsx
new file mode 100644
index 00000000000..a5935dcb118
--- /dev/null
+++ b/frontend/src/metabase/visualizations/components/settings/ChartSettingSegmentsEditor/ChartSettingSegmentsEditor.unit.spec.tsx
@@ -0,0 +1,90 @@
+import userEvent from "@testing-library/user-event";
+
+import { fireEvent, render, screen, within } from "__support__/ui";
+
+import {
+ ChartSettingSegmentsEditor,
+ type Segment,
+} from "./ChartSettingSegmentsEditor";
+
+const createMockSegment = (opts?: Partial<Segment>): Segment => {
+ return { label: "", min: 0, max: 100, color: "red", ...opts };
+};
+
+const DEFAULT_VALUE = [
+ createMockSegment({ label: "bad" }),
+ createMockSegment({ label: "good", min: 100, max: 200, color: "green" }),
+];
+
+const setup = () => {
+ const onChange = jest.fn();
+ render(
+ <ChartSettingSegmentsEditor value={DEFAULT_VALUE} onChange={onChange} />,
+ );
+ return { onChange };
+};
+
+it("Should render a segment editor", () => {
+ setup();
+
+ // Add a row for the header
+ expect(screen.getAllByRole("row")).toHaveLength(3);
+
+ const firstRow = screen.getAllByRole("row").at(1) as HTMLElement;
+
+ expect(within(firstRow).getByPlaceholderText(/Label for this/)).toHaveValue(
+ "bad",
+ );
+ expect(within(firstRow).getByPlaceholderText(/Min/)).toHaveValue("0");
+ expect(within(firstRow).getByPlaceholderText(/Max/)).toHaveValue("100");
+});
+
+it("Should pass back a new array of segments on change", async () => {
+ const { onChange } = setup();
+
+ const min = await screen.findByDisplayValue("0");
+
+ await userEvent.clear(min);
+ await userEvent.type(min, "20");
+ fireEvent.blur(min);
+
+ expect(onChange).toHaveBeenCalledWith(
+ expect.arrayContaining([
+ expect.objectContaining({ ...DEFAULT_VALUE[0], min: 20 }),
+ // Need to use objectContaining here to account for the 'key' values that are added
+ expect.objectContaining(DEFAULT_VALUE[1]),
+ ]),
+ );
+});
+
+it("Should allow you to remove a segment", async () => {
+ const { onChange } = setup();
+
+ await userEvent.click(
+ (await screen.findAllByRole("img", { name: /trash/ })).at(0) as HTMLElement,
+ );
+
+ expect(onChange).toHaveBeenCalledWith([
+ // Need to use objectContaining here to account for the 'key' values that are added
+ expect.objectContaining(DEFAULT_VALUE[1]),
+ ]);
+});
+
+it("Should allow you to add a new segment with apropriate defaults", async () => {
+ const { onChange } = setup();
+
+ await userEvent.click(
+ await screen.findByRole("button", { name: /Add a range/ }),
+ );
+
+ expect(onChange).toHaveBeenCalledWith([
+ // Need to use objectContaining here to account for the 'key' values that are added
+ expect.objectContaining(DEFAULT_VALUE[0]),
+ expect.objectContaining(DEFAULT_VALUE[1]),
+ expect.objectContaining({
+ min: 200,
+ max: 400,
+ color: expect.anything(),
+ }),
+ ]);
+});
diff --git a/frontend/src/metabase/visualizations/components/settings/ChartSettingSegmentsEditor/index.ts b/frontend/src/metabase/visualizations/components/settings/ChartSettingSegmentsEditor/index.ts
new file mode 100644
index 00000000000..e87935a229c
--- /dev/null
+++ b/frontend/src/metabase/visualizations/components/settings/ChartSettingSegmentsEditor/index.ts
@@ -0,0 +1 @@
+export * from "./ChartSettingSegmentsEditor";
diff --git a/frontend/src/metabase/visualizations/visualizations/Gauge/Gauge.jsx b/frontend/src/metabase/visualizations/visualizations/Gauge/Gauge.jsx
index 3f425ff1381..c62d98eb440 100644
--- a/frontend/src/metabase/visualizations/visualizations/Gauge/Gauge.jsx
+++ b/frontend/src/metabase/visualizations/visualizations/Gauge/Gauge.jsx
@@ -10,7 +10,7 @@ import _ from "underscore";
import CS from "metabase/css/core/index.css";
import { formatValue } from "metabase/lib/formatting";
import { color } from "metabase/ui/utils/colors";
-import ChartSettingGaugeSegments from "metabase/visualizations/components/settings/ChartSettingGaugeSegments";
+import { ChartSettingSegmentsEditor } from "metabase/visualizations/components/settings/ChartSettingSegmentsEditor";
import { columnSettings } from "metabase/visualizations/lib/settings/column";
import {
getDefaultSize,
@@ -37,7 +37,7 @@ const ARROW_STROKE_THICKNESS = 1.25;
const getBackgroundArcColor = () => color("bg-medium");
const getSegmentLabelColor = () => color("text-dark");
const getCenterLabelColor = () => color("text-dark");
-const getArrowFillColor = () => color("text-medium");
+const getArrowFillColor = () => color("text-medium-opaque");
const getArrowStrokeColor = () => color("bg-white");
// in ems, but within the scaled 100px SVG element
@@ -118,10 +118,7 @@ export default class Gauge extends Component {
},
"gauge.segments": {
get section() {
- return t`Display`;
- },
- get title() {
- return t`Gauge ranges`;
+ return t`Ranges`;
},
getDefault(series) {
let value = 100;
@@ -134,8 +131,9 @@ export default class Gauge extends Component {
{ min: value, max: value * 2, color: color("success"), label: "" },
];
},
- widget: ChartSettingGaugeSegments,
+ widget: ChartSettingSegmentsEditor,
persistDefault: true,
+ noPadding: true,
},
};
@@ -417,16 +415,19 @@ const GaugeArc = ({
};
const GaugeNeedle = ({ angle, isAnimated = true }) => (
- <path
- d={`M-${ARROW_BASE} 0 L0 -${ARROW_HEIGHT} L${ARROW_BASE} 0 Z`}
- transform={`translate(0,-${INNER_RADIUS}) rotate(${degrees(
- angle,
- )}, 0, ${INNER_RADIUS})`}
+ <g
+ transform={`rotate(${degrees(angle)})`}
style={isAnimated ? { transition: "transform 1.5s ease-in-out" } : null}
- stroke={getArrowStrokeColor()}
- strokeWidth={ARROW_STROKE_THICKNESS}
- fill={getArrowFillColor()}
- />
+ >
+ <path
+ d={`M-${ARROW_BASE} 0 L0 -${ARROW_HEIGHT} L${ARROW_BASE} 0 Z`}
+ transform={`translate(0,-${INNER_RADIUS})`}
+ style={isAnimated ? { transition: "transform 1.5s ease-in-out" } : null}
+ stroke={getArrowStrokeColor()}
+ strokeWidth={ARROW_STROKE_THICKNESS}
+ fill={getArrowFillColor()}
+ />
+ </g>
);
const GaugeSegmentLabel = ({ position: [x, y], style = {}, children }) => (