Thanks to visit codestin.com
Credit goes to github.com

Skip to content
This repository was archived by the owner on Jul 29, 2019. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from all 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
20 changes: 17 additions & 3 deletions docs/network/nodes.html
Original file line number Diff line number Diff line change
Expand Up @@ -734,14 +734,28 @@ <h3>Options</h3>
<td><code>undefined</code></td>
<td>The id of the node. The id is mandatory for nodes and they have to be unique. This should obviously be set per node, not globally.</td>
</tr>
<tr>
<td>image</td>
<td>String</td>
<tr class='toggle collapsible' onclick="toggleTable('optionTable','image', this);">
<td><span parent="image" class="right-caret"></span> image</td>
<td>Object or String</td>
<td><code>undefined</code></td>
<td>When the shape is set to <code>image</code> or <code>circularImage</code>, this option should be the URL
to an image. If the image cannot be found, the brokenImage option can be used.
</td>
</tr>
<tr parent="image" class="hidden">
<td class="indent">image.unselected</td>
<td>String</td>
<td><code>undefined</code></td>
<td>Unselected (default) image URL.
</td>
</tr>
<tr parent="image" class="hidden">
<td class="indent">image.selected</td>
<td>String</td>
<td><code>undefined</code></td>
<td>Selected image URL.
</td>
</tr>
<tr>
<td>label</td>
<td>String</td>
Expand Down
Binary file added examples/network/imageSelected/broken-image.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
82 changes: 82 additions & 0 deletions examples/network/imageSelected/imageSelected.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
<html>
<head>
<title>Network | Selected/Unselected Image</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">
body {
font: 10pt arial;
}
#mynetwork {
width: 600px;
height: 600px;
border: 1px solid lightgray;
}
</style>
</head>

<body>
<div id="mynetwork"></div>
<script type="text/javascript">
// create an array with nodes
var nodes = new vis.DataSet([{
id: 1,
shape: 'image',
size: 20,
label: 'No select image',
image: './unselected.svg',
}, {
id: 2,
shape: 'image',
size: 20,
label: 'Select image broken',
image: {
unselected: './unselected.svg',
selected: './BROKEN_LINK/selected.svg',
},
Copy link
Member

Choose a reason for hiding this comment

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

Here is a unnecessary comma. Probably my mistake!

Copy link
Member

Choose a reason for hiding this comment

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

@mehl321 You don't have to change this of course, but you can if you want.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

You decide :) I like to use trailing commas as part of my code style because all lines look the same and you can safely copy-paste them. Really it's up to you, depending on the code style of vis.js.

}, {
id: 3,
shape: 'image',
size: 20,
label: 'Select works!',
image: {
unselected: './unselected.svg',
selected: './selected.svg',
},
shapeProperties: {
borderDashes: [15, 5],
interpolation: false,
}
}]);

// create an array with edges
var edges = new vis.DataSet([
{from: 1, to: 2},
{from: 2, to: 3},
]);

// create a network
var container = document.getElementById('mynetwork');

// provide the data in the vis format
var data = {
nodes: nodes,
edges: edges
};

var options = {
layout:{
randomSeed: 5
},
nodes: {
brokenImage: './broken-image.png',
}
};
// initialize!
var network = new vis.Network(container, data, options);
</script>

