Linear and Ordinal Scales
You'll learn how to choose and apply the right D3 scale type for your data: continuous linear scales for numbers and ordinal scales for categories.
What Are These Scales For?
After understanding D3's general scale concept (mapping data to visual properties), you need to pick the right kind of scale for your data type. This choice fundamentally affects how your visualization interprets values.
Linear Scales for Continuous Data
Linear scales (d3.scaleLinear()) map continuous numerical input to continuous output. Think of them like a ruler: every value between 0 and 100 has a precise position.
Use linear scales when:
- Your data is quantitative (temperatures, prices, counts)
- Values exist on a continuum (42.7 is meaningful, not just "42" or "43")
- You need proportional spacing (50 is exactly halfway between 0 and 100)
Example: Mapping stock prices (200) to pixel heights (0–400px). A price of $105 lands exactly halfway up the chart.
Ordinal Scales for Categories
Ordinal scales (d3.scaleOrdinal() or d3.scaleBand()) map discrete categories to discrete outputs. Think of them like labeled bins: "Apple," "Orange," "Banana" each get their own position—no in-between.
Use ordinal scales when:
- Your data is categorical (product names, departments, months)
- There's no numerical relationship between values
- Each category needs its own distinct position or color
Example: Mapping departments ["Sales", "Engineering", "HR"] to bar positions on an x-axis. "Sales" goes at position 0, "Engineering" at position 100, "HR" at position 200—nothing exists at position 50.
The Decision Rule
Is your data a measurement? → Linear scale
Is your data a label or category? → Ordinal scale
Key Takeaway: Use linear scales (d3.scaleLinear()) for continuous quantitative data where intermediate values matter, and ordinal scales for categorical data where each value is a distinct, non-numeric label.