Skip to content

Commit

Permalink
feat(TypeScript): rewrite Gephi parser into TypeScript (#39)
Browse files Browse the repository at this point in the history
  • Loading branch information
Thomaash authored Jul 31, 2019
1 parent 3a8f602 commit 35cf075
Show file tree
Hide file tree
Showing 16 changed files with 1,096 additions and 227 deletions.
5 changes: 3 additions & 2 deletions dist/types/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,14 @@ import * as data from 'vis-data';
export { data };
export { DataSet, DataView, Queue } from 'vis-data';
export * from './network/Network';
import * as gephiParser from './network/gephiParser';
export declare const network: {
Images: any;
dotparser: any;
gephiParser: any;
gephiParser: typeof gephiParser;
allOptions: any;
convertDot: any;
convertGephi: any;
convertGephi: typeof gephiParser.parseGephi;
};
import * as DOMutil from './DOMutil';
export { DOMutil };
Expand Down
2 changes: 1 addition & 1 deletion dist/types/index.d.ts.map

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

80 changes: 80 additions & 0 deletions dist/types/network/gephiParser.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
export declare type Id = number | string;
export interface ColorObject {
background: string;
border: string;
highlight: {
background: string;
border: string;
};
hover: {
background: string;
border: string;
};
}
export interface GephiData {
nodes: GephiNode[];
edges: GephiEdge[];
}
export interface GephiParseOptions {
fixed?: boolean;
inheritColor?: boolean;
parseColor?: boolean;
}
export interface GephiNode {
id: Id;
attributes?: {
title?: string;
};
color?: string;
label?: string;
size?: number;
title?: string;
x?: number;
y?: number;
}
export interface GephiEdge {
id: Id;
source: Id;
target: Id;
attributes?: {
title?: string;
};
color?: string;
label?: string;
type?: string;
}
export interface VisData {
nodes: VisNode[];
edges: VisEdge[];
}
export interface VisNode {
id: Id;
fixed: boolean;
color?: string | ColorObject;
label?: string;
size?: number;
title?: string;
x?: number;
y?: number;
attributes?: unknown;
}
export interface VisEdge {
id: Id;
from: Id;
to: Id;
arrows?: 'to';
color?: string;
label?: string;
title?: string;
attributes?: unknown;
}
/**
* Convert Gephi to Vis.
*
* @param gephiJSON - The parsed JSON data in Gephi format.
* @param optionsObj - Additional options.
*
* @returns The converted data ready to be used in Vis.
*/
export declare function parseGephi(gephiJSON: GephiData, optionsObj?: GephiParseOptions): VisData;
//# sourceMappingURL=gephiParser.d.ts.map
1 change: 1 addition & 0 deletions dist/types/network/gephiParser.d.ts.map

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

166 changes: 94 additions & 72 deletions dist/vis-network.esm.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
* A dynamic, browser-based visualization library.
*
* @version 5.1.1
* @date 2019-07-31T11:02:13Z
* @date 2019-07-31T17:55:24Z
*
* @copyright (c) 2011-2017 Almende B.V, http://almende.com
* @copyright (c) 2018-2019 visjs contributors, https://github.com/visjs
Expand Down Expand Up @@ -15818,15 +15818,15 @@ var dotparser$1 = /*#__PURE__*/Object.freeze({
DOTToGraph: DOTToGraph_1
});

/**
*
* @param {json} gephiJSON
* @param {obj} optionsObj
* @returns {{nodes: Array, edges: Array}}
/**
* Convert Gephi to Vis.
*
* @param gephiJSON - The parsed JSON data in Gephi format.
* @param optionsObj - Additional options.
*
* @returns The converted data ready to be used in Vis.
*/
function parseGephi(gephiJSON, optionsObj) {
var edges = [];
var nodes = [];
var options = {
edges: {
inheritColor: false
Expand All @@ -15837,93 +15837,115 @@ function parseGephi(gephiJSON, optionsObj) {
}
};

if (optionsObj !== undefined) {
if (optionsObj.fixed !== undefined) {
if (optionsObj != null) {
if (optionsObj.fixed != null) {
options.nodes.fixed = optionsObj.fixed;
}

if (optionsObj.parseColor !== undefined) {
if (optionsObj.parseColor != null) {
options.nodes.parseColor = optionsObj.parseColor;
}

if (optionsObj.inheritColor !== undefined) {
if (optionsObj.inheritColor != null) {
options.edges.inheritColor = optionsObj.inheritColor;
}
}

var gEdges = gephiJSON.edges;
var gNodes = gephiJSON.nodes;
var vEdges = gEdges.map(function (gEdge) {
var vEdge = {
from: gEdge.source,
id: gEdge.id,
to: gEdge.target
};

for (var i = 0; i < gEdges.length; i++) {
var edge = {};
var gEdge = gEdges[i];
edge['id'] = gEdge.id;
edge['from'] = gEdge.source;
edge['to'] = gEdge.target;
edge['attributes'] = gEdge.attributes;
edge['label'] = gEdge.label;
edge['title'] = gEdge.attributes !== undefined ? gEdge.attributes.title : undefined;
if (gEdge.attributes != null) {
vEdge.attributes = gEdge.attributes;
}

if (gEdge.label != null) {
vEdge.label = gEdge.label;
}

if (gEdge.attributes != null && gEdge.attributes.title != null) {
vEdge.title = gEdge.attributes.title;
}

if (gEdge['type'] === 'Directed') {
edge['arrows'] = 'to';
} // edge['value'] = gEdge.attributes !== undefined ? gEdge.attributes.Weight : undefined;
// edge['width'] = edge['value'] !== undefined ? undefined : edgegEdge.size;
if (gEdge.type === 'Directed') {
vEdge.arrows = 'to';
} // edge['value'] = gEdge.attributes != null ? gEdge.attributes.Weight : undefined;
// edge['width'] = edge['value'] != null ? undefined : edgegEdge.size;


if (gEdge.color && options.inheritColor === false) {
edge['color'] = gEdge.color;
if (gEdge.color && options.edges.inheritColor === false) {
vEdge.color = gEdge.color;
}

edges.push(edge);
}
return vEdge;
});
var vNodes = gephiJSON.nodes.map(function (gNode) {
var vNode = {
id: gNode.id,
fixed: options.nodes.fixed && gNode.x != null && gNode.y != null
};

for (var j = 0; j < gNodes.length; j++) {
var node = {};
var gNode = gNodes[j];
node['id'] = gNode.id;
node['attributes'] = gNode.attributes;
node['x'] = gNode.x;
node['y'] = gNode.y;
node['label'] = gNode.label;
node['title'] = gNode.attributes !== undefined ? gNode.attributes.title : gNode.title;
if (gNode.attributes != null) {
vNode.attributes = gNode.attributes;
}

if (options.nodes.parseColor === true) {
node['color'] = gNode.color;
} else {
node['color'] = gNode.color !== undefined ? {
background: gNode.color,
border: gNode.color,
highlight: {
background: gNode.color,
border: gNode.color
},
hover: {
background: gNode.color,
border: gNode.color
}
} : undefined;
if (gNode.label != null) {
vNode.label = gNode.label;
}

node['size'] = gNode.size;
node['fixed'] = options.nodes.fixed && gNode.x !== undefined && gNode.y !== undefined;
nodes.push(node);
}
if (gNode.size != null) {
vNode.size = gNode.size;
}

if (gNode.attributes != null && gNode.attributes.title != null) {
vNode.title = gNode.attributes.title;
}

if (gNode.title != null) {
vNode.title = gNode.title;
}

if (gNode.x != null) {
vNode.x = gNode.x;
}

if (gNode.y != null) {
vNode.y = gNode.y;
}

if (gNode.color != null) {
if (options.nodes.parseColor === true) {
vNode.color = gNode.color;
} else {
vNode.color = {
background: gNode.color,
border: gNode.color,
highlight: {
background: gNode.color,
border: gNode.color
},
hover: {
background: gNode.color,
border: gNode.color
}
};
}
}

return vNode;
});
return {
nodes: nodes,
edges: edges
nodes: vNodes,
edges: vEdges
};
}

var parseGephi_1 = parseGephi;
var gephiParser = {
parseGephi: parseGephi_1
};

var gephiParser$1 = /*#__PURE__*/Object.freeze({
'default': gephiParser,
__moduleExports: gephiParser,
parseGephi: parseGephi_1
var gephiParser = /*#__PURE__*/Object.freeze({
parseGephi: parseGephi
});

var keycharm = createCommonjsModule$2(function (module, exports) {
Expand Down Expand Up @@ -45638,7 +45660,7 @@ Network.prototype.setData = function (data) {
} else if (data && data.gephi) {
// parse DOT file
console.log('The gephi property has been deprecated. Please use the static convertGephi method to convert gephi into vis.network format and use the normal data format with nodes and edges. This converter is used like this: var data = vis.network.convertGephi(gephiJson);');
var gephiData = gephiParser.parseGephi(data.gephi);
var gephiData = parseGephi(data.gephi);
this.setData(gephiData);
return;
} else {
Expand Down Expand Up @@ -50827,10 +50849,10 @@ var moment$4 = /*#__PURE__*/Object.freeze({
var network = {
Images: Images,
dotparser: dotparser$1,
gephiParser: gephiParser$1,
gephiParser: gephiParser,
allOptions: allOptions$2,
convertDot: DOTToGraph_1,
convertGephi: parseGephi_1
convertGephi: parseGephi
}; // utils

var index$2 = /*#__PURE__*/Object.freeze({
Expand Down
2 changes: 1 addition & 1 deletion dist/vis-network.esm.js.map

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions dist/vis-network.esm.min.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion dist/vis-network.esm.min.js.map

Large diffs are not rendered by default.

Loading

0 comments on commit 35cf075

Please sign in to comment.