</body>
</html>
53 changes: 53 additions & 0 deletions examples/network/imageSelected/selected.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
6 changes: 6 additions & 0 deletions examples/network/imageSelected/unselected.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
15 changes: 10 additions & 5 deletions lib/network/modules/components/Node.js
Original file line number Diff line number Diff line change
Expand Up @@ -107,14 +107,14 @@ class Node {
if (!options) {
return;
}

// basic options
if (options.id !== undefined) {this.id = options.id;}

if (this.id === undefined) {
throw "Node must have an id";
}


// set these options locally
// clear x and y positions
if (options.x !== undefined) {
Expand Down Expand Up @@ -144,7 +144,12 @@ class Node {
// load the images
if (this.options.image !== undefined) {
if (this.imagelist) {
this.imageObj = this.imagelist.load(this.options.image, this.options.brokenImage, this.id);
if (typeof this.options.image === 'string') {
this.imageObj = this.imagelist.load(this.options.image, this.options.brokenImage, this.id);
} else {
this.imageObj = this.imagelist.load(this.options.image.unselected, this.options.brokenImage, this.id);
this.imageObjAlt = this.imagelist.load(this.options.image.selected, this.options.brokenImage, this.id);
}
}
else {
throw "No imagelist provided";
Expand Down Expand Up @@ -295,7 +300,7 @@ class Node {

updateShape(currentShape) {
if (currentShape === this.options.shape && this.shape) {
this.shape.setOptions(this.options, this.imageObj);
this.shape.setOptions(this.options, this.imageObj, this.imageObjAlt);
}
else {
// choose draw method depending on the shape
Expand All @@ -307,7 +312,7 @@ class Node {
this.shape = new Circle(this.options, this.body, this.labelModule);
break;
case 'circularImage':
this.shape = new CircularImage(this.options, this.body, this.labelModule, this.imageObj);
this.shape = new CircularImage(this.options, this.body, this.labelModule, this.imageObj, this.imageObjAlt);
break;
case 'database':
this.shape = new Database(this.options, this.body, this.labelModule);
Expand All @@ -325,7 +330,7 @@ class Node {
this.shape = new Icon(this.options, this.body, this.labelModule);
break;
case 'image':
this.shape = new Image(this.options, this.body, this.labelModule, this.imageObj);
this.shape = new Image(this.options, this.body, this.labelModule, this.imageObj, this.imageObjAlt);
break;
case 'square':
this.shape = new Square(this.options, this.body, this.labelModule);
Expand Down
12 changes: 9 additions & 3 deletions lib/network/modules/components/nodes/shapes/CircularImage.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
'use strict';


import CircleImageBase from '../util/CircleImageBase'

class CircularImage extends CircleImageBase {
constructor (options, body, labelModule, imageObj) {
constructor (options, body, labelModule, imageObj, imageObjAlt) {
super(options, body, labelModule);
this.imageObj = imageObj;

this.setImages(imageObj, imageObjAlt);

this._swapToImageResizeWhenImageLoaded = true;
}

Expand All @@ -31,6 +32,11 @@ class CircularImage extends CircleImageBase {
}

draw(ctx, x, y, selected, hover, values) {
// switch images depending on 'selected' if imageObjAlt exists
if (this.imageObjAlt) {
this.switchImages(selected);
}

this.resize();

this.left = x - this.width / 2;
Expand Down
12 changes: 10 additions & 2 deletions lib/network/modules/components/nodes/shapes/Image.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,24 @@
import CircleImageBase from '../util/CircleImageBase'

class Image extends CircleImageBase {
constructor (options, body, labelModule, imageObj) {
constructor (options, body, labelModule, imageObj, imageObjAlt) {
super(options, body, labelModule);
this.imageObj = imageObj;

this.setImages(imageObj, imageObjAlt);
}

resize() {
this._resizeImage();
}

draw(ctx, x, y, selected, hover, values) {
// switch images depending on 'selected' if imageObjAlt exists
if (this.imageObjAlt) {
this.switchImages(selected);
}

this.selected = selected;

this.resize();
this.left = x - this.width / 2;
this.top = y - this.height / 2;
Expand Down
25 changes: 24 additions & 1 deletion lib/network/modules/components/nodes/util/CircleImageBase.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,36 @@ class CircleImageBase extends NodeBase {
super(options, body, labelModule);
this.labelOffset = 0;
this.imageLoaded = false;
this.selected = false;
}

setOptions(options, imageObj) {
setOptions(options, imageObj, imageObjAlt) {
this.options = options;
this.setImages(imageObj, imageObjAlt);
}

setImages(imageObj, imageObjAlt) {
if (imageObj) {
this.imageObj = imageObj;

if (imageObjAlt) {
this.imageObjAlt = imageObjAlt;
}
}
}

/**
* Switch between the base and the selected image.
*/
switchImages(selected) {
if ((selected && !this.selected) || (!selected && this.selected)) {
let imageTmp = this.imageObj;
this.imageObj = this.imageObjAlt;
this.imageObjAlt = imageTmp;
}

// keep current state in memory
this.selected = selected;
}

/**
Expand Down
6 changes: 5 additions & 1 deletion lib/network/options.js
Original file line number Diff line number Diff line change
Expand Up @@ -280,7 +280,11 @@ let allOptions = {
__type__: { object }
},
id: { string, number },
image: { string, 'undefined': 'undefined' }, // --> URL
image: {
selected: { string, 'undefined': 'undefined' }, // --> URL
unselected: { string, 'undefined': 'undefined' }, // --> URL
__type__: { object, string }
},
label: { string, 'undefined': 'undefined' },
labelHighlightBold: { boolean: bool },
level: { number, 'undefined': 'undefined' },
Expand Down