From 69da702d08e187321036e96efabc45fccf89fa50 Mon Sep 17 00:00:00 2001
From: Max <m.giller.dev@gmail.com>
Date: Fri, 30 Jul 2021 21:27:40 +0200
Subject: [PATCH] Added simulation data extraction to clean data

---
 editor/js/graph.js | 26 +++++++++++++++++---------
 1 file changed, 17 insertions(+), 9 deletions(-)

diff --git a/editor/js/graph.js b/editor/js/graph.js
index 1eab6fa..7d47f38 100644
--- a/editor/js/graph.js
+++ b/editor/js/graph.js
@@ -17,6 +17,8 @@ const IMAGE_SRC = PLUGIN_PATH + "datasets/images/";
 
 const LINK_PARAMS = [LINK_TYPE];
 const NODE_PARAMS = [NODE_ID, NODE_LABEL, NODE_IMAGE, NODE_DESCRIPTION];
+const LINK_SIM_PARAMS = ["index"];
+const NODE_SIM_PARAMS = ["index", "x", "y", "vx", "vy", "fx", "fy"]; // Based on https://github.com/d3/d3-force#simulation_nodes
 
 const JSON_CONFIG = PLUGIN_PATH + "datasets/aud1.json";
 
@@ -151,7 +153,7 @@ class Graph extends ManagedData {
         );
     }
 
-    getCleanData(data = undefined, positions = false) {
+    getCleanData(data = undefined, simulationParameters = false) {
         if (data === undefined) {
             data = this.data;
         }
@@ -161,33 +163,33 @@ class Graph extends ManagedData {
         cleanData[GRAPH_NODES] = [];
 
         data[GRAPH_LINKS].forEach((link) =>
-            cleanData[GRAPH_LINKS].push(this.getCleanLink(link))
+            cleanData[GRAPH_LINKS].push(this.getCleanLink(link, simulationParameters))
         );
 
         data[GRAPH_NODES].forEach((node) =>
-            cleanData[GRAPH_NODES].push(this.getCleanNode(node, positions))
+            cleanData[GRAPH_NODES].push(this.getCleanNode(node, simulationParameters))
         );
 
-        console.log(cleanData);
         return cleanData;
     }
 
-    getCleanNode(node, positions) {
+    getCleanNode(node, simulationParameters) {
         var cleanNode = {};
 
         NODE_PARAMS.forEach((param) => {
             cleanNode[param] = node[param];
         });
 
-        if (positions) {
-            cleanNode.fx = node.fx;
-            cleanNode.fy = node.fy;
+        if (simulationParameters) {
+            NODE_SIM_PARAMS.forEach((param) => {
+                cleanNode[param] = node[param];
+            });
         }
 
         return cleanNode;
     }
 
-    getCleanLink(link) {
+    getCleanLink(link, simulationParameters) {
         var cleanLink = {};
 
         // Source and target nodes
@@ -200,6 +202,12 @@ class Graph extends ManagedData {
             cleanLink[param] = link[param];
         });
 
+        if (simulationParameters) {
+            LINK_SIM_PARAMS.forEach((param) => {
+                cleanLink[param] = link[param];
+            });
+        }
+
         return cleanLink;
     }
 
-- 
GitLab