Skip to content

Get Started ​

Installing ​

WARNING

Charming 0.2 is a new API, and it may still change before 1.0. Version 0.1 and earlier, the creative-coding API of the old site, isn't compatible with it.

Charming installs from npm:

sh
npm install charmingjs
sh
pnpm add charmingjs

and imports as a namespace:

js
import * as cm from "charmingjs";

A first chart ​

A chart is a flow from data to shapes. Make a renderer, root a flow at it, build the flow, render it, and put the renderer's node on the page:

js
const alphabet = await d3.csv("/data/alphabet.csv", d3.autoType);

const renderer = cm.svg({width: 640, height: 400});
const root = cm.flow().renderer(renderer);

root
  .data(alphabet) // one row per letter
  .transform(cm.encode, {x: "letter", y: "frequency"}) // channels from fields
  .transform(cm.spreadX) // a band per letter
  .transform(cm.plane) // scales, margins and axes
  .add(cm.rect); // a bar per row

root.render();
document.body.append(renderer.node());

Each step reads what the steps before it wrote:

Building only records the steps; root.render() runs them.

Changing it ​

Color the bars by a channel, and sort them by frequency:

js
const alphabet = await d3.csv("/data/alphabet.csv", d3.autoType);

const renderer = cm.svg({width: 640, height: 400});
const root = cm.flow().renderer(renderer);

root
  .data(alphabet)
  .transform(cm.encode, {x: "letter", y: "frequency", fill: "frequency"})
  .transform(cm.spreadX, {sort: "-y"})
  .transform(cm.plane, {y: {grid: true, percent: true}})
  .add(cm.rect);

root.render();
document.body.append(renderer.node());

Drawing without charts ​

Charts are a layer on top of a smaller core: flows of data into shapes, on any renderer. The same flow draws generative art, here a clover of circles on a canvas, placed by cm.fit:

js
const renderer = cm.canvas({width: 640, height: 640});
const root = cm.flow().renderer(renderer);

root
  .data(Array.from({length: 120}, (_, i) => (i * Math.PI) / 120))
  .transform(cm.bind, {
    x: (t) => Math.cos(t) * Math.cos(t * 3),
    y: (t) => Math.sin(t) * Math.cos(t * 3),
    r: (t) => t,
  })
  .transform(cm.fit, {margin: 30})
  .transform(cm.scale, {r: {range: [8, 15]}})
  .add(cm.circle, {fill: "black"});

root.render();
document.body.append(renderer.node());

Next: