D3 Color & Size Legends
✓ Published1🌍 Public
YYoung Song
Last edited Jun 22, 2022
Created on Jun 20, 2022
Forked from D3 Animation - Bowl of Fruit - Nested Version
This example builds two custom legend components for categorical color and quantitative size encodings. The color legend uses `scaleOrdinal` to map fruit names to colors, with SVG circles and text labels. The size legend uses `scaleSqrt` to map a 0-10 domain to circle radii, computing five ticks via `sizeScale.ticks()`, and stacking circles with dynamic vertical offsets so their spacing accounts for their varying diameters. Both components use a merge pattern to update existing SVG groups efficiently.
AI-generated description- Creating a color legend component
- Encoding area using a square root scale
- Computing ticks from size scale
- Creating a size legend component
- Tweaking the styles
MIT Licensed