Communitygithub.com

D3.js 技能

协助使用 D3.js 构建自定义交互式数据可视化。适用于需要超出图表库控制范围的图表、图形、地图、力导向布局或层级图。触发词:d3、数据可视化、图表、SVG、比例尺、力导向图、树图、分级统计图。

D3.js 技能 是什么?

D3.js 是一种底层 JavaScript 库,通过将数据绑定到 DOM 元素来构建自定义的交互式数据可视化。它提供比例尺、坐标轴、形状生成器、地理投影、力模拟和层级布局,让开发者完全控制视觉输出,同时通过计算的比例尺和布局与 React 集成。该技能指导如何使用 D3 v6+ 的数据绑定模式(`.join()` 方法)、选择合适的比例尺类型、使用形状生成器和布局函数创建复杂图表、添加过渡和交互效果(如悬停提示、缩放、拖拽),以及在 React 应用中分离 D3 的数学计算和 React 的 DOM 渲染。开发者将学习如何构建实时仪表板(包含折线图、条形图和饼图)以及交互式力导向网络图,同时掌握最佳实践,如用 `d3.format()` 格式化标签、用 `ResizeObserver` 实现响应式布局、以及用 `aria-label` 和 `<title>` 标签提升可访问性。

兼容平台~Claude Code~Codex CLI~Cursor
npx skills add https://github.com/TerminalSkills/skills/tree/HEAD/skills/d3

在你喜欢的 AI 中提问

打开一个已预加载此 Agent Skill 的新对话。

文档

D3.js

概览

D3.js 是一个底层 JavaScript 库,用于通过将数据绑定到 DOM 元素来构建自定义的交互式数据可视化。它提供比例尺、坐标轴、形状生成器、地理投影、力模拟和层次布局,让开发者完全控制视觉输出,同时通过计算的比例尺和布局与 React 集成。

使用说明

  • 绑定数据时,使用 .data(dataset).join("element")(D3 v6+)来实现 enter/update/exit 模式,通过访问器函数从绑定数据中设置属性和样式。
  • 创建比例尺时,选择合适的类型(连续型用 scaleLinear、分类条形图用 scaleBand、日期用 scaleTime、颜色映射用 scaleOrdinal),并始终使用 d3.extent() 从数据中设置 domain。
  • 构建坐标轴时,使用 d3.axisBottom(scale) 或 d3.axisLeft(scale) 创建,配置刻度和标签格式(d3.format()),并在窗口大小变化时重新生成。
  • 创建图表类型时,使用形状生成器(d3.line()、d3.area()、d3.arc()、d3.pie()、d3.stack())和布局函数(d3.treemap()、d3.forceSimulation()、d3.geoPath())实现复杂可视化。
  • 添加交互性时,使用过渡(.transition().duration(750))实现平滑数据更新、悬停提示、刷选和缩放。
  • 与 React 集成时,D3 负责数学计算(比例尺、布局、数据转换),React 负责 DOM 渲染,在 useMemo 中计算值,在 useEffect 中结合 ref 实现 D3 驱动的动画。

示例

示例 1:构建包含多种图表类型的实时仪表板

用户请求: “根据 API 数据创建包含折线图、条形图和饼图的仪表板”

操作步骤:

  1. 使用 d3.scaleTime() 和 d3.scaleLinear() 设置共享比例尺(x 轴和 y 轴)
  2. 使用 d3.line() 构建折线图,添加平滑曲线插值和面积填充
  3. 使用 d3.scaleBand() 构建分组条形图,添加交错进入过渡动画
  4. 使用 d3.pie() 和 d3.arc() 构建环形图,添加悬停提示

输出: 一个响应式仪表板,包含动画图表,数据变动时平滑更新。

示例 2:创建力导向网络图

用户请求: “将实体之间的关系可视化为交互式网络图”

操作步骤:

  1. 使用 d3.forceSimulation() 设置力模拟,包含 forceManyBody、forceLink 和 forceCenter
  2. 使用 d3.scaleOrdinal() 按类别着色,将节点渲染为圆
  3. 使用 d3.drag() 添加拖拽交互,重新定位节点
  4. 使用 d3.zoom() 实现缩放和平移,并添加悬停提示

输出: 一个交互式力导向图,包含可拖拽节点、颜色编码类别和缩放导航。

指南

  • 在 React 应用中使用 D3 进行比例尺计算、布局和数据处理,由 React 负责 DOM 渲染。
  • 使用 .join() 代替手动 enter/update/exit,实现更简洁、更清晰的数据绑定。
  • 始终使用 d3.extent() 从数据中设置比例尺的 domain,而不是硬编码范围。
  • 使用 d3.format() 格式化轴标签和提示:千位数用 ",.0f",货币用 "$.2f"。
  • 添加 750ms 的过渡动画,配以 easeCubicInOut 实现平滑数据更新动画。
  • 在 ResizeObserver 回调中重新计算比例尺,使可视化具有响应式。
  • 为 SVG 元素添加 aria-label 和有意义的 <title> 标签,提升可访问性。

Individual skills in this repo

This repo contains 20 individual skills — each has its own dedicated page.

TerminalSkills/skills

>- Run GitHub Actions locally with act. Use when a user asks to test GitHub Actions workflows locally, debug CI pipelines without pushing, or run workflows offline.

TerminalSkills/skills

