Transforms
Filter, aggregate, bin, fold, calculate and window data inside the spec, so published numbers trace back to source rows.
transform is an ordered list of steps run on data before anything is drawn, so a spec can carry its source rows unchanged and derive what it plots.
{
"mark": {
"type": "line",
"strokeWidth": 2,
"point": true
},
"data": [
{
"month": "Jan",
"low": 3.5,
"high": 9.3
},
{
"month": "Feb",
"low": 0.9,
"high": 9.2
},
{
"month": "Mar",
"low": 3.2,
"high": 10.8
},
{
"month": "Apr",
"low": 9.4,
"high": 19
},
{
"month": "May",
"low": 12,
"high": 22.1
},
{
"month": "Jun",
"low": 16.8,
"high": 25.4
},
{
"month": "Jul",
"low": 22.1,
"high": 30.2
},
{
"month": "Aug",
"low": 20.2,
"high": 27.6
},
{
"month": "Sep",
"low": 17.3,
"high": 24.3
},
{
"month": "Oct",
"low": 12.4,
"high": 19.3
},
{
"month": "Nov",
"low": 4.5,
"high": 11.8
},
{
"month": "Dec",
"low": 4.2,
"high": 9.8
}
],
"transform": [
{
"fold": [
"high",
"low"
],
"as": [
"measure",
"temperature"
]
}
],
"encoding": {
"x": {
"field": "month",
"type": "ordinal",
"sort": null,
"axis": {
"title": "2023"
}
},
"y": {
"field": "temperature",
"type": "quantitative",
"axis": {
"format": ".0f",
"labelSuffix": "°C",
"grid": true,
"title": "Temperature"
}
},
"color": {
"field": "measure",
"type": "nominal",
"scale": {
"domain": [
"high",
"low"
],
"range": [
"#c2410c",
"#0e7490"
]
}
}
},
"endpointLabels": {
"content": "label"
},
"chrome": {
"title": "Central Park's daily highs and lows both peaked in July 2023",
"subtitle": "Monthly average of each day's high and low temperature, Central Park, New York, 2023 (°C)"
}
}import { Chart } from "@opendata-ai/openchart-react";
import type { ChartSpec } from "@opendata-ai/openchart-core";
import "@opendata-ai/openchart-core/styles.css";
const data = [/* 12 rows, as in the Spec tab; Copy includes them */];
const spec: ChartSpec = {
data,
"mark": {
"type": "line",
"strokeWidth": 2,
"point": true
},
"transform": [
{
"fold": [
"high",
"low"
],
"as": [
"measure",
"temperature"
]
}
],
"encoding": {
"x": {
"field": "month",
"type": "ordinal",
"sort": null,
"axis": {
"title": "2023"
}
},
"y": {
"field": "temperature",
"type": "quantitative",
"axis": {
"format": ".0f",
"labelSuffix": "°C",
"grid": true,
"title": "Temperature"
}
},
"color": {
"field": "measure",
"type": "nominal",
"scale": {
"domain": [
"high",
"low"
],
"range": [
"#c2410c",
"#0e7490"
]
}
}
},
"endpointLabels": {
"content": "label"
},
"chrome": {
"title": "Central Park's daily highs and lows both peaked in July 2023",
"subtitle": "Monthly average of each day's high and low temperature, Central Park, New York, 2023 (°C)"
}
};
export function Example() {
return <Chart spec={spec} />;
}
<script setup lang="ts">
import { Chart } from "@opendata-ai/openchart-vue";
import type { ChartSpec } from "@opendata-ai/openchart-core";
import "@opendata-ai/openchart-core/styles.css";
const data = [/* 12 rows, as in the Spec tab; Copy includes them */];
const spec: ChartSpec = {
data,
"mark": {
"type": "line",
"strokeWidth": 2,
"point": true
},
"transform": [
{
"fold": [
"high",
"low"
],
"as": [
"measure",
"temperature"
]
}
],
"encoding": {
"x": {
"field": "month",
"type": "ordinal",
"sort": null,
"axis": {
"title": "2023"
}
},
"y": {
"field": "temperature",
"type": "quantitative",
"axis": {
"format": ".0f",
"labelSuffix": "°C",
"grid": true,
"title": "Temperature"
}
},
"color": {
"field": "measure",
"type": "nominal",
"scale": {
"domain": [
"high",
"low"
],
"range": [
"#c2410c",
"#0e7490"
]
}
}
},
"endpointLabels": {
"content": "label"
},
"chrome": {
"title": "Central Park's daily highs and lows both peaked in July 2023",
"subtitle": "Monthly average of each day's high and low temperature, Central Park, New York, 2023 (°C)"
}
};
</script>
<template>
<Chart :spec="spec" />
</template>
<script lang="ts">
import { Chart } from "@opendata-ai/openchart-svelte";
import type { ChartSpec } from "@opendata-ai/openchart-core";
import "@opendata-ai/openchart-core/styles.css";
const data = [/* 12 rows, as in the Spec tab; Copy includes them */];
const spec: ChartSpec = {
data,
"mark": {
"type": "line",
"strokeWidth": 2,
"point": true
},
"transform": [
{
"fold": [
"high",
"low"
],
"as": [
"measure",
"temperature"
]
}
],
"encoding": {
"x": {
"field": "month",
"type": "ordinal",
"sort": null,
"axis": {
"title": "2023"
}
},
"y": {
"field": "temperature",
"type": "quantitative",
"axis": {
"format": ".0f",
"labelSuffix": "°C",
"grid": true,
"title": "Temperature"
}
},
"color": {
"field": "measure",
"type": "nominal",
"scale": {
"domain": [
"high",
"low"
],
"range": [
"#c2410c",
"#0e7490"
]
}
}
},
"endpointLabels": {
"content": "label"
},
"chrome": {
"title": "Central Park's daily highs and lows both peaked in July 2023",
"subtitle": "Monthly average of each day's high and low temperature, Central Park, New York, 2023 (°C)"
}
};
</script>
<Chart {spec} />
import { createChart } from "@opendata-ai/openchart-vanilla";
import type { ChartSpec } from "@opendata-ai/openchart-core";
import "@opendata-ai/openchart-core/styles.css";
const data = [/* 12 rows, as in the Spec tab; Copy includes them */];
const spec: ChartSpec = {
data,
"mark": {
"type": "line",
"strokeWidth": 2,
"point": true
},
"transform": [
{
"fold": [
"high",
"low"
],
"as": [
"measure",
"temperature"
]
}
],
"encoding": {
"x": {
"field": "month",
"type": "ordinal",
"sort": null,
"axis": {
"title": "2023"
}
},
"y": {
"field": "temperature",
"type": "quantitative",
"axis": {
"format": ".0f",
"labelSuffix": "°C",
"grid": true,
"title": "Temperature"
}
},
"color": {
"field": "measure",
"type": "nominal",
"scale": {
"domain": [
"high",
"low"
],
"range": [
"#c2410c",
"#0e7490"
]
}
}
},
"endpointLabels": {
"content": "label"
},
"chrome": {
"title": "Central Park's daily highs and lows both peaked in July 2023",
"subtitle": "Monthly average of each day's high and low temperature, Central Park, New York, 2023 (°C)"
}
};
const container = document.getElementById("chart");
if (!container) throw new Error('Add <div id="chart"></div> to the page first.');
createChart(container, spec);
Fold wide columns into rows
The source has 12 rows, one per month, each with a low and a high column. A line chart with one color per series needs one row per value instead. { fold: ["high", "low"], as: ["measure", "temperature"] } turns the 12 rows into 24, each with a measure of “high” or “low” and its temperature. The chart then puts measure on color.
Calculate a new field
{
"mark": {
"type": "bar",
"fill": "#0e7490"
},
"data": [
{
"month": "Jan",
"low": 3.5,
"high": 9.3
},
{
"month": "Feb",
"low": 0.9,
"high": 9.2
},
{
"month": "Mar",
"low": 3.2,
"high": 10.8
},
{
"month": "Apr",
"low": 9.4,
"high": 19
},
{
"month": "May",
"low": 12,
"high": 22.1
},
{
"month": "Jun",
"low": 16.8,
"high": 25.4
},
{
"month": "Jul",
"low": 22.1,
"high": 30.2
},
{
"month": "Aug",
"low": 20.2,
"high": 27.6
},
{
"month": "Sep",
"low": 17.3,
"high": 24.3
},
{
"month": "Oct",
"low": 12.4,
"high": 19.3
},
{
"month": "Nov",
"low": 4.5,
"high": 11.8
},
{
"month": "Dec",
"low": 4.2,
"high": 9.8
}
],
"transform": [
{
"calculate": {
"op": "-",
"field": "high",
"field2": "low"
},
"as": "swing"
}
],
"encoding": {
"x": {
"field": "month",
"type": "ordinal",
"sort": null,
"axis": {
"title": "2023"
}
},
"y": {
"field": "swing",
"type": "quantitative",
"axis": {
"format": ".0f",
"labelSuffix": "°C",
"grid": true,
"title": "High minus low"
}
}
},
"labels": {
"format": ".1f"
},
"chrome": {
"title": "Central Park's gap between daily high and low was widest in May 2023, at 10.1°C",
"subtitle": "Average daily high minus average daily low by month, Central Park, New York, 2023 (°C)"
}
}import { Chart } from "@opendata-ai/openchart-react";
import type { ChartSpec } from "@opendata-ai/openchart-core";
import "@opendata-ai/openchart-core/styles.css";
const data = [/* 12 rows, as in the Spec tab; Copy includes them */];
const spec: ChartSpec = {
data,
"mark": {
"type": "bar",
"fill": "#0e7490"
},
"transform": [
{
"calculate": {
"op": "-",
"field": "high",
"field2": "low"
},
"as": "swing"
}
],
"encoding": {
"x": {
"field": "month",
"type": "ordinal",
"sort": null,
"axis": {
"title": "2023"
}
},
"y": {
"field": "swing",
"type": "quantitative",
"axis": {
"format": ".0f",
"labelSuffix": "°C",
"grid": true,
"title": "High minus low"
}
}
},
"labels": {
"format": ".1f"
},
"chrome": {
"title": "Central Park's gap between daily high and low was widest in May 2023, at 10.1°C",
"subtitle": "Average daily high minus average daily low by month, Central Park, New York, 2023 (°C)"
}
};
export function Example() {
return <Chart spec={spec} />;
}
<script setup lang="ts">
import { Chart } from "@opendata-ai/openchart-vue";
import type { ChartSpec } from "@opendata-ai/openchart-core";
import "@opendata-ai/openchart-core/styles.css";
const data = [/* 12 rows, as in the Spec tab; Copy includes them */];
const spec: ChartSpec = {
data,
"mark": {
"type": "bar",
"fill": "#0e7490"
},
"transform": [
{
"calculate": {
"op": "-",
"field": "high",
"field2": "low"
},
"as": "swing"
}
],
"encoding": {
"x": {
"field": "month",
"type": "ordinal",
"sort": null,
"axis": {
"title": "2023"
}
},
"y": {
"field": "swing",
"type": "quantitative",
"axis": {
"format": ".0f",
"labelSuffix": "°C",
"grid": true,
"title": "High minus low"
}
}
},
"labels": {
"format": ".1f"
},
"chrome": {
"title": "Central Park's gap between daily high and low was widest in May 2023, at 10.1°C",
"subtitle": "Average daily high minus average daily low by month, Central Park, New York, 2023 (°C)"
}
};
</script>
<template>
<Chart :spec="spec" />
</template>
<script lang="ts">
import { Chart } from "@opendata-ai/openchart-svelte";
import type { ChartSpec } from "@opendata-ai/openchart-core";
import "@opendata-ai/openchart-core/styles.css";
const data = [/* 12 rows, as in the Spec tab; Copy includes them */];
const spec: ChartSpec = {
data,
"mark": {
"type": "bar",
"fill": "#0e7490"
},
"transform": [
{
"calculate": {
"op": "-",
"field": "high",
"field2": "low"
},
"as": "swing"
}
],
"encoding": {
"x": {
"field": "month",
"type": "ordinal",
"sort": null,
"axis": {
"title": "2023"
}
},
"y": {
"field": "swing",
"type": "quantitative",
"axis": {
"format": ".0f",
"labelSuffix": "°C",
"grid": true,
"title": "High minus low"
}
}
},
"labels": {
"format": ".1f"
},
"chrome": {
"title": "Central Park's gap between daily high and low was widest in May 2023, at 10.1°C",
"subtitle": "Average daily high minus average daily low by month, Central Park, New York, 2023 (°C)"
}
};
</script>
<Chart {spec} />
import { createChart } from "@opendata-ai/openchart-vanilla";
import type { ChartSpec } from "@opendata-ai/openchart-core";
import "@opendata-ai/openchart-core/styles.css";
const data = [/* 12 rows, as in the Spec tab; Copy includes them */];
const spec: ChartSpec = {
data,
"mark": {
"type": "bar",
"fill": "#0e7490"
},
"transform": [
{
"calculate": {
"op": "-",
"field": "high",
"field2": "low"
},
"as": "swing"
}
],
"encoding": {
"x": {
"field": "month",
"type": "ordinal",
"sort": null,
"axis": {
"title": "2023"
}
},
"y": {
"field": "swing",
"type": "quantitative",
"axis": {
"format": ".0f",
"labelSuffix": "°C",
"grid": true,
"title": "High minus low"
}
}
},
"labels": {
"format": ".1f"
},
"chrome": {
"title": "Central Park's gap between daily high and low was widest in May 2023, at 10.1°C",
"subtitle": "Average daily high minus average daily low by month, Central Park, New York, 2023 (°C)"
}
};
const container = document.getElementById("chart");
if (!container) throw new Error('Add <div id="chart"></div> to the page first.');
createChart(container, spec);
calculate adds a field to every row. { op: "-", field: "high", field2: "low" } subtracts the two columns, and as: "swing" names the result. The same 12 rows now give the gap between each month’s average high and low, which peaks at 10.1°C in May. The ops are arithmetic only: +, -, *, /, plus abs, round, floor, ceil, log and sqrt on one field.
Filter and aggregate
{
"mark": {
"type": "bar",
"fill": "#0e7490"
},
"data": [
{
"year": "2016",
"source": "Coal",
"share": 31.2
},
{
"year": "2016",
"source": "Gas",
"share": 34.8
},
{
"year": "2016",
"source": "Nuclear",
"share": 20.3
},
{
"year": "2016",
"source": "Hydro",
"share": 6.6
},
{
"year": "2016",
"source": "Wind",
"share": 5.7
},
{
"year": "2016",
"source": "Solar",
"share": 1.4
},
// … 42 more rows; Copy includes them
],
"transform": [
{
"filter": {
"field": "source",
"oneOf": [
"Wind",
"Solar"
]
}
},
{
"aggregate": [
{
"op": "sum",
"field": "share",
"as": "windSolar"
}
],
"groupby": [
"year"
]
}
],
"encoding": {
"x": {
"field": "year",
"type": "ordinal"
},
"y": {
"field": "windSolar",
"type": "quantitative",
"axis": {
"title": "Wind plus solar",
"format": ".0f",
"labelSuffix": "%"
}
}
},
"labels": {
"format": ".1f"
},
"chrome": {
"title": "Wind and solar went from 7.1% of generation from the six main sources in 2016 to 15.9% in 2023",
"subtitle": "Share of US generation from coal, gas, nuclear, hydro, wind and solar (%), wind and solar combined"
}
}import { Chart } from "@opendata-ai/openchart-react";
import type { ChartSpec } from "@opendata-ai/openchart-core";
import "@opendata-ai/openchart-core/styles.css";
const data = [/* 48 rows; Copy includes them */];
const spec: ChartSpec = {
data,
"mark": {
"type": "bar",
"fill": "#0e7490"
},
"transform": [
{
"filter": {
"field": "source",
"oneOf": [
"Wind",
"Solar"
]
}
},
{
"aggregate": [
{
"op": "sum",
"field": "share",
"as": "windSolar"
}
],
"groupby": [
"year"
]
}
],
"encoding": {
"x": {
"field": "year",
"type": "ordinal"
},
"y": {
"field": "windSolar",
"type": "quantitative",
"axis": {
"title": "Wind plus solar",
"format": ".0f",
"labelSuffix": "%"
}
}
},
"labels": {
"format": ".1f"
},
"chrome": {
"title": "Wind and solar went from 7.1% of generation from the six main sources in 2016 to 15.9% in 2023",
"subtitle": "Share of US generation from coal, gas, nuclear, hydro, wind and solar (%), wind and solar combined"
}
};
export function Example() {
return <Chart spec={spec} />;
}
<script setup lang="ts">
import { Chart } from "@opendata-ai/openchart-vue";
import type { ChartSpec } from "@opendata-ai/openchart-core";
import "@opendata-ai/openchart-core/styles.css";
const data = [/* 48 rows; Copy includes them */];
const spec: ChartSpec = {
data,
"mark": {
"type": "bar",
"fill": "#0e7490"
},
"transform": [
{
"filter": {
"field": "source",
"oneOf": [
"Wind",
"Solar"
]
}
},
{
"aggregate": [
{
"op": "sum",
"field": "share",
"as": "windSolar"
}
],
"groupby": [
"year"
]
}
],
"encoding": {
"x": {
"field": "year",
"type": "ordinal"
},
"y": {
"field": "windSolar",
"type": "quantitative",
"axis": {
"title": "Wind plus solar",
"format": ".0f",
"labelSuffix": "%"
}
}
},
"labels": {
"format": ".1f"
},
"chrome": {
"title": "Wind and solar went from 7.1% of generation from the six main sources in 2016 to 15.9% in 2023",
"subtitle": "Share of US generation from coal, gas, nuclear, hydro, wind and solar (%), wind and solar combined"
}
};
</script>
<template>
<Chart :spec="spec" />
</template>
<script lang="ts">
import { Chart } from "@opendata-ai/openchart-svelte";
import type { ChartSpec } from "@opendata-ai/openchart-core";
import "@opendata-ai/openchart-core/styles.css";
const data = [/* 48 rows; Copy includes them */];
const spec: ChartSpec = {
data,
"mark": {
"type": "bar",
"fill": "#0e7490"
},
"transform": [
{
"filter": {
"field": "source",
"oneOf": [
"Wind",
"Solar"
]
}
},
{
"aggregate": [
{
"op": "sum",
"field": "share",
"as": "windSolar"
}
],
"groupby": [
"year"
]
}
],
"encoding": {
"x": {
"field": "year",
"type": "ordinal"
},
"y": {
"field": "windSolar",
"type": "quantitative",
"axis": {
"title": "Wind plus solar",
"format": ".0f",
"labelSuffix": "%"
}
}
},
"labels": {
"format": ".1f"
},
"chrome": {
"title": "Wind and solar went from 7.1% of generation from the six main sources in 2016 to 15.9% in 2023",
"subtitle": "Share of US generation from coal, gas, nuclear, hydro, wind and solar (%), wind and solar combined"
}
};
</script>
<Chart {spec} />
import { createChart } from "@opendata-ai/openchart-vanilla";
import type { ChartSpec } from "@opendata-ai/openchart-core";
import "@opendata-ai/openchart-core/styles.css";
const data = [/* 48 rows; Copy includes them */];
const spec: ChartSpec = {
data,
"mark": {
"type": "bar",
"fill": "#0e7490"
},
"transform": [
{
"filter": {
"field": "source",
"oneOf": [
"Wind",
"Solar"
]
}
},
{
"aggregate": [
{
"op": "sum",
"field": "share",
"as": "windSolar"
}
],
"groupby": [
"year"
]
}
],
"encoding": {
"x": {
"field": "year",
"type": "ordinal"
},
"y": {
"field": "windSolar",
"type": "quantitative",
"axis": {
"title": "Wind plus solar",
"format": ".0f",
"labelSuffix": "%"
}
}
},
"labels": {
"format": ".1f"
},
"chrome": {
"title": "Wind and solar went from 7.1% of generation from the six main sources in 2016 to 15.9% in 2023",
"subtitle": "Share of US generation from coal, gas, nuclear, hydro, wind and solar (%), wind and solar combined"
}
};
const container = document.getElementById("chart");
if (!container) throw new Error('Add <div id="chart"></div> to the page first.');
createChart(container, spec);
Transforms run in order, each on the output of the one before. The source has 48 rows, six sources for each of eight years. filter with oneOf: ["Wind", "Solar"] keeps 16 of them. aggregate with op: "sum" and groupby: ["year"] collapses those to eight rows, one per year, holding the two shares added together: 5.7% plus 1.4% gives the 7.1% for 2016.
Bin values into ranges
{
"mark": {
"type": "bar",
"fill": "#0e7490"
},
"data": [
{
"state": "AL",
"rate": 2.7
},
{
"state": "AK",
"rate": 4.8
},
{
"state": "AZ",
"rate": 4.7
},
{
"state": "AR",
"rate": 4.2
},
{
"state": "CA",
"rate": 5.3
},
{
"state": "CO",
"rate": 3.8
},
// … 45 more rows; Copy includes them
],
"transform": [
{
"bin": {
"step": 0.5,
"extent": [
2,
6
]
},
"field": "rate",
"as": [
"rateFrom",
"rateTo"
]
},
{
"aggregate": [
{
"op": "count",
"field": "state",
"as": "states"
}
],
"groupby": [
"rateFrom",
"rateTo"
]
}
],
"encoding": {
"x": {
"field": "rateFrom",
"type": "quantitative",
"axis": {
"title": "Unemployment rate",
"format": ".1f",
"labelSuffix": "%"
}
},
"x2": {
"field": "rateTo",
"type": "quantitative"
},
"y": {
"field": "states",
"type": "quantitative",
"axis": {
"title": "States",
"tickCount": 4
}
}
},
"labels": {
"format": ".0f"
},
"chrome": {
"title": "Unemployment was between 3% and 5% in 38 of the 50 states and DC",
"subtitle": "Number of states by unemployment rate, in half-point ranges. Seasonally adjusted, latest month."
}
}import { Chart } from "@opendata-ai/openchart-react";
import type { ChartSpec } from "@opendata-ai/openchart-core";
import "@opendata-ai/openchart-core/styles.css";
const data = [/* 51 rows; Copy includes them */];
const spec: ChartSpec = {
data,
"mark": {
"type": "bar",
"fill": "#0e7490"
},
"transform": [
{
"bin": {
"step": 0.5,
"extent": [
2,
6
]
},
"field": "rate",
"as": [
"rateFrom",
"rateTo"
]
},
{
"aggregate": [
{
"op": "count",
"field": "state",
"as": "states"
}
],
"groupby": [
"rateFrom",
"rateTo"
]
}
],
"encoding": {
"x": {
"field": "rateFrom",
"type": "quantitative",
"axis": {
"title": "Unemployment rate",
"format": ".1f",
"labelSuffix": "%"
}
},
"x2": {
"field": "rateTo",
"type": "quantitative"
},
"y": {
"field": "states",
"type": "quantitative",
"axis": {
"title": "States",
"tickCount": 4
}
}
},
"labels": {
"format": ".0f"
},
"chrome": {
"title": "Unemployment was between 3% and 5% in 38 of the 50 states and DC",
"subtitle": "Number of states by unemployment rate, in half-point ranges. Seasonally adjusted, latest month."
}
};
export function Example() {
return <Chart spec={spec} />;
}
<script setup lang="ts">
import { Chart } from "@opendata-ai/openchart-vue";
import type { ChartSpec } from "@opendata-ai/openchart-core";
import "@opendata-ai/openchart-core/styles.css";
const data = [/* 51 rows; Copy includes them */];
const spec: ChartSpec = {
data,
"mark": {
"type": "bar",
"fill": "#0e7490"
},
"transform": [
{
"bin": {
"step": 0.5,
"extent": [
2,
6
]
},
"field": "rate",
"as": [
"rateFrom",
"rateTo"
]
},
{
"aggregate": [
{
"op": "count",
"field": "state",
"as": "states"
}
],
"groupby": [
"rateFrom",
"rateTo"
]
}
],
"encoding": {
"x": {
"field": "rateFrom",
"type": "quantitative",
"axis": {
"title": "Unemployment rate",
"format": ".1f",
"labelSuffix": "%"
}
},
"x2": {
"field": "rateTo",
"type": "quantitative"
},
"y": {
"field": "states",
"type": "quantitative",
"axis": {
"title": "States",
"tickCount": 4
}
}
},
"labels": {
"format": ".0f"
},
"chrome": {
"title": "Unemployment was between 3% and 5% in 38 of the 50 states and DC",
"subtitle": "Number of states by unemployment rate, in half-point ranges. Seasonally adjusted, latest month."
}
};
</script>
<template>
<Chart :spec="spec" />
</template>
<script lang="ts">
import { Chart } from "@opendata-ai/openchart-svelte";
import type { ChartSpec } from "@opendata-ai/openchart-core";
import "@opendata-ai/openchart-core/styles.css";
const data = [/* 51 rows; Copy includes them */];
const spec: ChartSpec = {
data,
"mark": {
"type": "bar",
"fill": "#0e7490"
},
"transform": [
{
"bin": {
"step": 0.5,
"extent": [
2,
6
]
},
"field": "rate",
"as": [
"rateFrom",
"rateTo"
]
},
{
"aggregate": [
{
"op": "count",
"field": "state",
"as": "states"
}
],
"groupby": [
"rateFrom",
"rateTo"
]
}
],
"encoding": {
"x": {
"field": "rateFrom",
"type": "quantitative",
"axis": {
"title": "Unemployment rate",
"format": ".1f",
"labelSuffix": "%"
}
},
"x2": {
"field": "rateTo",
"type": "quantitative"
},
"y": {
"field": "states",
"type": "quantitative",
"axis": {
"title": "States",
"tickCount": 4
}
}
},
"labels": {
"format": ".0f"
},
"chrome": {
"title": "Unemployment was between 3% and 5% in 38 of the 50 states and DC",
"subtitle": "Number of states by unemployment rate, in half-point ranges. Seasonally adjusted, latest month."
}
};
</script>
<Chart {spec} />
import { createChart } from "@opendata-ai/openchart-vanilla";
import type { ChartSpec } from "@opendata-ai/openchart-core";
import "@opendata-ai/openchart-core/styles.css";
const data = [/* 51 rows; Copy includes them */];
const spec: ChartSpec = {
data,
"mark": {
"type": "bar",
"fill": "#0e7490"
},
"transform": [
{
"bin": {
"step": 0.5,
"extent": [
2,
6
]
},
"field": "rate",
"as": [
"rateFrom",
"rateTo"
]
},
{
"aggregate": [
{
"op": "count",
"field": "state",
"as": "states"
}
],
"groupby": [
"rateFrom",
"rateTo"
]
}
],
"encoding": {
"x": {
"field": "rateFrom",
"type": "quantitative",
"axis": {
"title": "Unemployment rate",
"format": ".1f",
"labelSuffix": "%"
}
},
"x2": {
"field": "rateTo",
"type": "quantitative"
},
"y": {
"field": "states",
"type": "quantitative",
"axis": {
"title": "States",
"tickCount": 4
}
}
},
"labels": {
"format": ".0f"
},
"chrome": {
"title": "Unemployment was between 3% and 5% in 38 of the 50 states and DC",
"subtitle": "Number of states by unemployment rate, in half-point ranges. Seasonally adjusted, latest month."
}
};
const container = document.getElementById("chart");
if (!container) throw new Error('Add <div id="chart"></div> to the page first.');
createChart(container, spec);
A histogram counts how many rows fall in each range. bin with step: 0.5 writes each state’s range start and end into rateFrom and rateTo, and aggregate with op: "count" grouped by both fields turns 51 rows into one count per range. Putting rateTo on x2 makes each bar span its range on a continuous axis. mark: "histogram" does the same two steps for you.
Fields that matter
| Transform | What it does |
|---|---|
fold |
Wide to long: one output row per listed column, with key and value fields. |
calculate |
Adds a field from arithmetic on one or two fields, or a field and a constant. |
filter |
Keeps rows matching a predicate: equal, oneOf, lt, gte, range and more, combined with and, or and not. |
aggregate |
Collapses each group to one row: sum, mean, median, count, min, max and others. |
joinaggregate |
Same ops, but keeps every row and writes the group summary onto each. |
bin |
Writes the range a value falls in, by step or maxbins. |
window |
Running values in sort order: cumsum, rank, lag, diff, pct_change. |
timeUnit |
Truncates a date to a unit such as year or month. |
Related
- Histogram uses binning through
mark: "histogram". - Stacked area plots long rows like the folded ones here.
- Transforms reference lists every transform and its fields.