Vis.js node tooltip doesn't show up on hover using ReactJS
Asked Answered
Q

4

7

Greetings,

In my project I am displaying a vis.js graph, using ReactJS and I would like to have the nodes of the displayed network react to the mouse-hover event by displaying a popup/tooltip. Every node has a title attribute, and thats what the tooltip should display.

This is how they do it in their example:

<!doctype html>
<html>
<head>
    <title>Network | Interaction events</title>
    <script type="text/javascript" src="../../../dist/vis.js"></script>
    <link href="../../../dist/vis-network.min.css" rel="stylesheet" type="text/css" />
    <style type="text/css">
        #mynetwork {
            width: 600px;
            height: 400px;
            border: 1px solid lightgray;
        }
    </style>
</head>
<body>
    <div id="mynetwork"></div>
    <script type="text/javascript">
        // create an array with nodes
        var nodes = new vis.DataSet([
            { id: 1, label: 'Node 1', title: 'I have a popup!' },
            { id: 2, label: 'Node 2', title: 'I have a popup!' },
            { id: 3, label: 'Node 3', title: 'I have a popup!' },
            { id: 4, label: 'Node 4', title: 'I have a popup!' },
            { id: 5, label: 'Node 5', title: 'I have a popup!' }
        ]);
        // create an array with edges
        var edges = new vis.DataSet([
            { from: 1, to: 3 },
            { from: 1, to: 2 },
            { from: 2, to: 4 },
            { from: 2, to: 5 }
        ]);
        // create a network
        var container = document.getElementById('mynetwork');
        var data = {
            nodes: nodes,
            edges: edges
        };
        var options = {
            interaction: { hover: true },
            manipulation: {
                enabled: true
            }
        };
        var network = new vis.Network(container, data, options);
    </script>
</body>
</html>
    

Here the popups work.

I am loading the node related data from a database, and the nodes are drawn according to that data just fine. Everything works properly, except the popup doesn't show when triggered by the hover event. This is the relevant part of the component.tsx file:

import * as React from 'react';
import NodeGraph from 'react-graph-vis';
import { IEdge, ISkill } from '../../../models';
import Options from './skillTree.options';
import Props from './skillTree.props';
import State from './skillTree.state';

export default class extends React.Component<Props, State> {
constructor(props) {
    super(props);
    this.state = {
        graph: { nodes: [], edges: [] },
        options: Options,
        selectedNode: undefined,
    }
}
//...
private _skillTreeRequestCallback(graph: {
    nodes: ISkill[],
    edges: IEdge[]
}) {
    graph.nodes = graph.nodes.map(skill => ({
        ...skill,
        hidden: skill.Hidden,
        image: skill.Image,
        label: skill.Label,
        id: skill.Id,
        title: "Hardcoded popup message"
    }));
    graph.edges = graph.edges.map(edge => ({
        ...edge,
        from: edge.From,
        to: edge.To
    }));
    this.setState({ graph, isLoading: false });
}
//...
public render() {
    return (this.state.graph.nodes.length > 0 ? <main style={this.props.style} >
        <NodeGraph graph={this.state.graph} options={this.state.options}/>
    </main> : <LoadingView style={this.props.style} />);
}

And the data arrives just fine from the database. The title attributes of the nodes are there, which I was able to log into the console after the query ran. That title attribute is what the vis.js canvas should display/draw as a popup when the hover event is triggered. The relevant options that I load from this.state.options are:

interaction: {
    hover: true
},
manipulation: {
    enabled: true
},

Yet the popup message doesn't ever show up. I tried to check whether the popup event actually happens, and seemingly it does, since the showPopup event of vis.js, -which happens when a popup is shown- does get triggered, the console.log I put into it runs, but the popup still doesn't actually show up on the screen.

In theory, in ReactJS I use the NodeGraph to draw the graph with the adequate data and it should do basically the same thing as this line:

 var network = new vis.Network(container, data, options);

But apparently something with the popups gets lost in the ReactJS version, unless the reason is that I am doing something wrong.

Does anybody know of any solution that could help me display the title tooltip of a vis.js graph with a hover event in ReactJS? Any help is much appreciated.

Thanks,

Balint

Quoit answered 22/2, 2018 at 14:39 Comment(0)
B
4

Oddly by default the css is hiding the tooltip.

In your CSS set .vis-network: {overflow:visible;}

Ballata answered 21/8, 2018 at 18:43 Comment(1)
.....which CSS?Kyungkyushu
H
7

Cleanest way to do it,

in your index.css file add this line:

@import url("~vis/dist/vis.min.css");
Hafer answered 7/1, 2019 at 13:19 Comment(1)
Where is this file located? What does the notation ~vis mean?Kyungkyushu
B
4

Oddly by default the css is hiding the tooltip.

In your CSS set .vis-network: {overflow:visible;}

Ballata answered 21/8, 2018 at 18:43 Comment(1)
.....which CSS?Kyungkyushu
E
2

For me below CSS worked:

.vis-tooltip {
  position: absolute;
}
Extended answered 17/10, 2022 at 4:13 Comment(0)
I
1

I was having this issue as well a while back. It had to do with having the incorrect path to vis-network.min.css.

In the example they do this: <link href="../../../dist/vis-network.min.css" rel="stylesheet" type="text/css" />

but in your own project that is probably not the correct path. Check your path and that should fix your problem.

Ildaile answered 13/3, 2018 at 18:28 Comment(2)
You answer depends on directory structure... where can I find this CSS file?Kyungkyushu
That's the point of my answer. It does depend on your directory structure and without seeing yours, it's impossible to say. It might be the path to the min.css in your node modules? This was so many years ago I can't remember what it was for me. Or maybe try the latest answer from @manohar above.Ildaile

© 2022 - 2024 — McMap. All rights reserved.