9951 explained code solutions for 126 technologies


javascript-d3How can I use React.js and D3 together to create an example?


React.js and D3 can be used together to create an example. To do this, you need to create a React component that will render the D3 visualization. The component will use the React lifecycle methods to control when the D3 visualization is rendered and updated.

To create a basic example, we can use the following code:

import React, { Component } from 'react';
import * as d3 from 'd3';

class Chart extends Component {
  componentDidMount() {
    const data = [12, 5, 6, 6, 9, 10];

    const svg = d3.select("body").append("svg")
        .attr("width", 700)
        .attr("height", 300);

    svg.selectAll("rect")
      .data(data)
      .enter()
      .append("rect")
      .attr("x", (d, i) => i * 70)
      .attr("y", (d, i) => 300 - 10 * d)
      .attr("width", 65)
      .attr("height", (d, i) => d * 10)
      .attr("fill", "green")
  }

  render() {
    return <div id="chart"></div>
  }
}

export default Chart

This code will render a basic bar chart with the given data. The components lifecycle methods are used to select the body element from the DOM and append an SVG element, then use the data to create rectangles and give them attributes.

Code explanation

  • import React, { Component } from 'react'; - imports the React library and the Component class.
  • import * as d3 from 'd3'; - imports the D3 library.
  • componentDidMount() { - this React lifecycle method is called when the component is mounted, which is when the DOM is ready.
  • const data = [12, 5, 6, 6, 9, 10]; - an array of data to use for the chart.
  • const svg = d3.select("body").append("svg") - selects the body element from the DOM and appends an SVG element.
  • .attr("x", (d, i) => i * 70) - sets the x attribute of each rectangle to the index of the data multiplied by 70.
  • .attr("y", (d, i) => 300 - 10 * d) - sets the y attribute of each rectangle to 300 minus the data multiplied by 10.
  • .attr("width", 65) - sets the width attribute of each rectangle to 65.
  • .attr("height", (d, i) => d * 10) - sets the height attribute of each rectangle to the data multiplied by 10.
  • .attr("fill", "green") - sets the fill attribute of each rectangle to green.
  • render() { - this React lifecycle method is called when the component needs to be rendered.
  • return <div id="chart"></div> - returns a div element with an id of "chart".

Helpful links

Edit this code on GitHub