Skip to main content
100%

Color and size legend

✓ Published0🌍 Public
Lloicvdk
Last edited Jul 27, 2020
Created on Jul 27, 2020
Forked from Bowl of fruit

This example demonstrates how to create color and size legends for data visualization using D3.js v5. It shows two separate legend groups: a color legend using `scaleOrdinal` with four fruit categories (apple, lemon, melon, lime) and a size legend using `scaleSqrt` for numeric values from 0 to 10. The implementation uses data joins with `.enter()`, `.merge()`, and `.exit()` methods to manage SVG circle and text elements, with both legends rendering in an SVG container.

AI-generated description

/* When we call selectAll and data, we create a "data join", we join data (in this case our 5 apples (array) and some elements (DOM elements in this case: our circle)

We have 3 case > Enter : we only have data, no elements

Update : we have both data and elements Exit : we only have elements but no data

In d3 we need to explicitly call thos 3 functions (.enter, .update, .exit)

selectAll give the elements part, .data give the data (always an array)

thanks to that d3 know exactly how much element are on the enter, update and exit */

MIT Licensed

Similar vizzes