Skip to content

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

Restore table color range saturation for single conditional formatting rules (#77981)

Andersonf82bdbdd523b
diff --git a/frontend/src/metabase/visualizations/lib/table_format.ts b/frontend/src/metabase/visualizations/lib/table_format.ts
index 878792e901a..fad458b370b 100644
--- a/frontend/src/metabase/visualizations/lib/table_format.ts
+++ b/frontend/src/metabase/visualizations/lib/table_format.ts
@@ -144,6 +144,7 @@ export function compileFormatter(
columnName: string | null = null,
columnExtents: ColumnExtents | null = null,
isRowFormatter: boolean = false,
+ matchOnlyWithinRange: boolean = false,
): Formatter {
if (format.type === "single") {
let { operator, value, color } = format;
@@ -200,7 +201,10 @@ export function compileFormatter(
}),
).clamp(true);
return (value) => {
- if (!isNumber(value) || value < lowerBound || value > upperBound) {
+ if (!isNumber(value)) {
+ return null;
+ }
+ if (matchOnlyWithinRange && (value < lowerBound || value > upperBound)) {
return null;
}
const colorValue = scale(value);
@@ -273,11 +277,27 @@ function compileFormatters(
columnExtents: ColumnExtents,
): Formatters {
const formatters: Formatters = {};
+ const rangeFormatterCounts: Record<string, number> = {};
+ formats.forEach((format) => {
+ if (format.type === "range") {
+ format.columns.forEach((columnName) => {
+ rangeFormatterCounts[columnName] =
+ (rangeFormatterCounts[columnName] ?? 0) + 1;
+ });
+ }
+ });
+
formats.forEach((format) => {
format.columns.forEach((columnName) => {
formatters[columnName] = formatters[columnName] || [];
formatters[columnName].push(
- compileFormatter(format, columnName, columnExtents, false),
+ compileFormatter(
+ format,
+ columnName,
+ columnExtents,
+ false,
+ rangeFormatterCounts[columnName] > 1,
+ ),
);
});
});
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 30de0e7b903..5da11cf084f 100644
--- a/frontend/src/metabase/visualizations/lib/table_format.unit.spec.ts
+++ b/frontend/src/metabase/visualizations/lib/table_format.unit.spec.ts
@@ -245,6 +245,60 @@ describe("compileFormatter with extreme ranges", () => {
});
describe("makeCellBackgroundGetter", () => {
+ it("saturates values outside custom bounds when there is one range rule (#77957)", () => {
+ const getter = makeCellBackgroundGetter(
+ [[-0.1], [0], [0.6], [0.8]],
+ [createMockNumericColumn({ name: "score" })],
+ [
+ createMockColumnRangeFormattingSetting({
+ columns: ["score"],
+ colors: ["#ff0000", "#00ff00"],
+ min_type: "custom",
+ min_value: 0,
+ max_type: "custom",
+ max_value: 0.6,
+ }),
+ ],
+ false,
+ );
+
+ expect(getter(-0.1, 0, "score")).toBe(getter(0, 1, "score"));
+ expect(getter(0.8, 3, "score")).toBe(getter(0.6, 2, "score"));
+ });
+
+ it("saturates values when range rules target different columns (#77957)", () => {
+ const getter = makeCellBackgroundGetter(
+ [
+ [0.6, 0.6],
+ [0.8, 0.8],
+ ],
+ [
+ createMockNumericColumn({ name: "score" }),
+ createMockNumericColumn({ name: "total" }),
+ ],
+ [
+ createMockColumnRangeFormattingSetting({
+ columns: ["score"],
+ min_type: "custom",
+ min_value: 0,
+ max_type: "custom",
+ max_value: 0.6,
+ }),
+ createMockColumnRangeFormattingSetting({
+ columns: ["total"],
+ min_type: "custom",
+ min_value: 0,
+ max_type: "custom",
+ max_value: 0.6,
+ }),
+ ],
+ false,
+ );
+
+ expect(getter(0.8, 1, "score")).toBe(getter(0.6, 0, "score"));
+ expect(getter(0.8, 1, "total")).toBe(getter(0.6, 0, "total"));
+ });
+
it("applies each range rule to values in its own range when ranges do not overlap (#75933)", () => {
const getter = makeCellBackgroundGetter(
[[-1000], [1000]],