Skip to content

Commit

Permalink
Fix jacomyal#136 add column for node and edge categories/types
Browse files Browse the repository at this point in the history
  • Loading branch information
sheymann committed Jun 26, 2015
1 parent 0771c2b commit 5b9690b
Show file tree
Hide file tree
Showing 3 changed files with 65 additions and 10 deletions.
10 changes: 8 additions & 2 deletions examples/plugin-exporters-xlsx.html
Expand Up @@ -82,7 +82,8 @@
anUndef: undefined,
anObject: {},
aFunction: function (argument) {}
}
},
categories: ['catA', 'catB']
});

for (i = N/2; i < N; i++)
Expand Down Expand Up @@ -122,7 +123,8 @@
anUndef: undefined,
anObject: {},
aFunction: function (argument) {}
}
},
type: 'catA'
}
});

Expand All @@ -137,7 +139,11 @@
// Get the graph:
s.toXLSX({
nodesAttributes: 'data',
nodesCategories: 'categories',
nodesCategoriesName: 'categories',
edgesAttributes: 'data.properties',
edgesCategories: 'data.type',
edgesCategoriesName: 'type',
filename: 'myGraph.xlsx'
});

Expand Down
12 changes: 12 additions & 0 deletions plugins/sigma.exporters.xlsx/README.md
Expand Up @@ -56,6 +56,18 @@ s.toXLSX({
* **edgesAttributes** (optional)
* The accessor to the dictionnary of edges attributes (e.g. "attributes" or "data.properties"). If provided, write the attributes in the spreadsheet.
* type: *string*
* **nodesCategories** (optional)
* The accessor to the node categories (if any). If provided, write the categories in the spreadsheet.
* type: *string*
* **edgesCategories** (optional)
* The accessor to the edge categories (if any). If provided, write the categories in the spreadsheet.
* type: *string*
* **nodesCategoriesName** (optional)
* The column name of the node categories (if `nodesCategories` is used).
* type: *string*
* **edgesCategoriesName** (optional)
* The column name of the edge categories (if `edgesCategories` is used).
* type: *string*
* **filename** (optional)
* The full filename for the file to download.
* type: *string*
Expand Down
53 changes: 45 additions & 8 deletions plugins/sigma.exporters.xlsx/sigma.exporters.xlsx.js
Expand Up @@ -45,13 +45,29 @@
if (x === null || x === undefined)
return '';

if (typeof x === 'string' || typeof x === 'number')
return x;

if (typeof x === 'function')
x = x.toString();
return x.toString();

return JSON.stringify(x);
}

if (typeof x !== 'number')
x = (typeof x === 'string') ? x : JSON.stringify(x);
function formatCategories(x) {
if (x === null || x === undefined)
return '';

if (typeof x === 'string' || typeof x === 'number')
return x;

if (Array.isArray(x))
return x.join(',');

if (typeof x === 'function')
return x.toString();

return x;
return JSON.stringify(x);
}

/**
Expand Down Expand Up @@ -114,12 +130,21 @@
index = {},
attributesArr = [],
attributes,
attributesPath,
attributesPath = params.nodesAttributes,
categoryPath = params.nodesCategories,
categoriesColName = params.nodesCategoriesName || 'categories',
o,
arr,
extraCol = 0,
rows = [];

attributesPath = (params.what === 'edges') ?
params.edgesAttributes : params.nodesAttributes;
if (params.what === 'edges') {
attributesPath = params.edgesAttributes;
categoryPath = params.edgesCategories;
categoriesColName = params.edgesCategoriesName || 'categories';
}

extraCol = (categoryPath && categoryPath.length) ? 1 : 0;

// Find all attributes keys to provide fixed row length to deal with
// missing attributes
Expand All @@ -133,6 +158,11 @@
attributesArr.push('target');
}

if (extraCol) {
cpt++;
attributesArr.push(categoriesColName);
}

for (var i = 0 ; i < data.length ; i++) {
o = data[i];
attributes = strToObjectRef(o, attributesPath) || {};
Expand All @@ -148,14 +178,21 @@
// Get attribute values
for (var i = 0 ; i < data.length ; i++) {
o = data[i];
var arr = [];
arr = [];
arr.length = cpt;

arr[0] = format(o.id);

if (params.what === 'edges') {
arr[1] = format(o.source);
arr[2] = format(o.target);

if (extraCol) {
arr[3] = formatCategories(strToObjectRef(o, categoryPath));
}
}
else if (extraCol) {
arr[1] = formatCategories(strToObjectRef(o, categoryPath));
}

attributes = strToObjectRef(o, attributesPath) || {};
Expand Down

0 comments on commit 5b9690b

Please sign in to comment.