@tetrascience-npm/ts-lib-viz 0.0.0-stage → 0.1.4
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +202 -0
- package/README.md +2171 -2
- package/dist/src/builder/PlotPanel.d.ts +42 -0
- package/dist/src/builder/PlotPanel.js +433 -0
- package/dist/src/builder/Popover.d.ts +37 -0
- package/dist/src/builder/Popover.js +32 -0
- package/dist/src/builder/Stepper.d.ts +21 -0
- package/dist/src/builder/Stepper.js +41 -0
- package/dist/src/builder/index.d.ts +10 -0
- package/dist/src/builder/index.js +10 -0
- package/dist/src/core/adapters.d.ts +86 -0
- package/dist/src/core/adapters.js +219 -0
- package/dist/src/core/aggregate.d.ts +74 -0
- package/dist/src/core/aggregate.js +102 -0
- package/dist/src/core/annotations.d.ts +34 -0
- package/dist/src/core/annotations.js +71 -0
- package/dist/src/core/band-order.d.ts +86 -0
- package/dist/src/core/band-order.js +132 -0
- package/dist/src/core/binning.d.ts +123 -0
- package/dist/src/core/binning.js +193 -0
- package/dist/src/core/bounds.d.ts +40 -0
- package/dist/src/core/bounds.js +107 -0
- package/dist/src/core/brush.d.ts +73 -0
- package/dist/src/core/brush.js +102 -0
- package/dist/src/core/build.d.ts +322 -0
- package/dist/src/core/build.js +496 -0
- package/dist/src/core/camera.d.ts +152 -0
- package/dist/src/core/camera.js +303 -0
- package/dist/src/core/capabilities.d.ts +24 -0
- package/dist/src/core/capabilities.js +307 -0
- package/dist/src/core/chord.d.ts +49 -0
- package/dist/src/core/chord.js +57 -0
- package/dist/src/core/circular.d.ts +83 -0
- package/dist/src/core/circular.js +63 -0
- package/dist/src/core/cluster.d.ts +136 -0
- package/dist/src/core/cluster.js +607 -0
- package/dist/src/core/critique.d.ts +92 -0
- package/dist/src/core/critique.js +905 -0
- package/dist/src/core/density.d.ts +38 -0
- package/dist/src/core/density.js +82 -0
- package/dist/src/core/figure-set.d.ts +34 -0
- package/dist/src/core/figure-set.js +55 -0
- package/dist/src/core/finding-codes.d.ts +10 -0
- package/dist/src/core/finding-codes.js +1 -0
- package/dist/src/core/fit-types.d.ts +46 -0
- package/dist/src/core/fit-types.js +7 -0
- package/dist/src/core/fitter.d.ts +62 -0
- package/dist/src/core/fitter.js +102 -0
- package/dist/src/core/frame-geometry.d.ts +407 -0
- package/dist/src/core/frame-geometry.js +612 -0
- package/dist/src/core/frame.d.ts +329 -0
- package/dist/src/core/frame.js +1432 -0
- package/dist/src/core/geom-table.d.ts +503 -0
- package/dist/src/core/geom-table.js +484 -0
- package/dist/src/core/geom-types.d.ts +182 -0
- package/dist/src/core/geom-types.js +17 -0
- package/dist/src/core/grammar.d.ts +359 -0
- package/dist/src/core/grammar.js +397 -0
- package/dist/src/core/heat-reductions.d.ts +36 -0
- package/dist/src/core/heat-reductions.js +133 -0
- package/dist/src/core/hexbin.d.ts +84 -0
- package/dist/src/core/hexbin.js +168 -0
- package/dist/src/core/hierarchy.d.ts +59 -0
- package/dist/src/core/hierarchy.js +248 -0
- package/dist/src/core/hit.d.ts +75 -0
- package/dist/src/core/hit.js +37 -0
- package/dist/src/core/index.d.ts +33 -0
- package/dist/src/core/index.js +18 -0
- package/dist/src/core/internal.d.ts +58 -0
- package/dist/src/core/internal.js +58 -0
- package/dist/src/core/labels.d.ts +37 -0
- package/dist/src/core/labels.js +54 -0
- package/dist/src/core/layers.d.ts +594 -0
- package/dist/src/core/layers.js +2892 -0
- package/dist/src/core/load.d.ts +17 -0
- package/dist/src/core/load.js +356 -0
- package/dist/src/core/marginal.d.ts +65 -0
- package/dist/src/core/marginal.js +116 -0
- package/dist/src/core/mesh.d.ts +69 -0
- package/dist/src/core/mesh.js +237 -0
- package/dist/src/core/omission.d.ts +41 -0
- package/dist/src/core/omission.js +62 -0
- package/dist/src/core/palette-sets.d.ts +623 -0
- package/dist/src/core/palette-sets.js +1058 -0
- package/dist/src/core/palette.d.ts +871 -0
- package/dist/src/core/palette.js +511 -0
- package/dist/src/core/plot-data.d.ts +71 -0
- package/dist/src/core/plot-data.js +82 -0
- package/dist/src/core/predicate.d.ts +50 -0
- package/dist/src/core/predicate.js +20 -0
- package/dist/src/core/profile.d.ts +111 -0
- package/dist/src/core/profile.js +221 -0
- package/dist/src/core/readout.d.ts +108 -0
- package/dist/src/core/readout.js +215 -0
- package/dist/src/core/region.d.ts +56 -0
- package/dist/src/core/region.js +101 -0
- package/dist/src/core/rings.d.ts +96 -0
- package/dist/src/core/rings.js +171 -0
- package/dist/src/core/rules.d.ts +45 -0
- package/dist/src/core/rules.js +69 -0
- package/dist/src/core/sankey.d.ts +49 -0
- package/dist/src/core/sankey.js +96 -0
- package/dist/src/core/select.d.ts +52 -0
- package/dist/src/core/select.js +216 -0
- package/dist/src/core/side-track.d.ts +98 -0
- package/dist/src/core/side-track.js +197 -0
- package/dist/src/core/sizing.d.ts +54 -0
- package/dist/src/core/sizing.js +37 -0
- package/dist/src/core/spec-edit.d.ts +68 -0
- package/dist/src/core/spec-edit.js +105 -0
- package/dist/src/core/spec.d.ts +1111 -0
- package/dist/src/core/spec.js +39 -0
- package/dist/src/core/stats.d.ts +161 -0
- package/dist/src/core/stats.js +318 -0
- package/dist/src/core/tick-format.d.ts +21 -0
- package/dist/src/core/tick-format.js +87 -0
- package/dist/src/core/tokens.d.ts +47 -0
- package/dist/src/core/tokens.js +138 -0
- package/dist/src/core/transform.d.ts +21 -0
- package/dist/src/core/transform.js +19 -0
- package/dist/src/core/voxels.d.ts +53 -0
- package/dist/src/core/voxels.js +129 -0
- package/dist/src/core/zoom.d.ts +76 -0
- package/dist/src/core/zoom.js +103 -0
- package/dist/src/graph/GraphSurface.d.ts +127 -0
- package/dist/src/graph/GraphSurface.js +423 -0
- package/dist/src/graph/data.d.ts +103 -0
- package/dist/src/graph/data.js +140 -0
- package/dist/src/graph/index.d.ts +7 -0
- package/dist/src/graph/index.js +7 -0
- package/dist/src/mcp/data.d.ts +28 -0
- package/dist/src/mcp/data.js +131 -0
- package/dist/src/mcp/index.d.ts +7 -0
- package/dist/src/mcp/index.js +7 -0
- package/dist/src/mcp/paths.d.ts +12 -0
- package/dist/src/mcp/paths.js +30 -0
- package/dist/src/mcp/render.d.ts +67 -0
- package/dist/src/mcp/render.js +189 -0
- package/dist/src/mcp/server.d.ts +7 -0
- package/dist/src/mcp/server.js +25 -0
- package/dist/src/mcp/tools.d.ts +82 -0
- package/dist/src/mcp/tools.js +412 -0
- package/dist/src/react/BrushLayer.d.ts +20 -0
- package/dist/src/react/BrushLayer.js +98 -0
- package/dist/src/react/ColorLegend.d.ts +45 -0
- package/dist/src/react/ColorLegend.js +64 -0
- package/dist/src/react/Figure.d.ts +19 -0
- package/dist/src/react/Figure.js +6 -0
- package/dist/src/react/Frame.d.ts +87 -0
- package/dist/src/react/Frame.js +192 -0
- package/dist/src/react/MarkLegend.d.ts +32 -0
- package/dist/src/react/MarkLegend.js +24 -0
- package/dist/src/react/SeriesLegend.d.ts +38 -0
- package/dist/src/react/SeriesLegend.js +27 -0
- package/dist/src/react/SizeLegend.d.ts +24 -0
- package/dist/src/react/SizeLegend.js +25 -0
- package/dist/src/react/Toolbar.d.ts +109 -0
- package/dist/src/react/Toolbar.js +483 -0
- package/dist/src/react/Tooltip.d.ts +37 -0
- package/dist/src/react/Tooltip.js +59 -0
- package/dist/src/react/Viz.d.ts +112 -0
- package/dist/src/react/Viz.js +1311 -0
- package/dist/src/react/export.d.ts +32 -0
- package/dist/src/react/export.js +181 -0
- package/dist/src/react/geoms/Area.d.ts +38 -0
- package/dist/src/react/geoms/Area.js +48 -0
- package/dist/src/react/geoms/Bars.d.ts +130 -0
- package/dist/src/react/geoms/Bars.js +164 -0
- package/dist/src/react/geoms/Box.d.ts +50 -0
- package/dist/src/react/geoms/Box.js +32 -0
- package/dist/src/react/geoms/Chord.d.ts +60 -0
- package/dist/src/react/geoms/Chord.js +78 -0
- package/dist/src/react/geoms/Contour.d.ts +44 -0
- package/dist/src/react/geoms/Contour.js +78 -0
- package/dist/src/react/geoms/DensityCurve.d.ts +30 -0
- package/dist/src/react/geoms/DensityCurve.js +38 -0
- package/dist/src/react/geoms/FitCurve.d.ts +26 -0
- package/dist/src/react/geoms/FitCurve.js +52 -0
- package/dist/src/react/geoms/Freqpoly.d.ts +30 -0
- package/dist/src/react/geoms/Freqpoly.js +45 -0
- package/dist/src/react/geoms/Gauge.d.ts +42 -0
- package/dist/src/react/geoms/Gauge.js +90 -0
- package/dist/src/react/geoms/Heatmap.d.ts +79 -0
- package/dist/src/react/geoms/Heatmap.js +51 -0
- package/dist/src/react/geoms/Hexbin.d.ts +74 -0
- package/dist/src/react/geoms/Hexbin.js +89 -0
- package/dist/src/react/geoms/Icicle.d.ts +38 -0
- package/dist/src/react/geoms/Icicle.js +9 -0
- package/dist/src/react/geoms/Ideogram.d.ts +58 -0
- package/dist/src/react/geoms/Ideogram.js +77 -0
- package/dist/src/react/geoms/Interval.d.ts +28 -0
- package/dist/src/react/geoms/Interval.js +39 -0
- package/dist/src/react/geoms/Line.d.ts +39 -0
- package/dist/src/react/geoms/Line.js +52 -0
- package/dist/src/react/geoms/Parallel.d.ts +38 -0
- package/dist/src/react/geoms/Parallel.js +131 -0
- package/dist/src/react/geoms/Pie.d.ts +62 -0
- package/dist/src/react/geoms/Pie.js +59 -0
- package/dist/src/react/geoms/Points.d.ts +84 -0
- package/dist/src/react/geoms/Points.js +207 -0
- package/dist/src/react/geoms/Points3d.d.ts +49 -0
- package/dist/src/react/geoms/Points3d.js +58 -0
- package/dist/src/react/geoms/Radar.d.ts +39 -0
- package/dist/src/react/geoms/Radar.js +59 -0
- package/dist/src/react/geoms/Range.d.ts +31 -0
- package/dist/src/react/geoms/Range.js +20 -0
- package/dist/src/react/geoms/RingStack.d.ts +39 -0
- package/dist/src/react/geoms/RingStack.js +65 -0
- package/dist/src/react/geoms/Rule.d.ts +29 -0
- package/dist/src/react/geoms/Rule.js +51 -0
- package/dist/src/react/geoms/Sankey.d.ts +24 -0
- package/dist/src/react/geoms/Sankey.js +51 -0
- package/dist/src/react/geoms/Segment.d.ts +36 -0
- package/dist/src/react/geoms/Segment.js +39 -0
- package/dist/src/react/geoms/Sunburst.d.ts +46 -0
- package/dist/src/react/geoms/Sunburst.js +64 -0
- package/dist/src/react/geoms/Surface.d.ts +32 -0
- package/dist/src/react/geoms/Surface.js +49 -0
- package/dist/src/react/geoms/Text.d.ts +35 -0
- package/dist/src/react/geoms/Text.js +40 -0
- package/dist/src/react/geoms/Treemap.d.ts +39 -0
- package/dist/src/react/geoms/Treemap.js +16 -0
- package/dist/src/react/geoms/Violin.d.ts +56 -0
- package/dist/src/react/geoms/Violin.js +60 -0
- package/dist/src/react/geoms/Volume.d.ts +37 -0
- package/dist/src/react/geoms/Volume.js +40 -0
- package/dist/src/react/geoms/Waterfall.d.ts +43 -0
- package/dist/src/react/geoms/Waterfall.js +18 -0
- package/dist/src/react/geoms/cube-stage.d.ts +39 -0
- package/dist/src/react/geoms/cube-stage.js +85 -0
- package/dist/src/react/geoms/hierarchy-cells.d.ts +42 -0
- package/dist/src/react/geoms/hierarchy-cells.js +59 -0
- package/dist/src/react/geoms/sector-axis.d.ts +66 -0
- package/dist/src/react/geoms/sector-axis.js +99 -0
- package/dist/src/react/gl/GlMesh.d.ts +22 -0
- package/dist/src/react/gl/GlMesh.js +78 -0
- package/dist/src/react/gl/GlPoints.d.ts +45 -0
- package/dist/src/react/gl/GlPoints.js +117 -0
- package/dist/src/react/gl/GlVolume.d.ts +28 -0
- package/dist/src/react/gl/GlVolume.js +112 -0
- package/dist/src/react/gl/GlWalls.d.ts +24 -0
- package/dist/src/react/gl/GlWalls.js +55 -0
- package/dist/src/react/gl/context.d.ts +71 -0
- package/dist/src/react/gl/context.js +98 -0
- package/dist/src/react/gl/encode.d.ts +120 -0
- package/dist/src/react/gl/encode.js +215 -0
- package/dist/src/react/gl/index.d.ts +10 -0
- package/dist/src/react/gl/index.js +8 -0
- package/dist/src/react/gl/refresh.d.ts +7 -0
- package/dist/src/react/gl/refresh.js +19 -0
- package/dist/src/react/gl/renderer.d.ts +377 -0
- package/dist/src/react/gl/renderer.js +1081 -0
- package/dist/src/react/gl/shaders.d.ts +92 -0
- package/dist/src/react/gl/shaders.js +405 -0
- package/dist/src/react/gl/surface.d.ts +31 -0
- package/dist/src/react/gl/surface.js +73 -0
- package/dist/src/react/index.d.ts +12 -0
- package/dist/src/react/index.js +11 -0
- package/dist/src/react/orbit.d.ts +38 -0
- package/dist/src/react/orbit.js +81 -0
- package/dist/src/react/sides/DendrogramPanel.d.ts +32 -0
- package/dist/src/react/sides/DendrogramPanel.js +39 -0
- package/dist/src/react/sides/DensityPanel.d.ts +31 -0
- package/dist/src/react/sides/DensityPanel.js +37 -0
- package/dist/src/react/sides/HistogramPanel.d.ts +30 -0
- package/dist/src/react/sides/HistogramPanel.js +18 -0
- package/dist/src/react/sides/RugPanel.d.ts +28 -0
- package/dist/src/react/sides/RugPanel.js +21 -0
- package/dist/src/react/sides/TrackPanel.d.ts +37 -0
- package/dist/src/react/sides/TrackPanel.js +27 -0
- package/dist/src/react/theme.d.ts +48 -0
- package/dist/src/react/theme.js +148 -0
- package/dist/src/react/useFits.d.ts +26 -0
- package/dist/src/react/useFits.js +62 -0
- package/dist/src/react/useZoom.d.ts +50 -0
- package/dist/src/react/useZoom.js +141 -0
- package/package.json +164 -5
- package/theme/default.css +62 -0
|
@@ -0,0 +1,905 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* What is wrong with a chart, sorted by severity.
|
|
3
|
+
*
|
|
4
|
+
* `wrong` means the figure misstates the data and blocks a save. `bad` means a reader cannot
|
|
5
|
+
* read it. `ugly` is aesthetics.
|
|
6
|
+
*/
|
|
7
|
+
import { boundsOf } from "./aggregate.js";
|
|
8
|
+
import { GRAMMAR, groundedKinds } from "./grammar.js";
|
|
9
|
+
import { GL_ABOVE, pointCount, treatmentOf } from "./frame.js";
|
|
10
|
+
import { ASCENT, bandReach, buildAxis, chromeOf, isBand, textRun, ringLayout, slicesLegible, Y_TITLE_ABOVE, } from "./frame-geometry.js";
|
|
11
|
+
import { kindFacts, layerScale, paintAesthetic, paintColumn } from "./geom-table.js";
|
|
12
|
+
import { POINT_SHAPES } from "./geom-types.js";
|
|
13
|
+
import { SPLIT_CAP } from "./layers.js";
|
|
14
|
+
import { tickFormatter } from "./tick-format.js";
|
|
15
|
+
import { transformOf } from "./transform.js";
|
|
16
|
+
/**
|
|
17
|
+
* Blended fragments a frame can shade inside its budget, in CSS pixels of geom area.
|
|
18
|
+
*
|
|
19
|
+
* About 1.4 ms per million blended fragments. Half a 60fps frame is 8 ms.
|
|
20
|
+
*/
|
|
21
|
+
export const FILL_BUDGET_PX = 1_400_000;
|
|
22
|
+
/**
|
|
23
|
+
* How many color categories can a reader distinguish?
|
|
24
|
+
* The Okabe-Ito palette and Grammar of Graphics agree the maximum should be 8.
|
|
25
|
+
*/
|
|
26
|
+
export const COLORS_LEGIBLE = 8;
|
|
27
|
+
/** Words a headline can share with an axis title without having restated it. */
|
|
28
|
+
const STOP_WORDS = new Set(["the", "a", "an", "of", "and", "or", "by", "in", "on", "per", "vs", "against", "over"]);
|
|
29
|
+
const words = (text) => text
|
|
30
|
+
.toLowerCase()
|
|
31
|
+
.split(/[^a-z0-9]+/)
|
|
32
|
+
.filter((word) => word.length > 0 && !STOP_WORDS.has(word));
|
|
33
|
+
/**
|
|
34
|
+
* Whether a headline says anything the axes do not.
|
|
35
|
+
*
|
|
36
|
+
* Chart-design rule 10 asks for a claim. A headline with all its words in an axis title has
|
|
37
|
+
* restated the chart.
|
|
38
|
+
*/
|
|
39
|
+
const restatesAxes = (headline, axisTitles) => {
|
|
40
|
+
const claim = words(headline);
|
|
41
|
+
if (claim.length === 0)
|
|
42
|
+
return false;
|
|
43
|
+
const axes = new Set(axisTitles.flatMap(words));
|
|
44
|
+
return claim.every((word) => axes.has(word));
|
|
45
|
+
};
|
|
46
|
+
const layerPath = (index, field) => `layers[${index}]${field ? `.${field}` : ""}`;
|
|
47
|
+
/** A nonlinear transform as a reader names its scale. */
|
|
48
|
+
const SCALE_NAMES = {
|
|
49
|
+
log10: "log",
|
|
50
|
+
log2: "log",
|
|
51
|
+
sqrt: "square-root",
|
|
52
|
+
pseudo_log: "pseudo-log",
|
|
53
|
+
};
|
|
54
|
+
/**
|
|
55
|
+
* Findings a spec carries on its own, with no data behind it.
|
|
56
|
+
*
|
|
57
|
+
* These read the spec alone. The builder reports while an author types.
|
|
58
|
+
*/
|
|
59
|
+
function fromSpec(spec) {
|
|
60
|
+
const found = [];
|
|
61
|
+
const kinds = spec.layers.filter((series) => series.visible !== false).map((series) => series.kind);
|
|
62
|
+
const grounded = groundedKinds(kinds);
|
|
63
|
+
/* Proportional ink. A length is read from zero on a linear scale. */
|
|
64
|
+
const yTransform = transformOf(spec.scales?.y ?? {});
|
|
65
|
+
if (grounded && yTransform !== "identity") {
|
|
66
|
+
found.push({
|
|
67
|
+
severity: "wrong",
|
|
68
|
+
code: "grounded-nonlinear-axis",
|
|
69
|
+
path: "scales.y.transform",
|
|
70
|
+
message: `A bar or an area is read as a length from zero, and a ${SCALE_NAMES[yTransform]} scale draws those lengths out of proportion to the values. Draw the measure as points, or keep the axis linear.`,
|
|
71
|
+
});
|
|
72
|
+
}
|
|
73
|
+
const range = spec.scales?.y?.limits;
|
|
74
|
+
if (grounded && range && (range[0] > 0 || range[1] < 0)) {
|
|
75
|
+
const near = range[0] > 0 ? range[0] : range[1];
|
|
76
|
+
found.push({
|
|
77
|
+
severity: "wrong",
|
|
78
|
+
code: "grounded-truncated-axis",
|
|
79
|
+
path: "scales.y.limits",
|
|
80
|
+
message: `A bar starting at ${near} instead of zero draws a difference several times larger than the one in the data. Extend the y limits to zero, or draw the measure as points.`,
|
|
81
|
+
});
|
|
82
|
+
}
|
|
83
|
+
/*
|
|
84
|
+
A running total of anything but a sum or a count.
|
|
85
|
+
|
|
86
|
+
Adding means together answers no question a reader has: the third point is the mean of the
|
|
87
|
+
first period plus the mean of the second, which is neither a mean nor a total. A count and a
|
|
88
|
+
sum both accumulate into themselves, and nothing else does.
|
|
89
|
+
*/
|
|
90
|
+
spec.layers.forEach((series, index) => {
|
|
91
|
+
if (!series.cumulative)
|
|
92
|
+
return;
|
|
93
|
+
if (!kindFacts(series.kind).layer.includes("cumulative")) {
|
|
94
|
+
found.push({
|
|
95
|
+
severity: "wrong",
|
|
96
|
+
code: "cumulative-unsupported",
|
|
97
|
+
path: layerPath(index, "cumulative"),
|
|
98
|
+
message: `${series.kind} draws no running total, and the flag is read by nothing. Draw the layer as a bar, a line or an area, or drop the running total.`,
|
|
99
|
+
});
|
|
100
|
+
return;
|
|
101
|
+
}
|
|
102
|
+
/* The build sums a running total that states no aggregate. */
|
|
103
|
+
const reduction = series.aggregate;
|
|
104
|
+
if (reduction && reduction !== "sum" && reduction !== "count") {
|
|
105
|
+
found.push({
|
|
106
|
+
severity: "wrong",
|
|
107
|
+
code: "cumulative-of-a-statistic",
|
|
108
|
+
path: layerPath(index, "cumulative"),
|
|
109
|
+
message: `A running total of a ${reduction} adds one ${reduction} to the next, which stands for nothing. Accumulate a sum or a count, or drop the running total.`,
|
|
110
|
+
});
|
|
111
|
+
}
|
|
112
|
+
});
|
|
113
|
+
/*
|
|
114
|
+
A nought filled into a pair with no rows.
|
|
115
|
+
|
|
116
|
+
A count and a sum over no rows are nought. A mean or a median over none is undefined.
|
|
117
|
+
*/
|
|
118
|
+
spec.layers.forEach((series, index) => {
|
|
119
|
+
if (!series.style?.emptyAsZero)
|
|
120
|
+
return;
|
|
121
|
+
if (!kindFacts(series.kind).style.includes("emptyAsZero"))
|
|
122
|
+
return;
|
|
123
|
+
const reduction = series.aggregate ?? (typeof series.aes.z === "string" ? "mean" : "count");
|
|
124
|
+
if (reduction === "count" || reduction === "sum")
|
|
125
|
+
return;
|
|
126
|
+
found.push({
|
|
127
|
+
severity: "wrong",
|
|
128
|
+
code: "empty-as-zero-of-a-statistic",
|
|
129
|
+
path: layerPath(index, "style.emptyAsZero"),
|
|
130
|
+
message: `A pair with no rows has no ${reduction}, and the nought draws it as the lowest reading in the grid. Leave the gap, or reduce the cells to a count or a sum.`,
|
|
131
|
+
});
|
|
132
|
+
});
|
|
133
|
+
spec.layers.forEach((series, index) => {
|
|
134
|
+
/* A hidden layer draws nothing to misstate. */
|
|
135
|
+
if (series.visible === false)
|
|
136
|
+
return;
|
|
137
|
+
/*
|
|
138
|
+
An aggregate on a kind that fixes its own statistic. A `mean` here describes a figure the
|
|
139
|
+
chart will not draw. Only the kinds answering `false` outright.
|
|
140
|
+
*/
|
|
141
|
+
if (series.aggregate && GRAMMAR[series.kind]?.aggregated === false) {
|
|
142
|
+
found.push({
|
|
143
|
+
severity: "wrong",
|
|
144
|
+
code: "fixed-statistic",
|
|
145
|
+
path: layerPath(index, "aggregate"),
|
|
146
|
+
message: `${series.kind} runs its own statistic, so the ${series.aggregate} is read by nothing and the figure will not show it. Drop it, or pick a kind that reduces.`,
|
|
147
|
+
});
|
|
148
|
+
}
|
|
149
|
+
/*
|
|
150
|
+
Accessibility settings often require a second channel. Size can be used as a measure, but is
|
|
151
|
+
harder to judge than length.
|
|
152
|
+
*/
|
|
153
|
+
if (series.aes.size && (!series.aes.x || !series.aes.y)) {
|
|
154
|
+
found.push({
|
|
155
|
+
severity: "bad",
|
|
156
|
+
code: "size-carries-the-measure",
|
|
157
|
+
path: layerPath(index, "aes.size"),
|
|
158
|
+
message: `${series.aes.size} is encoded by area while ${series.aes.y ? "the horizontal" : "the vertical"} scale is unbound, and a reader reads position better than area. Put the measure on the free axis, and size by something the chart states second.`,
|
|
159
|
+
});
|
|
160
|
+
}
|
|
161
|
+
/*
|
|
162
|
+
A treemap over a hierarchy with nothing nested in it. An icicle reads the same three
|
|
163
|
+
channels and gives each level a row.
|
|
164
|
+
*/
|
|
165
|
+
if (GRAMMAR[series.kind]?.geom === "treemap" && !series.aes.parent) {
|
|
166
|
+
found.push({
|
|
167
|
+
severity: "ugly",
|
|
168
|
+
code: "area-with-no-nesting",
|
|
169
|
+
path: layerPath(index, "kind"),
|
|
170
|
+
message: "These cells are parts of one whole with nothing nested under them, and a reader takes a length off the page more accurately than an area. Draw it as an icicle, which reads the same three channels and gives each level a row, or bind `parent` so the packing has nesting to show.",
|
|
171
|
+
});
|
|
172
|
+
}
|
|
173
|
+
/*
|
|
174
|
+
A violin holding each group to one width while the groups differ in size. `scale: "count"`
|
|
175
|
+
spends the width on the readings.
|
|
176
|
+
*/
|
|
177
|
+
if (series.kind === "violin" && series.statParams?.scale === "width") {
|
|
178
|
+
found.push({
|
|
179
|
+
severity: "ugly",
|
|
180
|
+
code: "width-hides-the-count",
|
|
181
|
+
path: layerPath(index, "statParams.scale"),
|
|
182
|
+
message: 'Every violin is drawn to one width, so a group of ten looks as well supported as a group of four hundred. Take `scale: "count"` to spend the width on the readings behind each shape, or `"area"` to hold the area steady instead.',
|
|
183
|
+
});
|
|
184
|
+
}
|
|
185
|
+
/* A bound shape channel draws one glyph per level. It is the second channel. */
|
|
186
|
+
if (paintColumn(series) && !series.style?.shape && !series.aes.shape && GRAMMAR[series.kind]?.geom === "point") {
|
|
187
|
+
found.push({
|
|
188
|
+
severity: "bad",
|
|
189
|
+
code: "color-alone",
|
|
190
|
+
path: layerPath(index, "style.shape"),
|
|
191
|
+
message: "Color is the only thing telling these groups apart, so the figure is unreadable in grayscale and to a reader with color-vision deficiency. Give the series a shape as well.",
|
|
192
|
+
});
|
|
193
|
+
}
|
|
194
|
+
});
|
|
195
|
+
/*
|
|
196
|
+
Two smoothed layers on one frame at bandwidths the author stated differently.
|
|
197
|
+
|
|
198
|
+
A wider bandwidth flattens a mode the narrower one keeps. Stated bandwidths only.
|
|
199
|
+
*/
|
|
200
|
+
const stated = spec.layers
|
|
201
|
+
.map((series, index) => ({ index, bw: series.visible === false ? undefined : series.statParams?.bw }))
|
|
202
|
+
.filter((entry) => typeof entry.bw === "number");
|
|
203
|
+
if (stated.length > 1 && new Set(stated.map((entry) => entry.bw)).size > 1) {
|
|
204
|
+
found.push({
|
|
205
|
+
severity: "bad",
|
|
206
|
+
code: "bandwidths-disagree",
|
|
207
|
+
path: layerPath(stated[1].index, "statParams.bw"),
|
|
208
|
+
message: `These layers are smoothed at ${stated.map((entry) => entry.bw).join(" and ")}, so their shapes differ by how each was smoothed as well as by what the readings say. Smooth them alike, or draw them on separate panels.`,
|
|
209
|
+
});
|
|
210
|
+
}
|
|
211
|
+
/* A reference layer naming nothing it marks, by a label or by a mapped key. Chart-design rule 4. */
|
|
212
|
+
spec.layers.forEach((series, index) => {
|
|
213
|
+
if (series.kind !== "hline" && series.kind !== "vline" && series.kind !== "abline")
|
|
214
|
+
return;
|
|
215
|
+
if (series.aes.label || paintColumn(series) || series.aes.linetype)
|
|
216
|
+
return;
|
|
217
|
+
found.push({
|
|
218
|
+
severity: "bad",
|
|
219
|
+
code: "unlabeled-reference",
|
|
220
|
+
path: layerPath(index, "aes.label"),
|
|
221
|
+
message: `A ${series.kind} layer with no label leaves the reader guessing what its lines mark. Name each one.`,
|
|
222
|
+
});
|
|
223
|
+
});
|
|
224
|
+
found.push(...fromPolarSpec(spec));
|
|
225
|
+
/*
|
|
226
|
+
A guide channel bound with the legends turned off.
|
|
227
|
+
|
|
228
|
+
A figure mapping a column to color and printing no key has spent an undecodable channel.
|
|
229
|
+
Direct labels answer it too.
|
|
230
|
+
*/
|
|
231
|
+
const guides = spec.layers
|
|
232
|
+
.filter((series) => series.visible !== false)
|
|
233
|
+
.flatMap((series) => [
|
|
234
|
+
...(paintColumn(series) ? [paintAesthetic(series)] : []),
|
|
235
|
+
...(series.aes.shape ? ["shape"] : []),
|
|
236
|
+
]);
|
|
237
|
+
if (spec.legend?.position === "none" && guides.length > 0) {
|
|
238
|
+
found.push({
|
|
239
|
+
severity: "bad",
|
|
240
|
+
code: "legend-missing",
|
|
241
|
+
path: "legend.position",
|
|
242
|
+
message: `The ${[...new Set(guides)].join(" and ")} channel carries a column and the legends are off, so the reader has no key to the values. Take a position for them, or label the geoms directly.`,
|
|
243
|
+
});
|
|
244
|
+
}
|
|
245
|
+
const axisTitles = [spec.scales?.x?.title, spec.scales?.y?.title, spec.scales?.y2?.title].filter((title) => Boolean(title));
|
|
246
|
+
if (spec.headline && axisTitles.length > 0 && restatesAxes(spec.headline, axisTitles)) {
|
|
247
|
+
found.push({
|
|
248
|
+
severity: "bad",
|
|
249
|
+
code: "headline-restates-axes",
|
|
250
|
+
path: "headline",
|
|
251
|
+
message: "The headline names what is on the axes, which the axes already do. Write the claim the chart makes: what a reader should take away from it.",
|
|
252
|
+
});
|
|
253
|
+
}
|
|
254
|
+
return found;
|
|
255
|
+
}
|
|
256
|
+
/**
|
|
257
|
+
* Aggregates that add up to a measured total.
|
|
258
|
+
*
|
|
259
|
+
* A count of rows and a sum of a column each divide into parts. Five means add to nothing.
|
|
260
|
+
*/
|
|
261
|
+
const ADDITIVE = new Set(["count", "sum"]);
|
|
262
|
+
/** Whether a kind positions by angle, read off the grammar. */
|
|
263
|
+
const isPolar = (kind) => GRAMMAR[kind]?.coord === "polar";
|
|
264
|
+
/**
|
|
265
|
+
* Whether a kind divides a turn between slices a reader compares.
|
|
266
|
+
*
|
|
267
|
+
* The rings do. A gauge draws one arc against a range. A radar draws lengths along rotated rules.
|
|
268
|
+
*/
|
|
269
|
+
const drawsSlices = (kind) => isPolar(kind) && kind !== "gauge" && kind !== "radar";
|
|
270
|
+
/**
|
|
271
|
+
* Whether the slices carry text a reader takes the value off.
|
|
272
|
+
*
|
|
273
|
+
* `numbers` writes the reduction on the slice. A `label` covering all slices does the same.
|
|
274
|
+
*/
|
|
275
|
+
const slicesSpeak = (series) => Boolean(series.numbers) || series.directLabels?.when.kind === "all";
|
|
276
|
+
/**
|
|
277
|
+
* The reduction a series draws, including its fallback.
|
|
278
|
+
*
|
|
279
|
+
* The bar path defaults to a mean with a measure bound and a count without. `pie-not-parts` reads
|
|
280
|
+
* this.
|
|
281
|
+
*/
|
|
282
|
+
const effectiveAggregate = (series) => series.aggregate ?? (typeof series.aes.y === "string" ? "mean" : "count");
|
|
283
|
+
/**
|
|
284
|
+
* What the polar coordinate is permitted on, off the spec alone.
|
|
285
|
+
*
|
|
286
|
+
* Three of the four conditions. `slices-past-legible` needs the panel. `pie-not-parts` stops a
|
|
287
|
+
* save.
|
|
288
|
+
*/
|
|
289
|
+
function fromPolarSpec(spec) {
|
|
290
|
+
const found = [];
|
|
291
|
+
spec.layers.forEach((series, index) => {
|
|
292
|
+
if (series.visible === false || !isPolar(series.kind))
|
|
293
|
+
return;
|
|
294
|
+
if (drawsSlices(series.kind) && !slicesSpeak(series)) {
|
|
295
|
+
found.push({
|
|
296
|
+
severity: "bad",
|
|
297
|
+
code: "angle-encodes-comparison",
|
|
298
|
+
path: layerPath(index, "kind"),
|
|
299
|
+
message: "A reader judges an angle and an area worse than a length, and these slices carry no text to read instead. Write the values with `numbers`, name every slice with `label`, or draw the measure as bars.",
|
|
300
|
+
});
|
|
301
|
+
}
|
|
302
|
+
if (series.kind === "gauge" && !series.numbers) {
|
|
303
|
+
found.push({
|
|
304
|
+
severity: "ugly",
|
|
305
|
+
code: "single-value-as-arc",
|
|
306
|
+
path: layerPath(index, "numbers"),
|
|
307
|
+
message: "A dial with no number is a stat tile with decoration, so the reading is left to an angle the reader has nothing to compare. Set `numbers` to write it.",
|
|
308
|
+
});
|
|
309
|
+
}
|
|
310
|
+
if (drawsSlices(series.kind)) {
|
|
311
|
+
const aggregate = effectiveAggregate(series);
|
|
312
|
+
if (!ADDITIVE.has(aggregate)) {
|
|
313
|
+
found.push({
|
|
314
|
+
severity: "wrong",
|
|
315
|
+
code: "pie-not-parts",
|
|
316
|
+
path: layerPath(index, "aggregate"),
|
|
317
|
+
message: `A ring asserts that its slices are parts of one whole, and a ${aggregate} per slice adds to a total nothing in the table stands for. Reduce with \`sum\` or \`count\`, or draw the measure as bars.`,
|
|
318
|
+
});
|
|
319
|
+
}
|
|
320
|
+
}
|
|
321
|
+
});
|
|
322
|
+
return found;
|
|
323
|
+
}
|
|
324
|
+
/**
|
|
325
|
+
* Slices past the count this panel's ring can label.
|
|
326
|
+
*
|
|
327
|
+
* `ringLayout` picks the label radius and `slicesLegible` divides its circumference. Without a
|
|
328
|
+
* measured area nothing is reported.
|
|
329
|
+
*/
|
|
330
|
+
function fromPolarFrame(spec, frame, area) {
|
|
331
|
+
if (!area)
|
|
332
|
+
return [];
|
|
333
|
+
const found = [];
|
|
334
|
+
spec.layers.forEach((series, index) => {
|
|
335
|
+
if (series.visible === false || !drawsSlices(series.kind))
|
|
336
|
+
return;
|
|
337
|
+
const data = frame.traces.find((held) => held.layerId === series.id)?.data;
|
|
338
|
+
if (!data || data.at !== "groups" || !("bars" in data) || data.bars.length === 0)
|
|
339
|
+
return;
|
|
340
|
+
const reads = kindFacts(series.kind).style.includes("innerRadius");
|
|
341
|
+
const layout = ringLayout(area.width, area.height, reads ? (series.style?.innerRadius ?? 0) : 0);
|
|
342
|
+
const type = series.numbers?.fontSize ?? frame.metrics.axis;
|
|
343
|
+
/* The drawn text. A renamed category is measured as the reader sees it. */
|
|
344
|
+
const longest = data.bars.reduce((most, slice) => Math.max(most, slice.category.length), 0);
|
|
345
|
+
const holds = slicesLegible(layout, type, longest);
|
|
346
|
+
if (data.bars.length <= holds)
|
|
347
|
+
return;
|
|
348
|
+
found.push({
|
|
349
|
+
severity: "bad",
|
|
350
|
+
code: "slices-past-legible",
|
|
351
|
+
path: layerPath(index, "kind"),
|
|
352
|
+
message: `${data.bars.length} slices is past the ${holds} this panel's ring has room to label at ${type}px. Gather the tail into one slice, cut the categories, or draw the measure as bars.`,
|
|
353
|
+
});
|
|
354
|
+
});
|
|
355
|
+
return found;
|
|
356
|
+
}
|
|
357
|
+
/**
|
|
358
|
+
* Findings that need the built frame.
|
|
359
|
+
*
|
|
360
|
+
* A color count, a legend order and an overplotted scatter are facts about what the data
|
|
361
|
+
* produced.
|
|
362
|
+
*/
|
|
363
|
+
function fromFrame(spec, frame, substituted) {
|
|
364
|
+
const found = [];
|
|
365
|
+
/*
|
|
366
|
+
A clustered order the cap turned down.
|
|
367
|
+
|
|
368
|
+
A refused order draws the same grid an unclustered spec draws. Nothing on the figure says
|
|
369
|
+
the arithmetic declined.
|
|
370
|
+
*/
|
|
371
|
+
for (const refused of frame.clusterRefused) {
|
|
372
|
+
found.push({
|
|
373
|
+
severity: "bad",
|
|
374
|
+
code: "cluster-past-cap",
|
|
375
|
+
path: `scales.${refused.axis}.order`,
|
|
376
|
+
message: `${refused.rows} categories is past the ${refused.cap} an exact clustering is affordable at, so the band kept the order the build produced. Aggregate first with kmeans, or narrow the rows.`,
|
|
377
|
+
});
|
|
378
|
+
}
|
|
379
|
+
if (frame.legend.length > COLORS_LEGIBLE) {
|
|
380
|
+
found.push({
|
|
381
|
+
severity: "bad",
|
|
382
|
+
code: "colors-past-legible",
|
|
383
|
+
path: "layers",
|
|
384
|
+
message: `${frame.legend.length} colors is past the ${COLORS_LEGIBLE} a reader can match against a legend${frame.legend.length >= SPLIT_CAP ? ", and the split cap is holding back more" : ""}. Label the notable geoms directly, or facet on the field instead of coloring by it.`,
|
|
385
|
+
});
|
|
386
|
+
}
|
|
387
|
+
/*
|
|
388
|
+
A caller that drew raw points past rule 7's switch. The footer stays quiet and the finding
|
|
389
|
+
goes to the caller.
|
|
390
|
+
*/
|
|
391
|
+
const drawn = pointCount(frame);
|
|
392
|
+
if (!substituted && treatmentOf(drawn) === "binned") {
|
|
393
|
+
found.push({
|
|
394
|
+
severity: "bad",
|
|
395
|
+
code: "overplotted-past-switch",
|
|
396
|
+
path: "layers",
|
|
397
|
+
message: `${drawn.toLocaleString()} geoms drawn individually, past the ${GL_ABOVE.toLocaleString()} where they overplot into a block whose density the reader misjudges. Draw a hexbin or a contour off a binned aggregate, and pass the substitution to the footer.`,
|
|
398
|
+
});
|
|
399
|
+
}
|
|
400
|
+
/*
|
|
401
|
+
A legend ordered against the geoms. `legend.order: "geoms"` reads it the way the lines end.
|
|
402
|
+
|
|
403
|
+
A line's last reading is where a direct label would draw. Reported, and a scatter has no
|
|
404
|
+
such ordering.
|
|
405
|
+
*/
|
|
406
|
+
const lines = frame.traces.filter((trace) => trace.data.at === "records" && trace.data.geom === "line" && trace.data.points.length > 0);
|
|
407
|
+
if (lines.length > 1 && spec.legend?.order !== "geoms") {
|
|
408
|
+
const lastOf = (trace) => trace.data.at === "records" && trace.data.geom === "line"
|
|
409
|
+
? (trace.data.points[trace.data.points.length - 1]?.y ?? 0)
|
|
410
|
+
: 0;
|
|
411
|
+
const byHeight = [...lines].sort((a, b) => lastOf(b) - lastOf(a));
|
|
412
|
+
if (byHeight.some((trace, index) => trace.id !== lines[index].id)) {
|
|
413
|
+
found.push({
|
|
414
|
+
severity: "ugly",
|
|
415
|
+
code: "legend-order",
|
|
416
|
+
path: "layers",
|
|
417
|
+
message: "The legend runs in authoring order and the lines end in a different order, so the reader matches names to lines by hand. Order the series the way the geoms read, or label the lines directly.",
|
|
418
|
+
});
|
|
419
|
+
}
|
|
420
|
+
}
|
|
421
|
+
/*
|
|
422
|
+
Geom area a filter can push past the frame budget, summed over every sized trace. A row with
|
|
423
|
+
no size reading counts at the layer's own radius.
|
|
424
|
+
*/
|
|
425
|
+
let fill = 0;
|
|
426
|
+
let largest = 0;
|
|
427
|
+
for (const trace of frame.traces) {
|
|
428
|
+
if (trace.data.at !== "records" || trace.data.geom !== "point" || !trace.sizeRadii)
|
|
429
|
+
continue;
|
|
430
|
+
const own = spec.layers.find((one) => one.id === trace.layerId)?.style?.size ?? frame.metrics.pointRadius;
|
|
431
|
+
for (const point of trace.data.points) {
|
|
432
|
+
const radius = point.size ?? own;
|
|
433
|
+
fill += Math.PI * radius ** 2;
|
|
434
|
+
if (radius > largest)
|
|
435
|
+
largest = radius;
|
|
436
|
+
}
|
|
437
|
+
}
|
|
438
|
+
if (fill > FILL_BUDGET_PX) {
|
|
439
|
+
const fits = largest * Math.sqrt(FILL_BUDGET_PX / fill) * 2;
|
|
440
|
+
found.push({
|
|
441
|
+
severity: "ugly",
|
|
442
|
+
code: "size-past-fill-budget",
|
|
443
|
+
path: "layers",
|
|
444
|
+
message: `The size channel resolves to ${Math.round(fill / 1000).toLocaleString()}k pixels of geom area, past the ${Math.round(FILL_BUDGET_PX / 1000).toLocaleString()}k a frame can shade inside its budget. Cap the largest geom at ${fits.toFixed(1)} pixels across.`,
|
|
445
|
+
});
|
|
446
|
+
}
|
|
447
|
+
/*
|
|
448
|
+
A measure with no unit draws a bare axis. A stated axis title answers it.
|
|
449
|
+
*/
|
|
450
|
+
for (const [axis, field] of [
|
|
451
|
+
["x", frame.xField],
|
|
452
|
+
["y", frame.traces.find((trace) => trace.field)?.field],
|
|
453
|
+
]) {
|
|
454
|
+
/* A band axis draws categories. The measure beside it is read off the color. */
|
|
455
|
+
if (isBand(frame[axis]))
|
|
456
|
+
continue;
|
|
457
|
+
if (field?.numeric && !field.unit && !spec.scales?.[axis]?.title?.trim()) {
|
|
458
|
+
found.push({
|
|
459
|
+
severity: "bad",
|
|
460
|
+
code: "bare-numeric-axis",
|
|
461
|
+
path: `scales.${axis}.title`,
|
|
462
|
+
message: `${field.label} is a measure with no unit, so its axis reads as a bare number and the reader guesses the scale. Give the field a unit, or set the axis title.`,
|
|
463
|
+
});
|
|
464
|
+
}
|
|
465
|
+
}
|
|
466
|
+
return found;
|
|
467
|
+
}
|
|
468
|
+
/**
|
|
469
|
+
* Findings about a figure that states its own size.
|
|
470
|
+
*
|
|
471
|
+
* Both need the spec and the frame. The spec has the author's ask and the frame has the labels.
|
|
472
|
+
*/
|
|
473
|
+
function fromSizing(spec, frame, container) {
|
|
474
|
+
const found = [];
|
|
475
|
+
if (spec.sizing !== "content")
|
|
476
|
+
return found;
|
|
477
|
+
/*
|
|
478
|
+
A band with less room than its label.
|
|
479
|
+
|
|
480
|
+
`bandReach` measures the label along its own axis. A column label reads across its band.
|
|
481
|
+
*/
|
|
482
|
+
for (const side of ["x", "y"]) {
|
|
483
|
+
const stated = spec.scales?.[side]?.bandSize;
|
|
484
|
+
const axis = frame[side];
|
|
485
|
+
if (!stated || !("values" in axis) || axis.values.length === 0)
|
|
486
|
+
continue;
|
|
487
|
+
/* The drawn text. A renamed category is measured as the reader sees it. */
|
|
488
|
+
const longest = axis.values.reduce((most, value) => Math.max(most, (axis.names?.[value] ?? value).length), 0);
|
|
489
|
+
const chrome = chromeOf(axis, "start");
|
|
490
|
+
const needs = bandReach({ ...chrome, fontSize: axis.fontSize ?? frame.metrics.axis }, longest, side);
|
|
491
|
+
if (stated >= needs)
|
|
492
|
+
continue;
|
|
493
|
+
found.push({
|
|
494
|
+
severity: "ugly",
|
|
495
|
+
code: "band-below-legible",
|
|
496
|
+
path: `scales.${side}.bandSize`,
|
|
497
|
+
message: `${stated} pixels a category is under the ${Math.ceil(needs)} the longest label takes at this type size. Turn the labels with \`tickAngle\`, shorten them through \`labels\`, set a smaller \`fontSize\`, or widen \`bandSize\`.`,
|
|
498
|
+
});
|
|
499
|
+
}
|
|
500
|
+
/* Content sizing can outgrow the column it sits in, and the reader finds out through a scrollbar. */
|
|
501
|
+
const bands = spec.scales?.x?.bandSize;
|
|
502
|
+
const across = "values" in frame.x ? frame.x.values.length : 0;
|
|
503
|
+
/* Breaks are room the figure asked for. The width it wants counts them. */
|
|
504
|
+
const breaks = "values" in frame.x ? (frame.x.gaps?.length ?? 0) * (frame.x.gapSize ?? 0) : 0;
|
|
505
|
+
if (container && bands && across > 0) {
|
|
506
|
+
const wants = bands * across + breaks + frame.margin.left + frame.margin.right;
|
|
507
|
+
if (wants > container) {
|
|
508
|
+
found.push({
|
|
509
|
+
severity: "ugly",
|
|
510
|
+
code: "figure-past-container",
|
|
511
|
+
path: "sizing",
|
|
512
|
+
message: `The figure asks for ${Math.ceil(wants)} pixels and the room it sits in is ${Math.floor(container)}, so the reader gets a scrollbar. Narrow the band, size it to the container, or give it the room.`,
|
|
513
|
+
});
|
|
514
|
+
}
|
|
515
|
+
}
|
|
516
|
+
return found;
|
|
517
|
+
}
|
|
518
|
+
/**
|
|
519
|
+
* Cells asked to write numbers in less room than a number takes.
|
|
520
|
+
*
|
|
521
|
+
* Both directions are checked. A stated band size is read first, the plot area otherwise.
|
|
522
|
+
*/
|
|
523
|
+
function fromNumbers(spec, frame, area) {
|
|
524
|
+
const found = [];
|
|
525
|
+
/* Breaks are room the bands lose. The drawn band is what is left over the categories. */
|
|
526
|
+
const drawn = (side) => {
|
|
527
|
+
const stated = spec.scales?.[side]?.bandSize;
|
|
528
|
+
if (stated)
|
|
529
|
+
return stated;
|
|
530
|
+
const axis = frame[side];
|
|
531
|
+
const span = side === "x" ? area?.width : area?.height;
|
|
532
|
+
if (span == null || !("values" in axis) || axis.values.length === 0)
|
|
533
|
+
return undefined;
|
|
534
|
+
const breaks = (axis.gaps?.length ?? 0) * (axis.gapSize ?? 0);
|
|
535
|
+
return Math.max(0, span - breaks) / axis.values.length;
|
|
536
|
+
};
|
|
537
|
+
for (let at = 0; at < spec.layers.length; at += 1) {
|
|
538
|
+
const series = spec.layers[at];
|
|
539
|
+
const numbers = series.numbers;
|
|
540
|
+
if (!numbers || series.visible === false)
|
|
541
|
+
continue;
|
|
542
|
+
const trace = frame.traces.find((held) => held.layerId === series.id && held.colorDomain);
|
|
543
|
+
if (!trace?.colorDomain)
|
|
544
|
+
continue;
|
|
545
|
+
const scale = layerScale(spec, series);
|
|
546
|
+
const say = tickFormatter(numbers.format ?? scale?.format, numbers.decimals ?? scale?.decimals);
|
|
547
|
+
const type = numbers.fontSize ?? frame.metrics.axis;
|
|
548
|
+
const [, longest] = boundsOf(trace.colorDomain.map((end) => say(end).length));
|
|
549
|
+
const needs = { x: textRun(longest, type), y: type };
|
|
550
|
+
for (const side of ["x", "y"]) {
|
|
551
|
+
const band = drawn(side);
|
|
552
|
+
if (band == null || band >= needs[side])
|
|
553
|
+
continue;
|
|
554
|
+
found.push({
|
|
555
|
+
severity: "ugly",
|
|
556
|
+
code: "numbers-below-legible",
|
|
557
|
+
path: layerPath(at, "numbers"),
|
|
558
|
+
message: `A cell ${Math.round(band)} pixels ${side === "x" ? "wide" : "tall"} cannot hold ${Math.ceil(needs[side])} pixels of number at ${type}px. Drop the numbers, shorten them with \`decimals\`, set a smaller \`fontSize\`, or widen the band.`,
|
|
559
|
+
});
|
|
560
|
+
}
|
|
561
|
+
}
|
|
562
|
+
return found;
|
|
563
|
+
}
|
|
564
|
+
/**
|
|
565
|
+
* Findings about a side panel, the one thing on the spec making a claim of its own.
|
|
566
|
+
*
|
|
567
|
+
* A dendrogram states that a clustering was run. Both findings here are `wrong`.
|
|
568
|
+
*/
|
|
569
|
+
function fromSides(spec, frame) {
|
|
570
|
+
const sides = spec.sides ?? [];
|
|
571
|
+
if (sides.length === 0)
|
|
572
|
+
return [];
|
|
573
|
+
/*
|
|
574
|
+
A side under a facet, which the shared band makes unanswerable.
|
|
575
|
+
|
|
576
|
+
A cluster order belongs to the cells that produced it. Panel zero's order leads.
|
|
577
|
+
*/
|
|
578
|
+
if (spec.facet?.by) {
|
|
579
|
+
return [
|
|
580
|
+
{
|
|
581
|
+
severity: "wrong",
|
|
582
|
+
code: "sides-under-facet",
|
|
583
|
+
path: "sides",
|
|
584
|
+
message: "A facet shares one band across its panels and a cluster order belongs to the panel that computed it, so a side beside each panel would draw against rows that panel does not hold. Drop the facet, or drop the sides.",
|
|
585
|
+
},
|
|
586
|
+
];
|
|
587
|
+
}
|
|
588
|
+
const found = [];
|
|
589
|
+
sides.forEach((side, at) => {
|
|
590
|
+
const axis = side.position === "top" || side.position === "bottom" ? "x" : "y";
|
|
591
|
+
if (side.content.kind === "dendrogram") {
|
|
592
|
+
if (frame.clusterTrees[axis])
|
|
593
|
+
return;
|
|
594
|
+
const capped = frame.clusterRefused.some((refused) => refused.axis === axis);
|
|
595
|
+
found.push({
|
|
596
|
+
severity: "wrong",
|
|
597
|
+
code: "dendrogram-without-order",
|
|
598
|
+
path: `sides[${at}]`,
|
|
599
|
+
message: `The ${side.position} side shares the ${axis} axis and there is no tree behind it, so the dendrogram would draw merges nobody computed. ${capped
|
|
600
|
+
? "The cap turned this clustering down, so aggregate with `kmeans` or narrow the categories."
|
|
601
|
+
: `Give \`scales.${axis}.order\` a cluster order, or drop the side.`}`,
|
|
602
|
+
});
|
|
603
|
+
return;
|
|
604
|
+
}
|
|
605
|
+
if (side.content.kind === "histogram" || side.content.kind === "rug") {
|
|
606
|
+
if (frame.marginals.some((held) => held.axis === axis && held.kind === side.content.kind))
|
|
607
|
+
return;
|
|
608
|
+
/*
|
|
609
|
+
A marginal with nothing to distribute.
|
|
610
|
+
|
|
611
|
+
A band axis has no measure to spread along. A reduced figure has no records left to count.
|
|
612
|
+
*/
|
|
613
|
+
found.push({
|
|
614
|
+
severity: "bad",
|
|
615
|
+
code: "marginal-without-readings",
|
|
616
|
+
path: `sides[${at}]`,
|
|
617
|
+
message: `The ${side.position} side asks for the distribution along the ${axis} axis and there are no readings behind it, so it draws an empty strip. A marginal counts the records a point geom drew, so the axis has to run a measure and the geoms have to be points.`,
|
|
618
|
+
});
|
|
619
|
+
return;
|
|
620
|
+
}
|
|
621
|
+
if (side.content.kind !== "track")
|
|
622
|
+
return;
|
|
623
|
+
const field = side.content.field;
|
|
624
|
+
const track = frame.tracks.find((held) => held.field === field && held.axis === axis);
|
|
625
|
+
/*
|
|
626
|
+
A track the build could not resolve, which draws a strip of nothing.
|
|
627
|
+
|
|
628
|
+
A missing column, an axis running values, or a column with no reading for the band.
|
|
629
|
+
*/
|
|
630
|
+
if (!track) {
|
|
631
|
+
found.push({
|
|
632
|
+
severity: "bad",
|
|
633
|
+
code: "track-without-categories",
|
|
634
|
+
path: `sides[${at}].content.field`,
|
|
635
|
+
message: `Nothing resolved for ${field} along the ${axis} axis, so the ${side.position} side draws an empty strip. A track reads one value per category, so the axis has to run categories and the column has to carry a reading for them.`,
|
|
636
|
+
});
|
|
637
|
+
return;
|
|
638
|
+
}
|
|
639
|
+
/*
|
|
640
|
+
A category with two values in its group. A join error.
|
|
641
|
+
|
|
642
|
+
The modal value draws, and this finding stops it standing for the whole group.
|
|
643
|
+
*/
|
|
644
|
+
if (track.kind === "identity" && track.disagreed.length > 0) {
|
|
645
|
+
const named = track.disagreed.slice(0, 3).join(", ");
|
|
646
|
+
found.push({
|
|
647
|
+
severity: "bad",
|
|
648
|
+
code: "track-group-disagrees",
|
|
649
|
+
path: `sides[${at}].content.field`,
|
|
650
|
+
message: `${track.disagreed.length} ${track.disagreed.length === 1 ? "category carries" : "categories carry"} more than one ${field} (${named}${track.disagreed.length > 3 ? ", and more" : ""}), so the commonest drew. One value per category is what a track states, so split the band on ${field} as well, or annotate with a column that has one.`,
|
|
651
|
+
});
|
|
652
|
+
}
|
|
653
|
+
});
|
|
654
|
+
/*
|
|
655
|
+
More magnitude scales than a reader can hold apart.
|
|
656
|
+
|
|
657
|
+
Two is the ceiling. The cells take one and a numeric track takes the other.
|
|
658
|
+
*/
|
|
659
|
+
const magnitudes = (frame.traces.some((trace) => trace.colorDomain) ? 1 : 0) +
|
|
660
|
+
frame.tracks.filter((track) => track.kind === "magnitude").length;
|
|
661
|
+
if (magnitudes > 2) {
|
|
662
|
+
found.push({
|
|
663
|
+
severity: "ugly",
|
|
664
|
+
code: "magnitudes-past-legible",
|
|
665
|
+
path: "sides",
|
|
666
|
+
message: `${magnitudes} maps carrying magnitude on one figure is more ramps than a reader can hold apart. Annotate with a category instead, or move a measure to a second figure.`,
|
|
667
|
+
});
|
|
668
|
+
}
|
|
669
|
+
return found;
|
|
670
|
+
}
|
|
671
|
+
/**
|
|
672
|
+
* Rings outside a circular figure that read nothing.
|
|
673
|
+
*
|
|
674
|
+
* A ring reads a numeric column of the figure's own table against the sectors it drew.
|
|
675
|
+
*/
|
|
676
|
+
function fromRings(spec, frame) {
|
|
677
|
+
const found = [];
|
|
678
|
+
spec.layers.forEach((series, index) => {
|
|
679
|
+
const data = frame.traces.find((held) => held.layerId === series.id)?.data;
|
|
680
|
+
if (!data || data.at !== "groups" || (data.geom !== "chord" && data.geom !== "ideogram"))
|
|
681
|
+
return;
|
|
682
|
+
(series.rings ?? []).forEach((ring, at) => {
|
|
683
|
+
if (data.rings[at])
|
|
684
|
+
return;
|
|
685
|
+
const { field } = ring.content;
|
|
686
|
+
found.push({
|
|
687
|
+
severity: "bad",
|
|
688
|
+
code: "ring-without-readings",
|
|
689
|
+
path: layerPath(index, `rings[${at}]`),
|
|
690
|
+
message: `Ring ${at + 1} draws an empty band. Nothing in ${field} reads as a number against the sectors drawn. A ring reads a numeric column of the same table.`,
|
|
691
|
+
});
|
|
692
|
+
});
|
|
693
|
+
});
|
|
694
|
+
return found;
|
|
695
|
+
}
|
|
696
|
+
/**
|
|
697
|
+
* What is wrong with a chart, worst first.
|
|
698
|
+
*
|
|
699
|
+
* Hand the frame where there is one. Without it the spec-only checks run.
|
|
700
|
+
*/
|
|
701
|
+
export function critique(spec, { frame, substituted, exported = false, container, area } = {}) {
|
|
702
|
+
const order = { wrong: 0, bad: 1, ugly: 2 };
|
|
703
|
+
const found = [
|
|
704
|
+
...fromSpec(spec),
|
|
705
|
+
...(frame ? fromFrame(spec, frame, substituted) : []),
|
|
706
|
+
...(frame ? fromSizing(spec, frame, container) : []),
|
|
707
|
+
...(frame ? fromNumbers(spec, frame, area) : []),
|
|
708
|
+
...(frame ? fromPolarFrame(spec, frame, area) : []),
|
|
709
|
+
...(frame ? fromShapeChannel(frame) : []),
|
|
710
|
+
...(frame ? fromAxisTitles(frame, area) : []),
|
|
711
|
+
...(frame ? fromSides(spec, frame) : []),
|
|
712
|
+
...(frame ? fromRings(spec, frame) : []),
|
|
713
|
+
...(exported ? fromCopy(spec) : []),
|
|
714
|
+
];
|
|
715
|
+
return found.sort((a, b) => order[a.severity] - order[b.severity]);
|
|
716
|
+
}
|
|
717
|
+
/**
|
|
718
|
+
* The critic as a consumer calls it: the spec's own checks, and the copy checks for an export.
|
|
719
|
+
*
|
|
720
|
+
* `Viz` runs the checks that read the drawn frame and hands their findings to `onCritique`.
|
|
721
|
+
*/
|
|
722
|
+
export const critiqueSpec = critique;
|
|
723
|
+
/**
|
|
724
|
+
* Reports a vertical axis title drawn over the top tick label.
|
|
725
|
+
*
|
|
726
|
+
* The title draws `Y_TITLE_ABOVE` pixels over the plot area. A domain ending on a labeled tick
|
|
727
|
+
* collides. A frame drawing no axes has no title to collide.
|
|
728
|
+
*/
|
|
729
|
+
function fromAxisTitles(frame, area) {
|
|
730
|
+
if (!area || frame.axisFree)
|
|
731
|
+
return [];
|
|
732
|
+
const range = isBand(frame.y) ? [0, area.height] : [area.height, 0];
|
|
733
|
+
const axis = buildAxis(frame.y, range, frame.metrics.ticks, "end");
|
|
734
|
+
if (!axis.label || axis.chrome.titleAlign !== "end" || !axis.labelTicks)
|
|
735
|
+
return [];
|
|
736
|
+
const size = axis.chrome.fontSize;
|
|
737
|
+
/* A baseline sits a quarter of the type below the ink's bottom and three quarters under its top. */
|
|
738
|
+
const titleFloor = -Y_TITLE_ABOVE + size * (1 - ASCENT);
|
|
739
|
+
const [highest] = boundsOf(axis.ticks.map((tick) => axis.scale(tick)));
|
|
740
|
+
const labelCeiling = highest - size * ASCENT;
|
|
741
|
+
if (!Number.isFinite(labelCeiling) || labelCeiling >= titleFloor)
|
|
742
|
+
return [];
|
|
743
|
+
return [
|
|
744
|
+
{
|
|
745
|
+
severity: "ugly",
|
|
746
|
+
code: "title-over-numbers",
|
|
747
|
+
path: "scales.y.limits",
|
|
748
|
+
message: `The ${axis.label} title sits ${Y_TITLE_ABOVE} pixels over the plot area and the top tick label reaches into it, so the two overlap. Widen the y limits past the last tick, or run the title along the rule with titleAlign.`,
|
|
749
|
+
},
|
|
750
|
+
];
|
|
751
|
+
}
|
|
752
|
+
/**
|
|
753
|
+
* Reports a shape channel with more levels than there are glyphs.
|
|
754
|
+
*
|
|
755
|
+
* Four glyphs is the set and a fifth level takes the circle back. The count comes off the frame.
|
|
756
|
+
*/
|
|
757
|
+
function fromShapeChannel(frame) {
|
|
758
|
+
const crowded = frame.traces.find((trace) => (trace.shapeChannel?.levels?.length ?? 0) > POINT_SHAPES.length);
|
|
759
|
+
const channel = crowded?.shapeChannel;
|
|
760
|
+
if (!channel?.levels)
|
|
761
|
+
return [];
|
|
762
|
+
return [
|
|
763
|
+
{
|
|
764
|
+
severity: "ugly",
|
|
765
|
+
code: "shapes-past-glyphs",
|
|
766
|
+
path: "layers",
|
|
767
|
+
message: `${channel.field.label} holds ${channel.levels.length} values and the package draws ${POINT_SHAPES.length} glyphs, so two groups take the same mark. Facet on it, or bind the shape to a column with fewer values.`,
|
|
768
|
+
},
|
|
769
|
+
];
|
|
770
|
+
}
|
|
771
|
+
/**
|
|
772
|
+
* Findings about a copy of the figure.
|
|
773
|
+
*
|
|
774
|
+
* A file is one view of a cube, the static projection the design language refuses.
|
|
775
|
+
*/
|
|
776
|
+
function fromCopy(spec) {
|
|
777
|
+
const cube = spec.layers.findIndex((series) => series.visible !== false && GRAMMAR[series.kind]?.coord === "cube");
|
|
778
|
+
if (cube < 0)
|
|
779
|
+
return [];
|
|
780
|
+
return [
|
|
781
|
+
{
|
|
782
|
+
severity: "bad",
|
|
783
|
+
code: "cube-copied-static",
|
|
784
|
+
path: layerPath(cube, "kind"),
|
|
785
|
+
message: "A file cannot be turned, so the reader of this copy is guessing at depth, which is the case the third position scale is permitted around. The copy states it on itself; send the chart where the reader can still turn it.",
|
|
786
|
+
},
|
|
787
|
+
];
|
|
788
|
+
}
|
|
789
|
+
/**
|
|
790
|
+
* Findings that stop a save.
|
|
791
|
+
*
|
|
792
|
+
* The `wrong` ones absent from `ChartSpec.overrides`, plus the missing claim.
|
|
793
|
+
*
|
|
794
|
+
* An empty result passes the gate. An override waives the other codes.
|
|
795
|
+
*/
|
|
796
|
+
export const blocking = (spec, findings) => {
|
|
797
|
+
const held = findings.filter((finding) => finding.severity === "wrong" && !waived(spec, finding.code));
|
|
798
|
+
return spec.headline?.trim() ? held : [...held, MISSING_CLAIM];
|
|
799
|
+
};
|
|
800
|
+
/** Whether the spec names this code among its overrides. */
|
|
801
|
+
const waived = (spec, code) => spec.overrides?.includes(code) ?? false;
|
|
802
|
+
/** The refusal a spec with no headline meets at the gate. */
|
|
803
|
+
const MISSING_CLAIM = {
|
|
804
|
+
severity: "wrong",
|
|
805
|
+
code: "headline-missing",
|
|
806
|
+
path: "headline",
|
|
807
|
+
message: "A figure that is about to be read carries the claim it makes. Write the headline as a sentence a reader can take away, and put what is plotted in the subtitle under it.",
|
|
808
|
+
};
|
|
809
|
+
/**
|
|
810
|
+
* Overrides the reader is owed, as footer lines.
|
|
811
|
+
*
|
|
812
|
+
* An override is the author saying they know. The admission goes to the footer.
|
|
813
|
+
*/
|
|
814
|
+
export const overrideNotes = (spec, findings) => findings
|
|
815
|
+
.filter((finding) => finding.severity === "wrong" && waived(spec, finding.code))
|
|
816
|
+
.map((finding) => NOTE[finding.code] ?? finding.code);
|
|
817
|
+
/** What an accepted finding says in the footer, in the reader's terms. */
|
|
818
|
+
const NOTE = {
|
|
819
|
+
"grounded-nonlinear-axis": "lengths drawn on a nonlinear scale, so they are not proportional",
|
|
820
|
+
"grounded-truncated-axis": "the value axis does not start at zero, so differences read larger than they are",
|
|
821
|
+
"fixed-statistic": "an aggregate was set that this chart does not use",
|
|
822
|
+
"cumulative-unsupported": "a running total was set that this chart does not draw",
|
|
823
|
+
"empty-as-zero-of-a-statistic": "a pair with no rows is drawn as nought",
|
|
824
|
+
"pie-not-parts": "the slices are not parts of one whole, so the ring is a share of no total",
|
|
825
|
+
"sides-under-facet": "the side panels follow the first panel's rows, so the other panels can disagree with them",
|
|
826
|
+
"dendrogram-without-order": "the tree beside the axis draws merges no clustering of these rows produced",
|
|
827
|
+
"graph-links-dropped": "some links name a node the table lacks, so the network drawn is smaller than its source",
|
|
828
|
+
};
|
|
829
|
+
/**
|
|
830
|
+
* The geometry and the statistic a spec resolves to, as one string per visible series.
|
|
831
|
+
*
|
|
832
|
+
* Two members with the same string draw the same kind of picture.
|
|
833
|
+
*/
|
|
834
|
+
const formOf = (spec) => spec.layers
|
|
835
|
+
.filter((series) => series.visible !== false)
|
|
836
|
+
.map((series) => {
|
|
837
|
+
const row = GRAMMAR[series.kind];
|
|
838
|
+
return row ? `${row.geom}:${row.stat.join("+")}` : series.kind;
|
|
839
|
+
})
|
|
840
|
+
.join("|");
|
|
841
|
+
/** Whether a spec draws a statistic in place of the records. */
|
|
842
|
+
const derived = (spec) => {
|
|
843
|
+
const visible = spec.layers.filter((series) => series.visible !== false);
|
|
844
|
+
return visible.length > 0 && visible.every((series) => (GRAMMAR[series.kind]?.stat.length ?? 0) > 0);
|
|
845
|
+
};
|
|
846
|
+
/**
|
|
847
|
+
* What is wrong with a set of figures taken together.
|
|
848
|
+
*
|
|
849
|
+
* Both findings are `ugly`. `plots` holds the members by id and a missing member is skipped.
|
|
850
|
+
*/
|
|
851
|
+
export function critiqueSet(set, plots) {
|
|
852
|
+
const found = [];
|
|
853
|
+
const members = set.members ?? [];
|
|
854
|
+
const opener = plots[members[0]?.plotId ?? ""];
|
|
855
|
+
if (opener && derived(opener)) {
|
|
856
|
+
found.push({
|
|
857
|
+
severity: "ugly",
|
|
858
|
+
code: "set-opens-derived",
|
|
859
|
+
path: "members[0]",
|
|
860
|
+
message: "The set opens on a statistic, so a reader meets the summary before the readings it came from. Open on the records and let the derived figures follow.",
|
|
861
|
+
});
|
|
862
|
+
}
|
|
863
|
+
members.forEach((member, index) => {
|
|
864
|
+
if (index === 0)
|
|
865
|
+
return;
|
|
866
|
+
const held = plots[member.plotId];
|
|
867
|
+
const before = plots[members[index - 1].plotId];
|
|
868
|
+
if (!held || !before)
|
|
869
|
+
return;
|
|
870
|
+
if (formOf(held) !== formOf(before))
|
|
871
|
+
return;
|
|
872
|
+
found.push({
|
|
873
|
+
severity: "ugly",
|
|
874
|
+
code: "set-repeats-form",
|
|
875
|
+
path: `members[${index}]`,
|
|
876
|
+
message: "This figure draws the same geometry off the same statistic as the one before it, so the pair reads as one point made twice. Change what the second one shows, or merge them.",
|
|
877
|
+
});
|
|
878
|
+
});
|
|
879
|
+
return found;
|
|
880
|
+
}
|
|
881
|
+
/**
|
|
882
|
+
* What a graph has to admit, and what it has to claim.
|
|
883
|
+
*
|
|
884
|
+
* A published figure states its claim. A view holding less than its source reports the count.
|
|
885
|
+
*/
|
|
886
|
+
export function critiqueGraph(graph) {
|
|
887
|
+
const found = [];
|
|
888
|
+
if (!graph.headline || graph.headline.trim().length === 0) {
|
|
889
|
+
found.push({
|
|
890
|
+
severity: "wrong",
|
|
891
|
+
code: "headline-missing",
|
|
892
|
+
path: "headline",
|
|
893
|
+
message: "A graph published with no claim leaves the reader to find one in a cloud of nodes. Write what the layout shows: which groups separated, what holds them together.",
|
|
894
|
+
});
|
|
895
|
+
}
|
|
896
|
+
if (graph.linksDropped > 0) {
|
|
897
|
+
found.push({
|
|
898
|
+
severity: "wrong",
|
|
899
|
+
code: "graph-links-dropped",
|
|
900
|
+
path: "links",
|
|
901
|
+
message: `${graph.linksDropped.toLocaleString()} of ${(graph.links + graph.linksDropped).toLocaleString()} links name a node the table does not carry, so the drawn network is smaller than its source. State it in the footer, or supply the nodes the links reach for.`,
|
|
902
|
+
});
|
|
903
|
+
}
|
|
904
|
+
return found;
|
|
905
|
+
}
|