| 
									
										
										
										
											2018-10-10 18:03:03 -04:00
										 |  |  | --- | 
					
						
							|  |  |  | id: 587d7faa367417b2b2512bd6 | 
					
						
							| 
									
										
										
										
											2021-03-04 09:49:46 -08:00
										 |  |  | title: 给 D3 元素添加工具提示 | 
					
						
							| 
									
										
										
										
											2018-10-10 18:03:03 -04:00
										 |  |  | challengeType: 6 | 
					
						
							| 
									
										
										
										
											2020-09-18 00:13:05 +08:00
										 |  |  | forumTopicId: 301470 | 
					
						
							| 
									
										
										
										
											2021-01-13 03:31:00 +01:00
										 |  |  | dashedName: add-a-tooltip-to-a-d3-element | 
					
						
							| 
									
										
										
										
											2018-10-10 18:03:03 -04:00
										 |  |  | --- | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-12-16 00:37:30 -07:00
										 |  |  | # --description--
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-03-04 09:49:46 -08:00
										 |  |  | 当用户在一个对象上悬停时,提示框可以显示关于这个对象更多的信息。 在可视化中有多种方法添加提示框,这个挑战将使用 SVG 的 `title` 元素。 | 
					
						
							| 
									
										
										
										
											2020-12-16 00:37:30 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-03-04 09:49:46 -08:00
										 |  |  | `title` 和 `text()` 方法一起给每组动态添加数据。 | 
					
						
							| 
									
										
										
										
											2020-12-16 00:37:30 -07:00
										 |  |  | 
 | 
					
						
							|  |  |  | # --instructions--
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-03-04 09:49:46 -08:00
										 |  |  | 在每个 `rect` 节点下附加 `title` 元素。 然后用回调函数调用 `text()` 方法使它的文本显示数据值。 | 
					
						
							| 
									
										
										
										
											2020-12-16 00:37:30 -07:00
										 |  |  | 
 | 
					
						
							|  |  |  | # --hints--
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-03-04 09:49:46 -08:00
										 |  |  | 你应该有 9 个 `title` 元素。 | 
					
						
							| 
									
										
										
										
											2018-10-10 18:03:03 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-12-16 00:37:30 -07:00
										 |  |  | ```js | 
					
						
							|  |  |  | assert($('title').length == 9); | 
					
						
							| 
									
										
										
										
											2018-10-10 18:03:03 -04:00
										 |  |  | ``` | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-03-08 07:28:46 -07:00
										 |  |  | 第一个 `title` 元素的提示框文本应为 `12`。 | 
					
						
							| 
									
										
										
										
											2020-12-16 00:37:30 -07:00
										 |  |  | 
 | 
					
						
							|  |  |  | ```js | 
					
						
							|  |  |  | assert($('title').eq(0).text() == '12'); | 
					
						
							|  |  |  | ``` | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-03-08 07:28:46 -07:00
										 |  |  | 第二个 `title` 元素的提示框文本应为 `31`。 | 
					
						
							| 
									
										
										
										
											2020-12-16 00:37:30 -07:00
										 |  |  | 
 | 
					
						
							|  |  |  | ```js | 
					
						
							|  |  |  | assert($('title').eq(1).text() == '31'); | 
					
						
							| 
									
										
										
										
											2018-10-10 18:03:03 -04:00
										 |  |  | ``` | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-03-08 07:28:46 -07:00
										 |  |  | 第三个 `title` 元素的提示框文本应为 `22`。 | 
					
						
							| 
									
										
										
										
											2020-09-18 00:13:05 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-12-16 00:37:30 -07:00
										 |  |  | ```js | 
					
						
							|  |  |  | assert($('title').eq(2).text() == '22'); | 
					
						
							| 
									
										
										
										
											2018-10-10 18:03:03 -04:00
										 |  |  | ``` | 
					
						
							| 
									
										
										
										
											2020-08-13 17:24:35 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-03-08 07:28:46 -07:00
										 |  |  | 第四个 `title` 元素的提示框文本应为 `17`。 | 
					
						
							| 
									
										
										
										
											2020-12-16 00:37:30 -07:00
										 |  |  | 
 | 
					
						
							|  |  |  | ```js | 
					
						
							|  |  |  | assert($('title').eq(3).text() == '17'); | 
					
						
							|  |  |  | ``` | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-03-08 07:28:46 -07:00
										 |  |  | 第五个 `title` 元素的提示框文本应为 `25`。 | 
					
						
							| 
									
										
										
										
											2020-12-16 00:37:30 -07:00
										 |  |  | 
 | 
					
						
							|  |  |  | ```js | 
					
						
							|  |  |  | assert($('title').eq(4).text() == '25'); | 
					
						
							|  |  |  | ``` | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-03-08 07:28:46 -07:00
										 |  |  | 第六个 `title` 元素的提示框文本应为 `18`。 | 
					
						
							| 
									
										
										
										
											2020-12-16 00:37:30 -07:00
										 |  |  | 
 | 
					
						
							|  |  |  | ```js | 
					
						
							|  |  |  | assert($('title').eq(5).text() == '18'); | 
					
						
							|  |  |  | ``` | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-03-08 07:28:46 -07:00
										 |  |  | 第七个 `title` 元素的提示框文本应为 `29`。 | 
					
						
							| 
									
										
										
										
											2020-12-16 00:37:30 -07:00
										 |  |  | 
 | 
					
						
							|  |  |  | ```js | 
					
						
							|  |  |  | assert($('title').eq(6).text() == '29'); | 
					
						
							|  |  |  | ``` | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-03-08 07:28:46 -07:00
										 |  |  | 第八个 `title` 元素的提示框文本应为 `14`。 | 
					
						
							| 
									
										
										
										
											2020-12-16 00:37:30 -07:00
										 |  |  | 
 | 
					
						
							|  |  |  | ```js | 
					
						
							|  |  |  | assert($('title').eq(7).text() == '14'); | 
					
						
							|  |  |  | ``` | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-03-08 07:28:46 -07:00
										 |  |  | 第九个 `title` 元素的提示框文本应为 `9`。 | 
					
						
							| 
									
										
										
										
											2020-12-16 00:37:30 -07:00
										 |  |  | 
 | 
					
						
							|  |  |  | ```js | 
					
						
							|  |  |  | assert($('title').eq(8).text() == '9'); | 
					
						
							|  |  |  | ``` | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-01-13 03:31:00 +01:00
										 |  |  | # --seed--
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | ## --seed-contents--
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | ```html | 
					
						
							|  |  |  | <style> | 
					
						
							|  |  |  |   .bar:hover { | 
					
						
							|  |  |  |     fill: brown; | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | </style> | 
					
						
							|  |  |  | <body> | 
					
						
							|  |  |  |   <script> | 
					
						
							|  |  |  |     const dataset = [12, 31, 22, 17, 25, 18, 29, 14, 9]; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     const w = 500; | 
					
						
							|  |  |  |     const h = 100; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     const svg = d3.select("body") | 
					
						
							|  |  |  |                   .append("svg") | 
					
						
							|  |  |  |                   .attr("width", w) | 
					
						
							|  |  |  |                   .attr("height", h); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     svg.selectAll("rect") | 
					
						
							|  |  |  |        .data(dataset) | 
					
						
							|  |  |  |        .enter() | 
					
						
							|  |  |  |        .append("rect") | 
					
						
							|  |  |  |        .attr("x", (d, i) => i * 30) | 
					
						
							|  |  |  |        .attr("y", (d, i) => h - 3 * d) | 
					
						
							|  |  |  |        .attr("width", 25) | 
					
						
							|  |  |  |        .attr("height", (d, i) => d * 3) | 
					
						
							|  |  |  |        .attr("fill", "navy") | 
					
						
							|  |  |  |        .attr("class", "bar") | 
					
						
							|  |  |  |        // Add your code below this line | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |        // Add your code above this line | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     svg.selectAll("text") | 
					
						
							|  |  |  |        .data(dataset) | 
					
						
							|  |  |  |        .enter() | 
					
						
							|  |  |  |        .append("text") | 
					
						
							|  |  |  |        .text((d) => d) | 
					
						
							|  |  |  |        .attr("x", (d, i) => i * 30) | 
					
						
							|  |  |  |        .attr("y", (d, i) => h - (d * 3 + 3)) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   </script> | 
					
						
							|  |  |  | </body> | 
					
						
							|  |  |  | ``` | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-12-16 00:37:30 -07:00
										 |  |  | # --solutions--
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-01-13 03:31:00 +01:00
										 |  |  | ```html | 
					
						
							|  |  |  | <style> | 
					
						
							|  |  |  |   .bar:hover { | 
					
						
							|  |  |  |     fill: brown; | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | </style> | 
					
						
							|  |  |  | <body> | 
					
						
							|  |  |  |   <script> | 
					
						
							|  |  |  |     const dataset = [12, 31, 22, 17, 25, 18, 29, 14, 9]; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     const w = 500; | 
					
						
							|  |  |  |     const h = 100; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     const svg = d3.select("body") | 
					
						
							|  |  |  |                   .append("svg") | 
					
						
							|  |  |  |                   .attr("width", w) | 
					
						
							|  |  |  |                   .attr("height", h); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     svg.selectAll("rect") | 
					
						
							|  |  |  |        .data(dataset) | 
					
						
							|  |  |  |        .enter() | 
					
						
							|  |  |  |        .append("rect") | 
					
						
							|  |  |  |        .attr("x", (d, i) => i * 30) | 
					
						
							|  |  |  |        .attr("y", (d, i) => h - 3 * d) | 
					
						
							|  |  |  |        .attr("width", 25) | 
					
						
							|  |  |  |        .attr("height", (d, i) => d * 3) | 
					
						
							|  |  |  |        .attr("fill", "navy") | 
					
						
							|  |  |  |        .attr("class", "bar") | 
					
						
							|  |  |  |        .append("title") | 
					
						
							|  |  |  |        .text((d) => d) | 
					
						
							| 
									
										
										
										
											2021-02-06 04:42:36 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-01-13 03:31:00 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  |     svg.selectAll("text") | 
					
						
							|  |  |  |        .data(dataset) | 
					
						
							|  |  |  |        .enter() | 
					
						
							|  |  |  |        .append("text") | 
					
						
							|  |  |  |        .text((d) => d) | 
					
						
							|  |  |  |        .attr("x", (d, i) => i * 30) | 
					
						
							|  |  |  |        .attr("y", (d, i) => h - (d * 3 + 3)) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   </script> | 
					
						
							|  |  |  | </body> | 
					
						
							|  |  |  | ``` |