Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Vis js network - align label with one letter in node

I have to align label with one letter (number) inside node using vis js network. As documentation says, there is font.align property inside node option and by default it's set to center. It looks nice when there are more than one letter in label: enter image description here

However with one letter it looks like labels are aligned to the left: enter image description here

Is it a bug or am I doing something wrong? If it's a bug, how can I get around it?

My full code (i'm using react-graph-vis version 1.0.5):

import React from 'react';
import Graph from "react-graph-vis";
import 'vis-network/styles/vis-network.min.css';

const ExecutionPathGraph = ({issueTraces}) => {

  const graph = {
    nodes: [
      { id: 1, label: "1"},
      { id: 2, label: "2"},
      { id: 3, label: "3"},
      { id: 4, label: "4"},
      { id: 5, label: "5"}
    ],
    edges: [
      { from: 1, to: 2 },
      { from: 1, to: 3 },
      { from: 2, to: 4 },
      { from: 2, to: 5 }
    ]
  };

  const options = {
    height: '500px',
    nodes: {
      shape: 'circle',
      borderWidth: 0,
      color: '#000',
      font: {
        color: '#fff',
        size: 18,
        align: 'center'
      },
      shadow: true,
    },
    edges: {
      color: {
        color: '#F98323'
      },
      width: 1.5
    },
    layout: {
      hierarchical: {
        sortMethod: 'directed',
        shakeTowards: 'roots',
        direction: 'UD'
      }
    },
    interaction: {
      dragNodes: false,
      dragView: false,
      selectable: false,
      selectConnectedEdges: false,
      hover: false,
      hoverConnectedEdges: false,
      zoomView: false,
    },
    physics: false
  };

  return (
      <Graph
          graph={graph}
          options={options}
      />
  );
};

export default ExecutionPathGraph;
like image 810
Vyacheslav Kovalchuk Avatar asked Nov 20 '25 21:11

Vyacheslav Kovalchuk


1 Answers

I ran into this same issue and have a really dumb solution: add an empty space before and after your number.

nodes: [
  { id: 1, label: " 1 "},
  { id: 2, label: " 2 "},
  { id: 3, label: " 3 "},
  { id: 4, label: " 4 "},
  { id: 5, label: " 5 "}
like image 135
E Z Avatar answered Nov 23 '25 12:11

E Z



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!