Skip to content
This repository has been archived by the owner on Jul 29, 2019. It is now read-only.

adding the option of drawing a background to the edges. #3606

Merged
merged 5 commits into from
Oct 23, 2017
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1,741 changes: 969 additions & 772 deletions docs/network/edges.html

Large diffs are not rendered by default.

67 changes: 67 additions & 0 deletions examples/network/edgeStyles/background.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
<!doctype html>
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@yoavdamri Thanks for creating an example! 🥇

<html>
<head>
<title>Network | Edge background</title>

<script type="text/javascript" src="../../../dist/vis.js"></script>
<link href="../../../dist/vis-network.min.css" rel="stylesheet" type="text/css" />

<style type="text/css">
#mynetwork {
width: 600px;
height: 400px;
border: 1px solid lightgray;
}
</style>
</head>
<body>

<p>
Edge background.
</p>

<div id="mynetwork"></div>

<script type="text/javascript">
// create an array with nodes
var nodes = new vis.DataSet([
{id: 1, label: 'Node 1'},
{id: 2, label: 'Node 2'},
{id: 3, label: 'Node 3'},
{id: 4, label: 'Node 4'},
{id: 5, label: 'Node 5'},
{id: 6, label: 'Node 6'}
]);

// create an array with edges
var edges = new vis.DataSet([
{from: 1, to: 3, dashes:true},
{from: 1, to: 2, dashes:[5,5]},
{from: 2, to: 4, dashes:[5,5,3,3], background: false},
{from: 2, to: 5, dashes:[2,2,10,10]},
{from: 2, to: 6, dashes:false, background:{ enabled: true, color: 'rgba(111,111,111,0.5)', size:10, dashes: [20,10] }},
]);

// create a network
var container = document.getElementById('mynetwork');
var data = {
nodes: nodes,
edges: edges
};
var options = {
edges:{
shadow: true,
smooth: true,
background:{
enabled: true,
color: '#ff0000'
}
}
}

var network = new vis.Network(container, data, options);
</script>


