Skip to main content
100%

camelCase attributes

✓ Published0🌍 Public
11wheel
Last edited Aug 21, 2016
Created on Aug 12, 2016

This example demonstrates how d3-jetpack’s `st` method simplifies setting multiple style attributes using camelCase keys, which are automatically converted to hyphenated CSS properties. It shows a single line of code replacing several verbose `style` calls, making the syntax cleaner and more readable. The page renders the text “Centered Words” in the middle of the viewport, using d3.select and the jetpack `st` method to apply margins, text alignment, and font family. The visualization highlights the convenience of concise, unquoted attribute names.

AI-generated description

at and st in jetpack work like v3's attr and style. Passing an object sets multiple attributes, passing a string returns a single attribute and passing a string & second argument sets a single attribute.

To avoid having to use quotes around attributes and styles with hyphens when using the object notation, camelCase keys are hyphenated. Instead of:

selection
    .style('margin-top', '250px')
    .style('text-align', 'center')
    .style('font-weight', 600)

or with d3-selection-multi:

selection.styles({'margin-top': '250px', 'text-align': 'center', 'font-weight': 600})

you can write:

selection.st({fontSize: '250px', textAlign: 'center', fontWeight: 600})

With syntax highlighting on, it is a little easier to see the difference between keys and values when everything isn't a string. Plus there's less typing!

Similar vizzes