liveBook · Manning

Translation

As explained in D3.js in Action, Third Edition

A translation is a coordinate transformation that shifts every point by the same horizontal or vertical offset.

A translation shifts an SVG element or group horizontally, vertically, or both. Applying it to a group moves every descendant together and creates a local coordinate system for chart construction. section 3.6 section 4.1.1

Use translation to position visual elements without recalculating each child’s coordinates:

  • Reserve chart margins by translating one inner-chart group by the left and top margins. section 4.1.1
  • Place repeated chart groups horizontally with a D3 scale and vertically at the center of the available chart area. section 5.1.1
  • Arrange responsive small multiples by translating each group according to its column and row. section 14.4.2

Positioning an inner chart

Create the SVG container, append a group, and translate that group by the reserved margins: const innerChart = svg.append("g").attr("transform", `translate(${margin.left}, ${margin.top})`); Build axes and marks inside innerChart so they share the translated coordinate system. section 4.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>

Inspect the responsive-grid implementation to see modulo determine columns, row calculation determine vertical placement, and transform apply both offsets. section 14.4.2

How D3.js in Action, Third Edition covers it

How deep it goes
Explains group-level displacement, local coordinate systems, scale-based placement, and responsive row-column calculations.
Examples
Uses margin-convention line charts, year-based donut charts, responsive small-multiple grids, and geographic projections.
What it assumes
Assumes basic SVG groups, attributes, DOM manipulation, and D3 scales while keeping the mathematics light.
What you can do after
Apply it via SVG transform attributes and D3 scales to position chart elements and responsive visualization groups.
How it connects
Connects SVG groups, transform attributes, coordinate systems, margins, D3 scales, and responsive visualization.

Also explained in

Compare how 3 books explain translation →

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.