liveBook · Manning

Transform attribute

As explained in D3.js in Action, Third Edition

The SVG transform attribute specifies geometric transformations such as translation, rotation, scaling, or skewing for an element and its descendants.

The SVG transform attribute can move a group and everything inside it. Use `translate(x, y)` to place the group: `x` is the horizontal movement and `y` is the vertical movement. Child elements can then use positions relative to their group. section 3.5

const barAndLabel = svg
  .selectAll("g")
  .data(data)
  .join("g")
    .attr("transform", d => `translate(0, ${yScale(d.technology)})`);

In a horizontal bar chart, each data-bound group moves to its bar’s vertical position. `yScale(d.technology)` supplies that position; the horizontal movement stays at zero. A rectangle and its labels can then be added to the same group so they move together. section 3.5

Place several donut charts

Give each donut chart its own SVG group. Set its transform to `translate(${xScale(year)}, ${innerHeight/2})`: a band scale positions the group horizontally by year, while half the inner chart height sets its vertical position. A parent group can also be translated by the chart margins. section 5.1.1

Listing 14.4 Adding a responsive grid (Grid.svelte)
{#if svgWidth && svgHeight}
  <svg width={svgWidth} height={svgHeight}>
    {#each years as year, i}                                   #1
      <g transform="translate(                                 #2
        {(i % numColumns) * smWidth},                           
        {Math.floor(i / numColumns) * smHeight})"               
      >                                                       
        <rect x={0} y={0} width={smWidth}     #3
          height={smHeight} />              
      </g>
    {/each}
  </svg>
{/if}

<style>
  svg {
    border: 1px solid magenta;
  }
  rect {
    fill: none;
    stroke: cyan;
  }
</style>

For a responsive grid, translate each group by its column and row. The column comes from `i % numColumns`; the row comes from `Math.floor(i / numColumns)`. Multiplying by the grid-item width and height gives the two movements. Resize the screen to check that the groups rearrange with the column count. section 14.4.2

How D3.js in Action, Third Edition covers it

How deep it goes
Works through horizontal and vertical `translate` values for groups; does not develop other transformation types.
Examples
Uses grouped bars and labels, year-positioned donut charts, and a resizable grid of small multiples.
What it assumes
Assumes an SVG container and available scales or grid dimensions; explains translation through concrete horizontal and vertical placements.
What you can do after
Apply it via D3 or Svelte to position grouped chart elements using scales or calculated grid coordinates.
How it connects
Connects SVG groups and child coordinates to data binding, D3 scales, and responsive grids.

Related concepts

This explainer was generated by Manning's knowledge graph from the text of D3.js in Action, Third Edition. The section links point to the parts of the book it draws on.