Hello again,
I made a couple of workarounds to the example2 code for adding labels to edges.
Since this is not a built-in feature for the RGraph the code might look a little complicated at first sight.
The approach I've taken is to:
1) Add some custom code for creating a new custom EdgeType for the RGraph that plots an edge and also creates and places an edge label.
2) Then I set the edge.type property in the rgraph constructor as 'custom-line' for using this new edge type.
3) These edge labels are created when the adjacency data object contains the labelid and labeltext properties.
I attach the example2.js modified code in this message.
At line 33 I add an adjacency with labelid and labeltext parameters:
33 "adjacencies": [{
34 "nodeTo": "node1",
35 "data": {
36 "weight": 3,
37 "labeltext":"some label text",
38 "labelid":"node0-node1"
39 }
Then I define a new EdgeType just like specified in the documentation:
(This is kind of advanced so if you have any question about this code I'll be happy to help!)
http://thejit.org/docs/files/RGraph-js.html#RGraph.Plot.EdgeTypes268 RGraph.Plot.EdgeTypes.implement({
269 'custom-line': function(adj, canvas) {
270 //plot arrow edge
271 this.edgeTypes.arrow.call(this, adj, canvas);
272 //get nodes cartesian coordinates
273 var pos = adj.nodeFrom.pos.getc(true);
274 var posChild = adj.nodeTo.pos.getc(true);
275 //check for edge label in data
276 var data = adj.data;
277 if(data.labelid && data.labeltext) {
278 var domlabel = document.getElementById(data.labelid);
279 //if the label doesn't exist create it and append it
280 //to the label container
281 if(!domlabel) {
282 domlabel= document.createElement('div');
283
domlabel.id = data.labelid;
284 domlabel.innerHTML = data.labeltext;
285 //add some custom style
286 var style = domlabel.style;
287 style.position = 'absolute';
288 style.color = '#fff';
289 style.fontSize = '9px';
290 //append the label to the labelcontainer
291 this.getLabelContainer().appendChild(domlabel);
292 }
293
294 //now adjust the label placement
295 var radius = this.viz.canvas.getSize();
296 domlabel.style.left = parseInt((pos.x + posChild.x
297 + radius.width - domlabel.offsetWidth) /2) + 'px';
298 domlabel.style.top = parseInt((pos.y
299 + posChild.y + radius.height) /2) + 'px';
300 }
301 }
302 });
Now I set that EdgeType in the rgraph constructor:
318 Edge: {
319 'overridable': true,
320 'color': '#cccc00',
321 'type': 'custom-line'
322 },
And I think that's it :)
Hope it helps,