From 39c358bc8555c9720bbfcf1ca13b45c3f0038a69 Mon Sep 17 00:00:00 2001 From: sibeshkar Date: Sun, 22 Mar 2020 02:13:16 +0530 Subject: [PATCH] travel filter --- util/filters/state.js | 9 ++-- util/filters/travel.js | 113 +++++++++++++++++++++++++++++------------ 2 files changed, 84 insertions(+), 38 deletions(-) diff --git a/util/filters/state.js b/util/filters/state.js index 8044abe..0205958 100644 --- a/util/filters/state.js +++ b/util/filters/state.js @@ -58,7 +58,7 @@ export const removeStates = (graph, patients) => { image: state_node, } let index = _.findIndex(dotProp.get(graph, 'nodes'), function(o) { - return o.id == key + return o.id == node.id }) if (index !== -1) { @@ -80,12 +80,9 @@ export const removeStates = (graph, patients) => { color: { opacity: '0.3' }, } let edgeIndex = _.findIndex(graph.edges, function(o) { - return ( - o.to == patients[patientId].patientId && - o.from === hash(patients[patientId].state) - ) + return o.to == edge.to && o.from === edge.from }) - console.log('edge', edgeIndex) + graph = dotProp.delete(graph, `edges.${edgeIndex}`) } diff --git a/util/filters/travel.js b/util/filters/travel.js index 179529f..46816ca 100644 --- a/util/filters/travel.js +++ b/util/filters/travel.js @@ -5,52 +5,50 @@ import dotProp from 'dot-prop-immutable' import { plane_node } from '../../images' export const addTravel = (graph, patients) => { - let travelHistories = {} - let locations = [] + let locations = {} for (let patientId in patients) { if (patients[patientId].travel !== null) { - if (patients[patientId].travel.length !== 0) { - travelHistories[hash(patients[patientId].travel)] = - patients[patientId].travel - } + patients[patientId].travel.forEach(loc => { + if (!locations[hash(loc)]) { + locations[hash(loc)] = loc + } + }) } } - for (let key in travelHistories) { - travelHistories[key].forEach(location => { - if (!locations.includes(location)) { - locations.push(location) - } - }) - } + console.log(locations) - locations.forEach(location => { + for (let loc in locations) { let node = { - id: hash(location), - label: location, - size: 40, + id: loc, + label: locations[loc], + size: 30, shape: 'image', image: plane_node, } graph = dotProp.set(graph, 'nodes', list => [...list, node]) - }) + } + // Add edges from patient to location for (let patientId in patients) { - for (let location in patients[patientId].travel) { - let edge = { - from: hash(location), - to: patientId, - length: 250, - dashes: true, - arrows: { - to: { - enabled: false, + if (patients[patientId].travel !== null && patients[patientId].travel[0]) { + patients[patientId].travel.forEach(loc => { + let edge = { + from: hash(loc), + to: patients[patientId].patientId, + length: 500, + dashes: true, + arrows: { + to: { + enabled: false, + }, }, - }, - color: { opacity: '0.3' }, - } - graph = dotProp.set(graph, 'edges', list => [...list, edge]) + color: { opacity: '0.2' }, + } + + graph = dotProp.set(graph, 'edges', list => [...list, edge]) + }) } } @@ -58,5 +56,56 @@ export const addTravel = (graph, patients) => { } export const removeTravel = (graph, patients) => { - let travelHistories = {} + let locations = {} + + for (let patientId in patients) { + if (patients[patientId].travel !== null) { + patients[patientId].travel.forEach(loc => { + if (!locations[hash(loc)]) { + locations[hash(loc)] = loc + } + }) + } + } + + for (let loc in locations) { + let node = { + id: loc, + label: locations[loc], + size: 30, + shape: 'image', + image: plane_node, + } + let index = _.findIndex(dotProp.get(graph, 'nodes'), function(o) { + return o.id == node.id + }) + if (index !== -1) { + graph = dotProp.delete(graph, `nodes.${index}`) + } + } + for (let patientId in patients) { + if (patients[patientId].travel !== null && patients[patientId].travel[0]) { + patients[patientId].travel.forEach(loc => { + let edge = { + from: hash(loc), + to: patients[patientId].patientId, + length: 500, + dashes: true, + arrows: { + to: { + enabled: false, + }, + }, + color: { opacity: '0.2' }, + } + + let edgeIndex = _.findIndex(graph.edges, function(o) { + return o.to == edge.to && o.from === edge.from + }) + + graph = dotProp.delete(graph, `edges.${edgeIndex}`) + }) + } + } + return graph }