ErikGartner / dTree

A library for visualizing data trees with multiple parents, such as family trees. Built on top of D3.
https://treehouse.gartner.io/ErikGartner/58e58be650453b6d49d7
MIT License
521 stars 139 forks source link

How to use with React Js? #75

Closed mk48 closed 6 years ago

mk48 commented 6 years ago

Thanks for the excellent library.

I just tried to use with React Js and couldn't get through. Below is my approach. Could any please help me on this?

import React from 'react';
import dTree from 'd3-dtree';
import _ from 'lodash';
import * as d3 from "d3";

class FamTree extends React.Component { 
  componentDidMount() {
    let treeData = [{
      name: "Father",
      depthOffset: 1,
      marriages: [{
        spouse: {
          name: "Mother",
        },
        children: [{
          name: "Child",
        }]
      }],
      extra: {}
    }];

    dTree.init(treeData, {target: "#graph", height:800, width: 1200, debug:true});
  }

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

export default FamTree;

I am getting "d3 is not defined"

image

ErikGartner commented 6 years ago

I'm not that familiar with react. Basically you just need to make that D3 is loaded before loading dTree.

mk48 commented 6 years ago

I will try to find a way, If I get any then I will post my answer here.

theoperatore commented 6 years ago

@mk48 I believe the library depends on d3 being available globally. so something like

// in your src/index.js
import d3 from 'd3';
...
window.d3 = d3;

should do the trick.

mk48 commented 6 years ago

thanks @theoperatore , below code solved the issue

// in your src/index.js
import * as d3 from 'd3';
...
window.d3 = d3;
atefBB commented 2 years ago

@mk48 I believe the library depends on d3 being available globally. so something like

// in your src/index.js
import d3 from 'd3';
...
window.d3 = d3;

should do the trick.

I think this is not recommanded in react (to assign a new variable to window) !