d3-js-visualization
Creating interactive data visualisations using d3.js. This skill should be used when creating custom charts, graphs, network diagrams, geographic visualisations, or any complex SVG-based data visualisation that requires fine-grained control over visual elements, transitions, or interactions. Use this for bespoke visualisations beyond standard charting libraries, whether in React, Vue, Svelte, vanilla JavaScript, or any other environment.
1How It Works & Mechanism
Pure knowledge-base skill: All capabilities reside within a single markdown file, SKILL.md (820 lines / 21.3KB), featuring zero scripts, zero configurations, and zero runtime overhead. It performs no actions on its own, merely providing d3.js engineering methodologies to the host model as copy-ready code blocks.
This skill provides guidance for creating sophisticated, interactive data visualisations using d3.js. D3.js (Data-Driven Documents) excels at binding data to DOM elements and applying data-driven transformations to create custom, publication-quality visualisations with precise control over every visual element. The techniques work across any JavaScript environment, including vanilla JavaScript, React, Vue, Svelte, and other frameworks.
Context budget managed via progressive disclosure: The root SKILL.md only includes architectural decision guidance, a 4-step workflow, and common chart types; specialized topics (hierarchies, geographic maps, brush/link interactions, complete animation palettes) are delegated to 3 reference documents, templates are placed in assets, and explicit instructions at the end guide on-demand retrieval.
To use these resources, read the relevant files when detailed guidance is needed for specific visualisation types or patterns.
Cross-framework portability is achieved by consolidating framework variances into two integration paths: Pattern A imperatively manipulates the DOM (compatible with any JS environment), while Pattern B uses d3 solely for mathematical/layout calculations, delegating rendering back to the framework.
**Pattern A: Direct DOM manipulation (recommended for most cases)** Use d3 to select DOM elements and manipulate them imperatively. This works in any JavaScript environment:
Strict output discipline: Enforces that all plotting functions follow an identical skeleton (early exit on empty data → clear SVG → margin convention → scales/axes → data join), supported by numbered comments structured as a checklist to constrain unwarranted model hallucinations.
function drawVisualization(data) { if (!data || data.length === 0) return; const svg = d3.select('#chart'); // Or pass a selector/element svg.selectAll("*").remove(); // Clear previous render
Pinned strictly to d3 v7: CDN endpoints explicitly reference v7, and sample code adopts v7 API idioms (.join() binding, passing events as the first handler argument, absence of d3.event), rendering generated code incompatible with legacy d3 v5/v6 projects.
<script src="https://d3js.org/d3.v7.min.js"></script>
Non-chart engineering details are explicitly covered rather than left for the model to guess: Responsiveness (dual approaches with window resize listeners and ResizeObserver returning cleanup functions), accessibility (role/title/desc), performance degradation paths (transitioning to canvas for >1000 elements), and common troubleshooting steps.
### Accessibility Make visualisations accessible: ```javascript // Add ARIA labels svg.attr("role", "img") .attr("aria-label", "Bar chart showing quarterly revenue"); // Add title and description svg.append("title").text("Quarterly Revenue 2024"); svg.append("desc").text("Bar chart showing revenue growth across four quarters");
2Core Capabilities
3External Dependencies
| Kind | Dependency / Endpoint |
|---|---|
| package | d3(npm 包,v7 API 面) |
| network | d3js.org CDN(d3 v7 min,产物可选的加载路径,无 SRI) |
| package | react(assets 模板 import) |
| package | tailwindcss(模板用 className 原子类,无显式 import,隐含样式依赖) |
4Recommended Governance & Evidence
- SKILL.md: Pure prompt instructions and rule text; involves no script execution or external impact.
5Second-Pass Verification
- [ok] official_desc 原文(frontmatter description) — SKILL.md:1-4 有完整 frontmatter,name: d3-viz、description 441 字符,已原文写入 meta.official_desc。
- [ok] 外部资源/网络端点清单 — 全目录 URL 扫描仅 1 条:https://d3js.org/d3.v7.min.js(SKILL.md:39)。无 fetch/d3.json/d3.csv/XMLHttpRequest 调用,无 API 端点、无 CLI 调用;地理图几何数据由调用方传入(d3-patterns.md:608-611),无 tile/topojson/atlas 拉取(grep topojson|us-atlas|world-atlas 零命中)。
- [ok] 凭证/环境变量读取 — grep process.env|API_KEY|token|secret|localStorage 全目录零命中(不区分大小写)。
- [ok] 脚本执行面 — 无 scripts/ 目录、无 package.json、无 shell/python 文件;两份 .jsx 是给宿主工程用的 UI 模板,不被 skill 执行。唯一 exec 相关字面命中为 references/d3-patterns.md:366 `defs.append("linearGradient")` 中的 `fs.`(假阳性),人工确认非文件系统调用。
- [discrepancy] 资源引用与实际文件名一致 — SKILL.md:814-815 写 `chart-template.js` / `interactive-template.js`,实际文件为 `assets/chart-template.jsx` 与 `assets/interactive-template.jsx`(React JSX)。模型按正文文件名读取 assets 会落空(`.js` 不存在),只能靠 glob 或猜后缀——这是第一遍照抄资源段时容易漏掉的错,第二遍以 find 结果纠正。
- [discrepancy] 『跨框架可用』宣称与模板实现一致 — SKILL.md:3(description)与 SKILL.md:10(Overview)宣称 React/Vue/Svelte/vanilla 全支持,SKILL.md:818 更称两份模板『work with vanilla JavaScript, React, Vue, Svelte』;但两份模板首行均 `import { useEffect, useRef, useState } from 'react'`(chart-template.jsx:1、interactive-template.jsx:1),且正文/模板使用 Tailwind 类名(chart-template.jsx:78,101 `className="chart-container"` / `className="p-8"`)。Vue/Svelte 在全文只出现于 description 与 Pattern B 注释,无任何示例代码——宣称覆盖面被高估,实际是 React+Tailwind 优先、vanilla 次之。
- [ok] 地理可视化能力是否真实(描述主张之一) — 主干 SKILL.md 无地理章节(第一遍据此可能误判为『宣称未兑现』),但 references/d3-patterns.md:594 起确有 `## Geographic visualisations`,含 Basic map with points 与 Choropleth 两个完整实现(d3.geoMercator + geoPath)。结论修正为:能力存在,但只在下沉文档里,主干不提示地理章节位置。
- [ok] d3 版本一致性 — CDN 显式 v7(SKILL.md:39),示例用 .join() 与事件形参写法(v7 语义);全文无 d3.event(v5 语义)残留。
6Verdict & Scenarios
e9922be6af3bb98d…