D3

From TerminalSkills/skills · View interactive version

D3 is an AI agent skill from the GitHub repository TerminalSkills/skills. Assists with building custom interactive data visualizations using D3.js. Use when creating charts, graphs, maps, force layouts, or hierarchical diagrams that require fine-grained control beyond what charting libraries provide. Trigger words: d3, data visualization, chart, svg, scales, force graph,

How to use this skill

Skill file

D3.js

Overview

D3.js is a low-level JavaScript library for building custom, interactive data visualizations by binding data to DOM elements. It provides scales, axes, shape generators, geographic projections, force simulations, and hierarchical layouts, giving developers full control over the visual output while integrating with React through computed scales and layouts.

Install with npm install d3 and import * as d3 from "d3" (v7 is published as ES modules), or load https://cdn.jsdelivr.net/npm/d3@7 in a script tag. Import single modules (d3-scale, d3-shape) to keep bundles small.

Instructions

A minimal line chart (runs against D3 7.9.0):

const x = d3.scaleTime(d3.extent(sales, d => d.date), [48, 620]);
const y = d3.scaleLinear([0, d3.max(sales, d => d.revenue)], [280, 20]).nice();
const line = d3.line().x(d => x(d.date)).y(d => y(d.revenue)).curve(d3.curveMonotoneX);
svg.append("path").attr("d", line(sales)).attr("fill", "none").attr("stroke", "#2563eb");
svg.append("g").attr("transform", "translate(48,0)").call(d3.axisLeft(y).tickFormat(d3.format(",.0f")));

Examples

Data join with a transition (bars from a band scale):

const x = d3.scaleBand(regions.map(d => d.name), [48, 620]).padding(0.2);
svg.selectAll("rect").data(regions, d => d.name).join("rect")
  .attr("x", d => x(d.name)).attr("width", x.bandwidth())
  .attr("y", y(0)).attr("height", 0)
  .transition().duration(750).attr("y", d => y(d.total)).attr("height", d => y(0) - y(d.total));

Force layout setup:

const sim = d3.forceSimulation(nodes)
  .force("link", d3.forceLink(links).id(d => d.id).distance(60))
  .force("charge", d3.forceManyBody().strength(-120))
  .force("center", d3.forceCenter(320, 240))
  .on("tick", () => { /* update circle cx/cy and line x1..y2 from node positions */ });

Example 1: Build a real-time dashboard with multiple chart types

User request: "Create a dashboard with line charts, bar charts, and a pie chart from API data"

Actions:

  1. Set up shared scales with d3.scaleTime() for the x-axis and d3.scaleLinear() for the y-axis
  2. Build a line chart using d3.line() with smooth curve interpolation and area fill
  3. Build a grouped bar chart using d3.scaleBand() with staggered enter transitions
  4. Build a donut chart using d3.pie() and d3.arc() with hover tooltips

Output: A responsive dashboard with animated charts that update smoothly when data changes.

Example 2: Create a force-directed network graph

User request: "Visualize relationships between entities as an interactive network graph"

Actions:

  1. Set up d3.forceSimulation() with forceManyBody, forceLink, and forceCenter
  2. Render nodes as circles with d3.scaleOrdinal() coloring by category
  3. Add drag interaction with d3.drag() to reposition nodes
  4. Implement zoom and pan with d3.zoom() and tooltips on hover

Output: An interactive force-directed graph with draggable nodes, color-coded categories, and zoom navigation.

Guidelines

Use this skill with your AI agent: Open on Skill Issue to copy, download, or add to your library. Works with Claude, ChatGPT, Gemini, Cursor, Windsurf and more.