Skip to content

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

Fix conditional formatting range matching (#76137)

Andersone1e1fef04bab
diff --git a/frontend/src/metabase/visualizations/lib/table_format.ts b/frontend/src/metabase/visualizations/lib/table_format.ts
index aa0a512cd39..4a7ea5243d2 100644
--- a/frontend/src/metabase/visualizations/lib/table_format.ts
+++ b/frontend/src/metabase/visualizations/lib/table_format.ts
@@ -187,6 +187,9 @@ export function compileFormatter(
return () => null;
}
+ const lowerBound = Math.min(min, max);
+ const upperBound = Math.max(min, max);
+
const scale = getLinearColorScale(
[min, max],
format.colors.map((c) => {
@@ -196,7 +199,7 @@ export function compileFormatter(
}),
).clamp(true);
return (value) => {
- if (!isNumber(value)) {
+ if (!isNumber(value) || value < lowerBound || value > upperBound) {
return null;
}
const colorValue = scale(value);
diff --git a/frontend/src/metabase/visualizations/lib/table_format.unit.spec.ts b/frontend/src/metabase/visualizations/lib/table_format.unit.spec.ts
index d7008fda25e..8c82c3cc5d8 100644
--- a/frontend/src/metabase/visualizations/lib/table_format.unit.spec.ts
+++ b/frontend/src/metabase/visualizations/lib/table_format.unit.spec.ts
@@ -7,7 +7,12 @@ import {
extent,
isEmptyString,
isNonEmptyString,
+ makeCellBackgroundGetter,
} from "metabase/visualizations/lib/table_format";
+import {
+ createMockColumnRangeFormattingSetting,
+ createMockNumericColumn,
+} from "metabase-types/api/mocks";
describe("compileFormatter", () => {
it("should return a function, even for unsupported operators", () => {
@@ -237,3 +242,37 @@ describe("compileFormatter with extreme ranges", () => {
expect(colorMax).toBe("rgba(255, 255, 255, 0.75)");
});
});
+
+describe("makeCellBackgroundGetter", () => {
+ it("applies each range rule to values in its own range when ranges do not overlap (#75933)", () => {
+ const getter = makeCellBackgroundGetter(
+ [[-1000], [1000]],
+ [createMockNumericColumn({ name: "score" })],
+ [
+ createMockColumnRangeFormattingSetting({
+ columns: ["score"],
+ colors: ["#ff0000", "#ffffff"],
+ min_type: "custom",
+ min_value: -1800,
+ max_type: "custom",
+ max_value: 0,
+ }),
+ createMockColumnRangeFormattingSetting({
+ columns: ["score"],
+ colors: ["#ffffff", "#00ff00"],
+ min_type: "custom",
+ min_value: 0,
+ max_type: "custom",
+ max_value: 1800,
+ }),
+ ],
+ false,
+ );
+
+ const negativeColor = Color(getter(-1000, 0, "score") ?? "");
+ const positiveColor = Color(getter(1000, 1, "score") ?? "");
+
+ expect(negativeColor.red()).toBeGreaterThan(negativeColor.green());
+ expect(positiveColor.green()).toBeGreaterThan(positiveColor.red());
+ });
+});