Trusted answers to developer questions
Trusted Answers to Developer Questions

Related Tags

javascript

What are D3 dynamic properties?

Shahpar Khan
svg viewer

About D3

D3 is an interactive JavaScript library for data visualization that uses Scalar Vector Graphics (SVG), HTML, and CSS to display charts and figures that illustrate numeric data. You will need to edit the DOM elements you are making for these charts and figures. This can be done by selecting the desired element under the <script> tag and adding the appropriate features to it. After selecting, you can change them, style them, add attributes, or insert/remove more elements.

Click here to read our shot on D3 selection. We recommend you read this shot before going further.

The following is a list of D3 DOM manipulation methods that you can use after selecting elements using d3.select() or d3.selectAll():

d3.text()

You can use the text() method to add text to a selected element. The text() takes a single string argument and places it inside the selected tag. For example, in the code below, we first choose the <h3> element and then add our desired text into it using the text() method.

d3.append()

The append() method allows us to add an element right at the end of the selected element. append() takes on a string input argument that specifies which element should be appended. For example, in the code below, we have one <h1> element inside our <div> element. So, we will go ahead and add an <h3> tag by first selecting the <div> element and then appending the <h3> element.

You could also use d3.insert() instead of d3.append()– it will do the same thing.

d3.remove()

If you want to remove an element, you can use the remove() method. The remove() method does not take any input argument; instead, it follows a selection that it then deletes from the code. For example, in the code below, we first select the <h2> element then delete it using the remove() method.

d3.html()

If you want to add pure HTML text inside an element, you can use the html() method. The html() method takes a single string argument that contains the HTML code, which you want to insert inside the selected element. For example, in the code below, we have an empty <div> container. We use the html() to add a <h1> tag and a <h2> tag filled with text.

d3.attr()

The attr() tag lets you add attributes to a selected tag. This tag takes two string arguments:

  1. the name of the attribute
  2. the attribute’s specification

For example, in the code below, we select the <img> tag and then add the src, width, and height attributes to it.

Attributes of some elements, such as the checked property of the checkbox or radio button, cannot be set by the attr() method. In this case, use the property() method to apply attributes on the selected DOM elements.

d3.style

The style() method lets you style the content inside a selected element. It takes two input arguments:

  1. the name of the styling component
  2. the specification

For example, in the code below, we select the <h1> tag and style it by changing its color to dark blue.

d3.classed()

The classed() method lets you add or remove the class attribute from a tag. It has two input arguments:

  1. the name of the class
  2. a boolean indicating whether to add or remove the class

A true boolean value is used to add a class, and a false boolean value is used to remove a class. For example, in the code below, we have a class to style the headings we want to add to our <h1> tag. So, we select the <h1> tag and then add the class using the classed() method.

RELATED TAGS

javascript

CONTRIBUTOR

Shahpar Khan
Copyright ©2022 Educative, Inc. All rights reserved
RELATED COURSES

View all Courses

Keep Exploring