Skip to content

Labeled Lines ​

The closing prices of four stocks, one colored line per symbol: the stroke channel of cm.encode both groups and colors the lines. After Observable Plot, without its labels.

js
// Four CSV files merged into one table, with the symbol of each stock as its first column.
const symbols = ["AAPL", "AMZN", "GOOG", "IBM"];
const tables = await Promise.all(symbols.map((symbol) => d3.csv(`/data/${symbol.toLowerCase()}.csv`, d3.autoType)));
const data = symbols.flatMap((Symbol, i) => tables[i].map((d) => ({Symbol, ...d})));

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

root
  .data(data)
  .transform(cm.encode, {x: "Date", y: "Close", stroke: "Symbol"})
  .transform(cm.plane, {y: {grid: true}})
  .add(cm.lineY);

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