pyvis icon indicating copy to clipboard operation
pyvis copied to clipboard

Labels disappear when I ry to get rid of overlapping

Open S3j5b0 opened this issue 4 years ago • 6 comments

I'm doing a project where i am drawing a graph from a python repo, my code is really long, and not the important part. But here it is, in case someone wants to try to reporduce:

    import ast
    from radon.visitors import ComplexityVisitor
    import re
    import os
    from pyvis.network import Network
    
    
    class Vert:
        def __init__(self, name, id, size ,edges):
            self.name = name
            self.size = size
            self.edges = edges
            self.id = id
            
    from pathlib import Path
    
    rootDir = "/home/ask/Git/Zeeguu-API/"
    directories = set()
    # this is horrible
    for file in Path(rootDir).rglob("*.py"):
        localDirs = str(file).split('/')
        directories.add(localDirs[-2])
    
    
    def extract_importandClass_from_line(unline):
    
        x = re.search("^import (\S+)", unline) 
        x = re.search("^from (\S+)", unline) 
        return x.group(1)#, c.group(1).split('(')[0]
    def extractClass(inline):
        c = re.search("^class (\S+)", inline) 
        return c.group(1).split('(')[0]
    
    
    def importsAndClass(file):
        lines = [line for line in open(file)]
        classes = []
        all_imports = []
        for line in lines:
            try:
                imports = extract_importandClass_from_line(line)
                importEnd = imports.rsplit('.',1)[-1]
                importsFormatted = imports.replace('.', '/')
                if (importEnd not in directories):
                    all_imports.append(importsFormatted)
            except:
                try:
                    class1 = extractClass(line)
                    classes.append(class1)
                except:
                    continue  
      
        return all_imports, classes
    net = Network(directed=True, height="1500px", width="100%")
    nodes = {}
    nodeNames = set()
    counter = 0
    for file in Path(rootDir).rglob("*.py"):
        # Opening file, and looking at contents
        f = open(file, "r")
        s = f.read()
        # analyzing complexity
        filename = str(file).replace(rootDir, "")
        analyzer = ComplexityVisitor.from_code(s)
        
        # getting the file name 
        splitFile = os.path.splitext(file.name)
        #getting imports    
        imports, classes = importsAndClass(file)
    
        nodeNames.add(str(filename))
        v = Vert(str(filename), counter,analyzer.total_complexity, imports)
        #creating vertex
        nodes[v.name] = v
        counter = counter + 1 
        net.add_node(v.id, label=v.name, size=v.size*2)
    
    print("_________________________________")
    for k, v in nodes.items():
        
        for i in v.edges:
            withPY = i + ".py"
            print(withPY)
            try:
                to = nodes[withPY].id 
                net.add_edge(v.id, to)
            except:
                print("could not add edge to:" + str(i))    
            
    

    net.show("network.html")

Now, when i draw my graph, it looks like this, things are super bunched up on top of eachother. So I wanted some way to avoid overlapping. After researching a bit, I found that I should probably add this line:

net.barnes_hut(overlap=1) Which, I do. This results in this, which looks much nicer, but now all of the sudden, all the labels on the nodes are gone!?.

Why is my labels gone? and how can I get both non-overlapping, and labels?

S3j5b0 avatar May 04 '21 20:05 S3j5b0

Are the labels truly gone? If you zoom in can you see the labels?

boludo00 avatar May 04 '21 20:05 boludo00

I was just about to zoom in and screenshot to show you that the labels were gone, but really they were just very very small. thx!

Is there any way I can make them appear while less zoomed in?

S3j5b0 avatar May 04 '21 20:05 S3j5b0

You know im not entirely sure...

I'm looking at the docs here and maybe the scaling.label property has something to do with it? Have to play around with it a bit. and

boludo00 avatar May 04 '21 21:05 boludo00

Oh, I found out.

When adding nodes to your network, supply the scaling object:

g.add_node(nodeid, scaling={"label": {"min": 8, "max": 20}})

Looks like by adjusting the min the labels can show up when zoomed all the way out.

boludo00 avatar May 04 '21 21:05 boludo00

great thx a lot!

S3j5b0 avatar May 04 '21 21:05 S3j5b0

Hi again, sorry for being annoying with this issue, but I really can't figure out how to use the scaling. I've tried adding the nodes with a bunch of different values:

{"min": 8, "max": 20}
{"min": 10, "max": 25}
{"min": 20, "max": 40}
{"min": 40, "max": 80}
{"min": 800000000, "max": 200000000000}

None of it seems to have any effect at all, and the text on the nodes is the same size all oif the time

S3j5b0 avatar May 24 '21 13:05 S3j5b0