| 
									
										
										
										
											2018-10-12 15:37:13 -04:00
										 |  |  | --- | 
					
						
							|  |  |  | title: Set a Domain and a Range on a Scale | 
					
						
							|  |  |  | --- | 
					
						
							| 
									
										
										
										
											2019-07-24 00:59:27 -07:00
										 |  |  | # Set a Domain and a Range on a Scale
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | --- | 
					
						
							|  |  |  | ## Hints
 | 
					
						
							| 
									
										
										
										
											2018-10-12 15:37:13 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-11-07 03:59:28 +05:30
										 |  |  | ### Hint 1
 | 
					
						
							| 
									
										
										
										
											2018-10-12 15:37:13 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-11-07 03:59:28 +05:30
										 |  |  | Use the `.domain()` and `.range()` functions. | 
					
						
							| 
									
										
										
										
											2018-10-12 15:37:13 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-11-07 03:59:28 +05:30
										 |  |  | ### Hint 2
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | Both the the `.domain()` and `.range()` functions accept an array of two elements. | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-07-24 00:59:27 -07:00
										 |  |  | 
 | 
					
						
							|  |  |  | --- | 
					
						
							|  |  |  | ## Solutions
 | 
					
						
							| 
									
										
										
										
											2018-11-07 03:59:28 +05:30
										 |  |  | 
 | 
					
						
							|  |  |  | The `domain` and `range` functions can be chained, and the following is the solution: | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-07-24 00:59:27 -07:00
										 |  |  | <details><summary>Solution 1 (Click to Show/Hide)</summary> | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | ```html | 
					
						
							| 
									
										
										
										
											2018-11-07 03:59:28 +05:30
										 |  |  | <body> | 
					
						
							|  |  |  |   <script> | 
					
						
							|  |  |  |     const scale = d3.scaleLinear() | 
					
						
							|  |  |  |     .domain([250, 500]) | 
					
						
							|  |  |  |     .range([10, 150]); | 
					
						
							|  |  |  |     const output = scale(50); | 
					
						
							|  |  |  |     d3.select("body") | 
					
						
							|  |  |  |       .append("h2") | 
					
						
							|  |  |  |       .text(output); | 
					
						
							|  |  |  |   </script> | 
					
						
							|  |  |  | </body> | 
					
						
							|  |  |  | ``` | 
					
						
							| 
									
										
										
										
											2019-07-24 00:59:27 -07:00
										 |  |  | 
 | 
					
						
							|  |  |  | </details> |