Skip to content

An open source alternative to Tableau. Embeddable visual analytic

style(playground): restyle TerseSpec page and shared example frame

Elwynn Chen317aa1ecb456
diff --git a/packages/playground/src/examples/components/examplePage.tsx b/packages/playground/src/examples/components/examplePage.tsx
index d690de66..0c138f0a 100644
--- a/packages/playground/src/examples/components/examplePage.tsx
+++ b/packages/playground/src/examples/components/examplePage.tsx
@@ -11,28 +11,27 @@ export default function Example(props: {
}) {
return (
<div className="flex flex-col text-gray-900 dark:text-white">
- <div className="text-xl font-bold">{props.name}</div>
- {props.desc && <div className="text-xs mt-1">{props.desc}</div>}
- <React.Suspense fallback={<p>Loading component...</p>}>
+ <h1 className="text-2xl font-bold tracking-tight">{props.name}</h1>
+ {props.desc && <p className="mt-1.5 max-w-3xl text-sm text-zinc-500 dark:text-zinc-400">{props.desc}</p>}
+ <React.Suspense fallback={<p className="mt-6 text-sm text-zinc-500">Loading component...</p>}>
<div
- style={{
- width: '100%',
- height: '80vh',
- flexShrink: 0,
- border: '1px solid gray',
- marginTop: 24,
- overflow: 'auto',
- position: 'relative',
- ...props.style,
- }}
+ className="mt-6 w-full shrink-0 overflow-auto rounded-xl border border-zinc-200 bg-white shadow-sm dark:border-zinc-800 dark:bg-zinc-900"
+ style={{ height: '80vh', position: 'relative', ...props.style }}
>
{props.children}
</div>
</React.Suspense>
- <SyntaxHighlighter language="tsx" style={a11yDark}>
- {props.code}
- </SyntaxHighlighter>
+ <details open className="mt-6">
+ <summary className="cursor-pointer select-none text-sm font-semibold text-zinc-500 transition-colors hover:text-zinc-900 dark:text-zinc-400 dark:hover:text-white">
+ Source code
+ </summary>
+ <div className="mt-2 overflow-hidden rounded-xl border border-zinc-200 dark:border-zinc-800">
+ <SyntaxHighlighter language="tsx" style={a11yDark} customStyle={{ margin: 0, borderRadius: 0, fontSize: '0.8rem' }}>
+ {props.code}
+ </SyntaxHighlighter>
+ </div>
+ </details>
</div>
);
}
diff --git a/packages/playground/src/examples/pages/terseSpec.stories.tsx b/packages/playground/src/examples/pages/terseSpec.stories.tsx
index 6aed9188..ee1ed05f 100644
--- a/packages/playground/src/examples/pages/terseSpec.stories.tsx
+++ b/packages/playground/src/examples/pages/terseSpec.stories.tsx
@@ -8,19 +8,7 @@ import { useFetch, IDataSource } from '../util';
* TerseSpec is the human-authored spec grammar. You write a terse spec, call
* `normalize(spec, fields)` to expand it into a full canonical IChart, and feed
* that chart to any renderer. The terse layer is for authoring only — persistence
- * (export/import) always uses the canonical form.
- *
- * Grammar summary:
- * - Field references are BY NAME (`'Manufacturer'`), not by fid. Prefix with
- * `'fid:'` to bypass name resolution (e.g. `'fid:col_0_64'`).
- * - Aggregation shorthand: `'mean(Price_in_thousands)'`, `'sum(...)'`, `'count()'`.
- * - Object form for per-channel options: `{ field, aggregate, sort, timeUnit }`.
- * - `computed`: inline computed fields — one of `expr` (SQL, quote refs like
- * `"Horsepower"`), `bin`, or `log`. Reference them by `name` on any channel.
- * - `filters`: `oneOf` / `notIn` / `range` / `timeRange` (epoch ms).
- * - `timeUnit` expands to a real query-level date drill, identical to the UI's
- * drill-down — not just axis formatting.
- * - `config` / `layout` are canonical escape hatches, merged last.
+ * (export/import) always uses the canonical form. See docs/terse-spec-design.md.
*/
const DATASET_URL = '/datasets/ds-carsales-service.json';
@@ -30,7 +18,7 @@ const DATASET_URL = '/datasets/ds-carsales-service.json';
// Price_in_thousands, Horsepower, Curb_weight, Fuel_efficiency, ...).
const TERSE_EXAMPLES: { name: string; description: string; spec: TerseSpec }[] = [
{
- name: '1. Basic bar chart',
+ name: 'Basic bar chart',
description: 'Reference fields by NAME and use agg(field) shorthand — no fids, no pools, no layout boilerplate.',
spec: {
mark: 'bar',
@@ -40,7 +28,7 @@ const TERSE_EXAMPLES: { name: string; description: string; spec: TerseSpec }[] =
},
},
{
- name: '2. Computed field + filter + sort',
+ name: 'Computed field + filter + sort',
description:
'Define an inline computed field with a SQL expression (quote field names like "Horsepower"), reference it by name, filter rows, sort descending and keep the top 10.',
spec: {
@@ -53,7 +41,7 @@ const TERSE_EXAMPLES: { name: string; description: string; spec: TerseSpec }[] =
},
},
{
- name: '3. Time drill line chart',
+ name: 'Time drill line chart',
description: 'timeUnit expands into a real date-drill computed field at query level — the same drill the UI produces.',
spec: {
mark: 'line',
@@ -64,6 +52,17 @@ const TERSE_EXAMPLES: { name: string; description: string; spec: TerseSpec }[] =
},
];
+const CHEAT_SHEET: [string, string][] = [
+ [`"x": "Manufacturer"`, 'reference a field by name'],
+ [`"y": "mean(Price_in_thousands)"`, 'aggregate shorthand: sum / mean / count / median / min / max / variance / stdev'],
+ [`"x": "fid:col_0_64"`, 'fid: prefix bypasses name resolution'],
+ [`{ "field", "aggregate", "sort", "timeUnit" }`, 'object form for per-channel options'],
+ [`"computed": [{ "name", "expr" | "bin" | "log" }]`, 'inline computed fields; quote refs inside expr like "Horsepower"'],
+ [`"filters": [{ "field", "oneOf" | "notIn" | "range" | "timeRange" }]`, 'row filters; timeRange takes epoch milliseconds'],
+ [`"aggregate": false / "stack" / "limit" / "sort"`, 'chart-level knobs'],
+ [`"config" / "layout"`, 'canonical escape hatches, merged last'],
+];
+
export default function TerseSpecComponent() {
const { theme } = useContext(themeContext);
const { dataSource, fields } = useFetch<IDataSource>(DATASET_URL);
@@ -82,6 +81,8 @@ export default function TerseSpecComponent() {
setApplied((prev) => ({ text, revision: prev.revision + 1 }));
};
+ const apply = () => setApplied((prev) => ({ text: input, revision: prev.revision + 1 }));
+
const result = useMemo<{ chart: IChart } | { error: string }>(() => {
try {
const spec = JSON.parse(applied.text) as TerseSpec;
@@ -92,58 +93,127 @@ export default function TerseSpecComponent() {
}, [applied.text, fields]);
return (
- <div className="flex flex-col gap-3 p-3">
- <div className="text-sm">
- TerseSpec is the hand-writable spec grammar: fields by <b>name</b>, <code>agg(field)</code> shorthand, inline <code>computed</code> /{' '}
- <code>filters</code>, <code>timeUnit</code> drills. <code>normalize(spec, fields)</code> expands it into the full canonical chart spec that
- every renderer (and export/import) consumes. Edit the JSON below and hit Apply.
- </div>
- <div className="flex flex-wrap gap-2">
- {TERSE_EXAMPLES.map((example, i) => (
- <button
- key={example.name}
- className={`px-3 py-1.5 rounded-md text-sm font-medium border transition-colors ${
- i === exampleIndex ? 'bg-zinc-950 text-white dark:bg-white dark:text-zinc-950' : 'hover:bg-zinc-100 dark:hover:bg-zinc-800'
- }`}
- onClick={() => selectExample(i)}
- >
- {example.name}
- </button>
- ))}
- </div>
- <div className="text-xs text-gray-500 dark:text-gray-400">{TERSE_EXAMPLES[exampleIndex].description}</div>
- <div className="flex flex-col gap-2">
- <textarea
- value={input}
- onChange={(e) => setInput(e.target.value)}
- spellCheck={false}
- className="h-48 w-full border rounded font-mono text-xs p-2 bg-transparent"
- />
- <div className="flex gap-2 items-center">
- <button
- className="w-min h-9 px-4 py-2 bg-zinc-950 text-white shadow hover:bg-primary/90 inline-flex items-center justify-center whitespace-nowrap rounded-md text-sm font-medium transition-colors disabled:pointer-events-none disabled:opacity-50"
- onClick={() => setApplied((prev) => ({ text: input, revision: prev.revision + 1 }))}
- >
- Apply
- </button>
- <label className="text-xs flex items-center gap-1">
- <input type="checkbox" checked={showExpanded} onChange={(e) => setShowExpanded(e.target.checked)} />
- show expanded canonical spec
- </label>
- </div>
+ <div className="flex flex-col gap-5 p-4 sm:p-6">
+ <p className="max-w-4xl text-sm leading-6 text-zinc-600 dark:text-zinc-400">
+ TerseSpec is the hand-writable spec grammar: fields by <span className="font-semibold text-zinc-900 dark:text-zinc-100">name</span>,{' '}
+ <code className="rounded bg-zinc-100 px-1 py-0.5 text-xs dark:bg-zinc-800">agg(field)</code> shorthand, inline{' '}
+ <code className="rounded bg-zinc-100 px-1 py-0.5 text-xs dark:bg-zinc-800">computed</code> /{' '}
+ <code className="rounded bg-zinc-100 px-1 py-0.5 text-xs dark:bg-zinc-800">filters</code>,{' '}
+ <code className="rounded bg-zinc-100 px-1 py-0.5 text-xs dark:bg-zinc-800">timeUnit</code> drills.{' '}
+ <code className="rounded bg-zinc-100 px-1 py-0.5 text-xs dark:bg-zinc-800">normalize(spec, fields)</code> expands it into the full canonical
+ chart spec that every renderer (and export/import) consumes.
+ </p>
+
+ <div className="flex flex-wrap items-center gap-2">
+ {TERSE_EXAMPLES.map((example, i) => {
+ const active = i === exampleIndex;
+ return (
+ <button
+ key={example.name}
+ onClick={() => selectExample(i)}
+ className={`flex items-center gap-2 rounded-full border px-4 py-1.5 text-sm font-medium transition-colors ${
+ active
+ ? 'border-indigo-600 bg-indigo-600 text-white shadow-sm'
+ : 'border-zinc-300 text-zinc-700 hover:border-indigo-400 hover:text-indigo-600 dark:border-zinc-700 dark:text-zinc-300 dark:hover:border-indigo-500 dark:hover:text-indigo-400'
+ }`}
+ >
+ <span
+ className={`flex h-5 w-5 items-center justify-center rounded-full text-[11px] font-semibold ${
+ active ? 'bg-white/20 text-white' : 'bg-zinc-100 text-zinc-500 dark:bg-zinc-800 dark:text-zinc-400'
+ }`}
+ >
+ {i + 1}
+ </span>
+ {example.name}
+ </button>
+ );
+ })}
</div>
- {'error' in result ? (
- <div className="text-sm text-red-600 border border-red-300 rounded p-2 whitespace-pre-wrap">normalize() failed: {result.error}</div>
- ) : (
- <>
- {showExpanded && (
- <pre className="text-[10px] leading-tight border rounded p-2 max-h-64 overflow-auto">{JSON.stringify(result.chart, null, 2)}</pre>
+
+ <p className="max-w-4xl border-l-2 border-indigo-500 pl-3 text-sm leading-6 text-zinc-600 dark:text-zinc-400">
+ {TERSE_EXAMPLES[exampleIndex].description}
+ </p>
+
+ <div className="grid min-h-0 grow gap-5 xl:grid-cols-5">
+ <section className="flex flex-col gap-3 xl:col-span-2">
+ <div className="flex items-center justify-between">
+ <span className="text-xs font-semibold uppercase tracking-wide text-zinc-500 dark:text-zinc-400">TerseSpec JSON</span>
+ <span className="text-[11px] text-zinc-400 dark:text-zinc-500">⌘/Ctrl + ⏎ to apply</span>
+ </div>
+ <textarea
+ value={input}
+ onChange={(e) => setInput(e.target.value)}
+ onKeyDown={(e) => {
+ if ((e.metaKey || e.ctrlKey) && e.key === 'Enter') apply();
+ }}
+ spellCheck={false}
+ className="min-h-[16rem] w-full grow resize-y rounded-lg border border-zinc-300 bg-zinc-50 p-3 font-mono text-xs leading-5 text-zinc-900 shadow-inner focus:border-indigo-500 focus:outline-none focus:ring-2 focus:ring-indigo-500/30 dark:border-zinc-700 dark:bg-zinc-950 dark:text-zinc-100"
+ />
+ <div className="flex items-center gap-4">
+ <button
+ onClick={apply}
+ className="inline-flex h-9 items-center justify-center whitespace-nowrap rounded-md bg-indigo-600 px-4 text-sm font-medium text-white shadow-sm transition-colors hover:bg-indigo-500 focus-visible:outline-none focus-visible:ring-2 focus-visible:ring-indigo-500"
+ >
+ Apply
+ </button>
+ <label className="flex cursor-pointer items-center gap-1.5 text-xs text-zinc-600 dark:text-zinc-400">
+ <input
+ type="checkbox"
+ className="accent-indigo-600"
+ checked={showExpanded}
+ onChange={(e) => setShowExpanded(e.target.checked)}
+ />
+ show expanded canonical spec
+ </label>
+ </div>
+ {'error' in result && (
+ <div className="whitespace-pre-wrap rounded-lg border border-red-300 bg-red-50 p-3 font-mono text-xs leading-5 text-red-700 dark:border-red-900 dark:bg-red-950/40 dark:text-red-300">
+ normalize() failed: {result.error}
+ </div>
+ )}
+ {showExpanded && 'chart' in result && (
+ <div className="flex min-h-0 flex-col gap-1.5">
+ <span className="text-xs font-semibold uppercase tracking-wide text-zinc-500 dark:text-zinc-400">
+ Expanded canonical spec — normalize() output
+ </span>
+ <pre className="max-h-72 overflow-auto rounded-lg border border-zinc-200 bg-zinc-950 p-3 text-[11px] leading-4 text-zinc-100 dark:border-zinc-800">
+ {JSON.stringify(result.chart, null, 2)}
+ </pre>
+ </div>
)}
- <div style={{ border: '1px solid #ccc' }}>
- <GraphicRenderer key={applied.revision} fields={fields} data={dataSource} chart={[result.chart]} appearance={theme} />
+ <details className="mt-1">
+ <summary className="cursor-pointer select-none text-xs font-semibold uppercase tracking-wide text-zinc-500 transition-colors hover:text-zinc-900 dark:text-zinc-400 dark:hover:text-white">
+ Grammar cheat sheet
+ </summary>
+ <dl className="mt-2 flex flex-col gap-2 rounded-lg border border-zinc-200 p-3 dark:border-zinc-800">
+ {CHEAT_SHEET.map(([syntax, meaning]) => (
+ <div key={syntax} className="flex flex-col gap-0.5">
+ <dt>
+ <code className="break-all text-xs text-indigo-600 dark:text-indigo-400">{syntax}</code>
+ </dt>
+ <dd className="text-xs text-zinc-500 dark:text-zinc-400">{meaning}</dd>
+ </div>
+ ))}
+ </dl>
+ </details>
+ </section>
+
+ <section className="flex min-h-[26rem] flex-col overflow-hidden rounded-lg border border-zinc-200 dark:border-zinc-800 xl:col-span-3">
+ <div className="flex items-center justify-between border-b border-zinc-200 bg-zinc-50 px-3 py-2 dark:border-zinc-800 dark:bg-zinc-900">
+ <span className="text-xs font-semibold uppercase tracking-wide text-zinc-500 dark:text-zinc-400">Rendered chart</span>
+ <span className="text-[11px] text-zinc-400 dark:text-zinc-500">normalize(spec, fields) → GraphicRenderer</span>
+ </div>
+ <div className="min-h-0 grow overflow-auto p-2">
+ {'chart' in result ? (
+ <GraphicRenderer key={applied.revision} fields={fields} data={dataSource} chart={[result.chart]} appearance={theme} />
+ ) : (
+ <div className="flex h-full items-center justify-center text-sm text-zinc-400 dark:text-zinc-600">
+ Fix the spec to render the chart
+ </div>
+ )}
</div>
- </>
- )}
+ </section>
+ </div>
</div>
);
}