PTENES
MODULE 2.3

📊 D3.js — Data Is the Content

A data visualization pattern. Static data becomes powerful animations—you provide the dataset and describe the visual, and Claude does the rest.

6
Topics
~40
Minutes
📊
Data-driven
⚡
Intermediate
1

🌐 What D3 is and why it’s the standard

Data-Driven Documents — D3 binds data directly to DOM/SVG elements. It's not a library of "ready-made charts"; it's a mechanism for transforming data into visual shapes. That gives you flexibility that's impossible with Chart.js or others.

📄 Data [ 42, 78, 55, 91 ] ⚙️ D3 binding selectAll + data() 🎨 SVG/DOM <rect height="42px"> + transition()

🧠 The core paradigm

In D3, you don’t say “draw a 42px bar.” You say "bind this array to the set of rectangles" — and D3 automatically maps each value to an SVG attribute via scales. Change the data, and the visual changes with it.

selectAll()
Selects elements
.data()
Binds an array
.enter()
New elements
scales
Data → pixels
2

🏁 Bar race — ranking in motion

O bar race (bar race) is the most impactful chart for time-based comparisons. Each category gets screen time proportional to its value—and when the ranking changes, the bars reorder smoothly.

live demo — bar race
Brazil
🇧🇷
USA
🇺🇸
China
🇨🇳
India
🇮🇳
Europe
🇪🇺

↑ Bars grow and shrink over time — in D3+Remotion, each frame = interpolation between two data states

how to prompt Claude
// Prompt:
"Crie um bar race Remotion com D3.js mostrando evolução
 anual de 5 países. Dados: [ { ano: 2020, brasil: 42, ... } ]
 Duração: 10s, fps: 30, transição suave entre anos."

// D3 + Remotion equivalente:
const t = frame / fps; // tempo atual
const current = interpolateData(data, t);
bars.attr("width", d => xScale(current[d.name]));
interpolation
Between two snapshots
xScale
Value → width in px
frame/fps
Remotion time
reordering
dynamic yScale
3

🏗️ Animated architecture diagram

Nodes appear in sequence, and lines draw in to connect the topology. The stroke-dashoffset is the technique: you define the total length of the line and animate the offset from 100% → 0% — a "progressive drawing" effect.

live demo — line drawing
Client Auth API DB Server

↑ Nodes appear one by one; lines are drawn via animated stroke-dashoffset

💡 The stroke-dashoffset technique

Define stroke-dasharray: PATH_LENGTH and animate stroke-dashoffset of PATH_LENGTH up to 0. The line looks like it’s being drawn in real time. In Remotion, use interpolate(frame, [0, totalFrames], [pathLen, 0]).

dasharray
Total length
dashoffset
How much is hidden
getLength()
Actual path length
sequential
One node at a time
4

🤖 Clean data + a clear description = Claude does the rest

You don’t need to know D3 to use D3. You need to know what you want to show. The workflow is: clean the data → describe the visual → Claude generates the complete Remotion component with D3.

✗ Prompt without data context

"Create an animated sales chart"

Claude will invent fictional data, randomly choose a chart type, and probably use the wrong library.

✓ Prompt with real data

"Remotion + D3. Data: [{mes:'Jan',valor:42},{mes:'Fev',valor:78}...]. Vertical bar race. Bar grows from frame 0 to 60. Color: #3b82f6."

Exact, measurable, editable result.

1

Clean the data

CSV → JSON, remove nulls, normalize column names. Claude can do this too if you paste in the spreadsheet.

2

Describe the visuals

Chart type, color palette, duration, transitions. The more specific you are, the more precise the result.

3

Claude generates the component

Remotion + D3 + scales + animation + synchronized seeking. Ready to run with npx remotion dev.

4

Iterate in the conversation

"Increase the label font size", "change the Brazil bar color to blue", "add an average line". Each request refines the component.

CSV/JSON
Provide the data
Specification
Visual + duration
Iteration
Quick refinement
Result
Exportable MP4
5

🗃️ Data-Driven Use Cases

D3 isn’t just bars. The library covers every kind of visualization — from geographic maps to hierarchical trees. The key is identifying which data type you have and what visual form communicates it best.

📊

Bar race / charts

Temporal comparisons, rankings, business metrics. Data: categorical time series.

🏗️

Architecture diagrams

Nodes and edges. Force layout or manual positioning. Data: system graph/topology.

🗺️

Geographic maps

Routes, choropleth (heat map), points of interest. Data: GeoJSON + topojson.

🌲

Hierarchies / treemaps

Parent-child structures, budgets, taxonomies. Data: JSON tree with parent/children.

📈

Trend lines

Growth over time, curve comparisons. Data: continuous series.

🌐

Network graphs

Connections between entities, data flows, social networks. Data: nodes + edges.

Temporal
Bar race / lines
Geo
Maps + GeoJSON
Hierarchical
Treemap / sunburst
Relational
Force graph
6

⚠️ Common pitfalls with D3 + Remotion

D3 was created for the browser — it manipulates the DOM directly. Remotion renders on the server side (headless). This combination has specific gotchas that you need to know.

✗ Common mistakes

  • ✗Use d3.transition() — causes clock-based animation, not frame-based animation.
  • ✗Select DOM in the render function — D3 manipulates the DOM, React manages the DOM. Conflict.
  • ✗Calculate scales inside the render — recalculates every frame and hurts performance.

✓ The right approach

  • ✓Use D3 only for calculations (scales, paths, layouts)—let React render.
  • ✓Manual interpolation via interpolate(frame, ...) Remotion's.
  • ✓Scales in useMemo — calculates once per composition.

💡 D3+Remotion rule

Use D3 as math calculator, not as an animator. D3 calculates where things should be; React renders; Remotion captures the frame. When you ask Claude, specify: "Use D3 only for scales and path generators, not for direct animation".

scales
D3 calculates
paths
D3 generates an SVG path
animation
Remotion interpolates
render
React handles

📋 Module Summary

What we learned

  • ✓D3 binds data to visual elements — it’s not a chart library
  • ✓Bar race = interpolation between data snapshots over time
  • ✓stroke-dashoffset animates the "drawing" of SVG lines
  • ✓Clean data + visual description = Claude generates everything
  • ✓D3 as a math calculator, not a direct animator
  • ✓Avoid d3.transition() — use Remotion’s interpolate()

Next module

🧊 2.4 — Three.js

3D scenes rendered frame by frame. Materials, cameras, and procedural geometry for 3D motion graphics with WebGL.