>- You are an expert in AG2 (formerly AutoGen), the open-source multi-agent conversation framework. You help developers build systems where multiple AI agents collaborate through structured conversations — with tool use, human-in-the-loop, code execution, group chat orchestration, and nested conversations — for complex tasks like software development, research, and data analysis.

TerminalSkills/skills

>- Generate short-form videos with AI — script writing, text-to-speech narration, stock footage selection, subtitle generation, and video assembly. Use when: creating TikTok/YouTube Shorts/Reels content, automating video production, building content pipelines.

TerminalSkills/skills

>- Animate 3D objects and characters in Blender with Python. Use when the user wants to keyframe properties, create armatures and rigs, set up IK/FK chains, animate shape keys for facial animation, edit F-Curves, use the NLA editor to blend actions, add drivers for expression-based animation, or script any animation workflow in Blender.

TerminalSkills/skills

>- Automate motion capture and tracking workflows in Blender with Python. Use when the user wants to import BVH or FBX mocap data, retarget motion to armatures, track camera or object motion from video, solve camera motion, clean up motion capture data, or script any tracking pipeline in Blender.

TerminalSkills/skills

>- You are an expert in Bolt.new by StackBlitz, the AI-powered full-stack development environment that runs entirely in the browser. You help developers go from idea to deployed app in minutes using natural language prompts — Bolt generates complete applications with frontend, backend, database, and deployment, all running in a WebContainer without local setup.

TerminalSkills/skills

>- Assists with using Bun as an all-in-one JavaScript/TypeScript runtime, package manager, bundler, and test runner. Use when building HTTP servers, managing packages, running tests, or migrating from Node.js. Trigger words: bun, bun serve, bun install, bun test, bun build, javascript runtime, bun runtime.

TerminalSkills/skills

>- Self-host customer support with Chatwoot. Use when a user asks to set up open-source customer support, add live chat without SaaS costs, build a multi-channel inbox, or deploy a free Intercom alternative.

TerminalSkills/skills

>- You are an expert in Chi, the lightweight, idiomatic Go HTTP router built on `net/http`. You help developers build composable HTTP services using Chi's middleware stack, route groups, URL parameters, sub-routers, and context-based request scoping — providing Express-like ergonomics while staying 100% compatible with Go's standard library.

TerminalSkills/skills

When the user wants to edit, review, or improve existing marketing copy. Also use when the user mentions 'edit this copy,' 'review my copy,' 'copy feedback,' 'proofread,' 'polish this,' 'make this better,' or 'copy sweep.' This skill provides a systematic approach to editing marketing copy through multiple focused passes.

TerminalSkills/skills

>- Add live chat and customer support with Crisp. Use when a user asks to add a chat widget, implement live customer support, set up a help desk, create a knowledge base, or add a chatbot to a website.

TerminalSkills/skills

dbt (data build tool) transforms data in your warehouse using SQL SELECT statements. Learn project setup, models, tests, documentation, incremental materializations, and integration with data warehouses like PostgreSQL, BigQuery, and Snowflake.

TerminalSkills/skills

>- You are an expert in dlt, the open-source Python library for building data pipelines. You help developers load data from any API, file, or database into warehouses and lakes using simple Python decorators — with automatic schema inference, incremental loading, and built-in data contracts. dlt is the "requests library for data pipelines.

TerminalSkills/skills

Data Version Control for ML projects. Track large datasets and models alongside Git, build reproducible ML pipelines, and run experiments with metric comparison. Works with any storage backend including S3, GCS, Azure, and local filesystems.

TerminalSkills/skills

>- You are an expert in E2B, the cloud platform for running AI-generated code in secure sandboxes. You help developers give AI agents the ability to execute code, install packages, read/write files, and run long processes in isolated cloud environments — each sandbox is a lightweight VM that boots in ~150ms with full Linux, filesystem, and networking.

TerminalSkills/skills

>- Transcode, convert, edit, and process audio and video with FFmpeg. Use when a user asks to convert video formats (webm to mp4, mkv to mp4), extract audio from video, compress video files, trim or cut clips, concatenate videos, add subtitles, create thumbnails, apply filters, change resolution or bitrate, re-encode media, create GIFs from video, add watermarks, normalize audio, stream media, or build automated media processing pipelines.

TerminalSkills/skills

>- Create advanced animations with GSAP. Use when building complex timelines, scroll-driven animations, SVG morphing, text animations, or high-performance web animations beyond CSS capabilities.

TerminalSkills/skills

>- Generate deterministic MP4 videos from HTML, CSS, media, and seekable animations using HeyGen's HyperFrames framework. Use when someone asks to "render HTML to video", "make a video with HyperFrames", "create a programmatic video", "turn an animation into MP4", "build a launch/product video from HTML", "render GSAP/Lottie/Three.js to video", or set up an agent-driven video pipeline. Covers init/preview/render/add/lint/inspect commands, data-* timing attributes, animation adapters, and the component catalog.

TerminalSkills/skills

When the user wants to perform load testing, stress testing, or performance testing of APIs and websites using k6. Also use when the user mentions "k6," "load test," "performance test," "stress test," "spike test," "soak test," "thresholds," "virtual users," or "VUs." For browser-based testing, see selenium.

TerminalSkills/skills

>- Lovable (formerly GPT Engineer) is a browser-based AI app builder that turns a natural-language description into a working full-stack web app with a built-in backend, authentication, database and one-click publishing. Use when the user wants to prototype or ship a web app with Lovable, write effective Lovable prompts, connect Supabase or GitHub, review generated security rules, or move a Lovable project into their own editor.

相关技能