Skip to main content
🌍 Public

Share visualization

Send this visualization to others.

Fork of D3 D3 Color and Size Legends

Public visualization

Viewing a saved revision (read-only)

4ae5e7143cda4a1b97c0f419837be0ed

Fork of D3 D3 Color and Size Legends

0 stars0🌍 Public
Ccaedcagt76@gmail.com
Last edited Jan 3, 2024
Created on Jan 3, 2024

This example demonstrates how to build color and size legends for D3 visualizations using modular JavaScript functions. It shows a color legend with circles representing different fruit types, encoded through an ordinal color scale. The code defines a reusable `colorLegend` component that binds data to SVG groups, appending circles and text labels. Using D3 v7’s `scaleOrdinal`, the example maps categorical domains to specific colors, while SVG rendering handles the visual output. The implementation highlights a clean separation between the legend component and the main script’s render logic.

AI-generated description

Color and Size Legends

Topics covered:

  • 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