Back to Data Formulator

Vega-Lite Backend

src/lib/agents-chart/vegalite/README.md

0.64.0 KB
Original Source

Vega-Lite Backend

The primary and most feature-complete backend for agents-chart. Compiles the core semantic layer into declarative Vega-Lite JSON specifications.

Output Format

jsonc
{ "mark": "bar", "encoding": { "x": { "field": "Name", "type": "nominal" }, ... }, "data": { "values": [...] }, "width": 400, "height": 300 }

A declarative JSON spec consumed by the Vega-Lite renderer. Bar sizing uses width: { step: N }, axis/legend/tooltip configuration is fully declarative via encodings and config blocks, and faceting uses VL's native facet + columns structure.

Assembly Pipeline

All backends share Phases 0 and 1 from core/. Phase 2 is VL-specific.

PhaseStepDescription
0resolveSemanticsResolve field types, aggregates, ordinal sort orders
0adeclareLayoutModeTemplate declares banded axes, sizing hints, auto-detect binned axes
0bconvertTemporalDataParse temporal strings to JS Dates
0cfilterOverflowTruncate categories that overflow the canvas
1computeLayoutCompute step sizes, subplot dimensions, tick params (target-agnostic)
2buildVLEncodingstemplate.instantiaterestructureFacetsvlApplyLayoutToSpec → post-layout (facet binning, independent scales, tooltips)Final VL spec

Unique to VL: The buildVLEncodings step translates abstract ChannelSemantics into VL encoding objects before templates run — other backends skip this and let templates read channelSemantics directly.

File Structure

vegalite/
  assemble.ts          – assembleVegaLite(): Phase 2 assembly
  instantiate-spec.ts  – vlApplyLayoutToSpec(), vlApplyTooltips()
  index.ts             – barrel exports
  templates/
    index.ts           – template registry (27 templates, 7 categories)
    scatter.ts         – Scatter Plot, Linear Regression
    bar.ts             – Bar, Grouped Bar, Stacked Bar, Histogram, Lollipop, Pyramid
    line.ts            – Line, Dotted Line, Bump Chart
    area.ts            – Area, Streamgraph
    pie.ts             – Pie Chart
    rose.ts            – Rose Chart
    radar.ts           – Radar Chart
    density.ts         – Density Plot
    candlestick.ts     – Candlestick Chart
    waterfall.ts       – Waterfall Chart
    lollipop.ts        – Lollipop (also in bar.ts registry)
    jitter.ts          – Strip Plot
    bump.ts            – Bump Chart helpers
    custom.ts          – Custom Point/Line/Bar/Rect/Area
    map.ts             – US Map, World Map
    utils.ts           – shared template utilities

Template Definitions (27 templates)

CategoryCharts
Scatter & PointScatter Plot, Linear Regression, Boxplot, Strip Plot
BarBar Chart, Grouped Bar, Stacked Bar, Histogram, Lollipop, Pyramid
Line & AreaLine Chart, Dotted Line, Bump Chart, Area Chart, Streamgraph
Part-to-WholePie Chart, Rose Chart, Heatmap, Waterfall Chart
StatisticalDensity Plot, Ranged Dot Plot, Radar Chart, Candlestick Chart
MapUS Map, World Map
CustomCustom Point, Custom Line, Custom Bar, Custom Rect, Custom Area

Known Issues & Notes

  • Richest template set of all backends (27 templates). Maps and statistical charts (density, candlestick, waterfall) are only available in VL.
  • instantiate-spec.ts (421 lines) is the only file that contains Vega-Lite syntax knowledge — all other files work with abstract semantics.
  • Faceting is handled via VL's native facet mechanism; restructureFacets converts column-only facets to the facet + columns structure.
  • The buildVLEncodings step is an extra translation layer not present in other backends — it converts ChannelSemantics to { field, type, scale, axis, ... } encoding objects before template instantiation.
  • VL natively handles zero-baseline via scale: { zero: true }, bar step sizing via width: { step }, and legend positioning — no manual pixel math needed.