This repository has been archived by the owner on Jul 29, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1.5k
adding the option of drawing a background to the edges. #3606
Merged
Merged
Changes from 3 commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
<!doctype html> | ||
<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> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,6 @@ | ||
let util = require("../../../../../util"); | ||
let EndPoints = require("./EndPoints").default; | ||
|
||
|
||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 |
||
/** | ||
* The Base Class for all edges. | ||
* | ||
|
@@ -77,7 +76,6 @@ class EdgeBase { | |
} | ||
} | ||
|
||
|
||
/** | ||
* | ||
* @param {CanvasRenderingContext2D} ctx | ||
|
@@ -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); | ||
} | ||
} | ||
|
@@ -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; | ||
} | ||
|
@@ -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); | ||
} | ||
|
||
|
@@ -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 | ||
|
@@ -156,7 +154,6 @@ class EdgeBase { | |
} | ||
} | ||
|
||
|
||
/** | ||
* | ||
* @param {Node} nearNode | ||
|
@@ -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}; | ||
} | ||
|
@@ -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]; | ||
} | ||
|
||
/** | ||
|
@@ -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. | ||
|
@@ -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); | ||
|
@@ -418,7 +414,6 @@ class EdgeBase { | |
return returnValue; | ||
} | ||
|
||
|
||
/** | ||
* | ||
* @param {number} x1 | ||
|
@@ -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 { | ||
|
@@ -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); | ||
|
@@ -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}; | ||
} | ||
|
||
/** | ||
|
@@ -564,7 +559,6 @@ class EdgeBase { | |
this.disableShadow(ctx, values); | ||
} | ||
|
||
|
||
/** | ||
* | ||
* @param {CanvasRenderingContext2D} ctx | ||
|
@@ -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; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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! 🥇