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
