d3.scale.category20() randomizer
✓ Published0🌍 Public
11wheel
Last edited Dec 20, 2014
Created on Dec 20, 2014
This example demonstrates a randomizer for D3’s categorical color palette, replacing the standard `d3.scale.category20()` colors with newly generated hexadecimal values. It first shows how the original palette’s colors appear in the page’s SVG elements, then swaps them by iterating over the old color strings. The code uses `d3.scale.category20().range()` to capture the initial palette, maps each color to a random `#`-prefixed hex code via `Math.random`, and updates inline styles and `fill` attributes with `d3.selectAll`. Pressing the up arrow re-runs the script, reassigning the palette each time. The data source is the existing DOM elements’ style and attribute values.
AI-generated description