@vibes.diy/prompts 0.1.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE.md +232 -0
- package/README.md +29 -0
- package/catalog.d.ts +1 -0
- package/catalog.js +4 -0
- package/catalog.js.map +1 -0
- package/chat.d.ts +134 -0
- package/chat.js +2 -0
- package/chat.js.map +1 -0
- package/index.d.ts +7 -0
- package/index.js +8 -0
- package/index.js.map +1 -0
- package/json-docs.d.ts +29 -0
- package/json-docs.js +42 -0
- package/json-docs.js.map +1 -0
- package/llms/callai.json +9 -0
- package/llms/callai.txt +455 -0
- package/llms/d3.json +9 -0
- package/llms/d3.md +679 -0
- package/llms/fireproof.json +9 -0
- package/llms/fireproof.txt +451 -0
- package/llms/image-gen.json +9 -0
- package/llms/image-gen.txt +128 -0
- package/llms/three-js.json +9 -0
- package/llms/three-js.md +2232 -0
- package/llms/web-audio.json +8 -0
- package/llms/web-audio.txt +220 -0
- package/load-docs.d.ts +5 -0
- package/load-docs.js +28 -0
- package/load-docs.js.map +1 -0
- package/package.json +38 -0
- package/prompts.d.ts +36 -0
- package/prompts.js +307 -0
- package/prompts.js.map +1 -0
- package/settings.d.ts +16 -0
- package/settings.js +2 -0
- package/settings.js.map +1 -0
- package/tsconfig.json +22 -0
- package/txt-docs.d.ts +16 -0
- package/txt-docs.js +40 -0
- package/txt-docs.js.map +1 -0
- package/view-state.d.ts +17 -0
- package/view-state.js +2 -0
- package/view-state.js.map +1 -0
package/llms/d3.md
ADDED
|
@@ -0,0 +1,679 @@
|
|
|
1
|
+
# D3.js Reference for Coding Agents
|
|
2
|
+
|
|
3
|
+
**D3** (Data-Driven Documents) is a JavaScript library for creating bespoke data visualizations using web standards (SVG, Canvas, HTML). This guide provides essential patterns and examples for AI coding agents to create amazing D3 demonstrations.
|
|
4
|
+
|
|
5
|
+
## Core Philosophy
|
|
6
|
+
|
|
7
|
+
D3 is a **low-level toolbox** of 30+ modules that work together. It's not a chart library but a collection of primitives for building custom visualizations. Think of it as "assembly language for data visualization."
|
|
8
|
+
|
|
9
|
+
## Essential Setup
|
|
10
|
+
|
|
11
|
+
```js
|
|
12
|
+
import * as d3 from "d3";
|
|
13
|
+
// Or import specific modules
|
|
14
|
+
import { select, scaleLinear, line } from "d3";
|
|
15
|
+
```
|
|
16
|
+
|
|
17
|
+
## The D3 Pattern: Select, Bind, Transform
|
|
18
|
+
|
|
19
|
+
Every D3 visualization follows this pattern:
|
|
20
|
+
|
|
21
|
+
1. **Select** DOM elements
|
|
22
|
+
2. **Bind** data to elements
|
|
23
|
+
3. **Transform** elements based on data
|
|
24
|
+
|
|
25
|
+
```js
|
|
26
|
+
// Basic pattern
|
|
27
|
+
d3.select("body") // Select
|
|
28
|
+
.selectAll("div") // Select all divs
|
|
29
|
+
.data([4, 8, 15, 16]) // Bind data
|
|
30
|
+
.enter()
|
|
31
|
+
.append("div") // Enter new elements
|
|
32
|
+
.style("width", (d) => d * 10 + "px") // Transform
|
|
33
|
+
.text((d) => d);
|
|
34
|
+
```
|
|
35
|
+
|
|
36
|
+
## Essential Modules & Quick Reference
|
|
37
|
+
|
|
38
|
+
### 1. d3-selection (DOM Manipulation)
|
|
39
|
+
|
|
40
|
+
```js
|
|
41
|
+
// Selection basics
|
|
42
|
+
d3.select("#chart"); // Select by ID
|
|
43
|
+
d3.selectAll(".bar"); // Select by class
|
|
44
|
+
selection.append("rect"); // Add element
|
|
45
|
+
selection.attr("width", 100); // Set attribute
|
|
46
|
+
selection.style("fill", "red"); // Set style
|
|
47
|
+
selection.text("Hello"); // Set text content
|
|
48
|
+
|
|
49
|
+
// Data joining (the D3 way)
|
|
50
|
+
const bars = svg
|
|
51
|
+
.selectAll(".bar")
|
|
52
|
+
.data(data)
|
|
53
|
+
.enter()
|
|
54
|
+
.append("rect")
|
|
55
|
+
.attr("class", "bar");
|
|
56
|
+
```
|
|
57
|
+
|
|
58
|
+
### 2. d3-scale (Data Encoding)
|
|
59
|
+
|
|
60
|
+
```js
|
|
61
|
+
// Linear scale (continuous data)
|
|
62
|
+
const x = d3
|
|
63
|
+
.scaleLinear()
|
|
64
|
+
.domain([0, 100]) // Input range
|
|
65
|
+
.range([0, 500]); // Output range (pixels)
|
|
66
|
+
|
|
67
|
+
// Ordinal scale (categorical data)
|
|
68
|
+
const color = d3
|
|
69
|
+
.scaleOrdinal()
|
|
70
|
+
.domain(["A", "B", "C"])
|
|
71
|
+
.range(["red", "blue", "green"]);
|
|
72
|
+
|
|
73
|
+
// Time scale
|
|
74
|
+
const x = d3
|
|
75
|
+
.scaleTime()
|
|
76
|
+
.domain([new Date(2020, 0, 1), new Date(2024, 0, 1)])
|
|
77
|
+
.range([0, 800]);
|
|
78
|
+
|
|
79
|
+
// Band scale (for bar charts)
|
|
80
|
+
const x = d3
|
|
81
|
+
.scaleBand()
|
|
82
|
+
.domain(data.map((d) => d.name))
|
|
83
|
+
.range([0, width])
|
|
84
|
+
.padding(0.1);
|
|
85
|
+
```
|
|
86
|
+
|
|
87
|
+
### 3. d3-shape (SVG Path Generators)
|
|
88
|
+
|
|
89
|
+
```js
|
|
90
|
+
// Line generator
|
|
91
|
+
const line = d3
|
|
92
|
+
.line()
|
|
93
|
+
.x((d) => x(d.date))
|
|
94
|
+
.y((d) => y(d.value))
|
|
95
|
+
.curve(d3.curveCardinal); // Smooth curves
|
|
96
|
+
|
|
97
|
+
// Area generator
|
|
98
|
+
const area = d3
|
|
99
|
+
.area()
|
|
100
|
+
.x((d) => x(d.date))
|
|
101
|
+
.y0(y(0)) // Baseline
|
|
102
|
+
.y1((d) => y(d.value)); // Top line
|
|
103
|
+
|
|
104
|
+
// Arc generator (for pie charts)
|
|
105
|
+
const arc = d3.arc().innerRadius(0).outerRadius(radius);
|
|
106
|
+
```
|
|
107
|
+
|
|
108
|
+
### 4. d3-axis (Chart Axes)
|
|
109
|
+
|
|
110
|
+
```js
|
|
111
|
+
// Create axes
|
|
112
|
+
const xAxis = d3.axisBottom(x);
|
|
113
|
+
const yAxis = d3.axisLeft(y);
|
|
114
|
+
|
|
115
|
+
// Render axes
|
|
116
|
+
svg.append("g").attr("transform", `translate(0,${height})`).call(xAxis);
|
|
117
|
+
|
|
118
|
+
svg.append("g").call(yAxis);
|
|
119
|
+
```
|
|
120
|
+
|
|
121
|
+
### 5. d3-transition (Animation)
|
|
122
|
+
|
|
123
|
+
```js
|
|
124
|
+
// Smooth transitions
|
|
125
|
+
selection
|
|
126
|
+
.transition()
|
|
127
|
+
.duration(1000)
|
|
128
|
+
.attr("width", newWidth)
|
|
129
|
+
.style("opacity", 0.5);
|
|
130
|
+
|
|
131
|
+
// Staggered transitions
|
|
132
|
+
bars
|
|
133
|
+
.transition()
|
|
134
|
+
.delay((d, i) => i * 100)
|
|
135
|
+
.duration(500)
|
|
136
|
+
.attr("height", (d) => y(d.value));
|
|
137
|
+
```
|
|
138
|
+
|
|
139
|
+
## Complete Examples for Coding Agents
|
|
140
|
+
|
|
141
|
+
### 1. Simple Bar Chart
|
|
142
|
+
|
|
143
|
+
```js
|
|
144
|
+
import * as d3 from "d3";
|
|
145
|
+
|
|
146
|
+
const data = [4, 8, 15, 16, 23, 42];
|
|
147
|
+
const width = 500;
|
|
148
|
+
const height = 300;
|
|
149
|
+
const margin = {top: 20, right: 20, bottom: 30, left: 40};
|
|
150
|
+
|
|
151
|
+
const x = d3.scaleBand()
|
|
152
|
+
.domain(d3.range(data.length))
|
|
153
|
+
.range([margin.left, width - margin.right])
|
|
154
|
+
.padding(0.1);
|
|
155
|
+
|
|
156
|
+
const y = d3.scaleLinear()
|
|
157
|
+
.domain([0, d3.max(data)])
|
|
158
|
+
.range([height - margin.bottom, margin.top]);
|
|
159
|
+
|
|
160
|
+
const svg = d3.select("#chart")
|
|
161
|
+
.append("svg")
|
|
162
|
+
.attr("width", width)
|
|
163
|
+
.attr("height", height);
|
|
164
|
+
|
|
165
|
+
svg.selectAll(".bar")
|
|
166
|
+
.data(data)
|
|
167
|
+
.enter().append("rect")
|
|
168
|
+
.attr("class", "bar")
|
|
169
|
+
.attr("x", (d, i) => x(i))
|
|
170
|
+
.attr("y", d => y(d))
|
|
171
|
+
.attr("width", x.bandwidth())
|
|
172
|
+
.attr("height", d => height - margin.bottom - y(d))
|
|
173
|
+
.attr("fill", "steelblue");
|
|
174
|
+
|
|
175
|
+
// Add axes
|
|
176
|
+
svg.append("g")
|
|
177
|
+
.attr("transform", `translate(0,${height - margin.bottom})`)
|
|
178
|
+
.call(d3.axisBottom(x));
|
|
179
|
+
|
|
180
|
+
svg.append("g")
|
|
181
|
+
.attr("transform", `translate(${margin.left},0)`)
|
|
182
|
+
.call(d3.axisLeft(y));
|
|
183
|
+
</script>
|
|
184
|
+
```
|
|
185
|
+
|
|
186
|
+
### 2. Interactive Scatter Plot
|
|
187
|
+
|
|
188
|
+
```js
|
|
189
|
+
import * as d3 from "d3";
|
|
190
|
+
|
|
191
|
+
// Generate sample data
|
|
192
|
+
const data = d3.range(100).map(() => ({
|
|
193
|
+
x: Math.random() * 100,
|
|
194
|
+
y: Math.random() * 100,
|
|
195
|
+
radius: Math.random() * 10 + 2,
|
|
196
|
+
}));
|
|
197
|
+
|
|
198
|
+
const width = 600;
|
|
199
|
+
const height = 400;
|
|
200
|
+
const margin = { top: 20, right: 20, bottom: 30, left: 40 };
|
|
201
|
+
|
|
202
|
+
const x = d3
|
|
203
|
+
.scaleLinear()
|
|
204
|
+
.domain(d3.extent(data, (d) => d.x))
|
|
205
|
+
.range([margin.left, width - margin.right]);
|
|
206
|
+
|
|
207
|
+
const y = d3
|
|
208
|
+
.scaleLinear()
|
|
209
|
+
.domain(d3.extent(data, (d) => d.y))
|
|
210
|
+
.range([height - margin.bottom, margin.top]);
|
|
211
|
+
|
|
212
|
+
const svg = d3
|
|
213
|
+
.select("#scatter")
|
|
214
|
+
.append("svg")
|
|
215
|
+
.attr("width", width)
|
|
216
|
+
.attr("height", height);
|
|
217
|
+
|
|
218
|
+
// Create circles with interactions
|
|
219
|
+
svg
|
|
220
|
+
.selectAll("circle")
|
|
221
|
+
.data(data)
|
|
222
|
+
.enter()
|
|
223
|
+
.append("circle")
|
|
224
|
+
.attr("cx", (d) => x(d.x))
|
|
225
|
+
.attr("cy", (d) => y(d.y))
|
|
226
|
+
.attr("r", (d) => d.radius)
|
|
227
|
+
.attr("fill", "steelblue")
|
|
228
|
+
.attr("opacity", 0.7)
|
|
229
|
+
.on("mouseover", function (event, d) {
|
|
230
|
+
d3.select(this)
|
|
231
|
+
.transition()
|
|
232
|
+
.attr("r", d.radius * 1.5)
|
|
233
|
+
.attr("fill", "orange");
|
|
234
|
+
})
|
|
235
|
+
.on("mouseout", function (event, d) {
|
|
236
|
+
d3.select(this).transition().attr("r", d.radius).attr("fill", "steelblue");
|
|
237
|
+
});
|
|
238
|
+
|
|
239
|
+
// Add axes
|
|
240
|
+
svg
|
|
241
|
+
.append("g")
|
|
242
|
+
.attr("transform", `translate(0,${height - margin.bottom})`)
|
|
243
|
+
.call(d3.axisBottom(x));
|
|
244
|
+
|
|
245
|
+
svg
|
|
246
|
+
.append("g")
|
|
247
|
+
.attr("transform", `translate(${margin.left},0)`)
|
|
248
|
+
.call(d3.axisLeft(y));
|
|
249
|
+
```
|
|
250
|
+
|
|
251
|
+
### 3. Force-Directed Network
|
|
252
|
+
|
|
253
|
+
```js
|
|
254
|
+
import * as d3 from "d3";
|
|
255
|
+
|
|
256
|
+
const nodes = d3.range(30).map((i) => ({ id: i }));
|
|
257
|
+
const links = d3.range(nodes.length - 1).map((i) => ({
|
|
258
|
+
source: Math.floor(Math.sqrt(i)),
|
|
259
|
+
target: i + 1,
|
|
260
|
+
}));
|
|
261
|
+
|
|
262
|
+
const width = 600;
|
|
263
|
+
const height = 400;
|
|
264
|
+
|
|
265
|
+
const svg = d3
|
|
266
|
+
.select("#network")
|
|
267
|
+
.append("svg")
|
|
268
|
+
.attr("width", width)
|
|
269
|
+
.attr("height", height);
|
|
270
|
+
|
|
271
|
+
const simulation = d3
|
|
272
|
+
.forceSimulation(nodes)
|
|
273
|
+
.force(
|
|
274
|
+
"link",
|
|
275
|
+
d3.forceLink(links).id((d) => d.id),
|
|
276
|
+
)
|
|
277
|
+
.force("charge", d3.forceManyBody().strength(-300))
|
|
278
|
+
.force("center", d3.forceCenter(width / 2, height / 2));
|
|
279
|
+
|
|
280
|
+
const link = svg
|
|
281
|
+
.append("g")
|
|
282
|
+
.selectAll("line")
|
|
283
|
+
.data(links)
|
|
284
|
+
.enter()
|
|
285
|
+
.append("line")
|
|
286
|
+
.attr("stroke", "#999")
|
|
287
|
+
.attr("stroke-width", 2);
|
|
288
|
+
|
|
289
|
+
const node = svg
|
|
290
|
+
.append("g")
|
|
291
|
+
.selectAll("circle")
|
|
292
|
+
.data(nodes)
|
|
293
|
+
.enter()
|
|
294
|
+
.append("circle")
|
|
295
|
+
.attr("r", 8)
|
|
296
|
+
.attr("fill", "steelblue")
|
|
297
|
+
.call(
|
|
298
|
+
d3.drag().on("start", dragstarted).on("drag", dragged).on("end", dragended),
|
|
299
|
+
);
|
|
300
|
+
|
|
301
|
+
simulation.on("tick", () => {
|
|
302
|
+
link
|
|
303
|
+
.attr("x1", (d) => d.source.x)
|
|
304
|
+
.attr("y1", (d) => d.source.y)
|
|
305
|
+
.attr("x2", (d) => d.target.x)
|
|
306
|
+
.attr("y2", (d) => d.target.y);
|
|
307
|
+
|
|
308
|
+
node.attr("cx", (d) => d.x).attr("cy", (d) => d.y);
|
|
309
|
+
});
|
|
310
|
+
|
|
311
|
+
function dragstarted(event) {
|
|
312
|
+
if (!event.active) simulation.alphaTarget(0.3).restart();
|
|
313
|
+
event.subject.fx = event.subject.x;
|
|
314
|
+
event.subject.fy = event.subject.y;
|
|
315
|
+
}
|
|
316
|
+
|
|
317
|
+
function dragged(event) {
|
|
318
|
+
event.subject.fx = event.x;
|
|
319
|
+
event.subject.fy = event.y;
|
|
320
|
+
}
|
|
321
|
+
|
|
322
|
+
function dragended(event) {
|
|
323
|
+
if (!event.active) simulation.alphaTarget(0);
|
|
324
|
+
event.subject.fx = null;
|
|
325
|
+
event.subject.fy = null;
|
|
326
|
+
}
|
|
327
|
+
```
|
|
328
|
+
|
|
329
|
+
### 4. Dynamic Line Chart with Real Data
|
|
330
|
+
|
|
331
|
+
```js
|
|
332
|
+
import * as d3 from "d3";
|
|
333
|
+
|
|
334
|
+
// Sample time series data
|
|
335
|
+
const data = d3.range(50).map((d, i) => ({
|
|
336
|
+
date: new Date(2024, 0, i + 1),
|
|
337
|
+
value: Math.sin(i * 0.1) * 50 + 50 + Math.random() * 20,
|
|
338
|
+
}));
|
|
339
|
+
|
|
340
|
+
const width = 700;
|
|
341
|
+
const height = 400;
|
|
342
|
+
const margin = { top: 20, right: 30, bottom: 40, left: 40 };
|
|
343
|
+
|
|
344
|
+
const x = d3
|
|
345
|
+
.scaleTime()
|
|
346
|
+
.domain(d3.extent(data, (d) => d.date))
|
|
347
|
+
.range([margin.left, width - margin.right]);
|
|
348
|
+
|
|
349
|
+
const y = d3
|
|
350
|
+
.scaleLinear()
|
|
351
|
+
.domain([0, d3.max(data, (d) => d.value)])
|
|
352
|
+
.range([height - margin.bottom, margin.top]);
|
|
353
|
+
|
|
354
|
+
const line = d3
|
|
355
|
+
.line()
|
|
356
|
+
.x((d) => x(d.date))
|
|
357
|
+
.y((d) => y(d.value))
|
|
358
|
+
.curve(d3.curveCardinal);
|
|
359
|
+
|
|
360
|
+
const svg = d3
|
|
361
|
+
.select("#line-chart")
|
|
362
|
+
.append("svg")
|
|
363
|
+
.attr("width", width)
|
|
364
|
+
.attr("height", height);
|
|
365
|
+
|
|
366
|
+
// Add gradient definition
|
|
367
|
+
const gradient = svg
|
|
368
|
+
.append("defs")
|
|
369
|
+
.append("linearGradient")
|
|
370
|
+
.attr("id", "line-gradient")
|
|
371
|
+
.attr("gradientUnits", "userSpaceOnUse")
|
|
372
|
+
.attr("x1", 0)
|
|
373
|
+
.attr("y1", height)
|
|
374
|
+
.attr("x2", 0)
|
|
375
|
+
.attr("y2", 0);
|
|
376
|
+
|
|
377
|
+
gradient
|
|
378
|
+
.append("stop")
|
|
379
|
+
.attr("offset", "0%")
|
|
380
|
+
.attr("stop-color", "lightblue")
|
|
381
|
+
.attr("stop-opacity", 0.1);
|
|
382
|
+
|
|
383
|
+
gradient
|
|
384
|
+
.append("stop")
|
|
385
|
+
.attr("offset", "100%")
|
|
386
|
+
.attr("stop-color", "steelblue")
|
|
387
|
+
.attr("stop-opacity", 0.8);
|
|
388
|
+
|
|
389
|
+
// Draw line
|
|
390
|
+
svg
|
|
391
|
+
.append("path")
|
|
392
|
+
.datum(data)
|
|
393
|
+
.attr("fill", "none")
|
|
394
|
+
.attr("stroke", "url(#line-gradient)")
|
|
395
|
+
.attr("stroke-width", 3)
|
|
396
|
+
.attr("d", line);
|
|
397
|
+
|
|
398
|
+
// Add dots with hover effects
|
|
399
|
+
svg
|
|
400
|
+
.selectAll(".dot")
|
|
401
|
+
.data(data)
|
|
402
|
+
.enter()
|
|
403
|
+
.append("circle")
|
|
404
|
+
.attr("class", "dot")
|
|
405
|
+
.attr("cx", (d) => x(d.date))
|
|
406
|
+
.attr("cy", (d) => y(d.value))
|
|
407
|
+
.attr("r", 4)
|
|
408
|
+
.attr("fill", "steelblue")
|
|
409
|
+
.on("mouseover", function (event, d) {
|
|
410
|
+
d3.select(this).attr("r", 6);
|
|
411
|
+
// Add tooltip
|
|
412
|
+
const tooltip = svg.append("g").attr("id", "tooltip");
|
|
413
|
+
tooltip
|
|
414
|
+
.append("rect")
|
|
415
|
+
.attr("x", x(d.date) + 10)
|
|
416
|
+
.attr("y", y(d.value) - 25)
|
|
417
|
+
.attr("width", 60)
|
|
418
|
+
.attr("height", 20)
|
|
419
|
+
.attr("fill", "black")
|
|
420
|
+
.attr("opacity", 0.8);
|
|
421
|
+
tooltip
|
|
422
|
+
.append("text")
|
|
423
|
+
.attr("x", x(d.date) + 15)
|
|
424
|
+
.attr("y", y(d.value) - 10)
|
|
425
|
+
.attr("fill", "white")
|
|
426
|
+
.style("font-size", "12px")
|
|
427
|
+
.text(d.value.toFixed(1));
|
|
428
|
+
})
|
|
429
|
+
.on("mouseout", function () {
|
|
430
|
+
d3.select(this).attr("r", 4);
|
|
431
|
+
svg.select("#tooltip").remove();
|
|
432
|
+
});
|
|
433
|
+
|
|
434
|
+
// Add axes
|
|
435
|
+
svg
|
|
436
|
+
.append("g")
|
|
437
|
+
.attr("transform", `translate(0,${height - margin.bottom})`)
|
|
438
|
+
.call(d3.axisBottom(x).tickFormat(d3.timeFormat("%b %d")));
|
|
439
|
+
|
|
440
|
+
svg
|
|
441
|
+
.append("g")
|
|
442
|
+
.attr("transform", `translate(${margin.left},0)`)
|
|
443
|
+
.call(d3.axisLeft(y));
|
|
444
|
+
```
|
|
445
|
+
|
|
446
|
+
## Key Modules Cheat Sheet
|
|
447
|
+
|
|
448
|
+
### Scales (Data Encoding)
|
|
449
|
+
|
|
450
|
+
```js
|
|
451
|
+
// Continuous scales
|
|
452
|
+
d3.scaleLinear(); // Numbers to numbers
|
|
453
|
+
d3.scaleTime(); // Dates to numbers
|
|
454
|
+
d3.scalePow(); // Power/sqrt scales
|
|
455
|
+
d3.scaleLog(); // Logarithmic scales
|
|
456
|
+
|
|
457
|
+
// Discrete scales
|
|
458
|
+
d3.scaleOrdinal(); // Categories to values
|
|
459
|
+
d3.scaleBand(); // Categories to positions (bar charts)
|
|
460
|
+
d3.scalePoint(); // Categories to points
|
|
461
|
+
|
|
462
|
+
// Color scales
|
|
463
|
+
d3.scaleSequential(d3.interpolateViridis); // Continuous colors
|
|
464
|
+
d3.scaleOrdinal(d3.schemeCategory10); // Categorical colors
|
|
465
|
+
```
|
|
466
|
+
|
|
467
|
+
### Shapes (SVG Path Generators)
|
|
468
|
+
|
|
469
|
+
```js
|
|
470
|
+
// Lines and areas
|
|
471
|
+
d3.line().x(x).y(y);
|
|
472
|
+
d3.area().x(x).y0(y0).y1(y1);
|
|
473
|
+
|
|
474
|
+
// Arcs and pies
|
|
475
|
+
d3.arc().innerRadius(r1).outerRadius(r2);
|
|
476
|
+
d3.pie().value((d) => d.value);
|
|
477
|
+
|
|
478
|
+
// Symbols (scatter plot markers)
|
|
479
|
+
d3.symbol().type(d3.symbolCircle).size(100);
|
|
480
|
+
```
|
|
481
|
+
|
|
482
|
+
### Arrays (Data Processing)
|
|
483
|
+
|
|
484
|
+
```js
|
|
485
|
+
// Statistics
|
|
486
|
+
d3.min(data, (d) => d.value);
|
|
487
|
+
d3.max(data, (d) => d.value);
|
|
488
|
+
d3.extent(data, (d) => d.value); // [min, max]
|
|
489
|
+
d3.mean(data, (d) => d.value);
|
|
490
|
+
d3.median(data, (d) => d.value);
|
|
491
|
+
|
|
492
|
+
// Grouping and nesting
|
|
493
|
+
d3.group(data, (d) => d.category);
|
|
494
|
+
d3.rollup(
|
|
495
|
+
data,
|
|
496
|
+
(v) => v.length,
|
|
497
|
+
(d) => d.category,
|
|
498
|
+
);
|
|
499
|
+
|
|
500
|
+
// Sorting
|
|
501
|
+
d3.ascending(a, b);
|
|
502
|
+
d3.descending(a, b);
|
|
503
|
+
```
|
|
504
|
+
|
|
505
|
+
### Forces (Physics Simulations)
|
|
506
|
+
|
|
507
|
+
```js
|
|
508
|
+
const simulation = d3
|
|
509
|
+
.forceSimulation(nodes)
|
|
510
|
+
.force("link", d3.forceLink(links))
|
|
511
|
+
.force("charge", d3.forceManyBody().strength(-300))
|
|
512
|
+
.force("center", d3.forceCenter(width / 2, height / 2))
|
|
513
|
+
.force("collision", d3.forceCollide().radius(10));
|
|
514
|
+
```
|
|
515
|
+
|
|
516
|
+
## Common Patterns for Amazing Demos
|
|
517
|
+
|
|
518
|
+
### 1. Responsive SVG Setup
|
|
519
|
+
|
|
520
|
+
```js
|
|
521
|
+
const container = d3.select("#chart");
|
|
522
|
+
const svg = container
|
|
523
|
+
.append("svg")
|
|
524
|
+
.attr("viewBox", `0 0 ${width} ${height}`)
|
|
525
|
+
.style("max-width", "100%")
|
|
526
|
+
.style("height", "auto");
|
|
527
|
+
```
|
|
528
|
+
|
|
529
|
+
### 2. Smooth Data Updates
|
|
530
|
+
|
|
531
|
+
```js
|
|
532
|
+
function updateChart(newData) {
|
|
533
|
+
const bars = svg.selectAll(".bar").data(newData);
|
|
534
|
+
|
|
535
|
+
// Update existing
|
|
536
|
+
bars
|
|
537
|
+
.transition()
|
|
538
|
+
.duration(750)
|
|
539
|
+
.attr("height", (d) => y(d.value));
|
|
540
|
+
|
|
541
|
+
// Add new
|
|
542
|
+
bars
|
|
543
|
+
.enter()
|
|
544
|
+
.append("rect")
|
|
545
|
+
.attr("class", "bar")
|
|
546
|
+
.attr("height", 0)
|
|
547
|
+
.transition()
|
|
548
|
+
.duration(750)
|
|
549
|
+
.attr("height", (d) => y(d.value));
|
|
550
|
+
|
|
551
|
+
// Remove old
|
|
552
|
+
bars.exit().transition().duration(750).attr("height", 0).remove();
|
|
553
|
+
}
|
|
554
|
+
```
|
|
555
|
+
|
|
556
|
+
### 3. Color Schemes
|
|
557
|
+
|
|
558
|
+
```js
|
|
559
|
+
// Beautiful built-in color schemes
|
|
560
|
+
d3.schemeCategory10; // 10 categorical colors
|
|
561
|
+
d3.schemeSet3; // 12 pastel colors
|
|
562
|
+
d3.interpolateViridis; // Blue to yellow gradient
|
|
563
|
+
d3.interpolatePlasma; // Purple to yellow gradient
|
|
564
|
+
d3.schemePaired; // Paired categorical colors
|
|
565
|
+
|
|
566
|
+
// Usage
|
|
567
|
+
const color = d3.scaleOrdinal(d3.schemeCategory10);
|
|
568
|
+
```
|
|
569
|
+
|
|
570
|
+
### 4. Tooltips and Interactions
|
|
571
|
+
|
|
572
|
+
```js
|
|
573
|
+
// Create tooltip div
|
|
574
|
+
const tooltip = d3.select("body").append("div")
|
|
575
|
+
.style("position", "absolute")
|
|
576
|
+
.style("padding", "10px")
|
|
577
|
+
.style("background", "rgba(0,0,0,0.8)")
|
|
578
|
+
.style("color", "white")
|
|
579
|
+
.style("border-radius", "5px")
|
|
580
|
+
.style("pointer-events", "none")
|
|
581
|
+
.style("opacity", 0);
|
|
582
|
+
|
|
583
|
+
// Add to elements
|
|
584
|
+
.on("mouseover", function(event, d) {
|
|
585
|
+
tooltip.transition().duration(200).style("opacity", .9);
|
|
586
|
+
tooltip.html(`Value: ${d.value}`)
|
|
587
|
+
.style("left", (event.pageX + 10) + "px")
|
|
588
|
+
.style("top", (event.pageY - 28) + "px");
|
|
589
|
+
})
|
|
590
|
+
.on("mouseout", function() {
|
|
591
|
+
tooltip.transition().duration(500).style("opacity", 0);
|
|
592
|
+
});
|
|
593
|
+
```
|
|
594
|
+
|
|
595
|
+
### 5. Data Loading and Processing
|
|
596
|
+
|
|
597
|
+
```js
|
|
598
|
+
// Load CSV data
|
|
599
|
+
d3.csv("data.csv").then((data) => {
|
|
600
|
+
// Process data
|
|
601
|
+
data.forEach((d) => {
|
|
602
|
+
d.value = +d.value; // Convert to number
|
|
603
|
+
d.date = d3.timeParse("%Y-%m-%d")(d.date); // Parse dates
|
|
604
|
+
});
|
|
605
|
+
|
|
606
|
+
// Create visualization
|
|
607
|
+
createChart(data);
|
|
608
|
+
});
|
|
609
|
+
|
|
610
|
+
// Load JSON data
|
|
611
|
+
d3.json("data.json").then(createChart);
|
|
612
|
+
```
|
|
613
|
+
|
|
614
|
+
## Pro Tips for Coding Agents
|
|
615
|
+
|
|
616
|
+
1. **Start with the canvas**: Set up SVG dimensions and margins first
|
|
617
|
+
2. **Define scales early**: Map your data domain to visual range before drawing
|
|
618
|
+
3. **Use method chaining**: D3's fluent API allows `selection.attr().style().on()`
|
|
619
|
+
4. **Leverage data joins**: Use enter/update/exit pattern for dynamic data
|
|
620
|
+
5. **Add transitions**: `transition().duration(500)` makes everything better
|
|
621
|
+
6. **Use color schemes**: Built-in color palettes save time and look professional
|
|
622
|
+
7. **Make it responsive**: Use viewBox for scalable graphics
|
|
623
|
+
8. **Add interactions**: hover, click, drag make demos engaging
|
|
624
|
+
9. **Consider performance**: Use Canvas for 1000+ elements, SVG for detailed graphics
|
|
625
|
+
10. **Test with real data**: Random data is fine for prototypes, realistic data for demos
|
|
626
|
+
|
|
627
|
+
## Quick Chart Types
|
|
628
|
+
|
|
629
|
+
### Bar Chart: `d3.scaleBand()` + `rect` elements
|
|
630
|
+
|
|
631
|
+
### Line Chart: `d3.line()` + `path` element
|
|
632
|
+
|
|
633
|
+
### Scatter Plot: `circle` elements with `cx`, `cy` from scales
|
|
634
|
+
|
|
635
|
+
### Pie Chart: `d3.pie()` + `d3.arc()` + `path` elements
|
|
636
|
+
|
|
637
|
+
### Network: `d3.forceSimulation()` + `line` + `circle` elements
|
|
638
|
+
|
|
639
|
+
### Map: `d3.geoPath()` + GeoJSON data
|
|
640
|
+
|
|
641
|
+
### Treemap: `d3.treemap()` + hierarchical data
|
|
642
|
+
|
|
643
|
+
### Heatmap: `rect` grid + `d3.scaleSequential()` colors
|
|
644
|
+
|
|
645
|
+
## Framework Integration
|
|
646
|
+
|
|
647
|
+
### React
|
|
648
|
+
|
|
649
|
+
```jsx
|
|
650
|
+
// Use refs for D3 DOM manipulation
|
|
651
|
+
const svgRef = useRef();
|
|
652
|
+
|
|
653
|
+
useEffect(() => {
|
|
654
|
+
const svg = d3.select(svgRef.current);
|
|
655
|
+
// D3 code here...
|
|
656
|
+
}, [data]);
|
|
657
|
+
|
|
658
|
+
return <svg ref={svgRef}></svg>;
|
|
659
|
+
```
|
|
660
|
+
|
|
661
|
+
## Performance & Best Practices
|
|
662
|
+
|
|
663
|
+
- Use `d3.select()` for single elements, `d3.selectAll()` for multiple
|
|
664
|
+
- Cache selections when reusing: `const bars = svg.selectAll(".bar")`
|
|
665
|
+
- Use Canvas for 1000+ data points, SVG for interactive/detailed graphics
|
|
666
|
+
- Debounce resize events for responsive charts
|
|
667
|
+
- Use `d3.csv()`, `d3.json()` for async data loading
|
|
668
|
+
- Leverage D3's built-in interpolators for smooth animations
|
|
669
|
+
|
|
670
|
+
## Resources for Inspiration
|
|
671
|
+
|
|
672
|
+
- [Observable D3 Gallery](https://observablehq.com/@d3/gallery)
|
|
673
|
+
- [Bl.ocks.org](https://bl.ocks.org) - Community examples
|
|
674
|
+
- [D3 Graph Gallery](https://d3-graph-gallery.com) - Chart type tutorials
|
|
675
|
+
- [Mike Bostock's Blocks](https://bl.ocks.org/mbostock) - Creator's examples
|
|
676
|
+
|
|
677
|
+
---
|
|
678
|
+
|
|
679
|
+
_This reference focuses on practical patterns for creating impressive D3 demonstrations. Each example is self-contained and copy-pasteable for rapid prototyping._
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "fireproof",
|
|
3
|
+
"label": "useFireproof",
|
|
4
|
+
"llmsTxtUrl": "https://use-fireproof.com/llms-full.txt",
|
|
5
|
+
"module": "use-fireproof",
|
|
6
|
+
"description": "local-first database with encrypted live sync",
|
|
7
|
+
"importModule": "use-fireproof",
|
|
8
|
+
"importName": "useFireproof"
|
|
9
|
+
}
|