Skip to main content

Ant Design Charts: 2024 AFC points for and against, each team a logo over its team-coloured point

With JavaScript on, Ant Design Charts draws this figure here.

[
  {
    "team": "KC",
    "pf": 385,
    "pa": 326,
    "record": "15-2",
    "color": "#e31837"
  },
  {
    "team": "LAC",
    "pf": 402,
    "pa": 301,
    "record": "11-6",
    "color": "#007bc7"
  },
  {
    "team": "DEN",
    "pf": 425,
    "pa": 311,
    "record": "10-7",
    "color": "#002244"
  },
  {
    "team": "LV",
    "pf": 309,
    "pa": 434,
    "record": "4-13",
    "color": "#000000"
  },
  {
    "team": "BUF",
    "pf": 525,
    "pa": 368,
    "record": "13-4",
    "color": "#00338d"
  },
  {
    "team": "MIA",
    "pf": 345,
    "pa": 364,
    "record": "8-9",
    "color": "#008e97"
  },
  {
    "team": "NYJ",
    "pf": 338,
    "pa": 404,
    "record": "5-12",
    "color": "#003f2d"
  },
  {
    "team": "NE",
    "pf": 289,
    "pa": 417,
    "record": "4-13",
    "color": "#002244"
  }
]
import { Scatter } from "@ant-design/plots";
import { STANDINGS } from "@sportsdataverse/examples/data";
import { loadLeague } from "@sportsdataverse/sdvplot";
import { logoMark, teamColorScale } from "@sportsdataverse/sdvplot/g2";

await loadLeague("nfl");
// the AFC West and East, picked by id
const TEAMS = ["KC", "LAC", "DEN", "LV", "BUF", "MIA", "NYJ", "NE"];
const rows = TEAMS.map((team) => {
const s = STANDINGS.find((r) => r.team === team);
if (s === undefined) throw new Error(`${team} is not in STANDINGS`);
return { team, pf: s.pf, pa: s.pa, record: `${s.wins}-${s.losses}` };
});
// The config is a G2 spec underneath: a G2 scale.color, and an image mark in `annotations`, which Ant Design Charts
// hands to G2 as it is (a mark in `children` would take the plot's xField, yField and colorField too)
const config = {
data: rows,
xField: "pf",
yField: "pa",
colorField: "team",
scale: { color: teamColorScale("nfl", TEAMS), y: { range: [0, 1] } }, // fewer points allowed is better: up
legend: false,
axis: { x: { title: "Points for" }, y: { title: "Points against (fewer is up)" } },
tooltip: { title: "team", items: [{ field: "record", name: "W-L" }] },
annotations: [logoMark(rows, { x: "pf", y: "pa", team: "team", league: "nfl", size: 30 })],
height: 400,
};

// In the browser: <Scatter {...config} />, here at 560 px wide
export const browser = {
lib: "ant-design-charts",
chart: () => <Scatter {...config} />,
width: 560,
height: 400,
label:
"2024 points for against points against for the AFC West and East, each team drawn as its logo over a point in its colour: Buffalo scored most (525), the Chargers allowed fewest (301)",
} as const;

// Ant Design Charts draws only in a browser; without JavaScript the page shows each team's points and colour
rows.map((r, i) => ({ ...r, color: config.scale.color.range[i] }));

Tags: react · ant-design-charts · g2 · logoMark · teamColorScale · nfl. All sdvplot examples.