Skip to content

← Examples

Scatter Plot with Pie Glyphs ​

A scatter plot placing each lake at its geographic location and drawing a miniature pie chart of its species composition as the point glyph.

ts
import { Coord, chart, join, rect, scatter, stack } from "gofish-graphics";
import { catchLocationsArray, seafood } from "./dataset";

const container = document.getElementById("app");

chart(catchLocationsArray, { w: 400, h: 400 })
  .flow(scatter({ by: "lake",  x: "x", y: "y" }))
  // The glyph chart leaves off its data: as a nested mark it inherits its
  // parent partition (the lake's row), joins in that lake's catch rows, and
  // draws them as a polar pie — no `(data) => chart(data, ...)` callback.
  .mark(
    chart({ coord: Coord.clock() })
      .flow(
        join(seafood, { on: "lake" }),
        stack({ by: "species",  dir: "x", /* h: "count" */ h: 20 })
      )
      .mark(rect({ w: "count", fill: "species" }))
  )
  .render(container);
dataset.ts
ts
export type Lakes =
  | "Lake A"
  | "Lake B"
  | "Lake C"
  | "Lake D"
  | "Lake E"
  | "Lake F";

export type CatchData = {
  lake: Lakes;
  species: "Bass" | "Trout" | "Catfish" | "Perch" | "Salmon";
  count: number;
};

export const catchLocations: Record<Lakes, { x: number; y: number }> = {
  "Lake A": { x: 5.26, y: 22.64 },
  "Lake B": { x: 30.87, y: 120.75 },
  "Lake C": { x: 50.01, y: 60.94 },
  "Lake D": { x: 115.13, y: 94.16 },
  "Lake E": { x: 133.05, y: 50.44 },
  "Lake F": { x: 85.99, y: 172.78 },
};

export const catchLocationsArray = Object.entries(catchLocations).map(
  ([lake, { x, y }]) => ({
    lake,
    x,
    y,
  })
);

export const seafood: CatchData[] = [
  {
    lake: "Lake A",
    species: "Bass",
    count: 23,
  },
  {
    lake: "Lake A",
    species: "Trout",
    count: 31,
  },
  {
    lake: "Lake A",
    species: "Catfish",
    count: 29,
  },
  {
    lake: "Lake A",
    species: "Perch",
    count: 12,
  },
  {
    lake: "Lake A",
    species: "Salmon",
    count: 8,
  },
  // … 25 more rows
];

export const catchData = seafood;

export const catchDataWithLocations = seafood.map((catchItem) => {
  const location = catchLocationsArray.find(
    (loc) => loc.lake === catchItem.lake
  );
  return {
    ...catchItem,
    x: location?.x,
    y: location?.y,
  };
});

export type CatchDataWithLocation = (typeof catchDataWithLocations)[0];