Skip to content

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

GDGT-1775 Convert Gauge Viz to Functional Component + Typescript (#69468)

Kamil Mielnik3339413f6ed6
diff --git a/frontend/src/metabase/visualizations/visualizations/Gauge/Gauge.jsx b/frontend/src/metabase/visualizations/visualizations/Gauge/Gauge.jsx
deleted file mode 100644
index 3b04b1733f3..00000000000
--- a/frontend/src/metabase/visualizations/visualizations/Gauge/Gauge.jsx
+++ /dev/null
@@ -1,495 +0,0 @@
-/* eslint-disable react/prop-types */
-import cx from "classnames";
-import Color from "color";
-import * as d3 from "d3";
-import { Component, useCallback, useEffect, useRef, useState } from "react";
-import * as React from "react";
-import { t } from "ttag";
-import _ from "underscore";
-
-import CS from "metabase/css/core/index.css";
-import { color as colorHex } from "metabase/lib/colors";
-import { formatValue } from "metabase/lib/formatting";
-import { color } from "metabase/ui/utils/colors";
-import { ChartSettingSegmentsEditor } from "metabase/visualizations/components/settings/ChartSettingSegmentsEditor";
-import { columnSettings } from "metabase/visualizations/lib/settings/column";
-import { segmentIsValid } from "metabase/visualizations/lib/utils";
-import {
- getDefaultSize,
- getMinSize,
-} from "metabase/visualizations/shared/utils/sizes";
-import { isDate, isNumeric } from "metabase-lib/v1/types/utils/isa";
-
-import { GaugeArcPath } from "./Gauge.styled";
-import { getValue } from "./utils";
-
-const MAX_WIDTH = 500;
-const PADDING_BOTTOM = 10;
-
-const OUTER_RADIUS = 45; // within 100px SVG element
-const INNER_RADIUS_RATIO = 3.7 / 5;
-const INNER_RADIUS = OUTER_RADIUS * INNER_RADIUS_RATIO;
-
-// arrow shape, currently an equilateral triangle
-const ARROW_HEIGHT = ((OUTER_RADIUS - INNER_RADIUS) * 2.5) / 4; // 2/3 of segment thickness
-const ARROW_BASE = ARROW_HEIGHT / Math.tan((64 / 180) * Math.PI);
-const ARROW_STROKE_THICKNESS = 1.25;
-
-// colors
-const getBackgroundArcColor = () => color("background-tertiary");
-const getSegmentLabelColor = () => color("text-primary");
-const getCenterLabelColor = () => color("text-primary");
-const getArrowFillColor = () => color("text-secondary-opaque");
-const getArrowStrokeColor = () => color("background-primary");
-
-// in px, because scaling was not working well with PDF Exports (metabase#65322)
-const FONT_SIZE_SEGMENT_LABEL = 4;
-
-// in ems, but within the scaled 100px SVG element
-const FONT_SIZE_CENTER_LABEL_MIN = 0.5;
-const FONT_SIZE_CENTER_LABEL_MAX = 0.7;
-
-// hide labels if SVG width is smaller than this
-const MIN_WIDTH_LABEL_THRESHOLD = 250;
-
-const LABEL_OFFSET_PERCENT = 1.025;
-
-// total degrees of the arc (180 = semicircle, etc)
-const ARC_DEGREES = 180 + 45 * 2; // semicircle plus a bit
-
-const radians = (degrees) => (degrees * Math.PI) / 180;
-const degrees = (radians) => (radians * 180) / Math.PI;
-
-export class Gauge extends Component {
- static getUiName = () => t`Gauge`;
- static identifier = "gauge";
- static iconName = "gauge";
-
- static minSize = getMinSize("gauge");
- static defaultSize = getDefaultSize("gauge");
-
- static isSensible({ cols, rows }) {
- return rows.length === 1 && cols.length === 1;
- }
-
- static checkRenderable([
- {
- data: { cols, rows },
- },
- ]) {
- if (!isNumeric(cols[0]) || isDate(cols[0])) {
- throw new Error(t`Gauge visualization requires a number.`);
- }
- }
-
- constructor(props) {
- super(props);
-
- /** @type {React.RefObject<SVGTextElement>} */
- this.labelRef = React.createRef();
-
- this.state = { mounted: false };
- }
-
- static settings = {
- ...columnSettings({
- getColumns: (
- [
- {
- data: { cols },
- },
- ],
- settings,
- ) => [
- _.find(cols, (col) => col.name === settings["scalar.field"]) || cols[0],
- ],
- }),
- "gauge.range": {
- // currently not exposed in settings, just computed from gauge.segments
- getDefault(series, vizSettings) {
- const segments = vizSettings["gauge.segments"].filter(segmentIsValid);
- const values = [
- ...segments.map((s) => s.max),
- ...segments.map((s) => s.min),
- ];
- return values.length > 0
- ? [Math.min(...values), Math.max(...values)]
- : [0, 1];
- },
- readDependencies: ["gauge.segments"],
- },
- "gauge.segments": {
- get section() {
- return t`Ranges`;
- },
- getDefault(series) {
- let value = 100;
- try {
- value = series[0].data.rows[0][0] || 0;
- } catch (e) {}
- const errorColor = Color(colorHex("error")).hex();
- const warningColor = Color(colorHex("warning")).hex();
- const successColor = Color(colorHex("success")).hex();
- return [
- { min: 0, max: value / 2, color: errorColor, label: "" },
- { min: value / 2, max: value, color: warningColor, label: "" },
- { min: value, max: value * 2, color: successColor, label: "" },
- ];
- },
- widget: ChartSettingSegmentsEditor,
- persistDefault: true,
- noPadding: true,
- },
- };
-
- componentDidMount() {
- this.setState({ mounted: true });
- this._updateLabelSize();
- }
- componentDidUpdate() {
- this._updateLabelSize();
- }
-
- _updateLabelSize() {
- const label = this.labelRef.current;
- if (label) {
- const { width: currentWidth } = label.getBBox();
- // maxWidth currently 95% of inner diameter, could be more intelligent based on text aspect ratio
- const maxWidth = INNER_RADIUS * 2 * 0.95;
- const currentFontSize = parseFloat(
- label.style.fontSize.replace("em", ""),
- );
- // scale the font based on currentWidth/maxWidth, within min and max
- // TODO: if text is too big wrap or ellipsis?
- const desiredFontSize = Math.max(
- FONT_SIZE_CENTER_LABEL_MIN,
- Math.min(
- FONT_SIZE_CENTER_LABEL_MAX,
- currentFontSize * (maxWidth / currentWidth),
- ),
- );
- // don't resize if within 5% to avoid potential thrashing
- if (Math.abs(1 - currentFontSize / desiredFontSize) > 0.05) {
- label.style.fontSize = desiredFontSize + "em";
- }
- }
- }
-
- render() {
- const {
- series: [
- {
- data: { rows, cols },
- },
- ],
- settings,
- className,
- isSettings,
- onHoverChange,
- visualizationIsClickable,
- onVisualizationClick,
- } = this.props;
-
- const width = this.props.width;
- const height = this.props.height - PADDING_BOTTOM;
-
- const viewBoxHeight =
- (ARC_DEGREES > 180 ? 50 : 0) + Math.sin(radians(ARC_DEGREES / 2)) * 50;
- const viewBoxWidth = 100;
-
- const svgAspectRatio = viewBoxHeight / viewBoxWidth;
- const containerAspectRadio = height / width;
-
- let svgWidth;
- if (containerAspectRadio < svgAspectRatio) {
- svgWidth = Math.min(MAX_WIDTH, height / svgAspectRatio);
- } else {
- svgWidth = Math.min(MAX_WIDTH, width);
- }
- const svgHeight = svgWidth * svgAspectRatio;
-
- const showLabels = svgWidth > MIN_WIDTH_LABEL_THRESHOLD;
-
- const range = settings["gauge.range"];
- const segments = settings["gauge.segments"].filter(segmentIsValid);
-
- // value to angle in radians, clamped
- const angle = d3
- .scaleLinear()
- .domain(range) // NOTE: confusing, but the "range" is the domain for the arc scale
- .range([
- ((ARC_DEGREES / 180) * -Math.PI) / 2,
- ((ARC_DEGREES / 180) * Math.PI) / 2,
- ])
- .clamp(true);
-
- const value = getValue(rows);
- const column = cols[0];
-
- const valuePosition = (value, distance) => {
- return [
- Math.cos(angle(value) - Math.PI / 2) * distance,
- Math.sin(angle(value) - Math.PI / 2) * distance,
- ];
- };
-
- // get unique min/max plus range endpoints
- const numberLabels = Array.from(
- new Set(
- range.concat(...segments.map((segment) => [segment.min, segment.max])),
- ),
- );
-
- const textLabels = segments
- .filter((segment) => segment.label)
- .map((segment) => ({
- label: segment.label,
- value: segment.min + (segment.max - segment.min) / 2,
- }));
-
- // expand the width to fill available space so that labels don't overflow as often
- const expandWidthFactor = width / svgWidth;
-
- return (
- <div className={cx(className, CS.relative)}>
- <div
- className={cx(CS.absolute, CS.overflowHidden)}
- style={{
- width: svgWidth * expandWidthFactor,
- height: svgHeight,
- top: (height - svgHeight) / 2,
- left:
- (width - svgWidth) / 2 -
- // shift to the left the
- (svgWidth * expandWidthFactor - svgWidth) / 2,
- }}
- >
- <svg
- viewBox={`0 0 ${viewBoxWidth * expandWidthFactor} ${viewBoxHeight}`}
- >
- <g
- transform={`translate(${
- (viewBoxWidth * expandWidthFactor) / 2
- },50)`}
- >
- {/* BACKGROUND ARC */}
- <GaugeArc
- start={angle(range[0])}
- end={angle(range[1])}
- fill={getBackgroundArcColor()}
- />
- {/* SEGMENT ARCS */}
- {segments.map((segment, index) => (
- <GaugeArc
- key={index}
- start={angle(segment.min)}
- end={angle(segment.max)}
- fill={segment.color}
- segment={segment}
- column={column}
- settings={settings}
- visualizationIsClickable={visualizationIsClickable}
- testId={"gauge-arc-" + index}
- onHoverChange={!showLabels ? onHoverChange : null}
- onVisualizationClick={onVisualizationClick}
- />
- ))}
- {/* NEEDLE */}
- <GaugeNeedle
- angle={angle(this.state.mounted ? value : 0)}
- isAnimated={!isSettings}
- />
- {/* NUMBER LABELS */}
- {showLabels &&
- numberLabels.map((value, index) => (
- <GaugeSegmentLabel
- key={index}
- position={valuePosition(
- value,
- OUTER_RADIUS * LABEL_OFFSET_PERCENT,
- )}
- >
- {formatValue(value, settings.column(column))}
- </GaugeSegmentLabel>
- ))}
- {/* TEXT LABELS */}
- {showLabels &&
- textLabels.map(({ label, value }, index) => (
- <HideIfOverflowingSVG key={index}>
- <GaugeSegmentLabel
- position={valuePosition(
- value,
- OUTER_RADIUS * LABEL_OFFSET_PERCENT,
- )}
- style={{
- fill: getSegmentLabelColor(),
- }}
- >
- {label}
- </GaugeSegmentLabel>
- </HideIfOverflowingSVG>
- ))}
- {/* CENTER LABEL */}
- {/* NOTE: can't be a component because ref doesn't work? */}
- <text
- ref={this.labelRef}
- x={0}
- y={0}
- style={{
- fill: getCenterLabelColor(),
- fontSize: "1em",
- fontWeight: "bold",
- textAnchor: "middle",
- transform: "translate(0,0.2em)",
- }}
- >
- {formatValue(value, settings.column(column))}
- </text>
- </g>
- </svg>
- </div>
- </div>
- );
- }
-}
-
-const GaugeArc = ({
- start,
- end,
- fill,
- segment,
- settings,
- column,
- visualizationIsClickable,
- testId,
- onHoverChange,
- onVisualizationClick,
-}) => {
- const arc = d3
- .arc()
- .outerRadius(OUTER_RADIUS)
- .innerRadius(OUTER_RADIUS * INNER_RADIUS_RATIO);
-
- const isClickable = segment != null && onVisualizationClick != null;
- const options = column && settings?.column ? settings.column(column) : {};
- const range = segment ? [segment.min, segment.max] : [];
- const value = range.map((v) => formatValue(v, options)).join(" - ");
- const hovered = segment ? { data: [{ key: segment.label, value }] } : {};
-
- const handleClick = (e) => {
- if (!segment) {
- return;
- }
- const clickData = {
- value: segment.min,
- column,
- settings,
- event: e.nativeEvent,
- };
- if (onVisualizationClick && visualizationIsClickable(clickData)) {
- onVisualizationClick(clickData);
- }
- };
-
- const handleMouseMove = (e) => {
- if (onHoverChange) {
- onHoverChange({ ...hovered, event: e.nativeEvent });
- }
- };
-
- const handleMouseLeave = () => {
- if (onHoverChange) {
- onHoverChange(null);
- }
- };
-
- return (
- <GaugeArcPath
- d={arc({
- startAngle: start,
- endAngle: end,
- })}
- fill={fill}
- isClickable={isClickable}
- data-testid={testId}
- onClick={handleClick}
- onMouseMove={handleMouseMove}
- onMouseLeave={handleMouseLeave}
- />
- );
-};
-
-const GaugeNeedle = ({ angle, isAnimated = true }) => (
- <g
- transform={`rotate(${degrees(angle)})`}
- style={isAnimated ? { transition: "transform 1.5s ease-in-out" } : null}
- >
- <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 }) => (
- <text
- x={x}
- y={y}
- style={{
- fill: "var(--mb-color-text-secondary)",
- fontSize: `${FONT_SIZE_SEGMENT_LABEL}px`,
- textAnchor: Math.abs(x) < 5 ? "middle" : x > 0 ? "start" : "end",
- // shift text in the lower half down a bit
- transform:
- y > 0 ? `translate(0,${FONT_SIZE_SEGMENT_LABEL / 2}px)` : undefined,
- ...style,
- }}
- >
- {children}
- </text>
-);
-
-const HideIfOverflowingSVG = ({ children }) => {
- const elementRef = useRef(null);
- const [isHidden, setIsHidden] = useState(false);
-
- const hideIfClipped = useCallback(() => {
- const element = elementRef.current;
-
- if (element) {
- let svg = element;
-
- while (svg && svg.nodeName.toLowerCase() !== "svg") {
- svg = svg.parentNode;
- }
-
- if (svg) {
- const svgRect = svg.getBoundingClientRect();
- const elementRect = element.getBoundingClientRect();
-
- const shouldBeHidden = !(
- elementRect.left >= svgRect.left &&
- elementRect.right <= svgRect.right &&
- elementRect.top >= svgRect.top &&
- elementRect.bottom <= svgRect.bottom
- );
-
- setIsHidden(shouldBeHidden);
- }
- }
- }, []);
-
- useEffect(() => {
- hideIfClipped();
- });
-
- return (
- <g ref={elementRef} className={isHidden ? CS.hidden : undefined}>
- {children}
- </g>
- );
-};
diff --git a/frontend/src/metabase/visualizations/visualizations/Gauge/Gauge.styled.tsx b/frontend/src/metabase/visualizations/visualizations/Gauge/Gauge.styled.tsx
deleted file mode 100644
index 6aac7daca58..00000000000
--- a/frontend/src/metabase/visualizations/visualizations/Gauge/Gauge.styled.tsx
+++ /dev/null
@@ -1,10 +0,0 @@
-// eslint-disable-next-line no-restricted-imports
-import styled from "@emotion/styled";
-
-interface GaugeArcPathProps {
- isClickable: boolean;
-}
-
-export const GaugeArcPath = styled.path<GaugeArcPathProps>`
- cursor: ${(props) => props.isClickable && "pointer"};
-`;
diff --git a/frontend/src/metabase/visualizations/visualizations/Gauge/Gauge.tsx b/frontend/src/metabase/visualizations/visualizations/Gauge/Gauge.tsx
new file mode 100644
index 00000000000..f0f14026617
--- /dev/null
+++ b/frontend/src/metabase/visualizations/visualizations/Gauge/Gauge.tsx
@@ -0,0 +1,247 @@
+import { useMounted } from "@mantine/hooks";
+import cx from "classnames";
+import * as d3 from "d3";
+import { useCallback, useEffect, useRef } from "react";
+
+import CS from "metabase/css/core/index.css";
+import { formatValue } from "metabase/lib/formatting";
+import { segmentIsValid } from "metabase/visualizations/lib/utils";
+import type { VisualizationProps } from "metabase/visualizations/types";
+
+import { GaugeArc } from "./GaugeArc";
+import { GaugeNeedle } from "./GaugeNeedle";
+import { GaugeSegmentLabel } from "./GaugeSegmentLabel";
+import { HideIfOverflowingSVG } from "./HideIfOverflowingSVG";
+import { GAUGE_CHART_DEFINITION } from "./chart-definition";
+import {
+ ARC_DEGREES,
+ FONT_SIZE_CENTER_LABEL_MAX,
+ FONT_SIZE_CENTER_LABEL_MIN,
+ INNER_RADIUS,
+ LABEL_OFFSET_PERCENT,
+ MAX_WIDTH,
+ MIN_WIDTH_LABEL_THRESHOLD,
+ OUTER_RADIUS,
+ PADDING_BOTTOM,
+ getBackgroundArcColor,
+ getCenterLabelColor,
+ getSegmentLabelColor,
+} from "./constants";
+import { isGaugeRange, isGaugeSegmentsArray } from "./types";
+import { getValue, radians } from "./utils";
+
+Object.assign(Gauge, GAUGE_CHART_DEFINITION);
+
+export function Gauge({
+ className,
+ isSettings,
+ height: heightProp,
+ series: [
+ {
+ data: { rows, cols },
+ },
+ ],
+ settings,
+ visualizationIsClickable,
+ width,
+ onVisualizationClick,
+ onHoverChange,
+}: VisualizationProps) {
+ const labelRef = useRef<SVGTextElement>(null);
+ const isMounted = useMounted();
+
+ const height = heightProp - PADDING_BOTTOM;
+
+ const viewBoxHeight =
+ (ARC_DEGREES > 180 ? 50 : 0) + Math.sin(radians(ARC_DEGREES / 2)) * 50;
+ const viewBoxWidth = 100;
+
+ const svgAspectRatio = viewBoxHeight / viewBoxWidth;
+ const containerAspectRadio = height / width;
+
+ let svgWidth;
+ if (containerAspectRadio < svgAspectRatio) {
+ svgWidth = Math.min(MAX_WIDTH, height / svgAspectRatio);
+ } else {
+ svgWidth = Math.min(MAX_WIDTH, width);
+ }
+ const svgHeight = svgWidth * svgAspectRatio;
+
+ const showLabels = svgWidth > MIN_WIDTH_LABEL_THRESHOLD;
+
+ const gaugeRange = settings["gauge.range"];
+ const range: number[] = isGaugeRange(gaugeRange) ? gaugeRange : [];
+ const gaugeSegments = settings["gauge.segments"];
+ const segments = isGaugeSegmentsArray(gaugeSegments)
+ ? gaugeSegments.filter((segment) => segmentIsValid(segment))
+ : [];
+
+ // value to angle in radians, clamped
+ const angle = d3
+ .scaleLinear()
+ .domain(range) // NOTE: confusing, but the "range" is the domain for the arc scale
+ .range([
+ ((ARC_DEGREES / 180) * -Math.PI) / 2,
+ ((ARC_DEGREES / 180) * Math.PI) / 2,
+ ])
+ .clamp(true);
+
+ const value = getValue(rows);
+ const column = cols[0];
+
+ const valuePosition = (value: number, distance: number): [number, number] => {
+ return [
+ Math.cos(angle(value) - Math.PI / 2) * distance,
+ Math.sin(angle(value) - Math.PI / 2) * distance,
+ ];
+ };
+
+ // get unique min/max plus range endpoints
+ const numberLabels = Array.from(
+ new Set(
+ range.concat(...segments.map((segment) => [segment.min, segment.max])),
+ ),
+ );
+
+ const textLabels = segments
+ .filter((segment) => segment.label)
+ .map((segment) => ({
+ label: segment.label,
+ value: segment.min + (segment.max - segment.min) / 2,
+ }));
+
+ // expand the width to fill available space so that labels don't overflow as often
+ const expandWidthFactor = width / svgWidth;
+ const columnSettings = column && settings.column && settings.column(column);
+
+ const updateLabelSize = useCallback(() => {
+ const label = labelRef.current;
+
+ if (label) {
+ const { width: currentWidth } = label.getBBox();
+ // maxWidth currently 95% of inner diameter, could be more intelligent based on text aspect ratio
+ const maxWidth = INNER_RADIUS * 2 * 0.95;
+ const currentFontSize = parseFloat(
+ label.style.fontSize.replace("em", ""),
+ );
+ // scale the font based on currentWidth/maxWidth, within min and max
+ // TODO: if text is too big wrap or ellipsis?
+ const desiredFontSize = Math.max(
+ FONT_SIZE_CENTER_LABEL_MIN,
+ Math.min(
+ FONT_SIZE_CENTER_LABEL_MAX,
+ currentFontSize * (maxWidth / currentWidth),
+ ),
+ );
+ // don't resize if within 5% to avoid potential thrashing
+ if (Math.abs(1 - currentFontSize / desiredFontSize) > 0.05) {
+ label.style.fontSize = desiredFontSize + "em";
+ }
+ }
+ }, []);
+
+ useEffect(() => {
+ updateLabelSize();
+ });
+
+ return (
+ <div className={cx(className, CS.relative)}>
+ <div
+ className={cx(CS.absolute, CS.overflowHidden)}
+ style={{
+ width: svgWidth * expandWidthFactor,
+ height: svgHeight,
+ top: (height - svgHeight) / 2,
+ left:
+ (width - svgWidth) / 2 -
+ // shift to the left the
+ (svgWidth * expandWidthFactor - svgWidth) / 2,
+ }}
+ >
+ <svg
+ viewBox={`0 0 ${viewBoxWidth * expandWidthFactor} ${viewBoxHeight}`}
+ >
+ <g
+ transform={`translate(${
+ (viewBoxWidth * expandWidthFactor) / 2
+ },50)`}
+ >
+ {/* BACKGROUND ARC */}
+ <GaugeArc
+ start={angle(range[0])}
+ end={angle(range[1])}
+ fill={getBackgroundArcColor()}
+ />
+ {/* SEGMENT ARCS */}
+ {segments.map((segment, index) => (
+ <GaugeArc
+ column={column}
+ end={angle(segment.max)}
+ fill={segment.color}
+ key={index}
+ segment={segment}
+ settings={settings}
+ start={angle(segment.min)}
+ testId={"gauge-arc-" + index}
+ visualizationIsClickable={visualizationIsClickable}
+ onHoverChange={showLabels ? undefined : onHoverChange}
+ onVisualizationClick={onVisualizationClick}
+ />
+ ))}
+ {/* NEEDLE */}
+ <GaugeNeedle
+ angle={angle(isMounted ? value : 0)}
+ isAnimated={!isSettings}
+ />
+ {/* NUMBER LABELS */}
+ {showLabels &&
+ numberLabels.map((value, index) => (
+ <GaugeSegmentLabel
+ key={index}
+ position={valuePosition(
+ value,
+ OUTER_RADIUS * LABEL_OFFSET_PERCENT,
+ )}
+ >
+ {formatValue(value, columnSettings)}
+ </GaugeSegmentLabel>
+ ))}
+ {/* TEXT LABELS */}
+ {showLabels &&
+ textLabels.map(({ label, value }, index) => (
+ <HideIfOverflowingSVG key={index}>
+ <GaugeSegmentLabel
+ position={valuePosition(
+ value,
+ OUTER_RADIUS * LABEL_OFFSET_PERCENT,
+ )}
+ style={{
+ fill: getSegmentLabelColor(),
+ }}
+ >
+ {label}
+ </GaugeSegmentLabel>
+ </HideIfOverflowingSVG>
+ ))}
+ {/* CENTER LABEL */}
+ {/* NOTE: can't be a component because ref doesn't work? */}
+ <text
+ ref={labelRef}
+ x={0}
+ y={0}
+ style={{
+ fill: getCenterLabelColor(),
+ fontSize: "1em",
+ fontWeight: "bold",
+ textAnchor: "middle",
+ transform: "translate(0,0.2em)",
+ }}
+ >
+ {formatValue(value, columnSettings)}
+ </text>
+ </g>
+ </svg>
+ </div>
+ </div>
+ );
+}
diff --git a/frontend/src/metabase/visualizations/visualizations/Gauge/GaugeArc.tsx b/frontend/src/metabase/visualizations/visualizations/Gauge/GaugeArc.tsx
new file mode 100644
index 00000000000..50f1b28811c
--- /dev/null
+++ b/frontend/src/metabase/visualizations/visualizations/Gauge/GaugeArc.tsx
@@ -0,0 +1,98 @@
+import * as d3 from "d3";
+import type { MouseEvent } from "react";
+
+import { formatValue } from "metabase/lib/formatting";
+import type {
+ ClickObject,
+ ComputedVisualizationSettings,
+ VisualizationProps,
+} from "metabase/visualizations/types";
+import type { DatasetColumn } from "metabase-types/api";
+
+import { INNER_RADIUS_RATIO, OUTER_RADIUS } from "./constants";
+import type { GaugeSegment } from "./types";
+
+interface Props {
+ column?: DatasetColumn;
+ end: number;
+ fill: string | undefined;
+ segment?: GaugeSegment;
+ settings?: ComputedVisualizationSettings;
+ start: number;
+ testId?: string;
+ visualizationIsClickable?: VisualizationProps["visualizationIsClickable"];
+ onHoverChange?: VisualizationProps["onHoverChange"];
+ onVisualizationClick?: VisualizationProps["onVisualizationClick"];
+}
+
+export const GaugeArc = ({
+ column,
+ end,
+ fill,
+ segment,
+ settings,
+ start,
+ testId,
+ visualizationIsClickable,
+ onHoverChange,
+ onVisualizationClick,
+}: Props) => {
+ const arc = d3.arc();
+
+ const isClickable = segment != null && onVisualizationClick != null;
+ const options = column && settings?.column ? settings.column(column) : {};
+ const range = segment ? [segment.min, segment.max] : [];
+ const value = range.map((v) => formatValue(v, options)).join(" - ");
+
+ const handleClick = (event: MouseEvent<SVGPathElement>) => {
+ if (!segment) {
+ return;
+ }
+ const clickData: ClickObject = {
+ value: segment.min,
+ column,
+ settings,
+ event: event.nativeEvent,
+ };
+
+ if (onVisualizationClick && visualizationIsClickable?.(clickData)) {
+ onVisualizationClick(clickData);
+ }
+ };
+
+ const handleMouseMove = (event: MouseEvent<SVGPathElement>) => {
+ if (onHoverChange && segment?.label) {
+ onHoverChange({
+ data: [{ key: segment.label, col: null, value }],
+ event: event.nativeEvent,
+ });
+ }
+ };
+
+ const handleMouseLeave = () => {
+ if (onHoverChange) {
+ onHoverChange(null);
+ }
+ };
+
+ return (
+ <path
+ d={
+ arc({
+ startAngle: start,
+ endAngle: end,
+ innerRadius: OUTER_RADIUS * INNER_RADIUS_RATIO,
+ outerRadius: OUTER_RADIUS,
+ }) ?? undefined
+ }
+ data-testid={testId}
+ fill={fill}
+ style={{
+ cursor: isClickable ? "pointer" : undefined,
+ }}
+ onClick={handleClick}
+ onMouseLeave={handleMouseLeave}
+ onMouseMove={handleMouseMove}
+ />
+ );
+};
diff --git a/frontend/src/metabase/visualizations/visualizations/Gauge/GaugeNeedle.tsx b/frontend/src/metabase/visualizations/visualizations/Gauge/GaugeNeedle.tsx
new file mode 100644
index 00000000000..68615653bf8
--- /dev/null
+++ b/frontend/src/metabase/visualizations/visualizations/Gauge/GaugeNeedle.tsx
@@ -0,0 +1,34 @@
+import {
+ ARROW_BASE,
+ ARROW_HEIGHT,
+ ARROW_STROKE_THICKNESS,
+ INNER_RADIUS,
+ getArrowFillColor,
+ getArrowStrokeColor,
+} from "./constants";
+import { degrees } from "./utils";
+
+interface Props {
+ angle: number;
+ isAnimated?: boolean;
+}
+
+export const GaugeNeedle = ({ angle, isAnimated = true }: Props) => (
+ <g
+ style={{
+ transition: isAnimated ? "transform 1.5s ease-in-out" : undefined,
+ }}
+ transform={`rotate(${degrees(angle)})`}
+ >
+ <path
+ d={`M-${ARROW_BASE} 0 L0 -${ARROW_HEIGHT} L${ARROW_BASE} 0 Z`}
+ fill={getArrowFillColor()}
+ stroke={getArrowStrokeColor()}
+ strokeWidth={ARROW_STROKE_THICKNESS}
+ style={{
+ transition: isAnimated ? "transform 1.5s ease-in-out" : undefined,
+ }}
+ transform={`translate(0,-${INNER_RADIUS})`}
+ />
+ </g>
+);
diff --git a/frontend/src/metabase/visualizations/visualizations/Gauge/GaugeSegmentLabel.tsx b/frontend/src/metabase/visualizations/visualizations/Gauge/GaugeSegmentLabel.tsx
new file mode 100644
index 00000000000..f50feb57fc4
--- /dev/null
+++ b/frontend/src/metabase/visualizations/visualizations/Gauge/GaugeSegmentLabel.tsx
@@ -0,0 +1,31 @@
+import type { CSSProperties, ReactNode } from "react";
+
+import { FONT_SIZE_SEGMENT_LABEL } from "./constants";
+
+interface Props {
+ children: ReactNode;
+ position: [number, number];
+ style?: CSSProperties;
+}
+
+export const GaugeSegmentLabel = ({
+ children,
+ position: [x, y],
+ style,
+}: Props) => (
+ <text
+ style={{
+ fill: "var(--mb-color-text-secondary)",
+ fontSize: `${FONT_SIZE_SEGMENT_LABEL}px`,
+ textAnchor: Math.abs(x) < 5 ? "middle" : x > 0 ? "start" : "end",
+ // shift text in the lower half down a bit
+ transform:
+ y > 0 ? `translate(0,${FONT_SIZE_SEGMENT_LABEL / 2}px)` : undefined,
+ ...style,
+ }}
+ x={x}
+ y={y}
+ >
+ {children}
+ </text>
+);
diff --git a/frontend/src/metabase/visualizations/visualizations/Gauge/HideIfOverflowingSVG.tsx b/frontend/src/metabase/visualizations/visualizations/Gauge/HideIfOverflowingSVG.tsx
new file mode 100644
index 00000000000..7a515a388ed
--- /dev/null
+++ b/frontend/src/metabase/visualizations/visualizations/Gauge/HideIfOverflowingSVG.tsx
@@ -0,0 +1,57 @@
+import {
+ type ReactNode,
+ useCallback,
+ useEffect,
+ useRef,
+ useState,
+} from "react";
+
+interface Props {
+ children: ReactNode;
+}
+
+export const HideIfOverflowingSVG = ({ children }: Props) => {
+ const elementRef = useRef<SVGGElement>(null);
+ const [isHidden, setIsHidden] = useState(false);
+
+ const hideIfClipped = useCallback(() => {
+ const element = elementRef.current;
+
+ if (element) {
+ let svg: Node | null = element;
+
+ while (svg && svg.nodeName.toLowerCase() !== "svg") {
+ svg = svg.parentNode;
+ }
+
+ if (svg instanceof SVGElement) {
+ const svgRect = svg.getBoundingClientRect();
+ const elementRect = element.getBoundingClientRect();
+
+ const shouldBeHidden = !(
+ elementRect.left >= svgRect.left &&
+ elementRect.right <= svgRect.right &&
+ elementRect.top >= svgRect.top &&
+ elementRect.bottom <= svgRect.bottom
+ );
+
+ setIsHidden(shouldBeHidden);
+ }
+ }
+ }, []);
+
+ useEffect(() => {
+ hideIfClipped();
+ });
+
+ return (
+ <g
+ ref={elementRef}
+ style={{
+ visibility: isHidden ? "hidden" : undefined,
+ }}
+ >
+ {children}
+ </g>
+ );
+};
diff --git a/frontend/src/metabase/visualizations/visualizations/Gauge/chart-definition.ts b/frontend/src/metabase/visualizations/visualizations/Gauge/chart-definition.ts
new file mode 100644
index 00000000000..72bf0e0dac0
--- /dev/null
+++ b/frontend/src/metabase/visualizations/visualizations/Gauge/chart-definition.ts
@@ -0,0 +1,96 @@
+import Color from "color";
+import { t } from "ttag";
+import _ from "underscore";
+
+import { color as colorHex } from "metabase/lib/colors";
+import { ChartSettingSegmentsEditor } from "metabase/visualizations/components/settings/ChartSettingSegmentsEditor";
+import { columnSettings } from "metabase/visualizations/lib/settings/column";
+import { segmentIsValid } from "metabase/visualizations/lib/utils";
+import {
+ getDefaultSize,
+ getMinSize,
+} from "metabase/visualizations/shared/utils/sizes";
+import type {
+ VisualizationDefinition,
+ VisualizationSettingsDefinitions,
+} from "metabase/visualizations/types";
+import { isDate, isNumeric } from "metabase-lib/v1/types/utils/isa";
+
+import { isGaugeSegmentsArray } from "./types";
+
+export const GAUGE_CHART_DEFINITION: VisualizationDefinition = {
+ getUiName: () => t`Gauge`,
+ identifier: "gauge",
+ iconName: "gauge",
+ minSize: getMinSize("gauge"),
+ defaultSize: getDefaultSize("gauge"),
+ isSensible: ({ cols, rows }) => {
+ return rows.length === 1 && cols.length === 1;
+ },
+ checkRenderable: ([
+ {
+ data: { cols },
+ },
+ ]) => {
+ if (!isNumeric(cols[0]) || isDate(cols[0])) {
+ throw new Error(t`Gauge visualization requires a number.`);
+ }
+ },
+ settings: {
+ ...columnSettings({
+ getColumns: (
+ [
+ {
+ data: { cols },
+ },
+ ],
+ settings,
+ ) => [
+ _.find(cols, (col) => col.name === settings["scalar.field"]) || cols[0],
+ ],
+ }),
+ "gauge.range": {
+ // currently not exposed in settings, just computed from gauge.segments
+ getDefault(series, vizSettings) {
+ const gaugeSegments = vizSettings["gauge.segments"];
+ const segments = isGaugeSegmentsArray(gaugeSegments)
+ ? gaugeSegments.filter((segment) => segmentIsValid(segment))
+ : [];
+ const values = [
+ ...segments.map((segment) => segment.max),
+ ...segments.map((segment) => segment.min),
+ ];
+ return values.length > 0
+ ? [Math.min(...values), Math.max(...values)]
+ : [0, 1];
+ },
+ readDependencies: ["gauge.segments"],
+ },
+ "gauge.segments": {
+ get section() {
+ return t`Ranges`;
+ },
+ getDefault(series) {
+ let value = 100;
+ try {
+ const defaultValue = series[0].data.rows[0][0] || 0;
+
+ if (typeof defaultValue === "number") {
+ value = defaultValue;
+ }
+ } catch (error) {}
+ const errorColor = Color(colorHex("error")).hex();
+ const warningColor = Color(colorHex("warning")).hex();
+ const successColor = Color(colorHex("success")).hex();
+ return [
+ { min: 0, max: value / 2, color: errorColor, label: "" },
+ { min: value / 2, max: value, color: warningColor, label: "" },
+ { min: value, max: value * 2, color: successColor, label: "" },
+ ];
+ },
+ widget: ChartSettingSegmentsEditor,
+ persistDefault: true,
+ noPadding: true,
+ },
+ } as VisualizationSettingsDefinitions,
+};
diff --git a/frontend/src/metabase/visualizations/visualizations/Gauge/constants.ts b/frontend/src/metabase/visualizations/visualizations/Gauge/constants.ts
new file mode 100644
index 00000000000..25924c70432
--- /dev/null
+++ b/frontend/src/metabase/visualizations/visualizations/Gauge/constants.ts
@@ -0,0 +1,35 @@
+import { color } from "metabase/ui/utils/colors";
+
+export const MAX_WIDTH = 500;
+export const PADDING_BOTTOM = 10;
+
+export const OUTER_RADIUS = 45; // within 100px SVG element
+export const INNER_RADIUS_RATIO = 3.7 / 5;
+export const INNER_RADIUS = OUTER_RADIUS * INNER_RADIUS_RATIO;
+
+// arrow shape, currently an equilateral triangle
+export const ARROW_HEIGHT = ((OUTER_RADIUS - INNER_RADIUS) * 2.5) / 4; // 2/3 of segment thickness
+export const ARROW_BASE = ARROW_HEIGHT / Math.tan((64 / 180) * Math.PI);
+export const ARROW_STROKE_THICKNESS = 1.25;
+
+// colors
+export const getBackgroundArcColor = () => color("background-tertiary");
+export const getSegmentLabelColor = () => color("text-primary");
+export const getCenterLabelColor = () => color("text-primary");
+export const getArrowFillColor = () => color("text-secondary-opaque");
+export const getArrowStrokeColor = () => color("background-primary");
+
+// in px, because scaling was not working well with PDF Exports (metabase#65322)
+export const FONT_SIZE_SEGMENT_LABEL = 4;
+
+// in ems, but within the scaled 100px SVG element
+export const FONT_SIZE_CENTER_LABEL_MIN = 0.5;
+export const FONT_SIZE_CENTER_LABEL_MAX = 0.7;
+
+// hide labels if SVG width is smaller than this
+export const MIN_WIDTH_LABEL_THRESHOLD = 250;
+
+export const LABEL_OFFSET_PERCENT = 1.025;
+
+// total degrees of the arc (180 = semicircle, etc)
+export const ARC_DEGREES = 180 + 45 * 2; // semicircle plus a bit
diff --git a/frontend/src/metabase/visualizations/visualizations/Gauge/types.ts b/frontend/src/metabase/visualizations/visualizations/Gauge/types.ts
new file mode 100644
index 00000000000..f072424e13e
--- /dev/null
+++ b/frontend/src/metabase/visualizations/visualizations/Gauge/types.ts
@@ -0,0 +1,33 @@
+import { isObject } from "metabase-types/guards";
+
+export type GaugeSegment = {
+ color?: string;
+ label?: string;
+ min: number;
+ max: number;
+};
+
+export type GaugeRange = [number, number];
+
+export const isGaugeSegment = (value: unknown): value is GaugeSegment => {
+ return (
+ isObject(value) &&
+ typeof value.min === "number" &&
+ typeof value.max === "number"
+ );
+};
+
+export const isGaugeSegmentsArray = (
+ value: unknown,
+): value is GaugeSegment[] => {
+ return Array.isArray(value) && value.every(isGaugeSegment);
+};
+
+export const isGaugeRange = (value: unknown): value is GaugeRange => {
+ return (
+ Array.isArray(value) &&
+ value.length === 2 &&
+ typeof value[0] === "number" &&
+ typeof value[1] === "number"
+ );
+};
diff --git a/frontend/src/metabase/visualizations/visualizations/Gauge/utils.ts b/frontend/src/metabase/visualizations/visualizations/Gauge/utils.ts
index 1317ee308e2..7e48d8ecb4f 100644
--- a/frontend/src/metabase/visualizations/visualizations/Gauge/utils.ts
+++ b/frontend/src/metabase/visualizations/visualizations/Gauge/utils.ts
@@ -11,3 +11,7 @@ export const getValue = (rows: unknown[][]) => {
return rawValue;
};
+
+export const radians = (degrees: number) => (degrees * Math.PI) / 180;
+
+export const degrees = (radians: number) => (radians * 180) / Math.PI;