---
id: 587d7faa367417b2b2512bd6
title: Add a Tooltip to a D3 Element
required:
- src: 'https://cdnjs.cloudflare.com/ajax/libs/d3/4.3.0/d3.min.js'
challengeType: 6
videoUrl: ''
localeTitle: 将工具提示添加到D3元素
---
## Description
当用户将鼠标悬停在该项目上时,工具提示会显示有关页面上项目的更多信息。有几种方法可以向可视化添加工具提示,此挑战使用SVG title
元素。 title
对与text()
方法一起动态地向条形图添加数据。
## Instructions
在每个rect
节点下附加title
元素。然后使用回调函数调用text()
方法,以便文本显示数据值。
## Tests
```yml
tests:
- text: 您的代码应该有9个title
元素。
testString: 'assert($("title").length == 9, "Your code should have 9 title
elements.");'
- text: 第一个title
元素的工具提示文本应为12。
testString: 'assert($("title").eq(0).text() == "12", "The first title
element should have tooltip text of 12.");'
- text: 第二个title
元素的工具提示文本应为31。
testString: 'assert($("title").eq(1).text() == "31", "The second title
element should have tooltip text of 31.");'
- text: 第三个title
元素的工具提示文本应为22。
testString: 'assert($("title").eq(2).text() == "22", "The third title
element should have tooltip text of 22.");'
- text: 第四个title
元素的工具提示文本应为17。
testString: 'assert($("title").eq(3).text() == "17", "The fourth title
element should have tooltip text of 17.");'
- text: 第五个title
元素的工具提示文本应为25。
testString: 'assert($("title").eq(4).text() == "25", "The fifth title
element should have tooltip text of 25.");'
- text: 第六个title
元素的工具提示文本应为18。
testString: 'assert($("title").eq(5).text() == "18", "The sixth title
element should have tooltip text of 18.");'
- text: 第七个title
元素的工具提示文本应为29。
testString: 'assert($("title").eq(6).text() == "29", "The seventh title
element should have tooltip text of 29.");'
- text: 第八个title
元素的工具提示文本应为14。
testString: 'assert($("title").eq(7).text() == "14", "The eighth title
element should have tooltip text of 14.");'
- text: 第九个title
元素的工具提示文本应为9。
testString: 'assert($("title").eq(8).text() == "9", "The ninth title
element should have tooltip text of 9.");'
```
## Challenge Seed
## Solution
```js
// solution required
```