Skip to content

A reactive notebook for Python — run reproducible experiments, query with SQL, execute as a script, deploy as an app, and version with git. Stored as pure Python. All in a modern, AI-native editor.

fix: column summary bars spacing, expand tooltips (#4200)

Akshay Agrawale43dc8de7f0b
diff --git a/frontend/src/components/data-table/__tests__/__snapshots__/chart-spec-model.test.ts.snap b/frontend/src/components/data-table/__tests__/__snapshots__/chart-spec-model.test.ts.snap
index d835db0912..315591947b 100644
--- a/frontend/src/components/data-table/__tests__/__snapshots__/chart-spec-model.test.ts.snap
+++ b/frontend/src/components/data-table/__tests__/__snapshots__/chart-spec-model.test.ts.snap
@@ -18,50 +18,66 @@ exports[`ColumnChartSpecModel > snapshot > array 1`] = `
"c",
],
},
- "encoding": {
- "color": {
- "condition": {
- "test": "datum["bin_maxbins_10_a_range"] === "null"",
- "value": "#cc4e00",
- },
- "value": "#027864",
- },
- "tooltip": [
- {
- "bin": true,
- "field": "a",
- "format": ".2f",
- "title": "a",
- "type": "nominal",
+ "height": 100,
+ "layer": [
+ {
+ "encoding": {
+ "x": {
+ "axis": null,
+ "bin": {
+ "maxbins": 10,
+ },
+ "field": "a",
+ "type": "quantitative",
+ },
+ "y": {
+ "aggregate": "count",
+ "axis": null,
+ "type": "quantitative",
+ },
},
- {
- "aggregate": "count",
- "format": ",d",
- "title": "Count",
- "type": "quantitative",
+ "mark": {
+ "color": "#027864",
+ "type": "bar",
},
- ],
- "x": {
- "axis": null,
- "bin": true,
- "field": "a",
- "type": "nominal",
},
- "y": {
- "aggregate": "count",
- "axis": null,
- "scale": {
- "type": "linear",
+ {
+ "encoding": {
+ "tooltip": [
+ {
+ "bin": true,
+ "field": "a",
+ "format": ".2f",
+ "title": "a",
+ "type": "quantitative",
+ },
+ {
+ "aggregate": "count",
+ "format": ",d",
+ "title": "Count",
+ "type": "quantitative",
+ },
+ ],
+ "x": {
+ "axis": null,
+ "bin": {
+ "maxbins": 10,
+ },
+ "field": "a",
+ "type": "quantitative",
+ },
+ "y": {
+ "aggregate": "max",
+ "axis": null,
+ "type": "quantitative",
+ },
+ },
+ "mark": {
+ "opacity": 0,
+ "type": "bar",
},
- "type": "quantitative",
},
- },
- "height": 100,
- "mark": {
- "align": "right",
- "color": "#027864",
- "type": "bar",
- },
+ ],
}
`;
@@ -90,50 +106,66 @@ exports[`ColumnChartSpecModel > snapshot > csv data 1`] = `
},
],
},
- "encoding": {
- "color": {
- "condition": {
- "test": "datum["bin_maxbins_10_a_range"] === "null"",
- "value": "#cc4e00",
- },
- "value": "#027864",
- },
- "tooltip": [
- {
- "bin": true,
- "field": "a",
- "format": ".2f",
- "title": "a",
- "type": "nominal",
+ "height": 100,
+ "layer": [
+ {
+ "encoding": {
+ "x": {
+ "axis": null,
+ "bin": {
+ "maxbins": 10,
+ },
+ "field": "a",
+ "type": "quantitative",
+ },
+ "y": {
+ "aggregate": "count",
+ "axis": null,
+ "type": "quantitative",
+ },
},
- {
- "aggregate": "count",
- "format": ",d",
- "title": "Count",
- "type": "quantitative",
+ "mark": {
+ "color": "#027864",
+ "type": "bar",
},
- ],
- "x": {
- "axis": null,
- "bin": true,
- "field": "a",
- "type": "nominal",
},
- "y": {
- "aggregate": "count",
- "axis": null,
- "scale": {
- "type": "linear",
+ {
+ "encoding": {
+ "tooltip": [
+ {
+ "bin": true,
+ "field": "a",
+ "format": ".2f",
+ "title": "a",
+ "type": "quantitative",
+ },
+ {
+ "aggregate": "count",
+ "format": ",d",
+ "title": "Count",
+ "type": "quantitative",
+ },
+ ],
+ "x": {
+ "axis": null,
+ "bin": {
+ "maxbins": 10,
+ },
+ "field": "a",
+ "type": "quantitative",
+ },
+ "y": {
+ "aggregate": "max",
+ "axis": null,
+ "type": "quantitative",
+ },
+ },
+ "mark": {
+ "opacity": 0,
+ "type": "bar",
},
- "type": "quantitative",
},
- },
- "height": 100,
- "mark": {
- "align": "right",
- "color": "#027864",
- "type": "bar",
- },
+ ],
}
`;
@@ -162,50 +194,66 @@ exports[`ColumnChartSpecModel > snapshot > csv string 1`] = `
},
],
},
- "encoding": {
- "color": {
- "condition": {
- "test": "datum["bin_maxbins_10_a_range"] === "null"",
- "value": "#cc4e00",
- },
- "value": "#027864",
- },
- "tooltip": [
- {
- "bin": true,
- "field": "a",
- "format": ".2f",
- "title": "a",
- "type": "nominal",
+ "height": 100,
+ "layer": [
+ {
+ "encoding": {
+ "x": {
+ "axis": null,
+ "bin": {
+ "maxbins": 10,
+ },
+ "field": "a",
+ "type": "quantitative",
+ },
+ "y": {
+ "aggregate": "count",
+ "axis": null,
+ "type": "quantitative",
+ },
},
- {
- "aggregate": "count",
- "format": ",d",
- "title": "Count",
- "type": "quantitative",
+ "mark": {
+ "color": "#027864",
+ "type": "bar",
},
- ],
- "x": {
- "axis": null,
- "bin": true,
- "field": "a",
- "type": "nominal",
},
- "y": {
- "aggregate": "count",
- "axis": null,
- "scale": {
- "type": "linear",
+ {
+ "encoding": {
+ "tooltip": [
+ {
+ "bin": true,
+ "field": "a",
+ "format": ".2f",
+ "title": "a",
+ "type": "quantitative",
+ },
+ {
+ "aggregate": "count",
+ "format": ",d",
+ "title": "Count",
+ "type": "quantitative",
+ },
+ ],
+ "x": {
+ "axis": null,
+ "bin": {
+ "maxbins": 10,
+ },
+ "field": "a",
+ "type": "quantitative",
+ },
+ "y": {
+ "aggregate": "max",
+ "axis": null,
+ "type": "quantitative",
+ },
+ },
+ "mark": {
+ "opacity": 0,
+ "type": "bar",
},
- "type": "quantitative",
},
- },
- "height": 100,
- "mark": {
- "align": "right",
- "color": "#027864",
- "type": "bar",
- },
+ ],
}
`;
@@ -223,52 +271,89 @@ exports[`ColumnChartSpecModel > snapshot > url data 1`] = `
"data": {
"values": [],
},
- "encoding": {
- "color": {
- "condition": {
- "test": "datum["bin_maxbins_10_date_range"] === "null"",
- "value": "#cc4e00",
- },
- "value": "#027864",
- },
- "tooltip": [
- {
- "bin": true,
- "field": "date",
- "format": "%Y-%m-%d",
- "title": "date",
- "type": "temporal",
+ "height": 100,
+ "layer": [
+ {
+ "encoding": {
+ "color": {
+ "condition": {
+ "test": "datum["bin_maxbins_10_date_range"] === "null"",
+ "value": "#cc4e00",
+ },
+ "value": "#027864",
+ },
+ "x": {
+ "axis": null,
+ "bin": true,
+ "field": "date",
+ "scale": {
+ "align": 0,
+ "paddingInner": 0,
+ "paddingOuter": {
+ "expr": "length(data('data_0')) == 2 ? 1 : length(data('data_0')) == 3 ? 0.5 : length(data('data_0')) == 4 ? 0 : 0",
+ },
+ },
+ "type": "temporal",
+ },
+ "y": {
+ "aggregate": "count",
+ "axis": null,
+ "type": "quantitative",
+ },
},
- {
- "aggregate": "count",
- "format": ",d",
- "title": "Count",
- "type": "quantitative",
+ "mark": {
+ "color": "#027864",
+ "type": "bar",
},
- ],
- "x": {
- "axis": null,
- "bin": true,
- "field": "date",
- "scale": {
- "align": 0,
- "paddingInner": 0,
- "paddingOuter": {
- "expr": "length(data('data_0')) == 2 ? 1 : length(data('data_0')) == 3 ? 0.5 : length(data('data_0')) == 4 ? 0 : 0",
+ },
+ {
+ "encoding": {
+ "color": {
+ "condition": {
+ "test": "datum["bin_maxbins_10_date_range"] === "null"",
+ "value": "#cc4e00",
+ },
+ "value": "#027864",
+ },
+ "tooltip": [
+ {
+ "bin": true,
+ "field": "date",
+ "format": "%Y-%m-%d",
+ "title": "date",
+ "type": "temporal",
+ },
+ {
+ "aggregate": "count",
+ "format": ",d",
+ "title": "Count",
+ "type": "quantitative",
+ },
+ ],
+ "x": {
+ "axis": null,
+ "bin": true,
+ "field": "date",
+ "scale": {
+ "align": 0,
+ "paddingInner": 0,
+ "paddingOuter": {
+ "expr": "length(data('data_0')) == 2 ? 1 : length(data('data_0')) == 3 ? 0.5 : length(data('data_0')) == 4 ? 0 : 0",
+ },
+ },
+ "type": "temporal",
+ },
+ "y": {
+ "aggregate": "max",
+ "axis": null,
+ "type": "quantitative",
},
},
- "type": "temporal",
- },
- "y": {
- "aggregate": "count",
- "axis": null,
- "type": "quantitative",
+ "mark": {
+ "opacity": 0,
+ "type": "bar",
+ },
},
- },
- "height": 100,
- "mark": {
- "color": "#027864",
- "type": "bar",
- },
+ ],
}
`;
diff --git a/frontend/src/components/data-table/__tests__/chart-spec-model.test.ts b/frontend/src/components/data-table/__tests__/chart-spec-model.test.ts
index 0cb834c014..26735eae81 100644
--- a/frontend/src/components/data-table/__tests__/chart-spec-model.test.ts
+++ b/frontend/src/components/data-table/__tests__/chart-spec-model.test.ts
@@ -87,9 +87,10 @@ describe("ColumnChartSpecModel", () => {
);
const summary = model.getHeaderSummary("column.with[special:chars]");
expect(summary.spec).toBeDefined();
- expect((summary.spec?.encoding?.x as { field: string })?.field).toBe(
- "column\\.with\\[special\\:chars\\]",
- );
+ expect(
+ // @ts-expect-error layer should be available
+ (summary.spec?.layer[0].encoding?.x as { field: string })?.field,
+ ).toBe("column\\.with\\[special\\:chars\\]");
});
describe("snapshot", () => {
diff --git a/frontend/src/components/data-table/chart-spec-model.tsx b/frontend/src/components/data-table/chart-spec-model.tsx
index 9134fd268a..8229acb67a 100644
--- a/frontend/src/components/data-table/chart-spec-model.tsx
+++ b/frontend/src/components/data-table/chart-spec-model.tsx
@@ -65,6 +65,10 @@ export class ColumnChartSpecModel<T> {
this.columnSummaries = new Map(summaries.map((s) => [s.column, s]));
}
+ public getColumnSummary(column: string) {
+ return this.columnSummaries.get(column);
+ }
+
public getHeaderSummary(column: string) {
return {
summary: this.columnSummaries.get(column),
@@ -77,7 +81,7 @@ export class ColumnChartSpecModel<T> {
if (!this.data) {
return null;
}
- const base: Omit<TopLevelFacetedUnitSpec, "mark"> = {
+ const base = {
data: this.dataSpec as TopLevelFacetedUnitSpec["data"],
background: "transparent",
config: {
@@ -108,97 +112,148 @@ export class ColumnChartSpecModel<T> {
case "time":
return {
...base,
- mark: {
- type: "bar",
- color: mint.mint11,
- },
- encoding: {
- x: {
- field: column,
- type: "temporal",
- axis: null,
- bin: true,
- scale: scale,
- },
- y: { aggregate: "count", type: "quantitative", axis: null },
- tooltip: [
- {
- field: column,
- type: "temporal",
- format:
- type === "date"
- ? "%Y-%m-%d"
- : type === "time"
- ? "%H:%M:%S"
- : "%Y-%m-%dT%H:%M:%S",
- bin: true,
- title: column,
+ // Two layers: one with the visible bars, and one with invisible bars
+ // that provide a larger tooltip area.
+ // @ts-expect-error 'layer' property not in TopLevelFacetedUnitSpec
+ layer: [
+ {
+ mark: {
+ type: "bar",
+ color: mint.mint11,
},
- {
- aggregate: "count",
- type: "quantitative",
- title: "Count",
- format: ",d",
+ encoding: {
+ x: {
+ field: column,
+ type: "temporal",
+ axis: null,
+ bin: true,
+ scale: scale,
+ },
+ y: { aggregate: "count", type: "quantitative", axis: null },
+ // Color nulls
+ color: {
+ condition: {
+ test: `datum["bin_maxbins_10_${column}_range"] === "null"`,
+ value: orange.orange11,
+ },
+ value: mint.mint11,
+ },
},
- ],
- // Color nulls
- color: {
- condition: {
- test: `datum["bin_maxbins_10_${column}_range"] === "null"`,
- value: orange.orange11,
+ },
+
+ // 0 opacity full-height bars with tooltips, since it is too hard to trigger
+ // the tooltip for very small bars.
+ {
+ mark: {
+ type: "bar",
+ opacity: 0,
+ },
+ encoding: {
+ x: {
+ field: column,
+ type: "temporal",
+ axis: null,
+ bin: true,
+ scale: scale,
+ },
+ y: { aggregate: "max", type: "quantitative", axis: null },
+ tooltip: [
+ {
+ field: column,
+ type: "temporal",
+ format:
+ type === "date"
+ ? "%Y-%m-%d"
+ : type === "time"
+ ? "%H:%M:%S"
+ : "%Y-%m-%dT%H:%M:%S",
+ bin: true,
+ title: column,
+ },
+ {
+ aggregate: "count",
+ type: "quantitative",
+ title: "Count",
+ format: ",d",
+ },
+ ],
+ // Color nulls
+ color: {
+ condition: {
+ test: `datum["bin_maxbins_10_${column}_range"] === "null"`,
+ value: orange.orange11,
+ },
+ value: mint.mint11,
+ },
},
- value: mint.mint11,
},
- },
+ ],
};
case "integer":
case "number": {
+ // Create a histogram spec that properly handles null values
const format = type === "integer" ? ",d" : ".2f";
+
return {
- ...base,
- mark: {
- type: "bar",
- color: mint.mint11,
- align: "right",
- },
- encoding: {
- x: {
- field: column,
- // nominal to support a null bin
- type: "nominal",
- axis: null,
- bin: true,
- },
- y: {
- aggregate: "count",
- type: "quantitative",
- axis: null,
- scale: { type: "linear" },
- },
- tooltip: [
- {
- field: column,
- type: "nominal",
- format: format,
- bin: true,
- title: column,
+ ...base, // Assuming base contains shared configurations
+ // Two layers: one with the visible bars, and one with invisible bars
+ // that provide a larger tooltip area.
+ // @ts-expect-error 'layer' property not in TopLevelFacetedUnitSpec
+ layer: [
+ {
+ mark: {
+ type: "bar",
+ color: mint.mint11,
},
- {
- aggregate: "count",
- type: "quantitative",
- title: "Count",
- format: ",d",
+ encoding: {
+ x: {
+ field: column,
+ type: "quantitative",
+ bin: { maxbins: 10 },
+ axis: null,
+ },
+ y: {
+ aggregate: "count",
+ type: "quantitative",
+ axis: null,
+ },
},
- ],
- // Color nulls
- color: {
- condition: {
- test: `datum["bin_maxbins_10_${column}_range"] === "null"`,
- value: orange.orange11,
+ },
+ {
+ mark: {
+ type: "bar",
+ opacity: 0,
+ },
+ encoding: {
+ x: {
+ field: column,
+ type: "quantitative",
+ bin: { maxbins: 10 },
+ axis: null,
+ },
+ y: {
+ aggregate: "max",
+ type: "quantitative",
+ axis: null,
+ },
+ tooltip: [
+ {
+ field: column,
+ type: "quantitative",
+ bin: true,
+ title: column,
+ format: format,
+ },
+ {
+ aggregate: "count",
+ type: "quantitative",
+ title: "Count",
+ format: ",d",
+ },
+ ],
},
- value: mint.mint11,
},
- },
+ ],
};
}
case "boolean":
diff --git a/frontend/src/components/data-table/column-summary.tsx b/frontend/src/components/data-table/column-summary.tsx
index 64b0c7d2c4..42b140470b 100644
--- a/frontend/src/components/data-table/column-summary.tsx
+++ b/frontend/src/components/data-table/column-summary.tsx
@@ -81,7 +81,6 @@ export const TableColumnSummary = <TData, TValue>({
<span>min: {renderDate(summary.min, type)}</span>
<span>max: {renderDate(summary.max, type)}</span>
<span>unique: {prettyNumber(summary.unique)}</span>
- <span>nulls: {prettyNumber(summary.nulls)}</span>
</div>
);
}
@@ -113,7 +112,6 @@ export const TableColumnSummary = <TData, TValue>({
: summary.max}
</span>
<span>unique: {prettyNumber(summary.unique)}</span>
- <span>nulls: {prettyNumber(summary.nulls)}</span>
</div>
);
}
@@ -123,7 +121,7 @@ export const TableColumnSummary = <TData, TValue>({
typeof summary.max === "number"
) {
return (
- <div className="flex justify-between w-full px-2 whitespace-pre">
+ <div className="flex justify-between w-full whitespace-pre">
<span>{prettyScientificNumber(summary.min)}</span>
{summary.min === summary.max ? null : (
<span>{prettyScientificNumber(summary.max)}</span>
@@ -149,22 +147,13 @@ export const TableColumnSummary = <TData, TValue>({
);
}
- if (summary.nulls == null || summary.nulls === 0) {
- return null;
- }
-
- return (
- <div className="flex flex-col whitespace-pre">
- <span>nulls: {prettyNumber(summary.nulls)}</span>
- </div>
- );
+ return null;
case "time":
return null;
case "string":
return (
<div className="flex flex-col whitespace-pre">
<span>unique: {prettyNumber(summary.unique)}</span>
- <span>nulls: {prettyNumber(summary.nulls)}</span>
</div>
);
case "unknown":
diff --git a/frontend/src/components/data-table/columns.tsx b/frontend/src/components/data-table/columns.tsx
index 82a8b8976b..ff05b45ddb 100644
--- a/frontend/src/components/data-table/columns.tsx
+++ b/frontend/src/components/data-table/columns.tsx
@@ -29,6 +29,7 @@ import { Popover, PopoverContent, PopoverTrigger } from "../ui/popover";
import { EmotionCacheProvider } from "../editor/output/EmotionCacheProvider";
import { PopoverClose } from "@radix-ui/react-popover";
import { Button } from "../ui/button";
+import type { ColumnChartSpecModel } from "./chart-spec-model";
function inferDataType(value: unknown): [type: DataType, displayType: string] {
if (typeof value === "string") {
@@ -94,6 +95,7 @@ export function generateColumns<T>({
rowHeaders,
selection,
fieldTypes,
+ chartSpecModel,
textJustifyColumns,
wrappedColumns,
showDataTypes,
@@ -101,6 +103,7 @@ export function generateColumns<T>({
rowHeaders: string[];
selection: DataTableSelection;
fieldTypes: FieldTypesWithExternalType;
+ chartSpecModel?: ColumnChartSpecModel<unknown>;
textJustifyColumns?: Record<string, "left" | "center" | "right">;
wrappedColumns?: string[];
showDataTypes?: boolean;
@@ -150,13 +153,26 @@ export function generateColumns<T>({
},
header: ({ column }) => {
+ const summary = chartSpecModel?.getColumnSummary(key);
const dtype = column.columnDef.meta?.dtype;
+ const dtypeHeader =
+ showDataTypes && dtype ? (
+ <div className="flex flex-row gap-1">
+ <span className="text-xs text-muted-foreground">{dtype}</span>
+ {summary &&
+ typeof summary.nulls === "number" &&
+ summary.nulls > 0 && (
+ <span className="text-xs text-muted-foreground">
+ (nulls: {summary.nulls})
+ </span>
+ )}
+ </div>
+ ) : null;
+
const headerWithType = (
<div className="flex flex-col">
<span className="font-bold">{key}</span>
- {showDataTypes && dtype && (
- <span className="text-xs text-muted-foreground">{dtype}</span>
- )}
+ {dtypeHeader}
</div>
);
diff --git a/frontend/src/plugins/impl/DataTablePlugin.tsx b/frontend/src/plugins/impl/DataTablePlugin.tsx
index b882a98607..61d4bcbe66 100644
--- a/frontend/src/plugins/impl/DataTablePlugin.tsx
+++ b/frontend/src/plugins/impl/DataTablePlugin.tsx
@@ -511,6 +511,7 @@ const DataTableComponent = ({
generateColumns({
rowHeaders: memoizedRowHeaders,
selection: selection,
+ chartSpecModel: chartSpecModel,
fieldTypes: memoizedFieldTypes,
textJustifyColumns: memoizedTextJustifyColumns,
wrappedColumns: memoizedWrappedColumns,