Search

Basic Chart

TypeScript
Basic Chart
View live example →

Click on the buttons above the chart to add or remove nodes.

ReGraph has full type definitions. See index.d.ts for Chart, TimeBar and object format definitions, and analysis.d.ts for analysis functions.

See also

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

import omitBy from "lodash/omitBy";
import { Chart, type Items, type Link, type Node } from "regraph";

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

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

interface Props {
  data: Data;
}

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

function BasicChart(props: Props) {
  const { data } = props;
  const ref = createRef<Chart>();

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

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

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

  const click = ({ id }: Chart.PreventablePointerEvent) => {
    const { items } = state;
    if (id != null && items[id] && ref.current) {
      ref.current.ping(id);
    }
  };

  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
          ref={ref}
          items={state.items}
          layout={state.layout}
          animation={{ animate: true, time: 650 }}
          onClick={click}
          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.