Files
freeCodeCamp/curriculum/challenges/english/04-data-visualization/data-visualization-with-d3/add-a-tooltip-to-a-d3-element.english.md
Prayag 17be165d66 Add solution d3challenges (#38222)
* added solutions to data visualization with D3

* Update add-a-tooltip-to-a-d3-element.english.md

* Update add-attributes-to-the-circle-elements.english.md

* Update add-axes-to-a-visualization.english.md

* Update add-document-elements-with-d3.english.md

* Update add-inline-styling-to-elements.english.md

* Update add-labels-to-scatter-plot-circles.english.md

* Update change-styles-based-on-data.english.md

* Update change-the-color-of-an-svg-element.english.md

* Update change-the-presentation-of-a-bar-chart.english.md

* Update create-a-bar-for-each-data-point-in-the-set.english.md

* Update create-a-linear-scale-with-d3.english.md

* Update create-a-scatterplot-with-svg-circles.english.md

* Update display-shapes-with-svg.english.md

* Update dynamically-change-the-height-of-each-bar.english.md

* Update dynamically-set-the-coordinates-for-each-bar.english.md

* Update invert-svg-elements.english.md

* Update learn-about-svg-in-d3.english.md

* Update select-a-group-of-elements-with-d3.english.md

* Update set-a-domain-and-a-range-on-a-scale.english.md

* Update style-d3-labels.english.md

* Update work-with-dynamic-data-in-d3.english.md

* Update use-a-pre-defined-scale-to-place-elements.english.md

* Update use-dynamic-scales.english.md

* Update use-the-d3.max-and-d3.min-functions-to-find-minimum-and-maximum-values-in-a-dataset.english.md

* Update work-with-data-in-d3.english.md
2020-04-24 06:34:53 -05:00

3.9 KiB

id, title, challengeType, forumTopicId
id title challengeType forumTopicId
587d7faa367417b2b2512bd6 Add a Tooltip to a D3 Element 6 301470

Description

A tooltip shows more information about an item on a page when the user hovers over that item. There are several ways to add a tooltip to a visualization, this challenge uses the SVG title element. title pairs with the text() method to dynamically add data to the bars.

Instructions

Append a title element under each rect node. Then call the text() method with a callback function so the text displays the data value.

Tests

tests:
  - text: Your code should have 9 <code>title</code> elements.
    testString: assert($('title').length == 9);
  - text: The first <code>title</code> element should have tooltip text of 12.
    testString: assert($('title').eq(0).text() == '12');
  - text: The second <code>title</code> element should have tooltip text of 31.
    testString: assert($('title').eq(1).text() == '31');
  - text: The third <code>title</code> element should have tooltip text of 22.
    testString: assert($('title').eq(2).text() == '22');
  - text: The fourth <code>title</code> element should have tooltip text of 17.
    testString: assert($('title').eq(3).text() == '17');
  - text: The fifth <code>title</code> element should have tooltip text of 25.
    testString: assert($('title').eq(4).text() == '25');
  - text: The sixth <code>title</code> element should have tooltip text of 18.
    testString: assert($('title').eq(5).text() == '18');
  - text: The seventh <code>title</code> element should have tooltip text of 29.
    testString: assert($('title').eq(6).text() == '29');
  - text: The eighth <code>title</code> element should have tooltip text of 14.
    testString: assert($('title').eq(7).text() == '14');
  - text: The ninth <code>title</code> element should have tooltip text of 9.
    testString: assert($('title').eq(8).text() == '9');

Challenge Seed

<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>

Solution

<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)
       

    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>