Search

Dynamic data

Data

Explore how ReGraph draws and animates changes to items.

Dynamic data
View live example →

ReGraph automatically draws and animates changes to your items.

When you update the items prop, ReGraph automatically finds any changes and provides a smooth transition animation from the old state to the new one.

See also

import React, { useState } from "react";
import { createRoot } from "react-dom/client";

import omitBy from "lodash/omitBy";
import { Chart } from "regraph";

import initialData from "./data";
import { style } from "@ci/theme/rg/js/storyStyles";

import "@ci/theme/rg/css/button.css";
import "@ci/theme/rg/css/layout.css";

export const Demo = () => <DynamicData data={initialData()} />;

function DynamicData(props) {
  const { data } = props;
  const [state, setState] = useState({
    count: 1,
    items: data.items,
    layout: data.layout,
  });

  const addNodeAndLink = () => {
    setState((current) => {
      const { count, items } = current;
      const newData = {};
      newData[`node${count}`] = {
        label: { ...style.primaryLabel1, text: count },
        ...style.primary1,
      };
      newData[`link${count}`] = { id1: "node0", id2: `node${count}`, ...style.link };
      const allItems = { ...items, ...newData };
      return { ...current, count: count + 1, items: allItems };
    });
  };

  const remove = () => {
    if (state.count > 1) {
      const nextItems = omitBy(
        state.items,
        (value, key) => parseInt(key.substr(4), 10) === state.count - 1
      );
      setState((current) => {
        return { ...current, count: current.count - 1, items: nextItems };
      });
    }
  };

  return (
    <div className="story">
      <div className="options">
        <button type="button" onClick={addNodeAndLink}>
          Add
        </button>
        <button type="button" onClick={remove} disabled={state.count <= 1}>
          Remove
        </button>
      </div>
      <div className="chart-wrapper">
        <Chart
          items={state.items}
          animation={{ animate: true, time: 650 }}
          layout={state.layout}
          options={{ navigation: false, overview: false }}
        />
      </div>
    </div>
  );
}

const root = createRoot(document.getElementById("regraph"));
root.render(<Demo />);
import { style } from "@ci/theme/rg/js/storyStyles";

function initialData() {
  return {
    items: {
      node0: {
        label: { ...style.primaryLabel2, text: "Press the Add button" },
        ...style.primary2,
      },
    },
    layout: { name: "organic", packing: "rectangle" },
  };
}

export default initialData;
<!doctype html>
<html>
  <body>
    <div id="regraph" style="height: 100vh"></div>
    <script type="module" src="./code.jsx"></script>
  </body>
</html>

Terms of use

These terms do not alter or supersede any existing agreements between you (or your employer) and us.

By accessing or using any Content you agree to be bound by these Terms of Use. Please review these terms carefully before using the website.

The contents of this website, including but not limited to any text, code samples, API references, schemas, interactive tools, and other materials (collectively, the 'Content'), are made available for informational and internal evaluation purposes only. All intellectual property rights in the Content are reserved. No licence is granted to use the Content for any commercial purpose, or to copy, distribute, modify, reverse-engineer, or incorporate any part of the Content into any product or service, without our prior written consent.

This Content is provided “as is” and “as available,” without any representations, warranties, or guarantees of any kind, whether express or implied, including but not limited to implied warranties of merchantability, fitness for a particular purpose, non-infringement, or accuracy. To the fullest extent permitted by applicable law, we expressly exclude and disclaim all implied warranties, conditions, and other terms that might otherwise be implied.

We disclaim all liability for any loss or damage, whether direct, indirect, incidental, consequential, or otherwise, arising from any reliance placed on the Content or from your use of it, to the fullest extent permitted by applicable law. By continuing to access or use the Content, you acknowledge and agree to these terms.