Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

JavaScript

I want to understand what does domain function do in d3.js?

The concept of domain is really confusing to understand. Anyone can explain this?

1 Answer

So you're probably working with a file that contains data. In the video the example data includes the highest temperature recorded every day for a period of time. For example the highest temperature recorded on 01/08/1973 was 294 degrees, and on 02/08/1973 it was 300 degrees.

The purpose of the domain function is to map the lowest and highest of those values to the visual range on a graph (which you will have already specified using the range function). For example, if the highest record temperature of the day in that time period was 100 and the lowest record temperature of the day was 0, the domain function would take those two values as an array. e.g:

//This assumes you are mapping temperatures to the y axis, and probably the date to the x axis.
yScale.domain([0, 100]);

Lets say the y axis range (in pixels) was from 0 to 100 e.g:

//Usually the highest number would be placed first for reasons explained in the video but for simplicity's sake I've done this.
yScale.range([0, 100]);

The domain function would then map the data values to the range. The element with the value of 100 degrees would be placed at position 100 on the graph. The element with the value of 0 degrees will be placed at position 0. An element that had a value of 60 degrees would be placed at position 60 etc.

I hope this helps and I haven't over complicated it...

Thanks, Geoffrey! Your explanation is good enough to understand the concept of domain function in d3.js .