diff --git a/public/partials/gist.htm b/public/partials/gist.htm
index 39d7f7e..f6a70ca 100644
--- a/public/partials/gist.htm
+++ b/public/partials/gist.htm
@@ -68,7 +68,7 @@
-
+
diff --git a/public/partials/pageView.htm b/public/partials/pageView.htm
index 935f6ba..72b680d 100644
--- a/public/partials/pageView.htm
+++ b/public/partials/pageView.htm
@@ -94,7 +94,7 @@
{{ gotoError || 'Line, or line:column' }}
-
+
diff --git a/public/script/review-pages.js b/public/script/review-pages.js
index 3c6fc92..8fc270d 100644
--- a/public/script/review-pages.js
+++ b/public/script/review-pages.js
@@ -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) => {
diff --git a/public/script/table-analysis.js b/public/script/table-analysis.js
index 86d00b4..80e2612 100644
--- a/public/script/table-analysis.js
+++ b/public/script/table-analysis.js
@@ -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,
diff --git a/public/script/table-chart.js b/public/script/table-chart.js
index 03f3935..c698dcf 100644
--- a/public/script/table-chart.js
+++ b/public/script/table-chart.js
@@ -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);
}
diff --git a/test/table-analysis.test.js b/test/table-analysis.test.js
index 867644b..f2248c2 100644
--- a/test/table-analysis.test.js
+++ b/test/table-analysis.test.js
@@ -144,7 +144,7 @@ describe("Table analysis", function () {
expect(g.header).to.deep.equal(["model", "Rows", "accuracy", "plausible"]);
expect(g.body).to.deep.equal([["Baseline", "2", "71.2", "412.0"], ["+ Aug.", "2", "74.0", "447.0"], ["Ours", "2", "78.9", "501.0"]]);
// "Treat as number" brings the seed back.
- expect(T.groupTable(t, all(t), 0, "mean", (c) => true).header).to.deep.equal(["model", "Rows", "seed", "accuracy", "plausible"]);
+ expect(T.groupTable(t, all(t), 0, "mean", () => true).header).to.deep.equal(["model", "Rows", "seed", "accuracy", "plausible"]);
expect(g.columns[2].agg).to.equal("mean");
expect(g.columns[2].keys[0]).to.be.closeTo(71.2, 1e-9);
});
@@ -160,6 +160,14 @@ describe("Table analysis", function () {
const t = table("k,v\na,1\n,2\na,3\n");
expect(T.groupTable(t, all(t), 0, "sum").body).to.deep.equal([["a", "2", "4"], ["(empty)", "1", "2"]]);
});
+ it("keeps 'none', 'NA' and '-' as categories in text columns", () => {
+ const t = table("augmentation,acc\nnone,1\nflip,2\nnone,3\nNA,4\n-,5\n,6\n");
+ expect(t.columns[0].type).to.equal("text");
+ expect(t.columns[0].empty).to.equal(1);
+ expect(T.groupTable(t, all(t), 0, "sum").body.map((r) => r[0])).to.deep.equal(["none", "flip", "NA", "-", "(empty)"]);
+ // In a number column they still mean "no value".
+ expect(table("v\n1\nNA\n-\n3\n").columns[0]).to.include({ type: "number", empty: 2 });
+ });
});
describe("export", () => {
@@ -216,6 +224,10 @@ describe("Table analysis", function () {
expect(line.series[0].path.startsWith("M")).to.equal(true);
const scatter = chart.buildChart(t, all(t), { type: "scatter", x: "v", y: ["v"] }, { width: 600 });
expect(scatter.series[0].dots).to.have.length(3);
+ // An ID-like text x column is refused instead of measuring every label.
+ const ids = table("id,v\n" + Array.from({ length: 200000 }, (_, i) => `row-${i},${i % 7}`).join("\n"));
+ const many = chart.buildChart(ids, all(ids), { type: "scatter", x: "id", y: ["v"] }, { width: 600 });
+ expect(many.empty).to.match(/200,000 different values/);
});
it("explains an empty chart", () => {
const t = table(RESULTS);