Files
freeCodeCamp/curriculum/challenges/english/04-data-visualization/data-visualization-with-d3/display-shapes-with-svg.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.5 KiB

id, title, challengeType, forumTopicId
id title challengeType forumTopicId
587d7fa8367417b2b2512bcc Display Shapes with SVG 6 301485

Description

The last challenge created an svg element with a given width and height, which was visible because it had a background-color applied to it in the style tag. The code made space for the given width and height. The next step is to create a shape to put in the svg area. There are a number of supported shapes in SVG, such as rectangles and circles. They are used to display data. For example, a rectangle (<rect>) SVG shape could create a bar in a bar chart. When you place a shape into the svg area, you can specify where it goes with x and y coordinates. The origin point of (0, 0) is in the upper-left corner. Positive values for x push the shape to the right, and positive values for y push the shape down from the origin point. To place a shape in the middle of the 500 (width) x 100 (height) svg from last challenge, the x coordinate would be 250 and the y coordinate would be 50. An SVG rect has four attributes. There are the x and y coordinates for where it is placed in the svg area. It also has a height and width to specify the size.

Instructions

Add a rect shape to the svg using append(), and give it a width attribute of 25 and height attribute of 100. Also, give the rect x and y attributes each set to 0.

Tests

tests:
  - text: Your document should have 1 <code>rect</code> element.
    testString: assert($('rect').length == 1);
  - text: The <code>rect</code> element should have a <code>width</code> attribute set to 25.
    testString: assert($('rect').attr('width') == '25');
  - text: The <code>rect</code> element should have a <code>height</code> attribute set to 100.
    testString: assert($('rect').attr('height') == '100');
  - text: The <code>rect</code> element should have an <code>x</code> attribute set to 0.
    testString: assert($('rect').attr('x') == '0');
  - text: The <code>rect</code> element should have a <code>y</code> attribute set to 0.
    testString: assert($('rect').attr('y') == '0');

Challenge Seed

<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)
                  // Add your code below this line



                  // Add your code above this line
  </script>
</body>

Solution

<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)
                  .append("rect")
                  .attr("width", 25)
                  .attr("height", 100)
                  .attr("x", 0)
                  .attr("y", 0);
  </script>
</body>