Book Image

Learning D3.js Mapping

Book Image

Learning D3.js Mapping

Overview of this book

Table of Contents (14 chapters)
Learning D3.js Mapping
Credits
About the Authors
About the Reviewers
www.PacktPub.com
Preface
6
Finding and Working with Geographic Data
Index

Grouping


The group tag <g> is used often in SVG, especially in maps. It is used to group elements and then apply a set of attributes to that set. It provides the following benefits:

  • It allows you to treat a set of shapes as a single shape for the purpose of scaling and translating.

  • It prevents code duplication by allowing you to set attributes at a higher level and have them inherit to all the elements included.

  • Groups are essential for applying transformations to large sets of SVG nodes in a performant manner. Grouping offsets the parent group rather than modifying each of the attributes in every item of the group.

Let's take the set of shapes used to explain Bézier curves and add all of them to a single group, combining everything we have learned so far, in the following code:

    <svg height="500" width="800">
      <g transform="translate(-200,-100), scale(2,2)">
        <path d="M 120 120 L 220 220 C 200 70 480 290 320 120 Z"></path>

        <line x1="220...