跳到正文
cccskills
免费在 GitHub 公开

d3-pro

Build custom data visualizations with D3: selections, scales, axes, transitions, and reusable chart patterns. Use when off-the-shelf charts aren't enough.

中文简介正在准备中。当前显示原文说明。

查看安装方法

包含的文件(1)

  • SKILL.md5.2 KB

SKILL.md(原文)

安装前可以查看交给 AI 代理的指令内容。

D3 Pro

A practical guide to D3.js: the low-level visualization toolkit — selections and the data join, scales, axes, shapes, transitions, and building reusable, responsive custom charts.

Overview

D3 is not a chart library; it's a visualization toolkit. You bind data to DOM/SVG elements and describe how data maps to visuals. That means total control (any chart ever invented, plus new ones) at the cost of writing more code. Use D3 when the design is custom; use a chart library when it's standard.

The core pattern: select elements → join data → enter/update/exit → map data through scales → transition changes.

When to use

  • Custom/bespoke chart designs not covered by chart libraries.
  • Interactive visualizations: brushing, zooming, linked views, animations.
  • Geographic maps (d3-geo + TopoJSON), hierarchies (treemap, sunburst), networks (force layout).
  • Learning the concepts that underpin all data viz (scales, joins).

Core concepts

  • Selections. d3.select('svg').selectAll('circle') — D3's jQuery-like DOM API with data binding.
  • Data join. .data(data, keyFn).join(enter => ..., update => ..., exit => ...) — the heart of D3. Key functions keep element identity stable across updates (no key = index-based, which scrambles on reorder).
  • Scales. scaleLinear, scaleTime, scaleBand, scaleOrdinal, scaleLog — map data domain → visual range. scaleBand for bars (with padding), scaleTime handles dates/irregular intervals.
  • Axes. axisBottom(scale) renders ticks; customize with tickFormat, ticks(n). Style via CSS, not attributes, for maintainability.
  • Shapes & layouts. line(), area(), arc() (pies/donuts/gauges); layouts: pie(), stack(), hierarchy() + treemap()/pack(), forceSimulation() for networks.
  • Transitions. .transition().duration(750).attr(...) — interpolates attributes; interrupt-safe. Animate data changes, not just entrances.
  • Zoom/brush. d3.zoom() (pan/zoom with rescaled axes), d3.brush() (range selection) — the building blocks of interactive exploration.
  • Modules. D3 is modular — import only what you need (d3-scale, d3-selection) to keep bundles small.

Practical workflow

1. Scaffold (margin convention).

const margin = { top: 20, right: 20, bottom: 40, left: 50 };
const width = 640 - margin.left - margin.right;
const height = 400 - margin.top - margin.bottom;

const svg = d3.select('#chart').append('svg')
  .attr('viewBox', `0 0 ${width + margin.left + margin.right} ${height + margin.top + margin.bottom}`)
  .attr('role', 'img').attr('aria-label', 'Monthly revenue bar chart');
const g = svg.append('g').attr('transform', `translate(${margin.left},${margin.top})`);

viewBox (not fixed width/height) = responsive for free.

2. Scales + axes.

const x = d3.scaleBand().domain(data.map(d => d.month)).range([0, width]).padding(0.2);
const y = d3.scaleLinear().domain([0, d3.max(data, d => d.revenue)]).nice().range([height, 0]);
g.append('g').attr('transform', `translate(0,${height})`).call(d3.axisBottom(x));
g.append('g').call(d3.axisLeft(y).tickFormat(d => `$${d / 1000}k`));

3. Join + transitions.

g.selectAll('rect').data(data, d => d.month)
  .join(
    enter => enter.append('rect').attr('y', height).attr('height', 0),
    update => update,
    exit => exit.transition().duration(300).attr('height', 0).attr('y', height).remove()
  )
  .transition().duration(750)
  .attr('x', d => x(d.month)).attr('width', x.bandwidth())
  .attr('y', d => y(d.revenue)).attr('height', d => height - y(d.revenue));

4. Interactivity. Tooltips via a positioned HTML div (easier to style than SVG text); brush/zoom for exploration; keyboard-accessible alternatives for critical data.

5. Reusable pattern. Wrap in a function taking (selection, data, options); keep scales/axes inside; expose update by re-calling with new data.

Common pitfalls

  • Missing key function. .data(data) without keys → index join → bars morph into wrong categories on update. Always key by stable ID.
  • Fixed pixel dimensions. Hard-coded width/height breaks responsiveness. viewBox + CSS width: 100%.
  • Y domain not starting at zero (bars). Truncated bar axes lie visually. Bars → zero baseline; lines → can zoom (but say so).
  • Too many ticks. Default tick counts overlap on small charts. Set .ticks(n) responsively.
  • No accessible alternative. SVG charts are invisible to screen readers. Add role="img" + aria-label, plus a data table or textual summary for key insights.
  • Transition pileup. Rapid data updates queue transitions; use .interrupt() or keyed joins to keep them coherent.
  • Importing all of d3. import * as d3 from 'dbl' bloats bundles. Import modules: import { scaleLinear } from 'd3-scale'.
  • Force layout without bounds. Unconstrained simulations fling nodes off-canvas; add centering/collision forces and tick limits.
  • Ignoring update/exit. Enter-only code works for static charts but breaks the moment data changes. Write the full join from the start.

评价

还没有评价。欢迎分享你的使用感受。

评论

无需登录即可发表。欢迎分享使用感受、提问,或说明在哪些环境下无法运行。

还没有评论。来写第一条吧。

提交后会立即公开。请勿填写邮箱、电话等个人信息。URL 最多 2 个。

0 / 1000 字

此表单受 reCAPTCHA 保护,适用 Google 的隐私权政策和服务条款。

同一仓库的技能

Master 1Password: vaults, Watchtower, passkeys, SSH agent, CLI, and family/team administration. Use when getting full value from 1Password personally or administering it for others.

中文简介正在准备中。当前显示原文说明。

aicodedecode/awesome-muse-skills282026年10月10日 更新

Create 3D visuals with modeling, texturing, lighting, rendering, and optimization for web and product.

中文简介正在准备中。当前显示原文说明。

aicodedecode/awesome-muse-skills282026年10月10日 更新

Create 3D web experiences: scene setup, models, materials, lighting, animation, scroll-driven scenes, and performance budgets. Use when adding 3D to websites beyond basic demos.

中文简介正在准备中。当前显示原文说明。

aicodedecode/awesome-muse-skills282026年10月10日 更新

Writing abstracts that get papers read — structured content, the 5-sentence core, and journal-specific constraints.

中文简介正在准备中。当前显示原文说明。

aicodedecode/awesome-muse-skills282026年10月10日 更新

Learn effectively from courses and academies: choosing programs, studying actively, and converting courses into skills. Use when investing time/money in structured learning.

中文简介正在准备中。当前显示原文说明。

aicodedecode/awesome-muse-skills282026年10月10日 更新

Audit designs for accessibility with WCAG checklists covering color, type, focus, motion, and content.

中文简介正在准备中。当前显示原文说明。

aicodedecode/awesome-muse-skills282026年10月10日 更新

查看 aicodedecode 的全部技能

报告此技能的问题