Skip to content

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

Fix funnel viz to handle increasing data (#68031)

kevinkelleher12fbe2dd23a41f
diff --git a/frontend/src/metabase/static-viz/components/FunnelChart/utils/funnel.ts b/frontend/src/metabase/static-viz/components/FunnelChart/utils/funnel.ts
index 07beb964396..b5cebf20d43 100644
--- a/frontend/src/metabase/static-viz/components/FunnelChart/utils/funnel.ts
+++ b/frontend/src/metabase/static-viz/components/FunnelChart/utils/funnel.ts
@@ -14,11 +14,12 @@ export const calculateFunnelSteps = (
stepWidth: number,
funnelHeight: number,
): FunnelStep[] => {
+ const firstMeasure = data[0][1];
+ const maxMeasure = Math.max(...data.map((datum) => datum[1]));
return data.map((datum, index) => {
- const [_step, firstMeasure] = data[0];
const [step, measure] = datum;
const left = index * stepWidth;
- const height = (measure * funnelHeight) / firstMeasure;
+ const height = (measure * funnelHeight) / maxMeasure;
const top = (funnelHeight - height) / 2;
const percent = firstMeasure > 0 ? measure / firstMeasure : 0;
diff --git a/frontend/src/metabase/static-viz/components/FunnelChart/utils/funnel.unit.spec.ts b/frontend/src/metabase/static-viz/components/FunnelChart/utils/funnel.unit.spec.ts
index 79a2326154a..c8eaed57649 100644
--- a/frontend/src/metabase/static-viz/components/FunnelChart/utils/funnel.unit.spec.ts
+++ b/frontend/src/metabase/static-viz/components/FunnelChart/utils/funnel.unit.spec.ts
@@ -49,6 +49,45 @@ describe("calculateFunnelSteps", () => {
},
]);
});
+
+ it("handles increasing data", () => {
+ const steps = calculateFunnelSteps(
+ [
+ ["funnel step 1", 50],
+ ["funnel step 2", 100],
+ ["funnel step 3", 25],
+ ],
+ 1,
+ 1,
+ );
+
+ expect(steps).toStrictEqual([
+ {
+ height: 0.5,
+ left: 0,
+ measure: 50,
+ percent: 1,
+ step: "funnel step 1",
+ top: 0.25,
+ },
+ {
+ height: 1,
+ left: 1,
+ measure: 100,
+ percent: 2,
+ step: "funnel step 2",
+ top: 0,
+ },
+ {
+ height: 0.25,
+ left: 2,
+ measure: 25,
+ percent: 0.5,
+ step: "funnel step 3",
+ top: 0.375,
+ },
+ ]);
+ });
});
describe("calculateFunnelPolygonPoints", () => {
diff --git a/frontend/src/metabase/visualizations/components/FunnelNormal.jsx b/frontend/src/metabase/visualizations/components/FunnelNormal.jsx
deleted file mode 100644
index 06cec5b6994..00000000000
--- a/frontend/src/metabase/visualizations/components/FunnelNormal.jsx
+++ /dev/null
@@ -1,284 +0,0 @@
-/* eslint-disable react/prop-types */
-import cx from "classnames";
-import Color from "color";
-import { Component } from "react";
-import { t } from "ttag";
-
-import { Ellipsified } from "metabase/common/components/Ellipsified";
-import CS from "metabase/css/core/index.css";
-import { color } from "metabase/lib/colors";
-import {
- formatChangeWithSign,
- formatNumber,
- formatValue,
-} from "metabase/lib/formatting";
-import { formatNullable } from "metabase/lib/formatting/nullable";
-import {
- FunnelNormalRoot,
- FunnelStart,
- FunnelStep,
- Head,
- Info,
- Subtitle,
- Title,
-} from "metabase/visualizations/components/FunnelNormal.styled";
-
-import { computeChange } from "../lib/numeric";
-
-export default class FunnelNormal extends Component {
- render() {
- const {
- className,
- rawSeries,
- gridSize,
- hovered,
- isDashboard,
- onHoverChange,
- onVisualizationClick,
- visualizationIsClickable,
- settings,
- } = this.props;
-
- const [series] = rawSeries;
- const {
- data: { cols, rows },
- } = series;
-
- const dimensionIndex = cols.findIndex(
- (col) => col.name === settings["funnel.dimension"],
- );
- const metricIndex = cols.findIndex(
- (col) => col.name === settings["funnel.metric"],
- );
-
- const sortedRows = settings["funnel.rows"]
- ? settings["funnel.rows"]
- .filter((fr) => fr.enabled)
- .map((fr) =>
- rows.find((row) => formatNullable(row[dimensionIndex]) === fr.key),
- )
- : rows;
-
- const isNarrow = gridSize && gridSize.width < 7;
- const isShort = gridSize && gridSize.height <= 5;
- const isSmall = isShort || isNarrow;
-
- const formatDimension = (dimension, jsx = true) =>
- formatValue(dimension, {
- ...settings.column(cols[dimensionIndex]),
- jsx,
- stringifyNull: true,
- majorWidth: 0,
- });
- const formatMetric = (metric, jsx = true) =>
- formatValue(metric, {
- ...settings.column(cols[metricIndex]),
- jsx,
- majorWidth: 0,
- });
- const formatPercent = (percent) => `${(100 * percent).toFixed(2)} %`;
-
- // Initial infos (required for step calculation)
- let infos = [
- {
- value: sortedRows[0][metricIndex],
- graph: {
- startBottom: 0.0,
- startTop: 1.0,
- endBottom: 0.0,
- endTop: 1.0,
- },
- },
- ];
-
- let remaining = sortedRows[0][metricIndex];
-
- sortedRows.map((row, rowIndex) => {
- remaining -= infos[rowIndex].value - row[metricIndex];
-
- const footerData = [
- {
- key: t`Retained`,
- value: formatNumber(row[metricIndex] / infos[0].value, {
- number_style: "percent",
- }),
- },
- ];
-
- const prevRow = sortedRows[rowIndex - 1];
- if (prevRow != null) {
- footerData.push({
- key: t`Compared to previous`,
- value: formatChangeWithSign(
- computeChange(prevRow[metricIndex], row[metricIndex]),
- ),
- });
- }
-
- infos[rowIndex + 1] = {
- value: row[metricIndex],
-
- graph: {
- startBottom: infos[rowIndex].graph.endBottom,
- startTop: infos[rowIndex].graph.endTop,
- endTop: 0.5 + remaining / infos[0].value / 2,
- endBottom: 0.5 - remaining / infos[0].value / 2,
- },
-
- hovered: {
- index: rowIndex,
- data: [
- {
- key: "Step",
- value: row[dimensionIndex],
- col: cols[dimensionIndex],
- },
- {
- key: cols[metricIndex].display_name,
- value: row[metricIndex],
- col: cols[metricIndex],
- },
- ],
- footerData,
- },
-
- clicked: {
- value: row[metricIndex],
- column: cols[metricIndex],
- dimensions: [
- {
- value: row[dimensionIndex],
- column: cols[dimensionIndex],
- },
- ],
- settings,
- },
- };
- });
-
- // Remove initial setup
- infos = infos.slice(1);
-
- const initial = infos[0];
-
- const isClickable = onVisualizationClick != null;
-
- const handleClick = (e) => {
- if (onVisualizationClick && visualizationIsClickable(infos[0].clicked)) {
- onVisualizationClick(e);
- }
- };
-
- return (
- <FunnelNormalRoot
- className={className}
- isSmall={isSmall}
- data-testid="funnel-chart"
- >
- <FunnelStep isFirst>
- <Head
- isNarrow={isNarrow}
- style={{ fontSize: isDashboard ? "0.8125rem" : "unset" }}
- >
- <Ellipsified data-testid="funnel-chart-header">
- {formatDimension(sortedRows[0][dimensionIndex])}
- </Ellipsified>
- </Head>
- <FunnelStart isNarrow={isNarrow}>
- <Title>{formatMetric(sortedRows[0][metricIndex])}</Title>
- <Subtitle>
- <Ellipsified>{cols[metricIndex].display_name}</Ellipsified>
- </Subtitle>
- </FunnelStart>
- {/* This part of code in used only to share height between .Start and .Graph columns. */}
- <Info isNarrow={isNarrow}>
- <Title>&nbsp;</Title>
- <Subtitle>&nbsp;</Subtitle>
- </Info>
- </FunnelStep>
- {infos.slice(1).map((info, index) => {
- const stepPercentage =
- initial.value > 0 ? info.value / initial.value : 0;
-
- return (
- <FunnelStep key={index}>
- <Head
- isNarrow={isNarrow}
- style={{ fontSize: isDashboard ? "0.8125rem" : "unset" }}
- >
- <Ellipsified data-testid="funnel-chart-header">
- {formatDimension(sortedRows[index + 1][dimensionIndex])}
- </Ellipsified>
- </Head>
- <GraphSection
- className={cx({ [CS.cursorPointer]: isClickable })}
- index={index}
- info={info}
- infos={infos}
- hovered={hovered}
- onHoverChange={onHoverChange}
- onVisualizationClick={handleClick}
- />
- <Info isNarrow={isNarrow}>
- <Title>
- <Ellipsified>{formatPercent(stepPercentage)}</Ellipsified>
- </Title>
- <Subtitle
- style={{ fontSize: isDashboard ? "0.8125rem" : "unset" }}
- >
- <Ellipsified>
- {formatMetric(sortedRows[index + 1][metricIndex])}
- </Ellipsified>
- </Subtitle>
- </Info>
- </FunnelStep>
- );
- })}
- </FunnelNormalRoot>
- );
- }
-}
-const GraphSection = ({
- index,
- info,
- infos,
- hovered,
- onHoverChange,
- onVisualizationClick,
- className,
-}) => {
- return (
- <div className={cx(CS.relative, CS.fullHeight)}>
- <svg
- height="100%"
- width="100%"
- className={cx(className, CS.absolute)}
- onMouseMove={(e) => {
- if (onHoverChange && info.hovered) {
- onHoverChange({
- ...info.hovered,
- event: e.nativeEvent,
- });
- }
- }}
- onMouseLeave={() => onHoverChange && onHoverChange(null)}
- onClick={(e) => {
- if (onVisualizationClick && info.clicked) {
- onVisualizationClick({
- ...info.clicked,
- event: e.nativeEvent,
- });
- }
- }}
- viewBox="0 0 1 1"
- preserveAspectRatio="none"
- >
- <polygon
- opacity={1 - index * (0.9 / (infos.length + 1))}
- fill={Color(color("brand")).hex()}
- points={`0 ${info.graph.startBottom}, 0 ${info.graph.startTop}, 1 ${info.graph.endTop}, 1 ${info.graph.endBottom}`}
- />
- </svg>
- </div>
- );
-};
diff --git a/frontend/src/metabase/visualizations/components/FunnelNormal.styled.tsx b/frontend/src/metabase/visualizations/components/FunnelNormal.styled.tsx
index 195a60666bc..19e000a716a 100644
--- a/frontend/src/metabase/visualizations/components/FunnelNormal.styled.tsx
+++ b/frontend/src/metabase/visualizations/components/FunnelNormal.styled.tsx
@@ -88,7 +88,6 @@ export const FunnelStart = styled.div<SharedProps>`
`;
interface FunnelNormalRootProps {
- isNarrow: boolean;
isSmall: boolean;
}
diff --git a/frontend/src/metabase/visualizations/components/FunnelNormal.tsx b/frontend/src/metabase/visualizations/components/FunnelNormal.tsx
new file mode 100644
index 00000000000..2119f2bb0fc
--- /dev/null
+++ b/frontend/src/metabase/visualizations/components/FunnelNormal.tsx
@@ -0,0 +1,301 @@
+import cx from "classnames";
+import Color from "color";
+import { t } from "ttag";
+
+import { Ellipsified } from "metabase/common/components/Ellipsified";
+import CS from "metabase/css/core/index.css";
+import { color } from "metabase/lib/colors";
+import {
+ formatChangeWithSign,
+ formatNumber,
+ formatValue,
+} from "metabase/lib/formatting";
+import { formatNullable } from "metabase/lib/formatting/nullable";
+import { isNotNull } from "metabase/lib/types";
+import {
+ calculateFunnelSteps,
+ calculateStepOpacity,
+} from "metabase/static-viz/components/FunnelChart/utils/funnel";
+import {
+ FunnelNormalRoot,
+ FunnelStart,
+ FunnelStep,
+ Head,
+ Info,
+ Subtitle,
+ Title,
+} from "metabase/visualizations/components/FunnelNormal.styled";
+import type {
+ ClickObject,
+ HoveredObject,
+ VisualizationProps,
+} from "metabase/visualizations/types";
+import type { RowValue } from "metabase-types/api";
+
+import { computeChange } from "../lib/numeric";
+
+type FunnelStepInfo = {
+ value: number;
+ percent: number;
+ dimension: RowValue;
+ graph: {
+ startBottom: number;
+ startTop: number;
+ endBottom: number;
+ endTop: number;
+ };
+ hovered?: HoveredObject;
+ clicked?: ClickObject;
+};
+
+export function FunnelNormal({
+ className,
+ rawSeries,
+ gridSize,
+ hovered,
+ isDashboard,
+ onHoverChange,
+ onVisualizationClick,
+ visualizationIsClickable,
+ settings,
+}: VisualizationProps) {
+ const [series] = rawSeries;
+ const {
+ data: { cols, rows },
+ } = series;
+
+ const dimensionIndex = cols.findIndex(
+ (col) => col.name === settings["funnel.dimension"],
+ );
+ const metricIndex = cols.findIndex(
+ (col) => col.name === settings["funnel.metric"],
+ );
+
+ const sortedRows = settings["funnel.rows"]
+ ? settings["funnel.rows"]
+ .filter((fr) => fr.enabled)
+ .map((fr) =>
+ rows.find((row) => formatNullable(row[dimensionIndex]) === fr.key),
+ )
+ .filter(isNotNull)
+ : rows;
+
+ const isNarrow = Boolean(gridSize && gridSize.width < 7);
+ const isShort = Boolean(gridSize && gridSize.height <= 5);
+ const isSmall = isShort || isNarrow;
+
+ const formatDimension = (dimension: RowValue, jsx = true) =>
+ formatValue(dimension, {
+ ...settings.column?.(cols[dimensionIndex]),
+ jsx,
+ stringifyNull: true,
+ majorWidth: 0,
+ });
+ const formatMetric = (metric: number, jsx = true) =>
+ formatValue(metric, {
+ ...settings.column?.(cols[metricIndex]),
+ jsx,
+ majorWidth: 0,
+ });
+ const formatPercent = (percent: number) => `${(100 * percent).toFixed(2)} %`;
+
+ const dimensions = sortedRows.map((row) => row[dimensionIndex]);
+ const metrics = sortedRows.map((row) => row[metricIndex]) as number[];
+
+ // this is a little hacky, since this component and static-viz use different data structures for the funnel steps
+ // but using the same function to calculate the height will help to prevent a regression until they're better aligned
+ const funnelSteps = calculateFunnelSteps(
+ metrics.map((metric, i) => [i, metric]),
+ 1,
+ 1,
+ ).map((step) => ({
+ ...step,
+ bottom: step.top,
+ top: step.top + step.height,
+ }));
+
+ const infos: FunnelStepInfo[] = funnelSteps.slice(1).map((step, _i) => {
+ const i = _i + 1; // +1 because we skip the first step
+ const dimension = dimensions[i];
+ const metric = metrics[i];
+ return {
+ value: step.measure,
+ percent: step.percent,
+ dimension,
+ graph: {
+ startBottom: funnelSteps[i - 1].bottom,
+ startTop: funnelSteps[i - 1].top,
+ endBottom: step.bottom,
+ endTop: step.top,
+ },
+ hovered: {
+ index: i,
+ data: [
+ {
+ key: "Step",
+ value: dimension,
+ col: cols[dimensionIndex],
+ },
+ {
+ key: cols[metricIndex].display_name,
+ value: metric,
+ col: cols[metricIndex],
+ },
+ ],
+ footerData: [
+ {
+ key: t`Retained`,
+ value: formatNumber(step.percent, { number_style: "percent" }),
+ col: null,
+ },
+ {
+ key: t`Compared to previous`,
+ value: formatChangeWithSign(
+ computeChange(metrics[i - 1], metrics[i]),
+ ),
+ col: null,
+ },
+ ],
+ },
+ clicked: {
+ value: metrics[i],
+ column: cols[metricIndex],
+ dimensions: [
+ {
+ value: dimension,
+ column: cols[dimensionIndex],
+ },
+ ],
+ settings,
+ },
+ };
+ });
+
+ const isClickable = onVisualizationClick != null;
+
+ const handleClick = (clickObject: ClickObject | null) => {
+ if (
+ onVisualizationClick &&
+ visualizationIsClickable(infos[0]?.clicked ?? null)
+ ) {
+ onVisualizationClick(clickObject);
+ }
+ };
+
+ return (
+ <FunnelNormalRoot
+ className={className}
+ isSmall={isSmall}
+ data-testid="funnel-chart"
+ >
+ <FunnelStep isFirst>
+ <Head
+ isNarrow={isNarrow}
+ style={{ fontSize: isDashboard ? "0.8125rem" : "unset" }}
+ >
+ <Ellipsified data-testid="funnel-chart-header">
+ {formatDimension(dimensions[0])}
+ </Ellipsified>
+ </Head>
+ <FunnelStart isNarrow={isNarrow}>
+ <Title>{formatMetric(metrics[0])}</Title>
+ <Subtitle>
+ <Ellipsified>{cols[metricIndex].display_name}</Ellipsified>
+ </Subtitle>
+ </FunnelStart>
+ {/* This part of code in used only to share height between .Start and .Graph columns. */}
+ <Info isNarrow={isNarrow}>
+ <Title>&nbsp;</Title>
+ <Subtitle>&nbsp;</Subtitle>
+ </Info>
+ </FunnelStep>
+ {infos.map((info, index) => {
+ return (
+ <FunnelStep key={index}>
+ <Head
+ isNarrow={isNarrow}
+ style={{ fontSize: isDashboard ? "0.8125rem" : "unset" }}
+ >
+ <Ellipsified data-testid="funnel-chart-header">
+ {formatDimension(info.dimension)}
+ </Ellipsified>
+ </Head>
+ <GraphSection
+ className={cx({ [CS.cursorPointer]: isClickable })}
+ index={index}
+ numSteps={infos.length}
+ info={info}
+ hovered={hovered}
+ onHoverChange={onHoverChange}
+ onVisualizationClick={handleClick}
+ />
+ <Info isNarrow={isNarrow}>
+ <Title>
+ <Ellipsified>{formatPercent(info.percent)}</Ellipsified>
+ </Title>
+ <Subtitle
+ style={{ fontSize: isDashboard ? "0.8125rem" : "unset" }}
+ >
+ <Ellipsified>{formatMetric(info.value)}</Ellipsified>
+ </Subtitle>
+ </Info>
+ </FunnelStep>
+ );
+ })}
+ </FunnelNormalRoot>
+ );
+}
+
+type GraphSectionProps = Pick<
+ VisualizationProps,
+ "hovered" | "onHoverChange" | "onVisualizationClick"
+> & {
+ index: number;
+ numSteps: number;
+ info: FunnelStepInfo;
+ className: string;
+};
+
+const GraphSection = ({
+ index,
+ numSteps,
+ info,
+ onHoverChange,
+ onVisualizationClick,
+ className,
+}: GraphSectionProps) => {
+ return (
+ <div className={cx(CS.relative, CS.fullHeight)}>
+ <svg
+ height="100%"
+ width="100%"
+ className={cx(className, CS.absolute)}
+ onMouseMove={(e) => {
+ if (onHoverChange && info.hovered) {
+ onHoverChange({
+ ...info.hovered,
+ event: e.nativeEvent,
+ });
+ }
+ }}
+ onMouseLeave={() => onHoverChange && onHoverChange(null)}
+ onClick={(e) => {
+ if (onVisualizationClick && info.clicked) {
+ onVisualizationClick({
+ ...info.clicked,
+ event: e.nativeEvent,
+ });
+ }
+ }}
+ viewBox="0 0 1 1"
+ preserveAspectRatio="none"
+ >
+ <polygon
+ opacity={calculateStepOpacity(index, numSteps)}
+ fill={Color(color("brand")).hex()}
+ points={`0 ${info.graph.startBottom}, 0 ${info.graph.startTop}, 1 ${info.graph.endTop}, 1 ${info.graph.endBottom}`}
+ />
+ </svg>
+ </div>
+ );
+};
diff --git a/frontend/src/metabase/visualizations/visualizations/Funnel/Funnel.tsx b/frontend/src/metabase/visualizations/visualizations/Funnel/Funnel.tsx
index 6cc2e5cf4c3..c4e40a5e239 100644
--- a/frontend/src/metabase/visualizations/visualizations/Funnel/Funnel.tsx
+++ b/frontend/src/metabase/visualizations/visualizations/Funnel/Funnel.tsx
@@ -31,7 +31,7 @@ import { BarChart } from "metabase/visualizations/visualizations/BarChart";
import { funnelToBarTransform } from "metabase/visualizations/visualizations/Funnel/funnel-bar-transform";
import type { DatasetData, RawSeries, RowValue } from "metabase-types/api";
-import FunnelNormal from "../../components/FunnelNormal";
+import { FunnelNormal } from "../../components/FunnelNormal";
import type { FunnelRow } from "./types";