liveBook · Manning

CSS transform property

As explained in D3.js in Action, Third Edition

The CSS transform property applies geometric transformations such as translation, rotation, or scaling to an element.

The CSS transform property positions SVG groups and applies geometric changes such as translation, rotation, or scaling. In chart components, it moves labels, badges, and grid elements without changing their internal coordinates. section 8.7 section 8.6.3

Use the property this way when building responsive visualizations:

  • Set child coordinates to simple values such as x={0} and y={0}, then translate the wrapping group to its chart position. section 8.7
  • Apply the transform to a group rather than its text child when Safari compatibility matters. section 8.7
  • Pair transform with a CSS transition to animate multiple labels or badges simply and efficiently. section 8.7

Moving a chart label

Wrap the label in a group and translate that group: <g className="label" style={{ transform: `translate(${props.x}px, ${props.y}px)` }}><text x={0} y={0}>{props.label}</text></g>. Then define .label { transition: transform 400ms cubic-bezier(0.33, 1, 0.68, 1); } to animate position changes. section 8.7

import "./Label.css";

<g
  className="label"
  style={{ transform: `translate(${props.x}px, ${props.y}px)` }}
>
  <text
    x={0}
    y={0}
    fill={props.color}
    textAnchor={props.textAnchor}
    alignmentBaseline="middle"
    style={{ fontWeight: "bold" }}
  >
    {props.label}
  </text>
</g>

Inspect the complete React and SVG pattern for translating a label group while keeping the text element at local coordinates. section 8.7

How D3.js in Action, Third Edition covers it

How deep it goes
Demonstrates translated SVG groups, transition timing, and a Safari workaround without exploring transform mathematics or broader syntax.
Examples
Uses translated labels, framework badges, vertical grid lines, and a rankings visualization as implementation examples.
What it assumes
Assumes familiarity with SVG groups, element coordinates, React props, and CSS classes.
What you can do after
Operate CSS transforms and transitions to position and smoothly animate SVG chart elements in React components.
How it connects
Connects SVG groups, React components, CSS transitions, and D3-based visualization integration.

Also explained in

Compare how 3 books explain CSS transform property →

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.