bumbeishvili / org-chart

Highly customizable org chart. Integrations available for Angular, React, Vue
https://stackblitz.com/edit/web-platform-o5t1ha
MIT License
893 stars 316 forks source link

How to properly implement OrgChart in React/NextJS. #437

Open jrso15 opened 1 week ago

jrso15 commented 1 week ago

Hello,

I’m currently trying to implement OrgChart in d3-org-chart but I’m getting OrgChart is not a constructor. I can’t find any solution for this. can someone please help me?

Here’s sample of my code:

import React, { useRef } from "react";
import dynamic from "next/dynamic";
import styles from "@/styles/OrganizationChart.module.scss";

const OrgChart = dynamic(
  () =>
    import("d3-org-chart").then((mod) => {
      console.log(mod); 
      return mod.OrgChart;
    }),
  { ssr: false }
);

const OrganizationChart = ({ organizationItemData, onNodeClick }) => {
  const d3Container = useRef(null);
  const chartRef = useRef(null);

  // Initialize the chart directly in the render function if the container is ready
  if (organizationItemData && d3Container.current && !chartRef.current) {
    chartRef.current = new OrgChart(); // Adjust this based on the module structure

    chartRef.current
      .container(d3Container.current)
      .data(organizationItemData)
      .nodeWidth(() => 200)
      .nodeHeight(() => 120)
      .onNodeClick((d) => {
        console.log(d, "Id of clicked node ");
        onNodeClick(d);
      })
      .render();
  }

  return (
    <div className={styles.orgWrapper}>
      <div ref={d3Container} />
    </div>
  );
};

export default OrganizationChart;

Thanks in advance!

bumbeishvili commented 6 days ago

Hi, there is a react integration sample.

Was not it useful?

  1. https://github.com/bumbeishvili/org-chart?tab=readme-ov-file#featured-customizations
  2. https://stackblitz.com/edit/d3-org-chart-react-integration-hooks?file=OrgChart.js

I also wrote a general guide about the integration of d3 visualization (check out the integration section), it might be useful

https://medium.com/@bumbeishvili/best-d3-coding-convention-subjective-practice-proven-through-years-of-work-03c521715b05

jrso15 commented 5 days ago

Hi Bumbeishviili,

I followed some of your documentation.

And this is exactly what I’m getting:

IMG_1714

And I can’t find any solution for this.

I want to implement something like this:

https://stackblitz.com/edit/d3-org-chart-react-integration-hooks-topjz5?file=index.js