(null);
- const [fit, setFit] = useState<{ size: number; k: number; shown: number }>({ size: max, k: Infinity, shown: Infinity });
- const key = rows.map((r) => (r.hot ? '+' : r.gap ? '~' : ' ') + r.text).join('\n');
+ const [fit, setFit] = useState<{ size: number; k: number; shown: number; cols: number }>({ size: max, k: Infinity, shown: Infinity, cols: 60 });
+ const layout = (cols: number) => (typeof source === 'function' ? source(Math.max(MIN_COLS, cols - 1)) : source);
+ const key = layout(60).map((r) => (r.hot ? '+' : r.gap ? '~' : ' ') + r.text).join('\n');
useEffect(() => {
const el = ref.current;
if (!el) return;
@@ -266,24 +290,29 @@ export function Code({ rows, max = 22, min = 14, tone = 'flint' }: {
if (w <= 0 || h <= 0) return;
// Screen lines a list of rows takes at a size, wrapping under each row's indent.
const lines = (list: CodeRow[], size: number) => {
- const cols = Math.max(8, Math.floor(w / (CHAR_W * size)));
+ const cols = colsAt(w, size);
return list.map(({ text }) => {
const hang = Math.min(cols - 4, indentOf(text) + 2);
return text.length <= cols ? 1 : 1 + Math.ceil((text.length - cols) / (cols - hang));
});
};
const fits = (list: CodeRow[], size: number) => sum(lines(list, size)) <= Math.floor(h / (LINE_H * size));
- let next = { size: min, k: Infinity, shown: Infinity };
+ const narrow = colsAt(w, min);
+ const rows = layout(narrow);
+ let next = { size: min, k: Infinity, shown: Infinity, cols: narrow };
let found = false;
- for (let size = max; size >= min && !found; size -= 0.5) if (fits(rows, size)) { next = { size, k: Infinity, shown: Infinity }; found = true; }
- for (let k = rows.length; k >= 1 && !found; k--) if (fits(sampled(rows, k), min)) { next = { size: min, k, shown: Infinity }; found = true; }
+ for (let size = max; size >= min && !found; size -= 0.5) {
+ const cols = colsAt(w, size);
+ if (fits(layout(cols), size)) { next = { size, k: Infinity, shown: Infinity, cols }; found = true; }
+ }
+ for (let k = rows.length; k >= 1 && !found; k--) if (fits(sampled(rows, k), min)) { next = { size: min, k, shown: Infinity, cols: narrow }; found = true; }
if (!found) {
const list = sampled(rows, 1), per = lines(list, min), budget = Math.floor(h / (LINE_H * min));
let used = 1, shown = 0;
while (shown < list.length && used + per[shown] <= budget) used += per[shown++];
- next = { size: min, k: 1, shown };
+ next = { size: min, k: 1, shown, cols: narrow };
}
- setFit((f) => (f.size === next.size && f.k === next.k && f.shown === next.shown ? f : next));
+ setFit((f) => (f.size === next.size && f.k === next.k && f.shown === next.shown && f.cols === next.cols ? f : next));
};
measure();
const ro = new ResizeObserver(measure);
@@ -291,7 +320,7 @@ export function Code({ rows, max = 22, min = 14, tone = 'flint' }: {
return () => ro.disconnect();
// eslint-disable-next-line react-hooks/exhaustive-deps
}, [key, max, min]);
- const list = sampled(rows, fit.k);
+ const list = sampled(layout(fit.cols), fit.k);
const shown = fit.shown < list.length ? [...list.slice(0, fit.shown), hiddenRow(list.slice(fit.shown))] : list;
return (
diff --git a/site/src/talk/slides.tsx b/site/src/talk/slides.tsx
index 9f5e611e..bcfc8dce 100644
--- a/site/src/talk/slides.tsx
+++ b/site/src/talk/slides.tsx
@@ -16,8 +16,8 @@ import {
penguins, picked, withData, type FlintState,
} from './data';
import {
- Chart, Code, Demo, FitTable, Stable, MINIMAPS, Minimap, Sketch, VegaLite, fullRows, lineDiff, plainRows, specText, stepRows, textRows,
- type CodeRow,
+ Chart, Code, Demo, FitTable, Stable, MINIMAPS, Minimap, Sketch, VegaLite, fullLayout, lineDiff, plainLayout, specLayout, stepLayout, textRows,
+ type CodeRow, type CodeSource,
} from './parts';
import glue from './specs/vl-final-glue.txt?raw';
@@ -39,7 +39,7 @@ const Cell = ({ caption, children, framed }: { caption?: string; children: React
);
/** One step of the traditional way: the code and chart before, then the code and chart after. */
-const Step = ({ before, beforeSpec, after, afterSpec, note }: { before: CodeRow[]; beforeSpec: unknown; after: CodeRow[]; afterSpec: unknown; note?: ReactNode }) => (<>
+const Step = ({ before, beforeSpec, after, afterSpec, note }: { before: CodeSource; beforeSpec: unknown; after: CodeSource; afterSpec: unknown; note?: ReactNode }) => (<>
→
@@ -50,11 +50,11 @@ const Step = ({ before, beforeSpec, after, afterSpec, note }: { before: CodeRow[
// Each step's code: the new spec with what changed in red. The next slide starts from it, in black.
const ROWS = {
- raw: stepRows(STEPS.raw),
- parse: stepRows(STEPS.parse, STEPS.raw),
- layout: stepRows(STEPS.layout, STEPS.parse),
- theme: stepRows(STEPS.theme, STEPS.layout, 72),
- final: stepRows(STEPS.final, STEPS.theme, 72),
+ raw: stepLayout(STEPS.raw),
+ parse: stepLayout(STEPS.parse, STEPS.raw),
+ layout: stepLayout(STEPS.layout, STEPS.parse),
+ theme: stepLayout(STEPS.theme, STEPS.layout),
+ final: stepLayout(STEPS.final, STEPS.theme),
};
const DIFF = {
parse: lineDiff(STEPS.raw, STEPS.parse),
@@ -116,21 +116,28 @@ const BESPOKE_ROWS: CodeRow[] = [
{ text: ' eventSource: lassoTrigger("contain"),', hot: true },
{ text: ' affordances: { plot: { cursor: "draw" } },' },
{ text: ' handle(event) {', hot: true },
- { text: ' // the stroke arrives as points in the data, not pixels', gap: true },
- { text: ' const guess = extendPath(drawn, event.geometry.domain.points);' },
+ { text: ' // the stroke arrives in data units, not pixels', gap: true },
+ { text: ' const points = event.geometry.domain.points;' },
+ { text: ' const guess = extendPath(drawn, points);' },
{ text: ' if (isComplete(guess)) reveal();' },
- { text: ' // answer with an update: draw the guess as an overlay', gap: true },
- { text: ' return { ops: [{ op: "set-overlay", name: "guess", value: guess }] };', hot: true },
+ { text: ' // answer with an update: an overlay', gap: true },
+ { text: ' return { ops: [', hot: true },
+ { text: ' { op: "set-overlay",', hot: true },
+ { text: ' name: "guess", value: guess },', hot: true },
+ { text: ' ] };', hot: true },
{ text: ' },' },
{ text: '};' },
{ text: '' },
- { text: '
' },
+ { text: '
' },
];
/** The World Cup demo's own wiring, condensed: the chart says what was clicked, the page does the rest. */
const APP_ROWS: CodeRow[] = [
{ text: 'const spec = {' },
- { text: ' data, semantic_types: { Team: "Category", Goals: "Count" },' },
+ { text: ' data,' },
+ { text: ' semantic_types: {' },
+ { text: ' Team: "Category", Goals: "Count" },' },
{ text: ' chart_spec: { chartType: "Bar Chart",' },
{ text: ' encodings: { x: "Goals", y: "Team" } },' },
{ text: ' interaction_spec: { interactions: [' },
@@ -140,7 +147,7 @@ const APP_ROWS: CodeRow[] = [
{ text: '' },
{ text: '
setTeam(pinnedTeam(change))}', hot: true },
+ { text: ' onChange={(c) => setTeam(pinnedTeam(c))}', hot: true },
{ text: '/>', hot: true },
{ text: '' },
];
@@ -158,7 +165,7 @@ const SEMANTIC_TYPES: [string, string][] = [
/** The parts of the real "economist" preset that the theme slides talk about. */
const ECON = (THEME_PRESETS as Record }>).economist.spec;
-const ECONOMIST_ROWS: CodeRow[] = specText({
+const ECONOMIST_ROWS = specLayout({
id: ECON.id,
ink: {
series: {
@@ -172,12 +179,13 @@ const ECONOMIST_ROWS: CodeRow[] = specText({
axis: { measure: { line: ECON.structure.axis.measure.line, placement: ECON.structure.axis.measure.placement } },
grid: { category: ECON.structure.grid.category, zero: ECON.structure.grid.zero },
},
-}, 84).split('\n').map((text) => ({ text }));
+});
const INTERACTION_ROWS: CodeRow[] = [
{ text: '"interaction_spec": {' },
{ text: ' "interactions": [' },
- { text: ' { "type": "click-group-focus", "options": { "groupBy": "game" } }', hot: true },
+ { text: ' { "type": "click-group-focus",', hot: true },
+ { text: ' "options": { "groupBy": "game" } }', hot: true },
{ text: ' ]' },
{ text: '}' },
{ text: '' },
@@ -187,8 +195,10 @@ const INTERACTION_ROWS: CodeRow[] = [
{ text: '' },
{ text: '// the update it can send back', gap: true },
{ text: '{ "ops": [' },
- { text: ' { "op": "set-style", "targets": [target], "value": { "state": "emphasized" } },', hot: true },
- { text: ' { "op": "set-annotation", "target": target, "value": { "text": "Launch month" } }', hot: true },
+ { text: ' { "op": "set-style", "targets": [target],', hot: true },
+ { text: ' "value": { "state": "emphasized" } },', hot: true },
+ { text: ' { "op": "set-annotation", "target": target,', hot: true },
+ { text: ' "value": { "text": "Launch month" } }', hot: true },
{ text: '] }' },
];
const TYPES_COMPILED = typesCompiled();
@@ -209,7 +219,7 @@ const UPDATE_SLIDES: SlideDef[] = UPDATES.reduce<{ state: FlintState; slides: Sl
@@ -268,7 +278,7 @@ export const SLIDES: SlideDef[] = [
id: 'ideal', render: () => (<>
Ideally, a chart is a few lines that are easy to change
>),
@@ -277,7 +287,7 @@ export const SLIDES: SlideDef[] = [
id: 'ideal-bar', render: () => (<>
Switching to a bar chart is one small edit
>),
@@ -313,7 +323,7 @@ export const SLIDES: SlideDef[] = [
id: 'attempt-2', render: () => (<>
Attempt two: five years of months need a new layout
Sixty months crowd the axis with Jan to Dec five times over. We widen the chart, label each year once, give long game names room, and move the legend to the top. That adds {DIFF.layout.plus} lines and changes {DIFF.layout.minus}.>}
/>
@@ -323,7 +333,7 @@ export const SLIDES: SlideDef[] = [
id: 'theme-vl', render: () => (<>
Making it look like The Economist is mostly new work
Fonts and axes go into the config, the house colours into the encoding, and the red tab, headline, and source line become extra views placed by hand. The spec grows by {DIFF.theme.plus} lines.>}
/>
@@ -333,7 +343,7 @@ export const SLIDES: SlideDef[] = [
id: 'interaction-vl', render: () => (<>
Then clicking a game should highlight its row
Selection parameters and conditional styles are woven into the opacity, the stroke, and the axis labels, adding {DIFF.final.plus} lines across the spec.>}
/>
@@ -430,7 +440,7 @@ export const SLIDES: SlideDef[] = [
id: 'flint-version', render: () => (<>
The same chart, written in Flint
-
+
A few lines on the raw data, with the same look. Click a game to try it.
@@ -453,7 +463,7 @@ export const SLIDES: SlideDef[] = [
id: 'a-what', render: () => (<>
Say what each field means, and the compiler does the rest
-
({ text, hot: /YearMonth|Category|Profit|semantic_types/.test(text) }))} />
+
>),
@@ -464,7 +474,7 @@ export const SLIDES: SlideDef[] = [
And this is what the compiler wrote in Vega-Lite
-
({ text, hot: /YearMonth|Category|Profit|semantic_types/.test(text) }))} />
+
The Flint spec, on the raw rows.
→
@@ -667,7 +677,7 @@ export const SLIDES: SlideDef[] = [
"Create a theme inspired by a Disneyland day out: candy colors, rounded shapes, and a playful font."
-
({ text }))} max={18} />
+
| )} /> |
diff --git a/site/src/talk/specs/vl-final-glue.txt b/site/src/talk/specs/vl-final-glue.txt
index 1042fe3f..8675fdce 100644
--- a/site/src/talk/specs/vl-final-glue.txt
+++ b/site/src/talk/specs/vl-final-glue.txt
@@ -13,10 +13,11 @@ export async function mount(el, spec, rows) {
showRows(rows.filter((r) => games.includes(r.game)));
});
- // picker -> chart: write a point tuple into the selection's internal store
+ // picker -> chart: write a tuple into the selection's store
gamePicker(rows, async (game) => {
const fields = [{ type: 'E', field: 'game' }];
- const tuples = game ? [{ unit: UNIT, fields, values: [game] }] : [];
+ const tuple = { unit: UNIT, fields, values: [game] };
+ const tuples = game ? [tuple] : [];
const change = changeset().remove(() => true).insert(tuples);
await view.change('pick_store', change).runAsync();
});