fix(tables): keep text placeholders as values, reset per file, bound scatter labels

- 'none', 'NA' and '-' are empty only in number and date columns; in
  text columns they are categories.
- The table viewer is keyed per file, so a search or filter in one CSV
  doesn't carry over to the next.
- A scatter chart with more than 500 text x values is refused instead of
  measuring every label (and spreading 100k values into Math.max).
- Gist CSV rows are cached per file object instead of a 1 MB string key
  built on every render.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
Thomas DurieuxandClaude Opus 5.5 committed 2026-11-29 12:35:46 +00:00
1 parent 37a01257b3
commit 68239afbbb
6 files changed
+44 -14

No files matched your search

+10 -4
View File
@@ -113,11 +113,17 @@ export function setupGistPage(state) {
state.gistMediaUrl = (file) => blobUrl(file, false);
state.gistRawUrl = (file) => blobUrl(file, true);
// Parsed once per file: the table viewer re-analyzes only when its rows change.
const csvCache = new Map();
// Keyed by the file object: no 1 MB key string per render, and a reloaded
// gist's old files are released with it.
const csvCache = new WeakMap();
state.gistCsvRows = (file) => {
const key = (file?.filename || "") + "\0" + (file?.content || "");
if (!csvCache.has(key)) csvCache.set(key, parseDelimited(file?.content || "", extension(file?.filename) === "tsv" ? "\t" : ","));
return csvCache.get(key);
if (!file) return [];
let entry = csvCache.get(file);
if (!entry || entry.content !== file.content || entry.filename !== file.filename) {
entry = { content: file.content, filename: file.filename, rows: parseDelimited(file.content || "", extension(file.filename) === "tsv" ? "\t" : ",") };
csvCache.set(file, entry);
}
return entry.rows;
};
state.gistCopied = null;
state.copyGistFile = async (index) => {
+11 -4
View File
@@ -11,9 +11,12 @@ export const AGGREGATE_LABEL = Object.fromEntries(AGGREGATES);
const AGGREGATE_SHORT = { mean: "Avg", sum: "Sum", median: "Median", min: "Min", max: "Max", count: "Count", std: "Std dev" };
export const aggregateShort = (fn) => AGGREGATE_SHORT[fn] || fn;
// Cells that mean "no value" in exported data (pandas, R, spreadsheets).
const MISSING = /^(?:|na|n\/a|nan|null|none|-|—|–)$/i;
export const isMissing = (raw) => raw == null || MISSING.test(String(raw).trim());
// Cells that mean "no value" in exported numbers and dates (pandas, R,
// spreadsheets). In a text column they are real categories ("none" as an
// augmentation, a model called "None"), so there only a blank cell is empty.
const PLACEHOLDER = /^(?:na|n\/a|nan|null|none|-|—|–)$/i;
export const isBlank = (raw) => raw == null || String(raw).trim() === "";
export const isMissing = (raw) => isBlank(raw) || PLACEHOLDER.test(String(raw).trim());
// 1,234 · -0.5 · +3 · 12% · 1e-3 · −2 (Unicode minus) · .5 — one match gives
// the value, the decimals as written and the percent sign.
@@ -140,7 +143,11 @@ export function makeColumn(name, index, values, forced) {
}
if (!type || type === "text") {
type = "text";
for (let i = 0; i < n; i++) keys[i] = isMissing(values[i]) ? null : String(values[i]).trim();
empty = 0;
for (let i = 0; i < n; i++) {
keys[i] = isBlank(values[i]) ? null : String(values[i]).trim();
if (keys[i] === null) empty++;
}
}
return {
name, index, type, keys, empty,
+8 -3
View File
@@ -9,7 +9,8 @@ export const SERIES_LIGHT = ["#2a78d6", "#eb6834", "#1baf7a", "#eda100"];
export const SERIES_DARK = ["#3987e5", "#d95926", "#199e70", "#c98500"];
export const MAX_SERIES = { bar: 4, line: 4, scatter: 3 };
export const MAX_BARS = 40;
export const MAX_POINTS = 2000;
export const MAX_CATEGORIES = 500;
const MAX_POINTS = 2000;
const isIdLike = (column) => column.type === "number" && !!column.identifier;
@@ -192,6 +193,9 @@ function scatterChart(table, rows, x, ys, base) {
// Text on the x axis: one position per value in first-seen order.
const order = new Map();
points.forEach((p) => { if (!order.has(p.label)) order.set(p.label, order.size); p.xv = order.get(p.label); });
// An ID-like column has a label per row: no readable axis, and measuring
// every label would stall the tab.
if (order.size > MAX_CATEGORIES) return { ...base, empty: `The x column has ${order.size.toLocaleString("en-US")} different values; choose a number or date column, or group the rows first.` };
base.categoryLabels = [...order.keys()];
}
points = thin(points, base);
@@ -210,7 +214,7 @@ function xyChart(points, x, ys, base, continuous, kind) {
const values = points.flatMap((p) => p.values).filter((v) => v != null);
if (!values.length) return { ...base, empty: "The selected columns have no values in these rows." };
const narrow = base.width < 480;
const yScale = niceTicks(Math.min(...values), Math.max(...values), narrow ? 4 : 5);
const yScale = niceTicks(values.reduce((a, v) => Math.min(a, v), Infinity), values.reduce((a, v) => Math.max(a, v), -Infinity), narrow ? 4 : 5);
const unit = ys.length === 1 ? ys[0].unit : "";
const yLabels = yScale.ticks.map((v) => tickLabel(v, yScale.step, unit));
const left = Math.max(...yLabels.map((l) => textWidth(l))) + 14;
@@ -230,7 +234,8 @@ function xyChart(points, x, ys, base, continuous, kind) {
sxv = (i) => plot.x + inset + i * stepW;
if (!cats) points.forEach((p, i) => { p.xv = i; });
const labels = cats || points.map((p) => p.label);
const maxLabels = Math.max(2, Math.floor(plot.w / (Math.max(...labels.map((l) => textWidth(l))) + 12)));
const widest = labels.reduce((w, l) => Math.max(w, textWidth(l)), 0);
const maxLabels = Math.max(2, Math.floor(plot.w / (widest + 12)));
const every = Math.ceil(labels.length / maxLabels);
xTicks = labels.map((label, i) => ({ x: sxv(i), label, show: i % every === 0 })).filter((t) => t.show);
}