d3-scale
Encodes abstract data as visual scales for position, color, size, and other chart dimensions.
Repository Health
Technical Analysis
d3-scale is the scale module of D3.js, providing the functions that map a dimension of abstract data — numbers, dates, categories — onto a visual representation such as pixel position, color, or stroke width. It implements the full family of scale types used across data visualization: continuous scales (linear, power, log, symlog, radial, time/UTC) that interpolate smoothly between a domain and range, and discrete scales (ordinal, band, point, quantile, quantize, threshold) that bucket or index values instead of interpolating them. Sequential and diverging scales extend the same machinery to color encoding, commonly paired with d3-scale-chromatic’s color schemes.
Each scale is a small, composable function built on a shared continuous()/transformer() core (in src/continuous.js), so behavior like clamping, custom interpolation, and nice() domain rounding is implemented once and reused by every continuous scale type rather than duplicated per scale. The module depends on other single-purpose D3 packages (d3-array, d3-interpolate, d3-time, d3-format) instead of bundling their logic, keeping it usable standalone or as part of the full D3 bundle.
What You Get
- Continuous scales — linear, power/sqrt, log, symlog, and radial — for mapping numeric domains to numeric ranges with optional clamping and custom interpolation
- Time and UTC scales that map Date domains to a range, with calendar-aware tick generation and formatting
- Discrete scales — ordinal, band, and point — for mapping categorical domains to colors, bar positions, or fixed points
- Quantile, quantize, and threshold scales for bucketing continuous input into a fixed set of discrete outputs
- Sequential and diverging scales for one- and two-hued color encodings, including quantile-based variants
- Built-in
nice(),ticks(), andtickFormat()helpers for producing human-readable axis gridlines from any continuous scale
Common Use Cases
- Mapping a numeric or time-series column to horizontal/vertical pixel position in a scatterplot or line chart
- Encoding a categorical field as color or bar position using an ordinal or band scale
- Building a choropleth or heatmap by feeding a numeric domain through a sequential or quantile color scale
- Generating axis ticks and labels automatically from a chart’s data domain via
ticks()/tickFormat() - Converting a continuous data range into a fixed number of visual buckets with quantize or threshold scales
Under The Hood
Architecture
The module is organized as one small file per scale type (linear.js, log.js, time.js, ordinal.js, band.js, quantile.js, sequential.js, diverging.js, etc.), all re-exported through a flat src/index.js. Continuous numeric scales share a single core: continuous.js exposes a transformer() closure that holds domain/range/interpolate/clamp state and produces the actual scale function via bimap/polymap piecewise interpolation, plus an invert path built the same way in reverse. Scale-specific files like linear.js, log.js, and pow.js wrap this core with a transform/untransform pair (identity, log, power) rather than reimplementing interpolation, so adding a new continuous scale type is a matter of supplying two functions. Time scales (time.js) reuse the same continuous core through a calendar() wrapper that swaps in calendar-aware tick and nice logic from d3-time. Discrete scales (ordinal.js, band.js) are structurally separate, built around an InternMap-backed index rather than the continuous transformer, since they classify rather than interpolate.
Tech Stack
Plain ES modules with no build-time framework dependency — type: module in package.json, published as both raw src/**/*.js and a Rollup-built UMD/minified dist bundle for unpkg/jsdelivr. It composes rather than reimplements the rest of the D3 ecosystem: d3-array for bisect/ticks/InternMap, d3-interpolate for value interpolation, d3-time/d3-time-format for calendar scales, and d3-format for tick formatting. Tests run under Mocha with plain assert, and ESLint lints both src and test; there is no TypeScript — types ship from DefinitelyTyped separately.
Code Quality
Every scale type has a dedicated *-test.js file exercising defaults, edge cases (reversed domains, degenerate zero-width domains, clamping), and copy() independence, run via TZ=America/Los_Angeles mocha 'test/**/*-test.js' so time-scale tests are timezone-deterministic. Code style favors terse, functional closures over classes — mutable state is closed over in local vars and every scale returns a plain function object with methods attached, a longstanding D3 idiom rather than an ES class. No runtime type checking; correctness leans entirely on the test suite and ESLint rather than TypeScript. CI (GitHub Actions) runs the Node test matrix plus lint on every push and PR.
What Makes It Unique
Rather than one monolithic scale abstraction, d3-scale factors the numeric-continuous case into a single reusable transformer shared by seven+ scale types (linear, pow, sqrt, log, symlog, radial, time), while keeping genuinely different behavior (ordinal/band’s index-based classification, quantile/quantize/threshold’s bucketing) as separate implementations — avoiding both a bloated one-size-fits-all scale class and needless duplication across the continuous family. This is a deliberate, narrow design choice rather than a novel algorithm, consistent with D3’s broader philosophy of small, focused, composable modules.
Used by 18 apps in this directory
NocoDB
Databases · Low Code Platforms · No Code Platforms
Turn any SQL database into a collaborative no-code spreadsheet with automatic REST APIs and real-time views.
OpenCode
AI Code Assistants
A fully open-source AI coding agent built for the terminal, with a TUI, desktop app, web client, plugin system, and SDK — one of the most-starred AI coding agents on GitHub.
OpenObserve
Analytics · Devops · Monitoring
Open source observability platform for logs, metrics, traces, and real user monitoring — delivering 140x lower storage costs than Elasticsearch with a single binary you can run in under 2 minutes.
PostHog
Ab Testing Experimentation · Analytics · Developer Tools
The all-in-one open source product platform combining analytics, session replay, feature flags, error tracking, AI observability, and a built-in data warehouse in a single self-hostable stack.
Rill
Analytics · Data Engineering
The fastest BI tool for humans and agents — define metrics, models, and dashboards as code and query them instantly on ClickHouse or DuckDB.
Sink
Analytics · Bookmarks Archiving
Serverless link shortener with real-time analytics, powered by Cloudflare
Swetrix
Analytics
Privacy-first, cookieless web analytics with error tracking, session replays, and performance monitoring — self-host or use Cloud.
Teleport
Authentication · Security
Zero-trust infrastructure access platform that replaces credentials and VPNs with short-lived certificates, SSO, and identity-aware proxies for SSH, Kubernetes, databases, RDP, and AI agents.
Valmis
AI Agents
A security-focused, self-hostable AI agent platform for production work — agents run in isolated containers that access credentials only through a host-side proxy, so the AI itself never sees API keys or plaintext credentials.