Skip to content

Commit

Permalink
added column mapping in the config (#45)
Browse files Browse the repository at this point in the history
  • Loading branch information
bengarvey authored Nov 3, 2024
1 parent 84ad124 commit 786a993
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 18 deletions.
8 changes: 7 additions & 1 deletion config/config.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,11 @@
"debug": false,
"menuDefaultOpen": true,
"showDead": true,
"timeStep": "year"
"timeStep": "month",
"column_mappings": {
"description": "name",
"createdAt": "birthDate",
"deletedAt": "deathDate",
"category": "lastName"
}
}
48 changes: 31 additions & 17 deletions js/lineage.js
Original file line number Diff line number Diff line change
Expand Up @@ -208,7 +208,7 @@ function Lineage() {
.style('display', 'block')
.style('top', m[1] - 20)
.style('left', m[0] + 20);
d3.select('#name').html(`${d.name} ${d.lastName}<br><span class='birthYear'>${d.birthDate.substring(0, 4)}</span>`);
d3.select('#name').html(`${d.description} ${d.category} <br><span class='birthYear'>${d.createdAt.substring(0, 4)}</span>`);
}

function loop() {
Expand Down Expand Up @@ -266,16 +266,16 @@ function Lineage() {
}

function addRemoveNode(n) {
const birthDate = new Date(n.birthDate);
if (nodes.indexOf(n) === -1 && birthDate <= currentTime) {
const createdAt = new Date(n.createdAt);
if (nodes.indexOf(n) === -1 && createdAt <= currentTime) {
nodes.push(n);
} else if (nodes.indexOf(n) !== -1 && birthDate > currentTime) {
} else if (nodes.indexOf(n) !== -1 && createdAt > currentTime) {
nodes.splice(nodes.indexOf(n), 1);
}

if (!showDead && n.deathDate !== null) {
const deathDate = new Date(n.deathDate);
if (nodes.indexOf(n) !== -1 && deathDate < currentTime) {
if (!showDead && n.deletedAt !== null) {
const deletedAt = new Date(n.deletedAt);
if (nodes.indexOf(n) !== -1 && deletedAt < currentTime) {
nodes.splice(nodes.indexOf(n), 1);
}
}
Expand All @@ -301,10 +301,10 @@ function Lineage() {
const rowCount = 11;
const colCount = 13;
clusterNodes.forEach((n, i) => {
if (clusters[n.lastName] == null) {
if (clusters[n.category] == null) {
const x = Math.round(i / colCount) + Math.round(i / colCount) * CLUSTER_COL_SPACING - width;
const y = (i % rowCount) + Math.round(i % rowCount) * CLUSTER_ROW_SPACING - height;
clusters[n.lastName] = { x, y };
clusters[n.category] = { x, y };
}
});
return clusters;
Expand All @@ -316,8 +316,8 @@ function Lineage() {
}

return nodes.reduce((earliest, node) => {
const birthDate = new Date(node.birthDate);
return birthDate < earliest ? birthDate : earliest;
const createdAt = new Date(node.createdAt);
return createdAt < earliest ? createdAt : earliest;
}, new Date());
}

Expand All @@ -328,7 +328,21 @@ function Lineage() {
}
}

function mapColumns(dataOb) {
const mappings = config.column_mappings;
dataOb.nodes.forEach((node, index) => {
Object.keys(mappings).forEach((key) => {
const value = mappings[key];
dataOb.nodes[index][key] = node[value];
delete dataOb.nodes[index][value];
});
});

return dataOb;
}

function prepareData(dataOb, filterString) {
dataOb = mapColumns(dataOb);
let filterItems = filterString.split(' ');
filterItems = filterItems.filter((i) => i.length > 0);
for (let i = 0; i < dataOb.nodes.length; i += 1) {
Expand Down Expand Up @@ -403,14 +417,14 @@ function Lineage() {
// const k = 0.1 * simulation.alpha;
users.forEach((o) => {
const u = o[0];
u.y += (clusters[u.lastName].y - u.y) * 0.08;
u.x += (clusters[u.lastName].x - u.x) * 0.08;
u.y += (clusters[u.category].y - u.y) * 0.08;
u.x += (clusters[u.category].x - u.x) * 0.08;
});

users.forEach((user) => {
context.beginPath();
user.forEach(drawNode);
context.fillStyle = color(user[0].lastName);
context.fillStyle = color(user[0].category);
context.fill();
});

Expand All @@ -429,7 +443,7 @@ function Lineage() {
users.forEach((user) => {
context.beginPath();
user.forEach(drawNode);
context.fillStyle = color(user[0].lastName);
context.fillStyle = color(user[0].category);
context.fill();
});

Expand All @@ -445,14 +459,14 @@ function Lineage() {

users.forEach((user) => {
const d = user[0];
const scale = ((d.birthDate.substring(0, 4) - 1900) / (2014 - 1900) - 0.5);
const scale = ((d.createdAt.substring(0, 4) - 1900) / (2014 - 1900) - 0.5);
d.x += (width * scale - d.x) * TIMELINE_SPEED;
});

users.forEach((user) => {
context.beginPath();
user.forEach(drawNode);
context.fillStyle = color(user[0].lastName);
context.fillStyle = color(user[0].category);
context.fill();
});

Expand Down

0 comments on commit 786a993

Please sign in to comment.