9951 explained code solutions for 126 technologies


javascript-d3How do I create a React component using d3.js?


Creating a React component using d3.js is not difficult. The basic steps are to import the d3 library, create a component, and then use the d3 library to manipulate the DOM.

For example:

import React from 'react';
import * as d3 from 'd3';

class MyComponent extends React.Component {
  render() {
    return (
      <div ref={node => this.node = node}>
      </div>
    );
  }
  componentDidMount() {
    d3.select(this.node).append("svg")
      .attr("width", 500)
      .attr("height", 500)
      .append("circle")
      .attr("cx", 250)
      .attr("cy", 250)
      .attr("r", 50)
      .style("fill", "red");
  }
}

This code will create a red circle in the middle of a 500x500 svg element.

The code consists of the following parts:

  1. Import React and d3 libraries: import React from 'react'; import * as d3 from 'd3';
  2. Create a React component: class MyComponent extends React.Component { ... }
  3. Render a div element with a node reference: <div ref={node => this.node = node}>
  4. Use d3 to manipulate the DOM in the componentDidMount function:
componentDidMount() {
  d3.select(this.node).append("svg")
    .attr("width", 500)
    .attr("height", 500)
    .append("circle")
    .attr("cx", 250)
    .attr("cy", 250)
    .attr("r", 50)
    .style("fill", "red");
}

For more information on using d3 with React, see the following links:

Edit this code on GitHub