Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a way to display a node label at the top inside the node?

I am creating a graph using cytoscape.js and I have compounded nodes which are inside the parent node. I would like to have the title of the main/parent node at the top of the node but inside the node. Is that possible in cytoscape?

I have tried using using halign and valign. Whenever I use top value, it shows outside the box.

Is there a extension or a plugin that lets us do it?

Example with child node: https://stackblitz.com/edit/cytoscape-call-method-child-efmbaj?file=src%2Fapp%2FstylesheetObject.ts

like image 641
Learn AspNet Avatar asked Jun 24 '21 18:06

Learn AspNet


People also ask

How do I show node labels?

Go to View tab--> Assembly display option-->Mesh-->Show node labels. Node numbers will be displayed when you are in Mesh module. pick the part instance where you want to find the node by using node label. write the node label then select highlight item in viewport.

What is the label of a node?

Labeling a node tells Kubernetes what type of application can run on a specific node. It is a means for identifying application processing and qualifying the application as a candidate to run on a specific host system. Labeling is required only for worker nodes and not for master nodes.


2 Answers

As you can read here, you can only place labels inside a node with the center option, a good configuration (label inside at the top) requires a margin to be added to your label:

.selector(':parent')
  .css({
    'text-valign': 'center',
    // the next line moves the parents label up to the top of the node and 5px down to create a padding 
    'text-margin-y': function (node) { return -node.height() + 5 }
})

Here is a working example:

var cy = cytoscape({
  container: document.getElementById('cy'),

  style: cytoscape.stylesheet()
    .selector(':parent')
    .css({
      'text-valign': 'center',
      'text-margin-y': function(node) {
        return -node.height() + 5
      }
    })
    .selector('node')
    .css({
      'height': 'data(size)',
      'width': 'data(size)',
      'border-color': '#000',
      'border-width': '1',
      'content': 'data(name)'
    })
    .selector('edge')
    .css({
      'width': 'data(strength)'
    })
    .selector('#1')
    .css({
      'background-color': 'red'
    })
    .selector('#4')
    .css({
      'background-color': 'green'
    }),

  elements: {
    nodes: [{
        data: {
          id: '1',
          size: 50,
          name: 'a'
        }
      },
      {
        data: {
          id: '2',
          size: 20,
          name: 'b',
          parent: '1'
        }
      },
      {
        data: {
          id: '3',
          size: 40,
          name: 'c',
          parent: '1'
        }
      },
      {
        data: {
          id: '4',
          size: 50,
          name: 'd'
        }
      },
      {
        data: {
          id: '5',
          size: 20,
          name: 'e',
          parent: '4'
        }
      },
      {
        data: {
          id: '6',
          size: 40,
          name: 'f',
          parent: '4'
        }
      }
    ],
  },
});
body {
  font: 14px helvetica neue, helvetica, arial, sans-serif;
}

#cy {
  height: 100%;
  width: 75%;
  position: absolute;
  left: 0;
  top: 0;
  float: left;
}
<html>

<head>
  <meta charset=utf-8 />
  <meta name="viewport" content="user-scalable=no, initial-scale=1.0, minimum-scale=1.0, maximum-scale=1.0, minimal-ui">
  <script src="https://cdnjs.cloudflare.com/ajax/libs/cytoscape/3.2.17/cytoscape.min.js"></script>
  <script src="https://unpkg.com/[email protected]/dist/jquery.js"></script>
</head>

<body>
  <div id="cy"></div>
</body>

</html>
like image 133
Stephan T. Avatar answered Oct 31 '22 16:10

Stephan T.


What I understand is that you want to put the parent label inside the box above child? apologies if I'm wrong

enter image description here

If so, My solution will be to add a padding and then apply margin for alignment.

Modified your example

Styles updated:

  1. 'padding-top':60

    selector: 'node',
    css: {
      content: 'data(label)',
      'text-valign': 'center',
      'text-halign': 'center',
      'font-size': 28,
      'padding-top':60
    }
    
  2. 'text-valign': 'top', and 'text-margin-y': function(node) { return node.height() - 10; }

    selector: 'node[type="parent"]',
    style: {
      shape: 'rectangle',
      'background-color': 'grey',
      width: 300,
      height: 100,
      'font-size': 25.5,
      'font-family': 'Lato, Helvetica Neue, Helvetica, Arial, sans-serif',
      color: 'black',
      'text-valign': 'top',
      'text-halign': 'center',
      'text-margin-y': function(node) {
        return node.height() - 10;
      }
    }
    
like image 23
Abhi Avatar answered Oct 31 '22 17:10

Abhi