Question

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?

Was it helpful?

Solution 2

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.

OTHER TIPS

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: {
    }                                                                  
});                                                                                                                                                  

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();
};
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top