Search

Animation

Advanced

Explore how ReGraph animates changes to its state.

Animation
View live example →

ReGraph animates any changes to its state.

To loop an animation, pass new states to ReGraph at intervals.

You can set animate to false to disable animation.

See also

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

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

import data, { styles } from "./data";

const layout = { name: "radial", top: ["node-0-"] };

const transitionTime = 500;

function styledItems(items, highlightFunction = () => false) {
  return mapValues(items, (item, id) => {
    // in this demo the structure of the id is used to tell nodes from links
    const type = id.substr(0, 4);
    const newStyle = styles[highlightFunction(id) ? "highlight" : "plain"][type];
    return { ...item, ...newStyle };
  });
}

export const Demo = () => <Animation items={data()} />;

function Animation(props) {
  const { items } = props;

  const [state, setState] = useState({
    step: 0,
    up: true,
  });

  const highlightFunction = (id) => {
    const { step } = state;
    return Number(id.match(/-(\d+)-/)[1]) < step;
  };

  useEffect(() => {
    const timer = setTimeout(() => {
      const { step, up } = state;
      if (up) {
        if (step === 5) {
          setState({ up: false, step: 4 });
        } else {
          setState({ up: true, step: step + 1 });
        }
      } else if (step === 0) {
        setState({ up: true, step: 1 });
      } else {
        setState({ up: false, step: step - 1 });
      }
    }, transitionTime);
    return () => clearTimeout(timer);
  }, [state]);

  return (
    <div style={{ width: "100%", height: "100%" }}>
      <Chart
        items={styledItems(items, highlightFunction)}
        layout={layout}
        animation={{ animate: true, time: transitionTime }}
        options={{ overview: false, navigation: false }}
      />
    </div>
  );
}

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

export const styles = {
  plain: {
    node: {
      color: style.colors.hackedCharcoal,
    },
    link: {
      color: style.colors.hackedCharcoal,
      width: 2,
    },
  },
  highlight: {
    node: {
      ...style.primary2,
    },
    link: {
      ...style.primary1,
      width: 6,
    },
  },
};

function data() {
  const items = {};
  const center = "node-0-";
  items[center] = {};
  for (let i = 1; i <= 10; i += 1) {
    const inner = `node-2-${i}`;
    items[inner] = {};
    items[`link-1-${i}`] = { id1: center, id2: inner };
    for (let j = 1; j <= 3; j += 1) {
      const outer = `node-4-${10 * i + j}`;
      items[outer] = {};
      items[`link-3-${10 * i + j}`] = { id1: inner, id2: outer };
    }
  }
  return items;
}

export default data;
<!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.