Giter Site home page Giter Site logo

react-native-vis-network's Introduction

react-native-vis-network

react-native-vis-network lets you use vis-network in your React Native projects.

iOS example

Android example

Installation

# react-native-vis-network needs to use react-native-webview to work correctly,
# so install it if you haven't previously installed it in your project
npm install react-native-webview

npm install react-native-vis-network

Usage

import React, { useState } from 'react';
import VisNetwork, { Data } from 'react-native-vis-network';

export default function MyComponent() {
  const [data, setData] = useState<Data>({
    edges: [
      { from: 1, to: 3 },
      { from: 1, to: 2 },
      { from: 2, to: 4 },
      { from: 2, to: 5 },
      { from: 3, to: 3 },
    ],
    nodes: [
      { id: 1, label: 'Node 1' },
      { id: 2, label: 'Node 2' },
      { id: 3, label: 'Node 3' },
      { id: 4, label: 'Node 4' },
      { id: 5, label: 'Node 5' },
    ],
  });
  return <VisNetwork data={data} />;
}

Event Listeners

You can add event listeners for any vis-network Event.

Note that the earliest time you can successfully add an event listener is during the onLoad callback. If you try to add an event listener before then your callback won't be called.

export default function MyComponent() {
  const [loading, setLoading] = useState<boolean>(false);
  const visNetworkRef = useRef<VisNetworkRef>(null);

  useEffect(() => {
    if (!loading || !visNetworkRef.current) {
      return;
    }

    const subscription = visNetworkRef.current.addEventListener(
      'click',
      (event: any) => console.log(JSON.stringify(event, null, 2))
    );

    return subscription.remove;
  }, [loading]);

  // ...

  return (
    <VisNetwork
      data={data}
      onLoad={() => setLoading(true)}
      ref={visNetworkRef}
    />
  );
}

Methods

You can use any of the vis-network methods that are not discussed in the caveats below.

Caveats

  1. It's not possible to use the following methods due to their non-serializable parameters:
  2. It's not possible to use the following methods due to various issues discussed in 87da46d:
  3. Intentionally, the methods on, off, and once have not been implemented. Instead, use addEventListener as described in Event Listeners.
  4. While using methods like addEdgeMode, addNodeMode, setData, and setOptions will successfully update and re-render the network, they will not update the data or options props passed into VisNetwork. Using any of these methods will cause the props to become out-of-sync with what is actually displayed.

Example

export default function MyComponent() {
  const visNetworkRef = useRef<VisNetworkRef>(null);

  // ...

  return (
    <>
      <VisNetwork data={data} ref={visNetworkRef} />
      <Button
        title="Focus on node 1"
        onPress={() => {
          const nodeId = 1;
          visNetworkRef.current?.focus(nodeId, { animation: true, scale: 5 });
        }}
      />
    </>
  );
}

Options

You can use any of the vis-network options that are serializable as JSON (i.e. not functions or callbacks).

Example

export default function MyComponent() {
  const [options, setOptions] = useState<Options>({
    edges: {
      color: 'blue',
      width: 2,
    },
    interaction: {
      dragNodes: false,
      keyboard: false,
    },
    nodes: {
      borderWidth: 4,
      color: 'pink',
    },
  });

  // ...

  return <VisNetwork data={data} options={options} />;
}

Additional Options

In addition to the vis-network options mentioned in the Options section, the following props are also available.

zoomFitOnStabilized

  • If true, the network is zoomed in or out to fill its container.
  • If false, nothing is done. As a result, nodes may be smaller than expected.
  • Default value: true

Contributing

See the contributing guide to learn how to contribute to the repository and the development workflow.

License

MIT


Made with create-react-native-library

react-native-vis-network's People

Contributors

high5apps avatar

Stargazers

 avatar  avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.