Plotly, Vega-Lite, ECharts and Chart.js
Not every project draws with Plot or D3. The Plotly, Vega-Lite and ECharts subpaths patch a plain spec object (a
figure, a spec, an option) and never import the charting library, so they work with whatever build you already
load; the Chart.js subpath returns dataset options and plugins. All of them are synchronous, so load the league first
(loadLeague). Each chart below, except the three static embedSources outputs, is drawn by its own library in
your browser, from the spec the example exports as browser; hover it, zoom it or save it as that library allows.
The code also renders the Vega-Lite, ECharts and Chart.js charts in Node, where each library runs without a browser:
that render is what the page shows before (or without) JavaScript.
Plotly
withLogos(figure, rows, { league, x, y, team }) returns a new figure with one layout.images entry per row, for
Plotly.newPlot(div, figure.data, figure.layout). plotly.js loads each image itself, so without JavaScript these
examples show the figure rather than a render.
With JavaScript on, plotly.js draws this figure here.
{
"data": [
{
"type": "scatter",
"mode": "markers",
"x": [
385,
402,
425,
309,
525,
345,
338,
289
],
"y": [
326,
301,
311,
434,
368,
364,
404,
417
]
}
],
"layout": {
"width": 560,
"height": 400,
"xaxis": {
"range": [
260,
550
]
},
"yaxis": {
"range": [
280,
460
]
},
"images": [
{
"source": "https://sdv.nyc3.cdn.digitaloceanspaces.com/assets/public/sha256/3d/3d77958dc6373768919bb2681cbe1b143f56c07a1f013460def665a5026a7f3d.png",
"x": 385,
"y": 326,
"xref": "x",
"yref": "y",
"sizex": 580,
"sizey": 21.599999999999998,
"sizing": "contain",
"xanchor": "center",
"yanchor": "middle",
"opacity": 1,
"layer": "above",
"name": "sdvplot:logo:12"
},
{
"source": "https://sdv.nyc3.cdn.digitaloceanspaces.com/assets/public/sha256/54/5400f85bd93129c056717a771da57a97225e15f39c0022107ce89ef993f15bb0.png",
"x": 402,
"y": 301,
"xref": "x",
"yref": "y",
"sizex": 580,
"sizey": 21.599999999999998,
"sizing": "contain",
"xanchor": "center",
"yanchor": "middle",
"opacity": 1,
"layer": "above",
"name": "sdvplot:logo:24"
},
{
"source": "https://sdv.nyc3.cdn.digitaloceanspaces.com/assets/public/sha256/c9/c98bec2be32e27b19f79f5da86ac6ef133c78d75ab78aadb28ef36696c3213e8.png",
"x": 425,
"y": 311,
"xref": "x",
"yref": "y",
"sizex": 580,
"sizey": 21.599999999999998,
"sizing": "contain",
"xanchor": "center",
"yanchor": "middle",
"opacity": 1,
"layer": "above",
"name": "sdvplot:logo:7"
},
{
"source": "https://sdv.nyc3.cdn.digitaloceanspaces.com/assets/public/sha256/25/25fbb03e972ae872fa024026b73c7b63ef9f23c2f2c51f87d1614d800dcee6e7.png",
"x": 309,
"y": 434,
"xref": "x",
"yref": "y",
"sizex": 580,
"sizey": 21.599999999999998,
"sizing": "contain",
"xanchor": "center",
"yanchor": "middle",
"opacity": 1,
"layer": "above",
"name": "sdvplot:logo:13"
},
{
"source": "https://sdv.nyc3.cdn.digitaloceanspaces.com/assets/public/sha256/79/79b71e2f536ee29f9d23834e89828883af2d95bf6968cbd07a505444229cdd20.png",
"x": 525,
"y": 368,
"xref": "x",
"yref": "y",
"sizex": 580,
"sizey": 21.599999999999998,
"sizing": "contain",
"xanchor": "center",
"yanchor": "middle",
"opacity": 1,
"layer": "above",
"name": "sdvplot:logo:2"
},
{
"source": "https://sdv.nyc3.cdn.digitaloceanspaces.com/assets/public/sha256/b9/b9631269a82abda39bd748afc82390679ca67bfc167bc3581665e237407de8e0.png",
"x": 345,
"y": 364,
"xref": "x",
"yref": "y",
"sizex": 580,
"sizey": 21.599999999999998,
"sizing": "contain",
"xanchor": "center",
"yanchor": "middle",
"opacity": 1,
"layer": "above",
"name": "sdvplot:logo:15"
},
{
"source": "https://sdv.nyc3.cdn.digitaloceanspaces.com/assets/public/sha256/5d/5de8d029f3602c97ffe02bd636eb2630e55d019d1a37cf819d3b9e39a219487b.png",
"x": 338,
"y": 404,
"xref": "x",
"yref": "y",
"sizex": 580,
"sizey": 21.599999999999998,
"sizing": "contain",
"xanchor": "center",
"yanchor": "middle",
"opacity": 1,
"layer": "above",
"name": "sdvplot:logo:20"
},
{
"source": "https://sdv.nyc3.cdn.digitaloceanspaces.com/assets/public/sha256/da/dac19aa9a573dcf9075e2422bee3e35a4955b9ab494bb3191ab3bacfd3609729.png",
"x": 289,
"y": 417,
"xref": "x",
"yref": "y",
"sizex": 580,
"sizey": 21.599999999999998,
"sizing": "contain",
"xanchor": "center",
"yanchor": "middle",
"opacity": 1,
"layer": "above",
"name": "sdvplot:logo:17"
}
]
}
}import { STANDINGS } from "@sportsdataverse/examples/data";
import { loadLeague } from "@sportsdataverse/sdvplot";
import { withLogos } from "@sportsdataverse/sdvplot/plotly";
await loadLeague("nfl");
const rows = STANDINGS.map((s) => ({ ...s }));
const figure = {
data: [{ type: "scatter", mode: "markers", x: rows.map((r) => r.pf), y: rows.map((r) => r.pa) }],
layout: { width: 560, height: 400, xaxis: { range: [260, 550] }, yaxis: { range: [280, 460] } },
};
// Each logo is one entry of layout.images, which plotly.js loads itself.
const logos = withLogos(figure, rows, { league: "nfl", x: "pf", y: "pa", team: "team", height: 0.12 });
// In the browser: Plotly.newPlot(div, data, layout), as this page does. Without JavaScript the page shows the figure.
export const browser = {
lib: "plotly",
figure: logos,
label: "2024 AFC points for against points against, each team drawn as its logo",
} as const;
logos;
withAxisLogos puts logos under an axis's ticks and teamColorway gives one team colour per bar; withHeadshots
and withWordmarks place images at each row's point.
With JavaScript on, plotly.js draws this figure here.
{
"data": [
{
"type": "bar",
"x": [
"KC",
"BUF",
"LAC",
"DEN",
"MIA",
"NYJ",
"LV",
"NE"
],
"y": [
15,
13,
11,
10,
8,
5,
4,
4
],
"marker": {
"color": [
"#e31837",
"#00338d",
"#007bc7",
"#002244",
"#008e97",
"#003f2d",
"#000000",
"#002244"
]
}
}
],
"layout": {
"width": 560,
"height": 360,
"xaxis": {
"tickmode": "array",
"showticklabels": false,
"tickvals": [
"KC",
"BUF",
"LAC",
"DEN",
"MIA",
"NYJ",
"LV",
"NE"
],
"ticktext": [
"KC",
"BUF",
"LAC",
"DEN",
"MIA",
"NYJ",
"LV",
"NE"
]
},
"margin": {
"b": 97
},
"images": [
{
"source": "https://sdv.nyc3.cdn.digitaloceanspaces.com/assets/public/sha256/3d/3d77958dc6373768919bb2681cbe1b143f56c07a1f013460def665a5026a7f3d.png",
"sizing": "contain",
"layer": "above",
"name": "sdvplot:axis:x:12",
"x": 0,
"y": 0,
"xref": "x",
"yref": "y domain",
"sizex": 16,
"sizey": 0.1,
"xanchor": "center",
"yanchor": "top"
},
{
"source": "https://sdv.nyc3.cdn.digitaloceanspaces.com/assets/public/sha256/79/79b71e2f536ee29f9d23834e89828883af2d95bf6968cbd07a505444229cdd20.png",
"sizing": "contain",
"layer": "above",
"name": "sdvplot:axis:x:2",
"x": 1,
"y": 0,
"xref": "x",
"yref": "y domain",
"sizex": 16,
"sizey": 0.1,
"xanchor": "center",
"yanchor": "top"
},
{
"source": "https://sdv.nyc3.cdn.digitaloceanspaces.com/assets/public/sha256/54/5400f85bd93129c056717a771da57a97225e15f39c0022107ce89ef993f15bb0.png",
"sizing": "contain",
"layer": "above",
"name": "sdvplot:axis:x:24",
"x": 2,
"y": 0,
"xref": "x",
"yref": "y domain",
"sizex": 16,
"sizey": 0.1,
"xanchor": "center",
"yanchor": "top"
},
{
"source": "https://sdv.nyc3.cdn.digitaloceanspaces.com/assets/public/sha256/c9/c98bec2be32e27b19f79f5da86ac6ef133c78d75ab78aadb28ef36696c3213e8.png",
"sizing": "contain",
"layer": "above",
"name": "sdvplot:axis:x:7",
"x": 3,
"y": 0,
"xref": "x",
"yref": "y domain",
"sizex": 16,
"sizey": 0.1,
"xanchor": "center",
"yanchor": "top"
},
{
"source": "https://sdv.nyc3.cdn.digitaloceanspaces.com/assets/public/sha256/b9/b9631269a82abda39bd748afc82390679ca67bfc167bc3581665e237407de8e0.png",
"sizing": "contain",
"layer": "above",
"name": "sdvplot:axis:x:15",
"x": 4,
"y": 0,
"xref": "x",
"yref": "y domain",
"sizex": 16,
"sizey": 0.1,
"xanchor": "center",
"yanchor": "top"
},
{
"source": "https://sdv.nyc3.cdn.digitaloceanspaces.com/assets/public/sha256/5d/5de8d029f3602c97ffe02bd636eb2630e55d019d1a37cf819d3b9e39a219487b.png",
"sizing": "contain",
"layer": "above",
"name": "sdvplot:axis:x:20",
"x": 5,
"y": 0,
"xref": "x",
"yref": "y domain",
"sizex": 16,
"sizey": 0.1,
"xanchor": "center",
"yanchor": "top"
},
{
"source": "https://sdv.nyc3.cdn.digitaloceanspaces.com/assets/public/sha256/25/25fbb03e972ae872fa024026b73c7b63ef9f23c2f2c51f87d1614d800dcee6e7.png",
"sizing": "contain",
"layer": "above",
"name": "sdvplot:axis:x:13",
"x": 6,
"y": 0,
"xref": "x",
"yref": "y domain",
"sizex": 16,
"sizey": 0.1,
"xanchor": "center",
"yanchor": "top"
},
{
"source": "https://sdv.nyc3.cdn.digitaloceanspaces.com/assets/public/sha256/da/dac19aa9a573dcf9075e2422bee3e35a4955b9ab494bb3191ab3bacfd3609729.png",
"sizing": "contain",
"layer": "above",
"name": "sdvplot:axis:x:17",
"x": 7,
"y": 0,
"xref": "x",
"yref": "y domain",
"sizex": 16,
"sizey": 0.1,
"xanchor": "center",
"yanchor": "top"
}
]
}
}import { STANDINGS } from "@sportsdataverse/examples/data";
import { loadLeague } from "@sportsdataverse/sdvplot";
import { teamColorway, withAxisLogos } from "@sportsdataverse/sdvplot/plotly";
await loadLeague("nfl");
const teams = [...STANDINGS].sort((a, b) => b.wins - a.wins).map((s) => s.team);
const figure = {
data: [
{
type: "bar",
x: teams,
y: teams.map((t) => STANDINGS.find((s) => s.team === t)?.wins),
// one colour per bar; as `layout.colorway`, Plotly would cycle them per trace instead
marker: { color: teamColorway("nfl", teams) },
},
],
layout: { width: 560, height: 360 },
};
// The axis keeps its categories; their labels are hidden, not blanked, so hovering a bar names its team.
const logos = withAxisLogos(figure, "x", { league: "nfl", height: 0.1 });
// In the browser: Plotly.newPlot(div, data, layout), as this page does. Without JavaScript the page shows the figure.
export const browser = {
lib: "plotly",
figure: logos,
label: "2024 AFC wins by team, best first, each bar in its team's colour with the team's logo under it",
} as const;
logos;
With JavaScript on, plotly.js draws this figure here.
{
"data": [
{
"type": "bar",
"x": [
"KC",
"BUF",
"LAC",
"DEN",
"MIA",
"NYJ",
"LV",
"NE"
],
"y": [
15,
13,
11,
10,
8,
5,
4,
4
],
"marker": {
"color": "#e8e8e8"
}
}
],
"layout": {
"width": 560,
"height": 360,
"yaxis": {
"range": [
0,
18
]
},
"xaxis": {
"range": [
-0.8797086891009549,
7.879708689100955
],
"autorange": false
},
"images": [
{
"source": "https://a.espncdn.com/combiner/i?img=/i/headshots/nfl/players/full/3139477.png",
"x": 0,
"y": 16.7,
"xref": "x",
"yref": "y",
"sizex": 17.51883475640382,
"sizey": 2.5200000000000005,
"sizing": "contain",
"xanchor": "center",
"yanchor": "middle",
"opacity": 1,
"layer": "above",
"name": "sdvplot:headshot:3139477"
},
{
"source": "https://a.espncdn.com/combiner/i?img=/i/headshots/nfl/players/full/3918298.png",
"x": 1,
"y": 14.7,
"xref": "x",
"yref": "y",
"sizex": 17.51883475640382,
"sizey": 2.5200000000000005,
"sizing": "contain",
"xanchor": "center",
"yanchor": "middle",
"opacity": 1,
"layer": "above",
"name": "sdvplot:headshot:3918298"
},
{
"source": "https://a.espncdn.com/combiner/i?img=/i/headshots/nfl/players/full/4038941.png",
"x": 2,
"y": 12.7,
"xref": "x",
"yref": "y",
"sizex": 17.51883475640382,
"sizey": 2.5200000000000005,
"sizing": "contain",
"xanchor": "center",
"yanchor": "middle",
"opacity": 1,
"layer": "above",
"name": "sdvplot:headshot:4038941"
},
{
"source": "https://a.espncdn.com/combiner/i?img=/i/headshots/nfl/players/full/4426338.png",
"x": 3,
"y": 11.7,
"xref": "x",
"yref": "y",
"sizex": 17.51883475640382,
"sizey": 2.5200000000000005,
"sizing": "contain",
"xanchor": "center",
"yanchor": "middle",
"opacity": 1,
"layer": "above",
"name": "sdvplot:headshot:4426338"
},
{
"source": "https://a.espncdn.com/combiner/i?img=/i/headshots/nfl/players/full/4241479.png",
"x": 4,
"y": 9.7,
"xref": "x",
"yref": "y",
"sizex": 17.51883475640382,
"sizey": 2.5200000000000005,
"sizing": "contain",
"xanchor": "center",
"yanchor": "middle",
"opacity": 1,
"layer": "above",
"name": "sdvplot:headshot:4241479"
},
{
"source": "https://a.espncdn.com/combiner/i?img=/i/headshots/nfl/players/full/8439.png",
"x": 5,
"y": 6.7,
"xref": "x",
"yref": "y",
"sizex": 17.51883475640382,
"sizey": 2.5200000000000005,
"sizing": "contain",
"xanchor": "center",
"yanchor": "middle",
"opacity": 1,
"layer": "above",
"name": "sdvplot:headshot:8439"
},
{
"source": "https://a.espncdn.com/combiner/i?img=/i/headshots/nfl/players/full/4038524.png",
"x": 6,
"y": 5.7,
"xref": "x",
"yref": "y",
"sizex": 17.51883475640382,
"sizey": 2.5200000000000005,
"sizing": "contain",
"xanchor": "center",
"yanchor": "middle",
"opacity": 1,
"layer": "above",
"name": "sdvplot:headshot:4038524"
},
{
"source": "https://a.espncdn.com/combiner/i?img=/i/headshots/nfl/players/full/4431452.png",
"x": 7,
"y": 5.7,
"xref": "x",
"yref": "y",
"sizex": 17.51883475640382,
"sizey": 2.5200000000000005,
"sizing": "contain",
"xanchor": "center",
"yanchor": "middle",
"opacity": 1,
"layer": "above",
"name": "sdvplot:headshot:4431452"
},
{
"source": "https://sdv.nyc3.cdn.digitaloceanspaces.com/assets/public/sha256/16/16a2c7e0cd5dcf2e6bdc646c0b22bf03e575f505aa4b843a0ebfdaf19650257b.png",
"x": 0,
"y": 7.5,
"xref": "x",
"yref": "y",
"sizex": 17.51883475640382,
"sizey": 0.9,
"sizing": "contain",
"xanchor": "center",
"yanchor": "middle",
"opacity": 1,
"layer": "above",
"name": "sdvplot:wordmark:12"
},
{
"source": "https://sdv.nyc3.cdn.digitaloceanspaces.com/assets/public/sha256/e3/e363b5d81c20f22a336be435f5ce14d5c86829f956064ec9b85c332661d42ac1.png",
"x": 1,
"y": 6.5,
"xref": "x",
"yref": "y",
"sizex": 17.51883475640382,
"sizey": 0.9,
"sizing": "contain",
"xanchor": "center",
"yanchor": "middle",
"opacity": 1,
"layer": "above",
"name": "sdvplot:wordmark:2"
},
{
"source": "https://sdv.nyc3.cdn.digitaloceanspaces.com/assets/public/sha256/ad/ad87ef1e14816a93f9cf06e2f356c526902186abfc4c729e8e01088a84e98cb7.png",
"x": 2,
"y": 5.5,
"xref": "x",
"yref": "y",
"sizex": 17.51883475640382,
"sizey": 0.9,
"sizing": "contain",
"xanchor": "center",
"yanchor": "middle",
"opacity": 1,
"layer": "above",
"name": "sdvplot:wordmark:24"
},
{
"source": "https://sdv.nyc3.cdn.digitaloceanspaces.com/assets/public/sha256/dd/ddced0c3708afd6977d26e85c6efd4ddf3f045fb4527dd88e940e5952653875d.png",
"x": 3,
"y": 5,
"xref": "x",
"yref": "y",
"sizex": 17.51883475640382,
"sizey": 0.9,
"sizing": "contain",
"xanchor": "center",
"yanchor": "middle",
"opacity": 1,
"layer": "above",
"name": "sdvplot:wordmark:7"
},
{
"source": "https://sdv.nyc3.cdn.digitaloceanspaces.com/assets/public/sha256/cf/cf20b7c2f3fbdbd3d67e6067495c1eae5dfc1d5e22aadb40c574da8fd4abcba0.png",
"x": 4,
"y": 4,
"xref": "x",
"yref": "y",
"sizex": 17.51883475640382,
"sizey": 0.9,
"sizing": "contain",
"xanchor": "center",
"yanchor": "middle",
"opacity": 1,
"layer": "above",
"name": "sdvplot:wordmark:15"
},
{
"source": "https://sdv.nyc3.cdn.digitaloceanspaces.com/assets/public/sha256/82/8215dbe17aa824c97de83f1a27d744dc2140e9fe69b42dab5c422738e78e0866.png",
"x": 5,
"y": 2.5,
"xref": "x",
"yref": "y",
"sizex": 17.51883475640382,
"sizey": 0.9,
"sizing": "contain",
"xanchor": "center",
"yanchor": "middle",
"opacity": 1,
"layer": "above",
"name": "sdvplot:wordmark:20"
},
{
"source": "https://sdv.nyc3.cdn.digitaloceanspaces.com/assets/public/sha256/c2/c22afa5471cd8e08e9c42f06df3d90f6b88b1b95fc16b4da5e610737a8fe7dac.png",
"x": 6,
"y": 2,
"xref": "x",
"yref": "y",
"sizex": 17.51883475640382,
"sizey": 0.9,
"sizing": "contain",
"xanchor": "center",
"yanchor": "middle",
"opacity": 1,
"layer": "above",
"name": "sdvplot:wordmark:13"
},
{
"source": "https://sdv.nyc3.cdn.digitaloceanspaces.com/assets/public/sha256/d8/d898a07bacac59e21dc35f7ad3d77e1d6fe57d21dd7000aa9ae9617ea949c7c4.png",
"x": 7,
"y": 2,
"xref": "x",
"yref": "y",
"sizex": 17.51883475640382,
"sizey": 0.9,
"sizing": "contain",
"xanchor": "center",
"yanchor": "middle",
"opacity": 1,
"layer": "above",
"name": "sdvplot:wordmark:17"
}
]
}
}import { STANDINGS } from "@sportsdataverse/examples/data";
import { loadLeague } from "@sportsdataverse/sdvplot";
import { withHeadshots, withWordmarks } from "@sportsdataverse/sdvplot/plotly";
await loadLeague("nfl");
// Each image sits at a row's (x, y): the headshot just above the bar, the wordmark halfway up it.
const rows = [...STANDINGS]
.sort((a, b) => b.wins - a.wins)
.map((s) => ({ ...s, qb_y: s.wins + 1.7, mark_y: s.wins / 2 }));
const figure = {
data: [
{ type: "bar", x: rows.map((r) => r.team), y: rows.map((r) => r.wins), marker: { color: "#e8e8e8" } },
],
layout: { width: 560, height: 360, yaxis: { range: [0, 18] } },
};
const headshots = withHeadshots(figure, rows, {
league: "nfl",
x: "team",
y: "qb_y",
player: "qb_espn_id",
height: 0.14,
});
const both = withWordmarks(headshots, rows, {
league: "nfl",
x: "team",
y: "mark_y",
team: "team",
height: 0.05,
});
// In the browser: Plotly.newPlot(div, data, layout), as this page does; plotly.js loads each layout image itself.
// Without JavaScript the page shows the figure.
export const browser = {
lib: "plotly",
figure: both,
label:
"2024 AFC wins by team, each bar topped by its most frequent starting quarterback's headshot, the team's wordmark inside it",
} as const;
both;
Plotly: a whole league, with a hover point under each logo
Layout images take no hover, so this chart keeps an invisible marker under each of the 32 logos for the tooltip. A
descending axis range reverses the y axis, and withLogos places every image on it as Plotly draws it.
With JavaScript on, plotly.js draws this figure here.
{
"data": [
{
"type": "scatter",
"mode": "markers",
"x": [
0.06790171573137074,
0.04094521739130435,
0.1973075142071494,
0.18687425633802818,
-0.04087427192982456,
-0.07460389633601429,
0.10641586262265834,
-0.17549994982993197,
-0.08977916753022452,
0.009665256694367498,
0.15521151466666666,
0.08039756022408964,
-0.039517823421774935,
-0.047262845581395346,
-0.021526078048780487,
0.06882989234875446,
0.03389467877094972,
0.045015692528735635,
-0.1314928203971119,
-0.04019005496453901,
0.03697167798165138,
-0.07891574768946395,
-0.058756264814814815,
-0.09175358839528558,
0.00079420884289746,
0.11059268379096546,
-0.028511586486486485,
-0.015515602954755311,
0.060653246240601505,
0.13204109929078015,
-0.10142502022058823,
0.13941898861646235
],
"y": [
0.05063307196261683,
0.04434347766636281,
-0.01809329170383586,
-0.0031065497716894976,
0.1544453020920502,
-0.0011727630331753555,
0.05469574553571428,
0.01726238725023787,
0.06770862234533703,
-0.09817395888013998,
-0.045118736890524376,
-0.06768193482309125,
-0.054001859245630175,
0.006128741000877963,
0.1270993804255319,
0.005753912231559291,
0.0496864036199095,
-0.055508027726432534,
0.014218293738489872,
-0.021265579198473283,
-0.08892758666666667,
0.08302175858951175,
0.035566412629757786,
0.07050022532588454,
0.04588688751107175,
-0.08551938246628131,
-0.02094945614035088,
-0.01821089964157706,
0.051679759276879164,
0.016552372282608696,
0.04975268552253116,
0.04316543802423113
],
"text": [
"ARI",
"ATL",
"BAL",
"BUF",
"CAR",
"CHI",
"CIN",
"CLE",
"DAL",
"DEN",
"DET",
"GB",
"HOU",
"IND",
"JAX",
"KC",
"LA",
"LAC",
"LV",
"MIA",
"MIN",
"NE",
"NO",
"NYG",
"NYJ",
"PHI",
"PIT",
"SEA",
"SF",
"TB",
"TEN",
"WAS"
],
"hovertemplate": "%{text}<br>offence %{x:.3f}<br>defence %{y:.3f}<extra></extra>",
"marker": {
"size": 24,
"opacity": 0
}
}
],
"layout": {
"width": 600,
"height": 480,
"xaxis": {
"title": {
"text": "Offensive EPA per play"
},
"range": [
-0.25,
0.25
]
},
"yaxis": {
"title": {
"text": "Defensive EPA per play allowed"
},
"range": [
0.18,
-0.12
]
},
"images": [
{
"source": "https://sdv.nyc3.cdn.digitaloceanspaces.com/assets/public/sha256/f8/f870b58b43585a5b7717d578dd433b0b1e0c462d64ef4bea2aeb8c03f4ae7854.png",
"x": 0.06790171573137074,
"y": 0.05063307196261683,
"xref": "x",
"yref": "y",
"sizex": 1,
"sizey": 0.021,
"sizing": "contain",
"xanchor": "center",
"yanchor": "middle",
"opacity": 1,
"layer": "above",
"name": "sdvplot:logo:22"
},
{
"source": "https://sdv.nyc3.cdn.digitaloceanspaces.com/assets/public/sha256/fe/fe566e9da6f7986b2bac321571bd751247ff93af15c51ae555486fb1bee90c91.png",
"x": 0.04094521739130435,
"y": 0.04434347766636281,
"xref": "x",
"yref": "y",
"sizex": 1,
"sizey": 0.021,
"sizing": "contain",
"xanchor": "center",
"yanchor": "middle",
"opacity": 1,
"layer": "above",
"name": "sdvplot:logo:1"
},
{
"source": "https://sdv.nyc3.cdn.digitaloceanspaces.com/assets/public/sha256/67/67796023c172c9aeab5bcdac9204823d3590097246ad1cb92971134b5f26dcc0.png",
"x": 0.1973075142071494,
"y": -0.01809329170383586,
"xref": "x",
"yref": "y",
"sizex": 1,
"sizey": 0.021,
"sizing": "contain",
"xanchor": "center",
"yanchor": "middle",
"opacity": 1,
"layer": "above",
"name": "sdvplot:logo:33"
},
{
"source": "https://sdv.nyc3.cdn.digitaloceanspaces.com/assets/public/sha256/79/79b71e2f536ee29f9d23834e89828883af2d95bf6968cbd07a505444229cdd20.png",
"x": 0.18687425633802818,
"y": -0.0031065497716894976,
"xref": "x",
"yref": "y",
"sizex": 1,
"sizey": 0.021,
"sizing": "contain",
"xanchor": "center",
"yanchor": "middle",
"opacity": 1,
"layer": "above",
"name": "sdvplot:logo:2"
},
{
"source": "https://sdv.nyc3.cdn.digitaloceanspaces.com/assets/public/sha256/2c/2cebc1bbdcfd89f28c1578b397f440d229d93fa9a30bfa1193cc286482c82298.png",
"x": -0.04087427192982456,
"y": 0.1544453020920502,
"xref": "x",
"yref": "y",
"sizex": 1,
"sizey": 0.021,
"sizing": "contain",
"xanchor": "center",
"yanchor": "middle",
"opacity": 1,
"layer": "above",
"name": "sdvplot:logo:29"
},
{
"source": "https://sdv.nyc3.cdn.digitaloceanspaces.com/assets/public/sha256/fd/fd7b5a207b9ad443f950b384d43dba6cb367c5737ad50a51a15e3893fa31753b.png",
"x": -0.07460389633601429,
"y": -0.0011727630331753555,
"xref": "x",
"yref": "y",
"sizex": 1,
"sizey": 0.021,
"sizing": "contain",
"xanchor": "center",
"yanchor": "middle",
"opacity": 1,
"layer": "above",
"name": "sdvplot:logo:3"
},
{
"source": "https://sdv.nyc3.cdn.digitaloceanspaces.com/assets/public/sha256/8e/8e549c0ecac92453140370b2aef3e4a140139b50d16e92507532e8ee49930d4e.png",
"x": 0.10641586262265834,
"y": 0.05469574553571428,
"xref": "x",
"yref": "y",
"sizex": 1,
"sizey": 0.021,
"sizing": "contain",
"xanchor": "center",
"yanchor": "middle",
"opacity": 1,
"layer": "above",
"name": "sdvplot:logo:4"
},
{
"source": "https://sdv.nyc3.cdn.digitaloceanspaces.com/assets/public/sha256/9b/9b286fc4286dc39b1b5a6a08aab042b456ba5fdce99e45e49a4d1e2672411fa2.png",
"x": -0.17549994982993197,
"y": 0.01726238725023787,
"xref": "x",
"yref": "y",
"sizex": 1,
"sizey": 0.021,
"sizing": "contain",
"xanchor": "center",
"yanchor": "middle",
"opacity": 1,
"layer": "above",
"name": "sdvplot:logo:5"
},
{
"source": "https://sdv.nyc3.cdn.digitaloceanspaces.com/assets/public/sha256/f4/f4ea9a2ec7d7d500f08db94c8c6f1b23f75361ffe39c4d4b480a684a511ce61e.png",
"x": -0.08977916753022452,
"y": 0.06770862234533703,
"xref": "x",
"yref": "y",
"sizex": 1,
"sizey": 0.021,
"sizing": "contain",
"xanchor": "center",
"yanchor": "middle",
"opacity": 1,
"layer": "above",
"name": "sdvplot:logo:6"
},
{
"source": "https://sdv.nyc3.cdn.digitaloceanspaces.com/assets/public/sha256/c9/c98bec2be32e27b19f79f5da86ac6ef133c78d75ab78aadb28ef36696c3213e8.png",
"x": 0.009665256694367498,
"y": -0.09817395888013998,
"xref": "x",
"yref": "y",
"sizex": 1,
"sizey": 0.021,
"sizing": "contain",
"xanchor": "center",
"yanchor": "middle",
"opacity": 1,
"layer": "above",
"name": "sdvplot:logo:7"
},
{
"source": "https://sdv.nyc3.cdn.digitaloceanspaces.com/assets/public/sha256/97/9776b567ebab0bd3640165e915d948d8e736e35967dfc19ce62b967cfe7b4eb1.png",
"x": 0.15521151466666666,
"y": -0.045118736890524376,
"xref": "x",
"yref": "y",
"sizex": 1,
"sizey": 0.021,
"sizing": "contain",
"xanchor": "center",
"yanchor": "middle",
"opacity": 1,
"layer": "above",
"name": "sdvplot:logo:8"
},
{
"source": "https://sdv.nyc3.cdn.digitaloceanspaces.com/assets/public/sha256/7e/7ea8154cdbff5db84d248f235c1f1c78a1a8b4cb8c14335b5886ec10ee20b00e.png",
"x": 0.08039756022408964,
"y": -0.06768193482309125,
"xref": "x",
"yref": "y",
"sizex": 1,
"sizey": 0.021,
"sizing": "contain",
"xanchor": "center",
"yanchor": "middle",
"opacity": 1,
"layer": "above",
"name": "sdvplot:logo:9"
},
{
"source": "https://sdv.nyc3.cdn.digitaloceanspaces.com/assets/public/sha256/54/54b0fe559761860e4953851c197d995b2f9c0c4e278164edda7b608ffd64f39b.png",
"x": -0.039517823421774935,
"y": -0.054001859245630175,
"xref": "x",
"yref": "y",
"sizex": 1,
"sizey": 0.021,
"sizing": "contain",
"xanchor": "center",
"yanchor": "middle",
"opacity": 1,
"layer": "above",
"name": "sdvplot:logo:34"
},
{
"source": "https://sdv.nyc3.cdn.digitaloceanspaces.com/assets/public/sha256/f1/f1f5222a876f810956aa8d4e75d4e6b47bcb3a6d11f876642b674010233c8a97.png",
"x": -0.047262845581395346,
"y": 0.006128741000877963,
"xref": "x",
"yref": "y",
"sizex": 1,
"sizey": 0.021,
"sizing": "contain",
"xanchor": "center",
"yanchor": "middle",
"opacity": 1,
"layer": "above",
"name": "sdvplot:logo:11"
},
{
"source": "https://sdv.nyc3.cdn.digitaloceanspaces.com/assets/public/sha256/15/15cb9a1c77ebfaea885d0d3a34f33dabc4e6a00eb7d5462148fceed641d49397.png",
"x": -0.021526078048780487,
"y": 0.1270993804255319,
"xref": "x",
"yref": "y",
"sizex": 1,
"sizey": 0.021,
"sizing": "contain",
"xanchor": "center",
"yanchor": "middle",
"opacity": 1,
"layer": "above",
"name": "sdvplot:logo:30"
},
{
"source": "https://sdv.nyc3.cdn.digitaloceanspaces.com/assets/public/sha256/3d/3d77958dc6373768919bb2681cbe1b143f56c07a1f013460def665a5026a7f3d.png",
"x": 0.06882989234875446,
"y": 0.005753912231559291,
"xref": "x",
"yref": "y",
"sizex": 1,
"sizey": 0.021,
"sizing": "contain",
"xanchor": "center",
"yanchor": "middle",
"opacity": 1,
"layer": "above",
"name": "sdvplot:logo:12"
},
{
"source": "https://sdv.nyc3.cdn.digitaloceanspaces.com/assets/public/sha256/e2/e289b738a5e1259cccea921bee02eec881d35b4241761a8c4e12fe5964cab75c.png",
"x": 0.03389467877094972,
"y": 0.0496864036199095,
"xref": "x",
"yref": "y",
"sizex": 1,
"sizey": 0.021,
"sizing": "contain",
"xanchor": "center",
"yanchor": "middle",
"opacity": 1,
"layer": "above",
"name": "sdvplot:logo:14"
},
{
"source": "https://sdv.nyc3.cdn.digitaloceanspaces.com/assets/public/sha256/54/5400f85bd93129c056717a771da57a97225e15f39c0022107ce89ef993f15bb0.png",
"x": 0.045015692528735635,
"y": -0.055508027726432534,
"xref": "x",
"yref": "y",
"sizex": 1,
"sizey": 0.021,
"sizing": "contain",
"xanchor": "center",
"yanchor": "middle",
"opacity": 1,
"layer": "above",
"name": "sdvplot:logo:24"
},
{
"source": "https://sdv.nyc3.cdn.digitaloceanspaces.com/assets/public/sha256/25/25fbb03e972ae872fa024026b73c7b63ef9f23c2f2c51f87d1614d800dcee6e7.png",
"x": -0.1314928203971119,
"y": 0.014218293738489872,
"xref": "x",
"yref": "y",
"sizex": 1,
"sizey": 0.021,
"sizing": "contain",
"xanchor": "center",
"yanchor": "middle",
"opacity": 1,
"layer": "above",
"name": "sdvplot:logo:13"
},
{
"source": "https://sdv.nyc3.cdn.digitaloceanspaces.com/assets/public/sha256/b9/b9631269a82abda39bd748afc82390679ca67bfc167bc3581665e237407de8e0.png",
"x": -0.04019005496453901,
"y": -0.021265579198473283,
"xref": "x",
"yref": "y",
"sizex": 1,
"sizey": 0.021,
"sizing": "contain",
"xanchor": "center",
"yanchor": "middle",
"opacity": 1,
"layer": "above",
"name": "sdvplot:logo:15"
},
{
"source": "https://sdv.nyc3.cdn.digitaloceanspaces.com/assets/public/sha256/72/72759956b149bc1c9496ea6ebb9a6ae34093dfed371cc6abb425c13f14f53448.png",
"x": 0.03697167798165138,
"y": -0.08892758666666667,
"xref": "x",
"yref": "y",
"sizex": 1,
"sizey": 0.021,
"sizing": "contain",
"xanchor": "center",
"yanchor": "middle",
"opacity": 1,
"layer": "above",
"name": "sdvplot:logo:16"
},
{
"source": "https://sdv.nyc3.cdn.digitaloceanspaces.com/assets/public/sha256/da/dac19aa9a573dcf9075e2422bee3e35a4955b9ab494bb3191ab3bacfd3609729.png",
"x": -0.07891574768946395,
"y": 0.08302175858951175,
"xref": "x",
"yref": "y",
"sizex": 1,
"sizey": 0.021,
"sizing": "contain",
"xanchor": "center",
"yanchor": "middle",
"opacity": 1,
"layer": "above",
"name": "sdvplot:logo:17"
},
{
"source": "https://sdv.nyc3.cdn.digitaloceanspaces.com/assets/public/sha256/68/68ddfdf1d7ee8317f1af083a0c6d59142a11e8af4dad73c3c50bbafc338e0a94.png",
"x": -0.058756264814814815,
"y": 0.035566412629757786,
"xref": "x",
"yref": "y",
"sizex": 1,
"sizey": 0.021,
"sizing": "contain",
"xanchor": "center",
"yanchor": "middle",
"opacity": 1,
"layer": "above",
"name": "sdvplot:logo:18"
},
{
"source": "https://sdv.nyc3.cdn.digitaloceanspaces.com/assets/public/sha256/62/62e361850e7ba3a50dfd09cbb38429e994d1c23b0f999c74421f10e37c7067e7.png",
"x": -0.09175358839528558,
"y": 0.07050022532588454,
"xref": "x",
"yref": "y",
"sizex": 1,
"sizey": 0.021,
"sizing": "contain",
"xanchor": "center",
"yanchor": "middle",
"opacity": 1,
"layer": "above",
"name": "sdvplot:logo:19"
},
{
"source": "https://sdv.nyc3.cdn.digitaloceanspaces.com/assets/public/sha256/5d/5de8d029f3602c97ffe02bd636eb2630e55d019d1a37cf819d3b9e39a219487b.png",
"x": 0.00079420884289746,
"y": 0.04588688751107175,
"xref": "x",
"yref": "y",
"sizex": 1,
"sizey": 0.021,
"sizing": "contain",
"xanchor": "center",
"yanchor": "middle",
"opacity": 1,
"layer": "above",
"name": "sdvplot:logo:20"
},
{
"source": "https://sdv.nyc3.cdn.digitaloceanspaces.com/assets/public/sha256/28/2875f50f8b756ed5ea3866105b4683f5c603aa542e2c7f7870287e1d3d006100.png",
"x": 0.11059268379096546,
"y": -0.08551938246628131,
"xref": "x",
"yref": "y",
"sizex": 1,
"sizey": 0.021,
"sizing": "contain",
"xanchor": "center",
"yanchor": "middle",
"opacity": 1,
"layer": "above",
"name": "sdvplot:logo:21"
},
{
"source": "https://sdv.nyc3.cdn.digitaloceanspaces.com/assets/public/sha256/80/806bcb72e75ed184a99bea34df458e6cd87145fc9a863945f3356b01122aeb6f.png",
"x": -0.028511586486486485,
"y": -0.02094945614035088,
"xref": "x",
"yref": "y",
"sizex": 1,
"sizey": 0.021,
"sizing": "contain",
"xanchor": "center",
"yanchor": "middle",
"opacity": 1,
"layer": "above",
"name": "sdvplot:logo:23"
},
{
"source": "https://sdv.nyc3.cdn.digitaloceanspaces.com/assets/public/sha256/25/2546b1d4fe5cf6c2d75cda53d3fb56bbc4dd43c6b00200586dab5a85df8fa492.png",
"x": -0.015515602954755311,
"y": -0.01821089964157706,
"xref": "x",
"yref": "y",
"sizex": 1,
"sizey": 0.021,
"sizing": "contain",
"xanchor": "center",
"yanchor": "middle",
"opacity": 1,
"layer": "above",
"name": "sdvplot:logo:26"
},
{
"source": "https://sdv.nyc3.cdn.digitaloceanspaces.com/assets/public/sha256/82/82ae812f6c15718ce5abdd402863e8b4553fa9971e4baa5d45ff585c52948a45.png",
"x": 0.060653246240601505,
"y": 0.051679759276879164,
"xref": "x",
"yref": "y",
"sizex": 1,
"sizey": 0.021,
"sizing": "contain",
"xanchor": "center",
"yanchor": "middle",
"opacity": 1,
"layer": "above",
"name": "sdvplot:logo:25"
},
{
"source": "https://sdv.nyc3.cdn.digitaloceanspaces.com/assets/public/sha256/bc/bcc5d267024650938c9f35558077fe00a31913b89c61768d4ebb63d1ea31fbfd.png",
"x": 0.13204109929078015,
"y": 0.016552372282608696,
"xref": "x",
"yref": "y",
"sizex": 1,
"sizey": 0.021,
"sizing": "contain",
"xanchor": "center",
"yanchor": "middle",
"opacity": 1,
"layer": "above",
"name": "sdvplot:logo:27"
},
{
"source": "https://sdv.nyc3.cdn.digitaloceanspaces.com/assets/public/sha256/27/27cf283fca5b2e9e1c5e995a465a54223424c157d2013b603a6491f99333cf45.png",
"x": -0.10142502022058823,
"y": 0.04975268552253116,
"xref": "x",
"yref": "y",
"sizex": 1,
"sizey": 0.021,
"sizing": "contain",
"xanchor": "center",
"yanchor": "middle",
"opacity": 1,
"layer": "above",
"name": "sdvplot:logo:10"
},
{
"source": "https://sdv.nyc3.cdn.digitaloceanspaces.com/assets/public/sha256/2f/2f67805ef9e385a4c67adb0a9320706bd481a52e3aa0e0faea995dad2b501112.png",
"x": 0.13941898861646235,
"y": 0.04316543802423113,
"xref": "x",
"yref": "y",
"sizex": 1,
"sizey": 0.021,
"sizing": "contain",
"xanchor": "center",
"yanchor": "middle",
"opacity": 1,
"layer": "above",
"name": "sdvplot:logo:28"
}
]
}
}import { NFL_TEAM_EPA_2024 } from "@sportsdataverse/examples/data";
import { loadLeague } from "@sportsdataverse/sdvplot";
import { withLogos } from "@sportsdataverse/sdvplot/plotly";
await loadLeague("nfl");
const rows = NFL_TEAM_EPA_2024.map((t) => ({
team: t.team,
off: t.off_epa / t.off_plays,
def: t.def_epa / t.def_plays,
}));
const figure = {
data: [
{
type: "scatter",
mode: "markers",
x: rows.map((r) => r.off),
y: rows.map((r) => r.def),
text: rows.map((r) => r.team),
hovertemplate: "%{text}<br>offence %{x:.3f}<br>defence %{y:.3f}<extra></extra>",
// invisible markers under the logos: layout images take no hover, the trace does
marker: { size: 24, opacity: 0 },
},
],
layout: {
width: 600,
height: 480,
xaxis: { title: { text: "Offensive EPA per play" }, range: [-0.25, 0.25] },
// a descending range reverses the axis: a good defence allows negative EPA, so the best teams sit top right
yaxis: { title: { text: "Defensive EPA per play allowed" }, range: [0.18, -0.12] },
},
};
// The images follow the axes as Plotly draws them, the reversed y axis included.
const logos = withLogos(figure, rows, { league: "nfl", x: "off", y: "def", team: "team", height: 0.07 });
// In the browser: Plotly.newPlot(div, data, layout), as this page does. Without JavaScript the page shows the figure.
export const browser = {
lib: "plotly",
figure: logos,
label:
"Every NFL team's 2024 offensive EPA per play against defensive EPA per play allowed, each team drawn as its logo, the best teams top right",
} as const;
logos;
Plotly: logos down the y axis
withAxisLogos(figure, "y", …) puts the logos left of a horizontal bar chart, one per category band, and grows the
left margin to fit them. stats.nba.com names teams by nickname, and sdvplot resolves "Clippers" as it would "LAC".
With JavaScript on, plotly.js draws this figure here.
{
"data": [
{
"type": "bar",
"orientation": "h",
"y": [
"Clippers",
"Suns",
"Lakers",
"Kings",
"Warriors"
],
"x": [
51,
49,
47,
46,
46
],
"marker": {
"color": [
"#12173f",
"#29127a",
"#552583",
"#5a2d81",
"#fdb927"
]
}
}
],
"layout": {
"width": 560,
"height": 360,
"xaxis": {
"title": {
"text": "Wins (2023-24)"
}
},
"yaxis": {
"range": [
4.5,
-0.5
],
"tickmode": "array",
"showticklabels": false,
"tickvals": [
"Clippers",
"Suns",
"Lakers",
"Kings",
"Warriors"
],
"ticktext": [
"Clippers",
"Suns",
"Lakers",
"Kings",
"Warriors"
]
},
"margin": {
"l": 107
},
"images": [
{
"source": "https://sdv.nyc3.cdn.digitaloceanspaces.com/assets/public/sha256/d4/d43253c0630f0d3d3a4c3c50ce92c9dbe8d5bf21e3bfd5326920c9487b26dbec.png",
"sizing": "contain",
"layer": "above",
"name": "sdvplot:axis:y:12",
"x": 0,
"y": 0,
"xref": "paper",
"yref": "y",
"sizex": 1,
"sizey": 0.75,
"xanchor": "right",
"yanchor": "middle"
},
{
"source": "https://sdv.nyc3.cdn.digitaloceanspaces.com/assets/public/sha256/5f/5f8932efdb7595069610b11ec2f417d78a9213691c2ba21e2bd491c172397ead.png",
"sizing": "contain",
"layer": "above",
"name": "sdvplot:axis:y:21",
"x": 0,
"y": 1,
"xref": "paper",
"yref": "y",
"sizex": 1,
"sizey": 0.75,
"xanchor": "right",
"yanchor": "middle"
},
{
"source": "https://sdv.nyc3.cdn.digitaloceanspaces.com/assets/public/sha256/82/82fda3df51058c5aad8a52123868f43959017be0c39c886e9aefbb77e6c69c89.png",
"sizing": "contain",
"layer": "above",
"name": "sdvplot:axis:y:13",
"x": 0,
"y": 2,
"xref": "paper",
"yref": "y",
"sizex": 1,
"sizey": 0.75,
"xanchor": "right",
"yanchor": "middle"
},
{
"source": "https://sdv.nyc3.cdn.digitaloceanspaces.com/assets/public/sha256/38/38728eaaf98d73d00363ee570601d64a4caa7fe2590fb0df5afb1b9129d6e20b.png",
"sizing": "contain",
"layer": "above",
"name": "sdvplot:axis:y:23",
"x": 0,
"y": 3,
"xref": "paper",
"yref": "y",
"sizex": 1,
"sizey": 0.75,
"xanchor": "right",
"yanchor": "middle"
},
{
"source": "https://sdv.nyc3.cdn.digitaloceanspaces.com/assets/public/sha256/b6/b623681ece167224ad4f51aef1ffa5a6f9e072af1cf1d6a6c158fd796e8d33ab.png",
"sizing": "contain",
"layer": "above",
"name": "sdvplot:axis:y:9",
"x": 0,
"y": 4,
"xref": "paper",
"yref": "y",
"sizex": 1,
"sizey": 0.75,
"xanchor": "right",
"yanchor": "middle"
}
]
}
}import { NBA_STANDINGS } from "@sportsdataverse/examples/data";
import { loadLeague } from "@sportsdataverse/sdvplot";
import { teamColorway, withAxisLogos } from "@sportsdataverse/sdvplot/plotly";
await loadLeague("nba");
// stats.nba.com's TeamName is the nickname ("Clippers"): sdvplot resolves it like an abbreviation.
const teams = NBA_STANDINGS.map((s) => s.team);
const figure = {
data: [
{
type: "bar",
orientation: "h",
y: teams,
x: NBA_STANDINGS.map((s) => s.wins),
marker: { color: teamColorway("nba", teams) },
},
],
layout: {
width: 560,
height: 360,
xaxis: { title: { text: "Wins (2023-24)" } },
// first place on top: a category axis runs bottom to top, so give it a descending range (categories are 0, 1, ...)
yaxis: { range: [teams.length - 0.5, -0.5] },
},
};
// On the y axis the logos sit left of the plot, one per category band; the tick labels are hidden, not blanked.
const logos = withAxisLogos(figure, "y", { league: "nba", height: 0.15 });
// In the browser: Plotly.newPlot(div, data, layout), as this page does. Without JavaScript the page shows the figure.
export const browser = {
lib: "plotly",
figure: logos,
label:
"The 2023-24 Pacific Division's wins, first place on top: the Clippers, Suns, Lakers, Kings and Warriors, each bar in its team's colour with its logo beside it",
} as const;
logos;
Vega-Lite
The same verbs add a native image layer to a Vega-Lite spec; compile it and render it as usual. logoLayer is that
layer on its own, to drop into a layered spec of your own, and teamColorScale gives a colour scale of team colours.
import { STANDINGS } from "@sportsdataverse/examples/data";
import { loadLeague } from "@sportsdataverse/sdvplot";
import { withLogos } from "@sportsdataverse/sdvplot/vega";
import * as vega from "vega";
import { type TopLevelSpec, compile } from "vega-lite";
await loadLeague("nfl");
const rows = STANDINGS.map((s) => ({ ...s }));
const chart: TopLevelSpec = {
width: 480,
height: 320,
data: { values: rows },
mark: { type: "point", opacity: 0 },
encoding: {
x: { field: "pf", type: "quantitative", scale: { domain: [260, 560] }, title: "Points for" },
y: { field: "pa", type: "quantitative", scale: { domain: [280, 460] }, title: "Points against" },
},
};
const spec = withLogos(chart, rows, { league: "nfl", x: "pf", y: "pa", team: "team", height: 0.12 });
// In the browser: vegaEmbed(div, spec, { renderer: "svg" }), as this page does (the SVG names each mark, "KC logo").
export const browser = { lib: "vega", spec } as const;
// Headless Vega: no DOM and no canvas, so the logos stay <image href> links the browser loads when it shows the SVG.
const view = new vega.View(vega.parse(compile(browser.spec).spec), { renderer: "none" });
await view.toSVG();
import { STANDINGS } from "@sportsdataverse/examples/data";
import { loadLeague } from "@sportsdataverse/sdvplot";
import { logoLayer } from "@sportsdataverse/sdvplot/vega";
import * as vega from "vega";
import { type TopLevelSpec, compile } from "vega-lite";
await loadLeague("nfl");
const rows = STANDINGS.map((s) => ({ ...s }));
// the dashed lines: these 8 teams' mean points for and against
const mean = (k: "pf" | "pa") => rows.reduce((t, r) => t + r[k], 0) / rows.length;
const height = 320;
// logoLayer is one image layer (its fields are "x" and "y") to drop into any layer list; the scales are shared.
const spec: TopLevelSpec = {
width: 480,
height,
layer: [
{
data: { values: [{ pf: mean("pf"), pa: mean("pa") }] },
mark: { type: "rule", strokeDash: [4, 4], color: "#888" },
encoding: {
x: { field: "pf", type: "quantitative", title: "Points for", scale: { domain: [260, 560] } },
},
},
{
data: { values: [{ pf: mean("pf"), pa: mean("pa") }] },
mark: { type: "rule", strokeDash: [4, 4], color: "#888" },
encoding: {
y: { field: "pa", type: "quantitative", title: "Points against", scale: { domain: [280, 460] } },
},
},
logoLayer(rows, { league: "nfl", x: "pf", y: "pa", team: "team", height: 0.12, chartHeight: height }),
],
};
// In the browser: vegaEmbed(div, spec, { renderer: "svg" }), as this page does (the SVG names each mark, "KC logo").
export const browser = { lib: "vega", spec } as const;
const view = new vega.View(vega.parse(compile(browser.spec).spec), { renderer: "none" });
await view.toSVG();
import { STANDINGS } from "@sportsdataverse/examples/data";
import { loadLeague } from "@sportsdataverse/sdvplot";
import { teamColorScale, withAxisLogos } from "@sportsdataverse/sdvplot/vega";
import * as vega from "vega";
import { type TopLevelSpec, compile } from "vega-lite";
await loadLeague("nfl");
const rows = STANDINGS.map((s) => ({ ...s }));
const teams = [...rows].sort((a, b) => b.wins - a.wins).map((r) => r.team);
const chart: TopLevelSpec = {
width: 480,
height: 300,
data: { values: rows },
mark: "bar",
encoding: {
x: { field: "team", type: "nominal", sort: teams, title: null },
y: { field: "wins", type: "quantitative", title: "Wins (2024)" },
color: { field: "team", type: "nominal", scale: teamColorScale("nfl", teams), legend: null },
},
};
// The axis keeps its categories: their labels are blanked and an image layer draws a logo under each tick.
const spec = withAxisLogos(chart, "x", { league: "nfl", height: 0.1 });
// In the browser: vegaEmbed(div, spec, { renderer: "svg" }), as this page does (the SVG names each mark, "KC logo").
export const browser = { lib: "vega", spec } as const;
// Headless Vega: no DOM and no canvas, so the logos stay <image href> links the browser loads when it shows the SVG.
const view = new vega.View(vega.parse(compile(browser.spec).spec), { renderer: "none" });
await view.toSVG();
import { STANDINGS } from "@sportsdataverse/examples/data";
import { loadLeague } from "@sportsdataverse/sdvplot";
import { withHeadshots, withWordmarks } from "@sportsdataverse/sdvplot/vega";
import * as vega from "vega";
import { type TopLevelSpec, compile } from "vega-lite";
await loadLeague("nfl");
// Each image sits at a row's (x, y): the headshot just above the bar, the wordmark halfway up it.
const rows = [...STANDINGS]
.sort((a, b) => b.wins - a.wins)
.map((s) => ({ ...s, qb_y: s.wins + 1.7, mark_y: s.wins / 2 }));
const chart: TopLevelSpec = {
width: 480,
height: 300,
data: { values: rows },
mark: { type: "bar", color: "#e8e8e8" },
encoding: {
x: { field: "team", type: "nominal", sort: null, axis: null },
y: { field: "wins", type: "quantitative", scale: { domain: [0, 18] }, title: "Wins (2024)" },
},
};
const headshots = withHeadshots(chart, rows, {
league: "nfl",
x: "team",
y: "qb_y",
player: "qb_espn_id",
height: 0.14,
});
const spec = withWordmarks(headshots, rows, {
league: "nfl",
x: "team",
y: "mark_y",
team: "team",
height: 0.05,
});
// In the browser: vegaEmbed(div, spec, { renderer: "svg" }), as this page does (the SVG names each mark, "KC logo").
export const browser = { lib: "vega", spec } as const;
const view = new vega.View(vega.parse(compile(browser.spec).spec), { renderer: "none" });
await view.toSVG();
Vega-Lite: logos down the y axis
On a nominal y axis, withAxisLogos blanks the labels and draws a logo left of each band, here for the NHL's Atlantic
Division in team colours.
import { NHL_STANDINGS } from "@sportsdataverse/examples/data";
import { loadLeague } from "@sportsdataverse/sdvplot";
import { teamColorScale, withAxisLogos } from "@sportsdataverse/sdvplot/vega";
import * as vega from "vega";
import { type TopLevelSpec, compile } from "vega-lite";
await loadLeague("nhl");
const rows = NHL_STANDINGS.map((s) => ({ ...s }));
const teams = rows.map((r) => r.team); // already in division order, first to eighth
const chart: TopLevelSpec = {
width: 440,
height: 340,
data: { values: rows },
mark: "bar",
encoding: {
y: { field: "team", type: "nominal", sort: teams, title: null },
x: { field: "points", type: "quantitative", title: "Points (2025-26)" },
color: { field: "team", type: "nominal", scale: teamColorScale("nhl", teams), legend: null },
},
};
// The y axis works like the x one: labels blanked, a logo left of each band. `height` is a fraction of the plot height.
const spec = withAxisLogos(chart, "y", { league: "nhl", height: 0.09 });
// In the browser: vegaEmbed(div, spec, { renderer: "svg" }), as this page does (the SVG names each mark, "BUF logo").
export const browser = { lib: "vega", spec } as const;
const view = new vega.View(vega.parse(compile(browser.spec).spec), { renderer: "none" });
await view.toSVG();
Vega-Lite: a whole league in a layered spec
withLogos also takes a layered spec: it reads the x and y encodings from the layer that has them, reversed scale
included, and adds its image layer beside the reference rules and a transparent hover layer.
import { NFL_TEAM_EPA_2024 } from "@sportsdataverse/examples/data";
import { loadLeague } from "@sportsdataverse/sdvplot";
import { withLogos } from "@sportsdataverse/sdvplot/vega";
import * as vega from "vega";
import { type TopLevelSpec, compile } from "vega-lite";
await loadLeague("nfl");
const rows = NFL_TEAM_EPA_2024.map((t) => ({
team: t.team,
off: t.off_epa / t.off_plays,
def: t.def_epa / t.def_plays,
}));
const chart: TopLevelSpec = {
width: 520,
height: 420,
data: { values: rows },
layer: [
// a hairline at zero on each axis: an average offence and an average defence
{ mark: { type: "rule", color: "#999" }, encoding: { x: { datum: 0, type: "quantitative" } } },
{ mark: { type: "rule", color: "#999" }, encoding: { y: { datum: 0, type: "quantitative" } } },
{
// the hover target: sdvplot's logo layer never shows a tooltip, so a transparent point under each logo does
mark: { type: "point", opacity: 0, size: 500 },
encoding: {
x: {
field: "off",
type: "quantitative",
title: "Offensive EPA per play",
scale: { domain: [-0.22, 0.22] },
},
// reversed: a good defence allows negative EPA, so the best teams sit top right
y: {
field: "def",
type: "quantitative",
title: "Defensive EPA per play allowed",
scale: { domain: [-0.12, 0.17], reverse: true },
},
tooltip: [{ field: "team" }, { field: "off", format: ".3f" }, { field: "def", format: ".3f" }],
},
},
],
};
// withLogos adds its image layer to the layered spec; it reads the x and y encodings, reversed scale and all.
const spec = withLogos(chart, rows, { league: "nfl", x: "off", y: "def", team: "team", height: 0.07 });
// In the browser: vegaEmbed(div, spec, { renderer: "svg" }), as this page does (the SVG names each mark, "KC logo").
export const browser = { lib: "vega", spec } as const;
const view = new vega.View(vega.parse(compile(browser.spec).spec), { renderer: "none" });
await view.toSVG();
ECharts
For ECharts the verbs add a custom series that draws the images, sized from the grid at render time; axis logos
become rich-text labels and teamColorPalette fills option.color. Server-side rendering to an SVG string works as
in any ECharts project.
import { STANDINGS } from "@sportsdataverse/examples/data";
import { loadLeague } from "@sportsdataverse/sdvplot";
import { withLogos } from "@sportsdataverse/sdvplot/echarts";
import * as echarts from "echarts";
await loadLeague("nfl");
const rows = STANDINGS.map((s) => ({ ...s }));
const option: echarts.EChartsOption = {
xAxis: { type: "value", name: "Points for", nameLocation: "middle", nameGap: 28, min: 260, max: 550 },
yAxis: { type: "value", name: "Points against", nameLocation: "middle", nameGap: 40, min: 270, max: 450 },
series: [{ type: "scatter", data: rows.map((r) => [r.pf, r.pa]), symbolSize: 0 }],
};
// In the browser: echarts.init(div, null, { width, height }) and setOption(option), as this page does.
export const browser = {
lib: "echarts",
option: withLogos(option, rows, { league: "nfl", x: "pf", y: "pa", team: "team", height: 0.12 }),
width: 560,
height: 380,
label: "2024 AFC points for against points against, each team drawn as its logo",
} as const;
// SSR: no DOM, no canvas. The logos are <image href> links the browser loads when it shows the SVG.
const { width, height } = browser;
const chart = echarts.init(null, null, { renderer: "svg", ssr: true, width, height });
chart.setOption(browser.option);
const svg = chart.renderToSVGString();
chart.dispose();
svg;
import { STANDINGS } from "@sportsdataverse/examples/data";
import { loadLeague } from "@sportsdataverse/sdvplot";
import { teamColorPalette, withAxisLogos } from "@sportsdataverse/sdvplot/echarts";
import * as echarts from "echarts";
await loadLeague("nfl");
const teams = [...STANDINGS].sort((a, b) => b.wins - a.wins);
const names = teams.map((t) => t.team);
const option: echarts.EChartsOption = {
tooltip: {}, // in the browser, hovering a bar shows its team and wins
color: teamColorPalette("nfl", names), // with colorBy "data", one colour per bar
xAxis: { type: "category", data: names },
yAxis: { type: "value", name: "Wins (2024)" },
series: [{ type: "bar", colorBy: "data", data: teams.map((t) => t.wins) }],
};
// In the browser: echarts.init(div, null, { width, height }) and setOption(option), as this page does.
export const browser = {
lib: "echarts",
option: withAxisLogos(option, "x", { league: "nfl", height: 0.1, chartHeight: 360 }),
width: 560,
height: 360,
label: "2024 AFC wins by team, best first, each bar in its team's colour with the team's logo under it",
} as const;
// SSR: no DOM, no canvas. The logos are <image href> links the browser loads when it shows the SVG.
const { width, height } = browser;
const chart = echarts.init(null, null, { renderer: "svg", ssr: true, width, height });
chart.setOption(browser.option);
const svg = chart.renderToSVGString();
chart.dispose();
svg;
import { STANDINGS } from "@sportsdataverse/examples/data";
import { loadLeague } from "@sportsdataverse/sdvplot";
import { withHeadshots, withWordmarks } from "@sportsdataverse/sdvplot/echarts";
import * as echarts from "echarts";
await loadLeague("nfl");
// Each image sits at a row's (x, y): the headshot just above the bar, the wordmark halfway up it.
const rows = [...STANDINGS]
.sort((a, b) => b.wins - a.wins)
.map((s) => ({ ...s, qb_y: s.wins + 1.7, mark_y: s.wins / 2 }));
const option: echarts.EChartsOption = {
tooltip: {}, // in the browser, hovering a bar shows its team and wins
xAxis: { type: "category", data: rows.map((r) => r.team), axisLabel: { show: false } },
yAxis: { type: "value", name: "Wins (2024)", max: 18 },
series: [{ type: "bar", data: rows.map((r) => r.wins), itemStyle: { color: "#e8e8e8" } }],
};
const headshots = withHeadshots(option, rows, {
league: "nfl",
x: "team",
y: "qb_y",
player: "qb_espn_id",
height: 0.14,
});
const both = withWordmarks(headshots, rows, {
league: "nfl",
x: "team",
y: "mark_y",
team: "team",
height: 0.05,
});
// In the browser: echarts.init(div, null, { width, height }) and setOption(option), as this page does.
export const browser = {
lib: "echarts",
option: both,
width: 560,
height: 360,
label:
"2024 AFC wins by team, each bar topped by its most frequent starting quarterback's headshot, the team's wordmark inside it",
} as const;
// SSR: no DOM, no canvas. The images are <image href> links the browser loads when it shows the SVG.
const { width, height } = browser;
const chart = echarts.init(null, null, { renderer: "svg", ssr: true, width, height });
chart.setOption(both);
const svg = chart.renderToSVGString();
chart.dispose();
svg;
ECharts: logos down the y axis
A category y axis takes rich-text logo labels too; inverse: true puts first place on top, as a standings table
reads.
import { NHL_STANDINGS } from "@sportsdataverse/examples/data";
import { loadLeague } from "@sportsdataverse/sdvplot";
import { teamColorPalette, withAxisLogos } from "@sportsdataverse/sdvplot/echarts";
import * as echarts from "echarts";
await loadLeague("nhl");
const teams = NHL_STANDINGS.map((s) => s.team); // division order, first to eighth
const option: echarts.EChartsOption = {
tooltip: {}, // in the browser, hovering a bar shows its team and points
color: teamColorPalette("nhl", teams), // with colorBy "data", one colour per bar
grid: { left: 70 }, // room for the logos left of the plot
// inverse: the first category on top, as a standings table reads
yAxis: { type: "category", data: teams, inverse: true },
xAxis: { type: "value", name: "Points (2025-26)", nameLocation: "middle", nameGap: 28 },
series: [{ type: "bar", colorBy: "data", data: NHL_STANDINGS.map((s) => s.points) }],
};
// In the browser: echarts.init(div, null, { width, height }) and setOption(option), as this page does.
export const browser = {
lib: "echarts",
// on the y axis each label becomes a rich-text image; `chartHeight` sizes it against the plot area
option: withAxisLogos(option, "y", { league: "nhl", height: 0.09, chartHeight: 400 }),
width: 540,
height: 400,
label:
"The 2025-26 Atlantic Division's points, first place on top: Buffalo, Tampa Bay, Montreal, Boston, Ottawa, Detroit, Florida and Toronto, each bar in its team's colour with its logo beside it",
} as const;
// SSR: no DOM, no canvas. The logos are <image href> links the browser loads when it shows the SVG.
const { width, height } = browser;
const chart = echarts.init(null, null, { renderer: "svg", ssr: true, width, height });
chart.setOption(browser.option);
const svg = chart.renderToSVGString();
chart.dispose();
svg;
ECharts: a logo at the end of each line
withLogos places images at any row's point, so one row per team at its last value ends each line with its logo, and
teamColorPalette colours the lines in series order.
import { KC_PHI_GAMES_2024 } from "@sportsdataverse/examples/data";
import { loadLeague } from "@sportsdataverse/sdvplot";
import { teamColorPalette, withLogos } from "@sportsdataverse/sdvplot/echarts";
import * as echarts from "echarts";
await loadLeague("nfl");
const teams = ["KC", "PHI"] as const;
// Each team's running point differential after every game it played, by week (bye weeks have no point).
const running = teams.map((team) => {
let total = 0;
return KC_PHI_GAMES_2024.filter((g) => g.home_team === team || g.away_team === team)
.sort((a, b) => a.week - b.week)
.map((g) => {
total += (g.home_team === team ? 1 : -1) * (g.home_score - g.away_score);
return [g.week, total] as [number, number];
});
});
const option: echarts.EChartsOption = {
tooltip: { trigger: "axis" },
color: teamColorPalette("nfl", [...teams]), // one colour per series, in order
xAxis: { type: "value", name: "Week", min: 1, max: 19.5, interval: 2 },
yAxis: { type: "value", name: "Point differential" },
series: teams.map((team, i) => ({
type: "line" as const,
name: team,
data: running[i] ?? [],
symbolSize: 5,
})),
};
// One row per logo: each team one week past its last game (so the logo doesn't cover the last point), at its final
// differential.
const ends = teams.map((team, i) => {
const [week, total] = running[i]?.at(-1) ?? [0, 0];
return { team, week: week + 1, total };
});
const both = withLogos(option, ends, { league: "nfl", x: "week", y: "total", team: "team", height: 0.12 });
// In the browser: echarts.init(div, null, { width, height }) and setOption(option), as this page does.
export const browser = {
lib: "echarts",
option: both,
width: 600,
height: 360,
label:
"Kansas City's and Philadelphia's running point differential over the 2024 regular season, by week, each line in its team's colour and ending at its logo",
} as const;
// SSR: no DOM, no canvas. The logos are <image href> links the browser loads when it shows the SVG.
const { width, height } = browser;
const chart = echarts.init(null, null, { renderer: "svg", ssr: true, width, height });
chart.setOption(both);
const svg = chart.renderToSVGString();
chart.dispose();
svg;
Images that travel with the file
By default every image is a link the viewer's browser loads. embedSources(urls) fetches each URL once and returns a
map from URL to data URI; pass it as embed and the spec carries the image bytes instead, for a saved file, an email
or a page that has to work offline. Each of the three subpaths exports it.
import { STANDINGS } from "@sportsdataverse/examples/data";
import { loadLeague, logoUrlSync } from "@sportsdataverse/sdvplot";
import { embedSources, withLogos } from "@sportsdataverse/sdvplot/echarts";
import * as echarts from "echarts";
await loadLeague("nfl");
// The AFC West's two playoff teams of 2024.
const rows = STANDINGS.filter((s) => s.division === "West" && s.wins >= 11).map((s) => ({ ...s }));
// embedSources fetches each distinct URL once and maps it to a data URI; `embed` swaps each link for its bytes,
// so the SVG below is one self-contained file (a static export, an email, an offline page).
const embed = await embedSources(rows.flatMap((r) => logoUrlSync(r.team, "nfl") ?? []));
const option: echarts.EChartsOption = {
xAxis: { type: "value", name: "Points for", nameLocation: "middle", nameGap: 28, min: 370, max: 420 },
yAxis: { type: "value", name: "Points against", nameLocation: "middle", nameGap: 40, min: 290, max: 340 },
series: [{ type: "scatter", data: rows.map((r) => [r.pf, r.pa]), symbolSize: 0 }],
};
const chart = echarts.init(null, null, { renderer: "svg", ssr: true, width: 420, height: 320 });
chart.setOption(
withLogos(option, rows, { league: "nfl", x: "pf", y: "pa", team: "team", height: 0.25, embed }),
);
const svg = chart.renderToSVGString();
chart.dispose();
svg;
import { STANDINGS } from "@sportsdataverse/examples/data";
import { loadLeague, logoUrlSync } from "@sportsdataverse/sdvplot";
import { embedSources, withLogos } from "@sportsdataverse/sdvplot/vega";
import * as vega from "vega";
import { type TopLevelSpec, compile } from "vega-lite";
await loadLeague("nfl");
// The AFC West's two playoff teams of 2024.
const rows = STANDINGS.filter((s) => s.division === "West" && s.wins >= 11).map((s) => ({ ...s }));
// One fetch per distinct logo; the image layer then carries the bytes instead of the link.
const embed = await embedSources(rows.flatMap((r) => logoUrlSync(r.team, "nfl") ?? []));
const chart: TopLevelSpec = {
width: 360,
height: 260,
data: { values: rows },
mark: { type: "point", opacity: 0 },
encoding: {
x: { field: "pf", type: "quantitative", scale: { domain: [370, 420] }, title: "Points for" },
y: { field: "pa", type: "quantitative", scale: { domain: [290, 340] }, title: "Points against" },
},
};
const spec = withLogos(chart, rows, { league: "nfl", x: "pf", y: "pa", team: "team", height: 0.25, embed });
const view = new vega.View(vega.parse(compile(spec).spec), { renderer: "none" });
await view.toSVG();
[
{
"points for, against": "385, 326",
"source": "data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAf… (53662 characters)"
},
{
"points for, against": "402, 301",
"source": "data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAf… (43822 characters)"
}
]import { STANDINGS } from "@sportsdataverse/examples/data";
import { loadLeague, logoUrlSync } from "@sportsdataverse/sdvplot";
import { type PlotlyLayout, embedSources, withLogos } from "@sportsdataverse/sdvplot/plotly";
await loadLeague("nfl");
// The AFC West's two playoff teams of 2024.
const rows = STANDINGS.filter((s) => s.division === "West" && s.wins >= 11).map((s) => ({ ...s }));
// A figure saved to a file (JSON, an HTML page) shows its logos offline only if their bytes travel with it.
const embed = await embedSources(rows.flatMap((r) => logoUrlSync(r.team, "nfl") ?? []));
const layout: PlotlyLayout = {};
const data = [{ type: "scatter", mode: "markers", x: rows.map((r) => r.pf), y: rows.map((r) => r.pa) }];
const figure = withLogos({ data, layout }, rows, {
league: "nfl",
x: "pf",
y: "pa",
team: "team",
height: 0.25,
embed,
});
// Each layout image's source is now the PNG itself (shown cut short; the full figure goes to Plotly.newPlot).
(figure.layout.images ?? []).map((im) => ({
"points for, against": `${im.x}, ${im.y}`,
source: `${im.source.slice(0, 48)}… (${im.source.length} characters)`,
}));
Chart.js
teamColor and teamFill from @sportsdataverse/sdvplot/chartjs return Chart.js colours (one per team) for
backgroundColor and borderColor; teamFill is the translucent version for areas. These examples draw on
@napi-rs/canvas in Node and register a bundled font first, since a build
server may have none; in your browser the same config is drawn on a canvas with the same font. In React, pass the same
pieces to react-chartjs-2's <Chart>: see react-chartjs-2.
import { dirname, join } from "node:path";
import { fileURLToPath } from "node:url";
import { GlobalFonts, createCanvas } from "@napi-rs/canvas";
import { STANDINGS } from "@sportsdataverse/examples/data";
import { loadLeague } from "@sportsdataverse/sdvplot";
import { teamColor, teamFill } from "@sportsdataverse/sdvplot/chartjs";
import { Chart, type ChartConfiguration, registerables } from "chart.js";
await loadLeague("nfl");
const teams = STANDINGS.map((s) => s.team);
// One config for the browser and for Node, built fresh per chart (Chart.js keeps state on what it is given).
const config = (): ChartConfiguration<"bar"> => ({
type: "bar",
data: {
labels: teams,
datasets: [
// one colour per bar: teamColor for the solid points-for bars, teamFill (0.2 opacity) under the outlined ones
{ label: "Points for", data: STANDINGS.map((s) => s.pf), backgroundColor: teamColor(teams, "nfl") },
{
label: "Points against",
data: STANDINGS.map((s) => s.pa),
backgroundColor: teamFill(teams, "nfl"),
borderColor: teamColor(teams, "nfl"),
borderWidth: 2,
},
],
},
options: { responsive: false, animation: false, plugins: { legend: { display: false } } },
});
// In the browser: new Chart(canvas, config()) on a 640 x 320 canvas, as this page does.
export const browser = {
lib: "chartjs",
config,
width: 640,
height: 320,
label: "2024 AFC points for (solid) and against (outlined) by team",
} as const;
Chart.register(...registerables);
// A build server may have no system fonts (Vercel's has none): register a bundled one before drawing, or every
// label is blank. Source Sans 3, SIL Open Font License (examples/fonts/OFL.txt).
const font = join(dirname(fileURLToPath(import.meta.url)), "../../../fonts/SourceSans3-Regular.ttf");
if (!GlobalFonts.registerFromPath(font, "Source Sans 3")) throw new Error(`no font at ${font}`);
Chart.defaults.font.family = "Source Sans 3";
const canvas = createCanvas(browser.width, browser.height);
new Chart(canvas.getContext("2d") as unknown as CanvasRenderingContext2D, config());
const png = canvas.toBuffer("image/png").toString("base64");
`<img src="data:image/png;base64,${png}" width="640" height="320" alt="${browser.label}">`;
surface(league, { team }) from @sportsdataverse/sdvplot/chartjs/surface returns a plugin that paints the court
under the datasets and scales that keep a foot the same length on both axes.
import { createCanvas } from "@napi-rs/canvas";
import { NBA_SHOTS } from "@sportsdataverse/examples/data";
import { loadLeague } from "@sportsdataverse/sdvplot";
import { teamColor } from "@sportsdataverse/sdvplot/chartjs";
import { surface } from "@sportsdataverse/sdvplot/chartjs/surface";
import { toSurfaceFrame } from "@sportsdataverse/sporty";
import { Chart, type ChartConfiguration, registerables } from "chart.js";
await loadLeague("nba");
// The Plot shot chart's stats.nba.com shots (x_legacy/y_legacy, tenths of a foot from the hoop), in court feet.
const shots = toSurfaceFrame(NBA_SHOTS, { from: "nba-legacy" });
const court = surface("nba", { team: "DEN", displayRange: "defense" });
// made shots filled in the shooting team's colour, misses white with a ring in it: both read on the wood and the paint
const color = teamColor(
shots.map((s) => s.team),
"nba",
);
// One config for the browser and for Node, built fresh per chart (Chart.js keeps state on what it is given).
const config = (): ChartConfiguration<"scatter"> => ({
type: "scatter",
data: {
datasets: [
{
data: shots.map((s) => ({ x: Number(s.surface_x), y: Number(s.surface_y) })),
backgroundColor: color.map((c, i) => (shots[i]?.made ? c : "#ffffff")),
borderColor: color.map((c, i) => (shots[i]?.made ? "#ffffff" : c)),
borderWidth: 2,
pointRadius: 7,
},
],
},
// court.plugin paints the court under the datasets; court.scales keeps one foot the same length on both axes
plugins: [court.plugin],
options: {
responsive: false,
animation: false,
layout: { padding: 0 },
plugins: { legend: { display: false } },
scales: { x: { ...court.scales.x, display: false }, y: { ...court.scales.y, display: false } },
},
});
const [x0, y0, x1, y1] = court.scene.bbox;
const width = 640;
// In the browser: new Chart(canvas, config()) on a canvas of the court's aspect, as this page does.
export const browser = {
lib: "chartjs",
config,
width,
height: Math.round((width * (y1 - y0)) / (x1 - x0)),
label:
"The 38 fourth-quarter shots of the Lakers at the Nuggets, 24 October 2023, on the defensive half of a Nuggets-painted court: made shots filled in the team colour, misses white",
} as const;
Chart.register(...registerables);
const canvas = createCanvas(browser.width, browser.height);
new Chart(canvas.getContext("2d") as unknown as CanvasRenderingContext2D, config());
const png = canvas.toBuffer("image/png").toString("base64");
`<img src="data:image/png;base64,${png}" width="${canvas.width}" height="${canvas.height}" alt="${browser.label}">`;
The image helpers (logoPoints, wordmarkPoints, headshotPoints, axisLogos, logoWatermarks) build <img>
elements in the browser. Give them a loadImage option and they load each image through it instead, which is how
these examples draw them in Node: the loader fetches the mark and decodes it with @napi-rs/canvas, and the example
awaits the loads before taking the PNG. Point styles also need the pointImages plugin, which updates the chart as
each image lands.
import { dirname, join } from "node:path";
import { fileURLToPath } from "node:url";
import { GlobalFonts, type Image, createCanvas, loadImage } from "@napi-rs/canvas";
import { STANDINGS } from "@sportsdataverse/examples/data";
import { loadLeague } from "@sportsdataverse/sdvplot";
import { type PointOptions, logoPoints, pointImages } from "@sportsdataverse/sdvplot/chartjs";
import { Chart, type ChartConfiguration, registerables } from "chart.js";
await loadLeague("nfl");
const west = STANDINGS.filter((s) => s.division === "West");
// One config for the browser and for Node, built fresh per chart (Chart.js keeps state on what it is given). A
// browser loads each logo itself; Node passes `loadImage` (below).
const config = (o: Pick<PointOptions, "loadImage"> = {}): ChartConfiguration<"scatter"> => ({
type: "scatter",
data: {
datasets: [
{
data: west.map((s) => ({ x: s.pf, y: s.pa })),
// one logo per point, in data order; each slot draws nothing until its image lands
...logoPoints(
west.map((s) => s.team),
{ league: "nfl", radius: 20, ...o },
),
},
],
},
options: {
responsive: false,
animation: false,
plugins: { legend: { display: false } },
scales: {
x: { min: 280, max: 440, title: { display: true, text: "Points for" } },
// fewer points against is better: reversed, so the best teams sit top right
y: { min: 280, max: 440, reverse: true, title: { display: true, text: "Points against" } },
},
},
// pointImages updates the chart as each point's image lands
plugins: [pointImages],
});
// In the browser: new Chart(canvas, config()) on a 640 x 400 canvas, as this page does.
export const browser = {
lib: "chartjs",
config,
width: 640,
height: 400,
label:
"The 2024 AFC West as team logos, points for against points against: Kansas City, the Chargers, Denver and Las Vegas",
} as const;
// In Node the image helpers load marks through `loadImage`: fetch each URL and decode it on @napi-rs/canvas (whose
// own loadImage(url) also fetches by itself). Keep the promises: once they settle, pointImages has redrawn.
const loads: Promise<Image>[] = [];
const load = (url: string): Promise<Image> => {
const p = fetch(url)
.then((r) => r.arrayBuffer())
.then((b) => loadImage(Buffer.from(b)));
loads.push(p);
return p;
};
Chart.register(...registerables);
// A build server may have no system fonts (Vercel's has none): register a bundled one before drawing, or every
// label is blank. Source Sans 3, SIL Open Font License (examples/fonts/OFL.txt).
const font = join(dirname(fileURLToPath(import.meta.url)), "../../../fonts/SourceSans3-Regular.ttf");
if (!GlobalFonts.registerFromPath(font, "Source Sans 3")) throw new Error(`no font at ${font}`);
Chart.defaults.font.family = "Source Sans 3";
const canvas = createCanvas(browser.width, browser.height);
const ctx = canvas.getContext("2d") as unknown as CanvasRenderingContext2D;
const chart = new Chart(ctx, config({ loadImage: load }));
await Promise.all(loads);
const png = canvas.toBuffer("image/png").toString("base64");
chart.destroy();
`<img src="data:image/png;base64,${png}" width="640" height="400" alt="${browser.label}">`;
axisLogos("x") paints a category axis's team labels as logos and blanks their text.
import { dirname, join } from "node:path";
import { fileURLToPath } from "node:url";
import { GlobalFonts, type Image, createCanvas, loadImage } from "@napi-rs/canvas";
import { STANDINGS } from "@sportsdataverse/examples/data";
import { loadLeague } from "@sportsdataverse/sdvplot";
import { type AxisLogoOptions, axisLogos, teamColor } from "@sportsdataverse/sdvplot/chartjs";
import { Chart, type ChartConfiguration, registerables } from "chart.js";
await loadLeague("nfl");
const west = STANDINGS.filter((s) => s.division === "West");
const teams = west.map((s) => s.team);
// One config for the browser and for Node, built fresh per chart (Chart.js keeps state on what it is given). A
// browser loads each logo itself; Node passes `loadImage` (below).
const config = (o: Pick<AxisLogoOptions, "loadImage"> = {}): ChartConfiguration<"bar"> => ({
type: "bar",
data: {
labels: teams,
datasets: [{ data: west.map((s) => s.wins), backgroundColor: teamColor(teams, "nfl") }],
},
options: {
responsive: false,
animation: false,
plugins: { legend: { display: false } },
scales: { y: { min: 0, max: 16, ticks: { stepSize: 4 }, title: { display: true, text: "Wins" } } },
},
// the x axis is a category scale: each label that names a team is painted as its logo, its text blanked
plugins: [axisLogos("x", { league: "nfl", size: 36, ...o })],
});
// In the browser: new Chart(canvas, config()) on a 640 x 320 canvas, as this page does.
export const browser = {
lib: "chartjs",
config,
width: 640,
height: 320,
label:
"2024 AFC West wins in team colours, each bar labelled by its team's logo: Kansas City 15, the Chargers 11, Denver 10, Las Vegas 4",
} as const;
// In Node the image helpers load marks through `loadImage`: fetch each URL and decode it on @napi-rs/canvas (whose
// own loadImage(url) also fetches by itself). Keep the promises: once they settle, axisLogos has redrawn.
const loads: Promise<Image>[] = [];
const load = (url: string): Promise<Image> => {
const p = fetch(url)
.then((r) => r.arrayBuffer())
.then((b) => loadImage(Buffer.from(b)));
loads.push(p);
return p;
};
Chart.register(...registerables);
// A build server may have no system fonts (Vercel's has none): register a bundled one before drawing, or every
// label is blank. Source Sans 3, SIL Open Font License (examples/fonts/OFL.txt).
const font = join(dirname(fileURLToPath(import.meta.url)), "../../../fonts/SourceSans3-Regular.ttf");
if (!GlobalFonts.registerFromPath(font, "Source Sans 3")) throw new Error(`no font at ${font}`);
Chart.defaults.font.family = "Source Sans 3";
const canvas = createCanvas(browser.width, browser.height);
const ctx = canvas.getContext("2d") as unknown as CanvasRenderingContext2D;
const chart = new Chart(ctx, config({ loadImage: load }));
// after new Chart(...): axisLogos asks for its images while the chart is first laid out
await Promise.all(loads);
const png = canvas.toBuffer("image/png").toString("base64");
chart.destroy();
`<img src="data:image/png;base64,${png}" width="640" height="320" alt="${browser.label}">`;
wordmarkPoints does the same with wordmarks, about 3.6 times as wide as they are tall, and headshotPoints with ESPN
headshots, built from the player id alone. ESPN's image server resizes on request, so this loader asks for a small
copy rather than the full-size original.
import { dirname, join } from "node:path";
import { fileURLToPath } from "node:url";
import { GlobalFonts, type Image, createCanvas, loadImage } from "@napi-rs/canvas";
import { STANDINGS } from "@sportsdataverse/examples/data";
import { loadLeague } from "@sportsdataverse/sdvplot";
import { type PointOptions, pointImages, wordmarkPoints } from "@sportsdataverse/sdvplot/chartjs";
import { Chart, type ChartConfiguration, registerables } from "chart.js";
await loadLeague("nfl");
const west = STANDINGS.filter((s) => s.division === "West");
// One config for the browser and for Node, built fresh per chart (Chart.js keeps state on what it is given). A
// browser loads each wordmark itself; Node passes `loadImage` (below).
const config = (o: Pick<PointOptions, "loadImage"> = {}): ChartConfiguration<"scatter"> => ({
type: "scatter",
data: {
datasets: [
{
data: west.map((s) => ({ x: s.wins, y: s.pf - s.pa })),
// a wordmark is about 3.6 times as wide as it is tall: radius sets the height (2 × radius px)
...wordmarkPoints(
west.map((s) => s.team),
{ league: "nfl", radius: 12, ...o },
),
},
],
},
options: {
responsive: false,
animation: false,
plugins: { legend: { display: false } },
scales: {
x: { min: 2, max: 18, title: { display: true, text: "Wins" } },
y: { min: -150, max: 150, title: { display: true, text: "Points for minus points against" } },
},
},
plugins: [pointImages],
});
// In the browser: new Chart(canvas, config()) on a 640 x 360 canvas, as this page does.
export const browser = {
lib: "chartjs",
config,
width: 640,
height: 360,
label:
"The 2024 AFC West as team wordmarks, wins against point differential: Kansas City 15 and +59, the Chargers 11 and +101, Denver 10 and +114, Las Vegas 4 and -125",
} as const;
// In Node the image helpers load marks through `loadImage`: fetch each URL and decode it on @napi-rs/canvas (whose
// own loadImage(url) also fetches by itself). Keep the promises: once they settle, pointImages has redrawn.
const loads: Promise<Image>[] = [];
const load = (url: string): Promise<Image> => {
const p = fetch(url)
.then((r) => r.arrayBuffer())
.then((b) => loadImage(Buffer.from(b)));
loads.push(p);
return p;
};
Chart.register(...registerables);
// A build server may have no system fonts (Vercel's has none): register a bundled one before drawing, or every
// label is blank. Source Sans 3, SIL Open Font License (examples/fonts/OFL.txt).
const font = join(dirname(fileURLToPath(import.meta.url)), "../../../fonts/SourceSans3-Regular.ttf");
if (!GlobalFonts.registerFromPath(font, "Source Sans 3")) throw new Error(`no font at ${font}`);
Chart.defaults.font.family = "Source Sans 3";
const canvas = createCanvas(browser.width, browser.height);
const ctx = canvas.getContext("2d") as unknown as CanvasRenderingContext2D;
const chart = new Chart(ctx, config({ loadImage: load }));
await Promise.all(loads);
const png = canvas.toBuffer("image/png").toString("base64");
chart.destroy();
`<img src="data:image/png;base64,${png}" width="640" height="360" alt="${browser.label}">`;
import { dirname, join } from "node:path";
import { fileURLToPath } from "node:url";
import { GlobalFonts, type Image, createCanvas, loadImage } from "@napi-rs/canvas";
import { STANDINGS } from "@sportsdataverse/examples/data";
import { type HeadshotPointOptions, headshotPoints, pointImages } from "@sportsdataverse/sdvplot/chartjs";
import { Chart, type ChartConfiguration, registerables } from "chart.js";
// ESPN player ids need no league data: headshotPoints builds each URL from the id alone.
const west = STANDINGS.filter((s) => s.division === "West");
// One config for the browser and for Node, built fresh per chart (Chart.js keeps state on what it is given). A
// browser loads each headshot itself; Node passes `loadImage` (below).
const config = (o: Pick<HeadshotPointOptions, "loadImage"> = {}): ChartConfiguration<"scatter"> => ({
type: "scatter",
data: {
datasets: [
{
data: west.map((s) => ({ x: s.pf, y: s.wins })),
// each team's most frequent starter, 2 × radius px tall (an ESPN headshot is 600:436, so a little wider)
...headshotPoints(
west.map((s) => s.qb_espn_id),
{ league: "nfl", radius: 24, ...o },
),
},
],
},
options: {
responsive: false,
animation: false,
plugins: { legend: { display: false } },
scales: {
x: { min: 280, max: 440, title: { display: true, text: "Points for" } },
y: { min: 0, max: 18, title: { display: true, text: "Wins" } },
},
},
plugins: [pointImages],
});
// In the browser: new Chart(canvas, config()) on a 640 x 360 canvas, as this page does.
export const browser = {
lib: "chartjs",
config,
width: 640,
height: 360,
label:
"Headshots of the 2024 AFC West's most frequent starting quarterbacks, Patrick Mahomes, Justin Herbert, Bo Nix and Gardner Minshew, at their teams' points for and wins",
} as const;
// ESPN's image combiner resizes on request, so the loader asks for twice the drawn size rather than the 1096 px
// original; it fetches each URL and decodes it on @napi-rs/canvas. Keep the promises: once they settle,
// pointImages has redrawn.
const loads: Promise<Image>[] = [];
const load = (url: string): Promise<Image> => {
const p = fetch(`${url}&w=96&h=70`)
.then((r) => r.arrayBuffer())
.then((b) => loadImage(Buffer.from(b)));
loads.push(p);
return p;
};
Chart.register(...registerables);
// A build server may have no system fonts (Vercel's has none): register a bundled one before drawing, or every
// label is blank. Source Sans 3, SIL Open Font License (examples/fonts/OFL.txt).
const font = join(dirname(fileURLToPath(import.meta.url)), "../../../fonts/SourceSans3-Regular.ttf");
if (!GlobalFonts.registerFromPath(font, "Source Sans 3")) throw new Error(`no font at ${font}`);
Chart.defaults.font.family = "Source Sans 3";
const canvas = createCanvas(browser.width, browser.height);
const ctx = canvas.getContext("2d") as unknown as CanvasRenderingContext2D;
const chart = new Chart(ctx, config({ loadImage: load }));
await Promise.all(loads);
const png = canvas.toBuffer("image/png").toString("base64");
chart.destroy();
`<img src="data:image/png;base64,${png}" width="640" height="360" alt="${browser.label}">`;
A matchup in two colours
matchupColors(home, away) returns a pair that reads apart on a light page and one for a dark page; here the light
pair colours Super Bowl LIX's win probability, and logoWatermarks puts the two teams' logos faintly behind it.
import { dirname, join } from "node:path";
import { fileURLToPath } from "node:url";
import { GlobalFonts, type Image, createCanvas, loadImage } from "@napi-rs/canvas";
import { SUPER_BOWL_LIX_WP } from "@sportsdataverse/examples/data";
import { matchupColors } from "@sportsdataverse/sdvplot";
import { type WatermarkOptions, logoWatermarks } from "@sportsdataverse/sdvplot/chartjs";
import { Chart, type ChartConfiguration, registerables } from "chart.js";
// Two colours that read apart and on a light page: Philadelphia (home) first, as on Game on Paper.
const { light } = await matchupColors("PHI", "KC", { league: "nfl" });
const [phi, kc] = light;
const line = (label: string, color: string, wp: (p: number) => number) => ({
label,
data: SUPER_BOWL_LIX_WP.map((p) => ({ x: p.minute, y: wp(p.home_wp) })),
borderColor: color,
backgroundColor: color,
borderWidth: 2,
pointRadius: 0,
});
// One config for the browser and for Node, built fresh per chart (Chart.js keeps state on what it is given). A
// browser loads each logo itself; Node passes `loadImage` (below).
const config = (o: Pick<WatermarkOptions, "loadImage"> = {}): ChartConfiguration<"line"> => ({
type: "line",
data: {
datasets: [line("Philadelphia", phi, (wp) => wp), line("Kansas City", kc, (wp) => 1 - wp)],
},
options: {
responsive: false,
animation: false,
scales: {
x: {
type: "linear",
min: 0,
max: 60,
ticks: { stepSize: 15 },
title: { display: true, text: "Minutes played" },
},
y: { min: 0, max: 1, ticks: { callback: (v) => `${Math.round(Number(v) * 100)}%` } },
},
},
// Game on Paper's watermarks: the home team top-left, the away team bottom-left, 40% opaque behind the lines
plugins: [logoWatermarks(["PHI", "KC"], { league: "nfl", size: 60, ...o })],
});
// In the browser: new Chart(canvas, config()) on a 640 x 320 canvas, as this page does.
export const browser = {
lib: "chartjs",
config,
width: 640,
height: 320,
label:
"Super Bowl LIX win probability, Philadelphia and Kansas City, by minute, with faint Eagles and Chiefs logos behind the lines",
} as const;
Chart.register(...registerables);
// A build server may have no system fonts (Vercel's has none): register a bundled one before drawing, or every
// label is blank. Source Sans 3, SIL Open Font License (examples/fonts/OFL.txt).
const font = join(dirname(fileURLToPath(import.meta.url)), "../../../fonts/SourceSans3-Regular.ttf");
if (!GlobalFonts.registerFromPath(font, "Source Sans 3")) throw new Error(`no font at ${font}`);
Chart.defaults.font.family = "Source Sans 3";
// In Node the image plugins load marks through `loadImage`: fetch each URL and decode it on @napi-rs/canvas (whose
// own loadImage(url) also fetches by itself). Keep the promises: once they settle, the plugins have redrawn.
const loads: Promise<Image>[] = [];
const load = (url: string): Promise<Image> => {
const p = fetch(url)
.then((r) => r.arrayBuffer())
.then((b) => loadImage(Buffer.from(b)));
loads.push(p);
return p;
};
const canvas = createCanvas(browser.width, browser.height);
// Chart.js types want a DOM canvas; @napi-rs/canvas has the same 2D context. Outside a DOM, Chart.js draws
// synchronously on its BasicPlatform (no resize, no events).
const ctx = canvas.getContext("2d") as unknown as CanvasRenderingContext2D;
const chart = new Chart(ctx, config({ loadImage: load }));
await Promise.all(loads);
const png = canvas.toBuffer("image/png").toString("base64");
chart.destroy();
`<img src="data:image/png;base64,${png}" width="640" height="320" alt="${browser.label}">`;
React chart libraries have guides of their own, among them Recharts, shadcn/ui charts
(Recharts inside shadcn's copy-paste chart.tsx, with team colours for its light and dark themes and logos in its
legend and tooltip) and MUI X Charts (team colours that follow MUI's light and dark mode, and logos
through MUI's marker, mark and axis tick label slots). For tables, an sdvtables spec also drives
MUI X Data Grid, AG Grid and the headless TanStack Table.
G2 and Ant Design Charts
AntV's G2 5 and Ant Design Charts, its React layer, have a guide of their own: G2 and Ant Design Charts, with team colours as a G2 colour scale, logos as G2 image marks and logos on a band axis.
TanStack Charts
TanStack Charts, a grammar of graphics with React, Svelte and DOM adapters, has a guide of its own:
TanStack Charts, with team colours as the chart's color option and logos as dots filled with
SVG patterns (TanStack Charts has no image mark).
MapLibre GL JS
MapLibre GL JS, an open-source vector map library, has a guide of its own: MapLibre GL JS, with team logos at their venues as a native symbol layer, team-coloured travel lines, deck.gl layers over the map, and a free OpenFreeMap basemap that needs no API key.
Unovis
Unovis, a modular set of chart components (graphs, Sankey diagrams, chord diagrams, maps and XY charts) for plain JavaScript, React and other frameworks, has a guide of its own: Unovis, with team colours as accessor functions for nodes, links and flows, and logos and headshots as a Graph's node icons.
Mosaic (experimental)
Mosaic, linked views over DuckDB-WASM, has a recipe rather than a subpath: Mosaic, an EPA histogram whose brush filters team-coloured bars and a table, with DuckDB-WASM served from your own site. It is experimental: Mosaic is pre-1.0 and depends on a development build of DuckDB-WASM.
Perspective
Perspective, a WebAssembly data grid and chart viewer, has a guide of its own: Perspective, with a viewer config that colours each team in the Datagrid and in split charts (the same config sdvplot for Python builds), a logo column, and play-by-play streamed into a table, all served from your own files.
Every other library
Each of these has a guide of its own: Svelte and LayerChart, nivo, Victory, uPlot, deck.gl, Cytoscape.js, Three.js and Lightweight Charts.
Plotly, Vega-Lite and ECharts: options
Each verb returns a new spec and leaves its input alone. TypeScript users can type the figure as plotly.js's own (and
a Vega-Lite TopLevelSpec or an ECharts EChartsOption the same way): the same type comes back. The options every
adapter takes (withLogos, withWordmarks and withHeadshots are the mark verbs):
| Option | Verbs | Meaning |
|---|---|---|
x, y | marks | Row columns holding each image's position |
team | withLogos, withWordmarks | Row column holding the team (abbreviation, name or id) |
player | withHeadshots | Row column holding the player id |
league | all | "nfl", "nba", "cfb", ... |
season | logos, wordmarks, withAxisLogos | A season; for the mark verbs also the name of a row column holding each row's season |
height | all | Image height as a fraction of the plot area's height, in (0, 1]; default 0.1 |
alpha | marks | Opacity in [0, 1]; default 1 |
variant | logos, wordmarks, withAxisLogos | Logo variant, e.g. "dark" |
idSystem | all | How the team values are read (default "auto"); for headshots "espn" (default) or "gsis" (needs loadGsis()) |
markType | withAxisLogos | "logo" (default) or "wordmark" |
embed | all | await embedSources(urls) (each subpath exports it): inlines the images as data URIs (offline HTML, static export) |
An unknown team or player is skipped with one warning per call. The colour helpers (teamColorway, teamColorScale,
teamColorPalette) take (league, teams, { which, season, idSystem, fallback }): which is "primary" (default) or
"secondary", and an unknown team gets fallback (default "#808080").
Plotly (layout.images):
| Option | Verbs | Meaning |
|---|---|---|
xref, yref | all | The subplot's axes, e.g. "x2", "y2"; default "x", "y" |
layer | marks | "above" (default) or "below" the traces |
height is a fraction of the subplot. The mark verbs pin the axis ranges (with half a mark of room), because a
data-placed image is sized in axis units; set layout.xaxis.range yourself to keep your own. withAxisLogos sizes
margins in pixels from layout.width/layout.height (Plotly's 700 × 450 when unset). x-axis images hang under their
subplot and margin.b grows by what reaches below the figure; under an upper subplot they hang into the one below.
Accessible name: plotly.js has no accessibility option and layout images carry no name, so name the chart on the
element you pass to Plotly.newPlot (role="img" and an aria-label).
Vega-Lite (a native image layer):
| Option | Verbs | Meaning |
|---|---|---|
chartHeight | logoLayer | The chart height in px that height is a fraction of; default 300 |
xType, yType | logoLayer | The layer's encoding types (its fields are x and y); default "quantitative" |
The verbs size from the chart's own height (else config.view.continuousHeight, else 300 px); a discrete y axis needs
height. A discrete sort that Vega-Lite drops once a layer is added raises: sort with a list (sort: [...]). The
shorthand "-y" works where Vega-Lite keeps it; on a stacked bar or area it sums the measure, which Vega-Lite drops,
so it raises too. A count sort is written out as its order (a list) on both layers, so the image rows do not add to
the counts; that needs inline data with no transform. withAxisLogos assumes the default axis orient (x at the
bottom, y on the left): with orient: "top" or "right" the images land on the opposite side.
ECharts (a custom series; axis logos as rich-text labels):
| Option | Verbs | Meaning |
|---|---|---|
xAxisIndex, yAxisIndex | marks | The axes (and so the grid) the series draws on; default 0. Calls on other axes (or another z) get their own series |
z | marks | The series' z; default 100 |
axisIndex | withAxisLogos | Which x (or y) axis, for an option with several; default 0 |
chartHeight | withAxisLogos | The canvas height in px (an option has none); default 400 |
Mark images are height × the grid's height at render time (they follow zoom and resize; no instance needed).
Axis images are height of the axis' grid on a chartHeight px canvas: grid.height, else chartHeight less
grid.top and grid.bottom (ECharts' 65 and 80 px when unset); containLabel can shrink the drawn grid a little
below that. The helper series have no name, so a default legend: {} lists only your series. Accessible name:
aria: { enabled: true, label: { description: "…" } } gives the chart's container role="img" and that aria-label
(in the browser; server-side rendering writes none); without a description ECharts writes one from every series,
the logo series too.
Chart.js: notes
@sportsdataverse/sdvplot/chartjs returns Chart.js 4 dataset options and plugin objects, so no framework needs a
wrapper: Game on Paper shows them in an Astro page with a Svelte 5 island, and
react-chartjs-2 in React. teamFill is teamColor at 0.2 opacity, for a radar's or an area's
fill; logoWatermarks([home, away]) puts the home logo top-left of the chart area and the away logo bottom-left, at 0.4
opacity and 75 px tall by default.
- Plugins are fixed when the chart is created (
new Chart): to change the watermark teams or the court, destroy the chart and create a new one. - Sizes are pixels:
radius(point styles),size(axis logos, watermarks). Chart.js draws an image at its own size. - Add
pointImagestopluginswith any*Points: Chart.js does not redraw when an<img>finishes loading. - An unknown team draws its own label as text (or pass
fallback: "circle"), with one warning per call; the text is grey on a light chart and light grey on a dark one (background, default white, or black withvariant: "dark"). - Dark theme:
variant: "dark"(readprefers-color-scheme); a team with no dark mark falls back to a light one by polarity, so noonerrorretry is needed. - Two teams on one chart:
teamColor(team, league, { which: "secondary" })is the alternate. For a two-team chart usematchupColors(above). axisLogosneeds a category axis (any other scale is left as it is, with one warning); onythe axis widens to the widest mark, so wordmarks fit; unresolved labels keep their text; your own scale options are not modified, and replacingchart.options(chart.options = next; chart.update()) keeps the logos.surfacepaints before the datasets, clipped to the chart area, through the chart's own scales (so it follows resizes and a reversed axis); keep both axes linear (court.scales; any other scale is left unpainted, with one warning). Anxlimorylimof zero width throwsInputErrorwhen the surface is built. WithlogoWatermarkson the same chart, listcourt.pluginfirst: both paint before the datasets, inpluginsorder (the wrong order warns once).aspectRatiosizes the canvas, not the chart area, so the axes and the legend skew the court's proportions by a few percent: hide the legend and match the court's aspect through layout padding for undistorted circles. On a reversed x axis the football field's yard numbers render mirrored; courts and rinks are unaffected. It needs@sportsdataverse/sporty, assdvplot/d3does.- Destroying a chart drops its pending image listeners, so unmounting before the logos arrive is safe.
- Accessible name: Chart.js adds none (it draws pixels), so name the chart on the canvas:
role="img"and anaria-label, or fallback content between<canvas>and</canvas>.
Chart.js in Node
Pass loadImage and no DOM image is made: Chart.js renders on @napi-rs/canvas (server-rendered PNGs, social cards),
as every Chart.js example on this page does. @napi-rs/canvas's own loadImage fits as it is; wrap it to keep the
promises, and once they settle the chart is complete.
- Every image helper takes it:
logoPoints,wordmarkPoints,headshotPoints,axisLogos,logoWatermarks. Point styles still needpointImagesinplugins, which updates the chart as they land. loadImageis called once per URL and drawn size (per loader function), and the image'swidthandheightare set to that size, so resolve a new image on every call, as@napi-rs/canvas's does (a loader that hands out one shared image warns once and skips the use at a different size). AwaitPromise.all(loads)afternew Chart(...), sinceaxisLogosfirst calls the loader inside it.- In Node an unknown team's text fallback is a circle (there is no canvas to letter it on); pass
fallbackfor another style. A load that fails warns once and is skipped.
In the gallery
Plotly, Vega-Lite, ECharts, Chart.js, react-chartjs-2, Recharts, shadcn/ui, G2, Ant Design Charts, MUI X Charts, TanStack Charts, MapLibre GL JS, Unovis, Mosaic, Perspective, Svelte, nivo, Victory, uPlot, deck.gl, Cytoscape.js, Three.js and Lightweight Charts.