</body>
</html>
6 changes: 6 additions & 0 deletions lib/network/modules/EdgesHandler.js
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,12 @@ class EdgesHandler {
x:5,
y:5
},
background:{
enabled: false,
color: 'rgba(111,111,111,1)',
size:10,
dashes: false
},
smooth: {
enabled: true,
type: "dynamic",
Expand Down
7 changes: 6 additions & 1 deletion lib/network/modules/components/Edge.js
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,7 @@ class Edge {

util.mergeOptions(parentOptions, newOptions, 'smooth', globalOptions);
util.mergeOptions(parentOptions, newOptions, 'shadow', globalOptions);
util.mergeOptions(parentOptions, newOptions, 'background', globalOptions);

if (newOptions.dashes !== undefined && newOptions.dashes !== null) {
parentOptions.dashes = newOptions.dashes;
Expand Down Expand Up @@ -270,7 +271,11 @@ class Edge {
shadowX: this.options.shadow.x,
shadowY: this.options.shadow.y,
dashes: this.options.dashes,
width: this.options.width
width: this.options.width,
background: this.options.background.enabled,
backgroundColor: this.options.background.color,
backgroundSize: this.options.background.size,
backgroundDashes: this.options.background.dashes
};
if (this.selected || this.hover) {
if (this.chooser === true) {
Expand Down
2 changes: 2 additions & 0 deletions lib/network/modules/components/edges/util/BezierEdgeBase.js
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,8 @@ class BezierEdgeBase extends EdgeBase {
// fallback to normal straight edge
ctx.lineTo(this.toPoint.x, this.toPoint.y);
}
// draw a background
this.drawBackground(ctx, values);

// draw shadow if enabled
this.enableShadow(ctx, values);
Expand Down
92 changes: 71 additions & 21 deletions lib/network/modules/components/edges/util/EdgeBase.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
let util = require("../../../../../util");
let EndPoints = require("./EndPoints").default;


Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@yoavdamri Could you please only change the lines of code that really needs to be changed. Syntax "improvements" should not be mixed with other pull requests. The only change in this file should be the drawBackground and the setStrokeDashed functions.
I know this was probably your editor that did those changes but maybe you can teach it not to this!? Thanks

/**
* The Base Class for all edges.
*
Expand Down Expand Up @@ -77,7 +76,6 @@ class EdgeBase {
}
}


/**
*
* @param {CanvasRenderingContext2D} ctx
Expand All @@ -93,7 +91,7 @@ class EdgeBase {
this._line(ctx, values, viaNode, fromPoint, toPoint);
}
else {
let [x,y,radius] = this._getCircleData(ctx);
let [x, y, radius] = this._getCircleData(ctx);
this._circle(ctx, values, x, y, radius);
}
}
Expand All @@ -109,7 +107,7 @@ class EdgeBase {
*/
_drawDashedLine(ctx, values, viaNode, fromPoint, toPoint) { // eslint-disable-line no-unused-vars
ctx.lineCap = 'round';
let pattern = [5,5];
let pattern = [5, 5];
if (Array.isArray(values.dashes) === true) {
pattern = values.dashes;
}
Expand All @@ -128,7 +126,7 @@ class EdgeBase {
this._line(ctx, values, viaNode);
}
else {
let [x,y,radius] = this._getCircleData(ctx);
let [x, y, radius] = this._getCircleData(ctx);
this._circle(ctx, values, x, y, radius);
}

Expand All @@ -143,7 +141,7 @@ class EdgeBase {
ctx.dashedLine(this.from.x, this.from.y, this.to.x, this.to.y, pattern);
}
else {
let [x,y,radius] = this._getCircleData(ctx);
let [x, y, radius] = this._getCircleData(ctx);
this._circle(ctx, values, x, y, radius);
}
// draw shadow if enabled
Expand All @@ -156,7 +154,6 @@ class EdgeBase {
}
}


/**
*
* @param {Node} nearNode
Expand Down Expand Up @@ -186,10 +183,10 @@ class EdgeBase {
to = this._findBorderPosition(this.to, ctx);
}
else {
let [x,y] = this._getCircleData(ctx).slice(0, 2);
let [x, y] = this._getCircleData(ctx).slice(0, 2);

from = this._findBorderPositionCircle(this.from, ctx, {x, y, low:0.25, high:0.6, direction:-1});
to = this._findBorderPositionCircle(this.from, ctx, {x, y, low:0.6, high:0.8, direction:1});
from = this._findBorderPositionCircle(this.from, ctx, {x, y, low: 0.25, high: 0.6, direction: -1});
to = this._findBorderPositionCircle(this.from, ctx, {x, y, low: 0.6, high: 0.8, direction: 1});
}
return {from, to};
}
Expand Down Expand Up @@ -220,7 +217,7 @@ class EdgeBase {
x = node.x + radius;
y = node.y - node.shape.height * 0.5;
}
return [x,y,radius];
return [x, y, radius];
}

/**
Expand Down Expand Up @@ -386,7 +383,6 @@ class EdgeBase {
this.disableShadow(ctx, values);
}


/**
* Calculate the distance between a point (x3,y3) and a line segment from (x1,y1) to (x2,y2).
* (x3,y3) is the point.
Expand All @@ -409,7 +405,7 @@ class EdgeBase {
returnValue = this._getDistanceToEdge(x1, y1, x2, y2, x3, y3, via)
}
else {
let [x,y,radius] = this._getCircleData(undefined);
let [x, y, radius] = this._getCircleData(undefined);
let dx = x - x3;
let dy = y - y3;
returnValue = Math.abs(Math.sqrt(dx * dx + dy * dy) - radius);
Expand All @@ -418,7 +414,6 @@ class EdgeBase {
return returnValue;
}


/**
*
* @param {number} x1
Expand Down Expand Up @@ -504,7 +499,7 @@ class EdgeBase {
if (position !== 'middle') {
// draw arrow head
if (this.options.smooth.enabled === true) {
arrowPoint = this.findBorderPosition(node1, ctx, { via: viaNode });
arrowPoint = this.findBorderPosition(node1, ctx, {via: viaNode});
let guidePos = this.getPoint(Math.max(0.0, Math.min(1.0, arrowPoint.t + guideOffset)), viaNode);
angle = Math.atan2((arrowPoint.y - guidePos.y), (arrowPoint.x - guidePos.x));
} else {
Expand All @@ -517,13 +512,13 @@ class EdgeBase {
}
} else {
// draw circle
let [x,y,radius] = this._getCircleData(ctx);
let [x, y, radius] = this._getCircleData(ctx);

if (position === 'from') {
arrowPoint = this.findBorderPosition(this.from, ctx, { x, y, low: 0.25, high: 0.6, direction: -1 });
arrowPoint = this.findBorderPosition(this.from, ctx, {x, y, low: 0.25, high: 0.6, direction: -1});
angle = arrowPoint.t * -2 * Math.PI + 1.5 * Math.PI + 0.1 * Math.PI;
} else if (position === 'to') {
arrowPoint = this.findBorderPosition(this.from, ctx, { x, y, low: 0.6, high: 1.0, direction: 1 });
arrowPoint = this.findBorderPosition(this.from, ctx, {x, y, low: 0.6, high: 1.0, direction: 1});
angle = arrowPoint.t * -2 * Math.PI + 1.5 * Math.PI - 1.1 * Math.PI;
} else {
arrowPoint = this._pointOnCircle(x, y, radius, 0.175);
Expand All @@ -536,9 +531,9 @@ class EdgeBase {

var xi = arrowPoint.x - length * 0.9 * Math.cos(angle);
var yi = arrowPoint.y - length * 0.9 * Math.sin(angle);
let arrowCore = { x: xi, y: yi };
let arrowCore = {x: xi, y: yi};

return { point: arrowPoint, core: arrowCore, angle: angle, length: length, type: type };
return {point: arrowPoint, core: arrowCore, angle: angle, length: length, type: type};
}

/**
Expand All @@ -564,7 +559,6 @@ class EdgeBase {
this.disableShadow(ctx, values);
}


/**
*
* @param {CanvasRenderingContext2D} ctx
Expand Down Expand Up @@ -592,6 +586,62 @@ class EdgeBase {
ctx.shadowOffsetY = 0;
}
}

/**
*
* @param {CanvasRenderingContext2D} ctx
* @param {{toArrow: boolean, toArrowScale: (allOptions.edges.arrows.to.scaleFactor|{number}|allOptions.edges.arrows.middle.scaleFactor|allOptions.edges.arrows.from.scaleFactor|Array|number), toArrowType: *, middleArrow: boolean, middleArrowScale: (number|allOptions.edges.arrows.middle.scaleFactor|{number}|Array), middleArrowType: (allOptions.edges.arrows.middle.type|{string}|string|*), fromArrow: boolean, fromArrowScale: (allOptions.edges.arrows.to.scaleFactor|{number}|allOptions.edges.arrows.middle.scaleFactor|allOptions.edges.arrows.from.scaleFactor|Array|number), fromArrowType: *, arrowStrikethrough: (*|boolean|allOptions.edges.arrowStrikethrough|{boolean}), color: undefined, inheritsColor: (string|string|string|allOptions.edges.color.inherit|{string, boolean}|Array|*), opacity: *, hidden: *, length: *, shadow: *, shadowColor: *, shadowSize: *, shadowX: *, shadowY: *, dashes: (*|boolean|Array|allOptions.edges.dashes|{boolean, array}), width: *}} values
*/
drawBackground(ctx, values) {
if (values.background !== false) {
let attrs = ['strokeStyle', 'lineWidth', 'dashes'];
let origCtxAttr = {};
// save original line attrs
attrs.forEach(function(attrname) {
origCtxAttr[attrname] = ctx[attrname];
});

ctx.strokeStyle = values.backgroundColor;
ctx.lineWidth = values.backgroundSize;
this.setStrokeDashed(ctx, values.backgroundDashes);

ctx.stroke();

// restore original line attrs
attrs.forEach(function(attrname) {
ctx[attrname] = origCtxAttr[attrname];
});
this.setStrokeDashed(ctx, values.dashes);
}
}

/**
*
* @param {CanvasRenderingContext2D} ctx
* @param {boolean|Array} dashes
*/
setStrokeDashed(ctx, dashes) {
if (dashes !== false) {
if (ctx.setLineDash !== undefined) {
let pattern = [5, 5];
if (Array.isArray(dashes) === true) {
pattern = dashes;
}
ctx.setLineDash(pattern);
}
else {
console.warn("setLineDash is not supported in this browser. The dashed stroke cannot be used.");
}
}
else {
if (ctx.setLineDash !== undefined) {
ctx.setLineDash([]);
}
else {
console.warn("setLineDash is not supported in this browser. The dashed stroke cannot be used.");
}
}
}
}

export default EdgeBase;
7 changes: 7 additions & 0 deletions lib/network/options.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,13 @@ let allOptions = {
__type__: { string: ['from', 'to', 'middle'], object }
},
arrowStrikethrough: { boolean: bool },
background: {
enabled: { boolean: bool },
color: { string },
size: { number },
dashes: { boolean: bool, array },
__type__: { object, boolean: bool }
},
chosen: {
label: { boolean: bool, 'function': 'function' },
edge: { boolean: bool, 'function': 'function' },
Expand Down