Vega for LiaScript: Interactive Data Visualizations in Your Course

Vega for LiaScript: Interactive Data Visualizations in Your Course

Data visualization is central to data science, statistics, and quantitative research education. Good charts communicate insights that tables cannot. Interactive charts — where students can hover, filter, and explore — communicate even more.

The Vega template brings Vega-Lite to LiaScript. Write a chart specification as JSON; the template renders it as a fully interactive visualization using the Vega stack.


Quick Start

<!--
import: https://raw.githubusercontent.com/liaTemplates/vega/master/README.md
-->

Two macros: @Vega.exec for an editable chart and @Vega.run in the fence opener for a rendered-only chart.


Macro 1: @Vega.exec — Editable Chart

@Vega.exec is attached at the end of a JSON code block. The block is shown to students, who can modify the specification and click run to re-render.

```json
{
  "$schema": "https://vega.github.io/schema/vega-lite/v4.json",
  "data": {
    "values": [
      {"subject": "Math",    "score": 82},
      {"subject": "Physics", "score": 76},
      {"subject": "CS",      "score": 91},
      {"subject": "English", "score": 68},
      {"subject": "History", "score": 74}
    ]
  },
  "mark": "bar",
  "encoding": {
    "x": {"field": "subject", "type": "nominal"},
    "y": {"field": "score",   "type": "quantitative"}
  }
}
```
@Vega.exec

Macro 2: @Vega.run — Silent Rendering

@Vega.run is placed in the fence header. The JSON specification is hidden from the student; only the rendered chart is shown. This is useful for charts that serve as illustrations rather than exercises.

```json @Vega.run
{
  "$schema": "https://vega.github.io/schema/vega-lite/v4.json",
  "data": {"url": "https://vega.github.io/vega-lite/examples/data/seattle-weather.csv"},
  "mark": "tick",
  "encoding": {
    "x": {"field": "precipitation", "type": "quantitative"}
  }
}
```

Vega-Lite Chart Types

Vega-Lite supports all standard chart types via the "mark" property:

MarkChart type
"bar"Bar chart (vertical or horizontal)
"line"Line chart
"point"Scatter plot
"area"Area chart
"tick"Strip plot (distribution)
"boxplot"Box-and-whisker plot
"rule"Reference lines
"text"Text labels on chart

All marks support aggregation ("aggregate": "mean", "count", "sum"), color encoding, tooltips, and interactive selections.


Scatter Plot Example

```json
{
  "$schema": "https://vega.github.io/schema/vega-lite/v4.json",
  "data": {"url": "https://vega.github.io/vega-lite/examples/data/cars.json"},
  "mark": "point",
  "encoding": {
    "x": {"field": "Horsepower", "type": "quantitative"},
    "y": {"field": "Miles_per_Gallon", "type": "quantitative"},
    "color": {"field": "Origin", "type": "nominal"},
    "tooltip": [
      {"field": "Name", "type": "nominal"},
      {"field": "Horsepower", "type": "quantitative"},
      {"field": "Miles_per_Gallon", "type": "quantitative"}
    ]
  }
}
```
@Vega.exec

Full Template Demo


Use Cases

Data science courses — Teach exploratory data analysis (EDA) by showing students live charts they can modify. Change the mark from "bar" to "line", add a color encoding, switch from count to mean — all without writing code.

Statistics — Illustrate distributions, histograms, and box plots. Students can load a real dataset from a URL and explore it visually.

Research methods — Show the difference between chart types for the same data. A scatter plot, bar chart, and line chart of the same dataset tell different stories.

STEM and engineering — Plot sensor data, simulation results, or experimental measurements as interactive charts embedded in the course material.


Technical Facts

Runs in browserYes
Server requiredNo (for local data)
Data sourcesInline JSON values or remote CSV/JSON URLs
Editable versionYes — @Vega.exec
Vega versionVega 5.9.0, Vega-Lite 4.0.2
Interactive featuresTooltips, selections, hover effects
LicenseMIT
MaintainedStable (version 0.0.2)

Try It

Try in LiaScript Open in LiveEditor View on GitHub
  • Algebrite — symbolic math and CAS computation in the browser
  • JSXGraph — interactive mathematical plots with a JavaScript API
  • VTK — 3D scientific data visualization with VTK.js
  • Pyodide — Python in the browser with Pandas and Matplotlib for data analysis

Related Posts

SpreadSheet for LiaScript: Excel-Style Interactive Tables in Your Course

Embed fully interactive spreadsheets with formulas, dropdowns, checkboxes, and multiple worksheets directly in LiaScript — powered by JSSpreadsheet, no backend required.

Read More

DuckDB for LiaScript: Teach Data Analytics Directly in the Browser

Use the DuckDB template to run analytical SQL queries, explore datasets, and teach data analytics directly inside LiaScript courses — no server, fully powered by WebAssembly.

Read More

PGlite for LiaScript: Full PostgreSQL in the Browser

Use the PGlite template to run full PostgreSQL 16 queries directly in your LiaScript courses — no server, no setup, just write SQL and teach.

Read More