Course contentsShow
← Web Development
Lesson 2632 of 2,98247. Data VisualizationFree lesson

Time and Log Scales

Work with specialized scales for temporal data and logarithmic relationships.

Time and Log Scales

You'll learn how to handle temporal data and logarithmic relationships in D3.js using specialized scale functions that automatically manage complex transformations.

Why You Need Specialized Scales

Remember from earlier lessons that scales map data values to visual properties. But what if your data represents dates, or spans several orders of magnitude (like 10 to 10,000)? Linear scales don't handle these elegantly.

Time scales (d3.scaleTime()) work with JavaScript Date objects, automatically understanding units like days, months, and years. They're essential for any chart with a timeline axis—think stock prices, temperature over time, or user signups per month.

Logarithmic scales (d3.scaleLog()) compress large ranges by using exponential spacing. Instead of 0-10-20-30, you get 1-10-100-1000. This is perfect for data like population growth, file sizes, or earthquake magnitudes where values vary exponentially.

Time Scales in Practice

A time scale maps Date objects to pixel positions:

const timeScale = d3.scaleTime()
  .domain([new Date(2023, 0, 1), new Date(2024, 0, 1)])
  .range([0, 500]);

// January 1st maps to 0px, December 31st to 500px

The scale understands calendar logic—leap years, month lengths, daylight saving—automatically.

Logarithmic Scales in Practice

A log scale compresses exponential growth into linear visual space:

const logScale = d3.scaleLog()
  .domain([1, 1000])
  .range([0, 300]);

// 1 → 0px, 10 → 100px, 100 → 200px, 1000 → 300px

Each tenfold increase gets equal visual spacing, making patterns visible across huge ranges.

Key Takeaway: Use d3.scaleTime() for date-based data and d3.scaleLog() for exponential relationships—both transform complex data ranges into meaningful visual positions while preserving their inherent structure.