vasturiano / timelines-chart

Timelines Chart
http://vasturiano.github.io/timelines-chart/example/categorical/
MIT License
556 stars 121 forks source link

react? #47

Closed martinch-kth closed 5 years ago

martinch-kth commented 5 years ago

How can I use this lib as a react component? A simple example would be great. Thanks!

vasturiano commented 5 years ago

@martinch-kth wrapping it with react-kapsule should provide you with a React interface.

peterbe commented 3 years ago

In case it helps anybody, here's what seems to work fine:

function Chart({ data }: { data: Group[] }) {
  const ref = useRef<null | HTMLDivElement>(null);

  useEffect(() => {
    let chart: TimelinesChartInstance | null = null;
    const { current: node } = ref;
    if (node) {
      chart = TimelinesChart()(node)
        // .zScaleLabel('My Scale Units')
        // .zQualitative(true)
        .data(data);
    }
    return () => {
      if (node) {
        while (node.firstChild) {
          node.removeChild(node.firstChild);
        }
      }
    };
  }, [data]);
  return <div ref={ref}></div>;
}

I've only just got it to work and not familiar with any problems yet.