How do I show edge labels on graphs?

空扰寡人 提交于 2019-12-30 05:08:35

问题


I am trying to load a gexf file with sigInst.parseGexf('data/test.gexf').

To create an edge with label I have this line in gexf file:

<edge label="test" id="7" source="14" target="18" type="directed"/>

but seems that sigma.js ignore this label field.

How can I show edge labels in graphs?


回答1:


It's not yet possible with the main sigma.js library. However, I just forked the repository and added the capability on github.com here: https://github.com/sam2themax/sigma.js

To enable this new feature, set edgeLabels to true in your drawingProperties.




回答2:


It's now possible to do this with a new plugin in the sigma repository: https://github.com/jacomyal/sigma.js/tree/master/plugins/sigma.renderers.edgeLabels

Just follow the instructions to build the Sigma project and you'll find this file in the /build/plugins folder: sigma.renderers.edgeLabels.min.js

Include that in your html file:

<script src="sigma.min.js"></script>
<script src="sigma.renderers.edgeLabels.min.js"></script>

Make sure your edges have a 'label' key defined

var data = {
    // specify 'nodes' as well
    edges: [                       
        {                          
            id: "e1",              
            source: "user", 
            target: "b1",        
            label: "This is the label", // <----- define 'label' key for your edges          
        }, 
    ]       
}

And then specify your renderer in the Sigma initialization.

var s = new sigma({                                                    
    graph: data,                                                       
    renderer: {                                                        
        container: "container",                                        
        type: "canvas"                                                 
    },                                                                 
    settings: {
    }                                                                  
});                                                                                                                                                  



回答3:


All credit goes to @sam2themax - who forked it out. I am just adding the relevant portions to make life easier.

On sigma.js main library - go to function drawEdge(edge) Add the below lines :

  var p1 = {};
  p1.x = sourceCoordinates[0];
  p1.y = sourceCoordinates[1];
  var p2 = {};
  p2.x = targetCoordinates[0];
  p2.y = targetCoordinates[1];
  drawEdgeLabel(ctx,edge['label'],p1,p2, color);

Create the new method :

  function drawEdgeLabel(ctx, text, p1, p2, color) {
console.log(text);
var alignment = 'center';
var padding = 10;

var dx = p2.x - p1.x;
var dy = p2.y - p1.y;
var len = Math.sqrt(dx * dx + dy * dy);
var avail = len - 2 * padding;

// Keep text upright
var angle = Math.atan2(dy, dx);
if (angle < -Math.PI / 2 || angle > Math.PI / 2) {
  var p = p1;
  p1 = p2;
  p2 = p;
  dx *= -1;
  dy *= -1;
  angle -= Math.PI;
}

var p = p1;
var pad = 1 / 2;

ctx.save();
ctx.textAlign = alignment;
ctx.translate(p.x + dx * pad, p.y + dy * pad);
ctx.rotate(angle);
var fontSize = self.p.defaultLabelSize;
ctx.font = self.p.fontStyle + fontSize + 'px ' + self.p.font;
ctx.fillStyle = color;
ctx.fillText(text, 0, -5);
ctx.restore();
};


来源:https://stackoverflow.com/questions/19401147/how-do-i-show-edge-labels-on-graphs

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!