-
Notifications
You must be signed in to change notification settings - Fork 0
/
timelineGenerator.js
243 lines (210 loc) · 7.17 KB
/
timelineGenerator.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
import fs from 'fs';
import path from 'path';
import * as d3 from 'd3';
import { JSDOM } from 'jsdom';
const outputDir = 'gemRepository';
const outputPath = `${outputDir}/integratedModelsTimeline.svg`;
const d3Curve = d3.line().curve(d3.curveNatural);
const WIDTH = 1500;
let GEMS, HEIGHT, GEM_POSITIONS, BRANCH_POINTS, TIME_SCALE;
const createTimelineChart = async (integratedModelsPath) => {
GEMS = setGems(integratedModelsPath);
HEIGHT = getHeight();
GEM_POSITIONS = getGemPositions();
BRANCH_POINTS = getBranchPoints();
TIME_SCALE = createTimeScale();
const fakeDom = new JSDOM('<!DOCTYPE html><html><body></body></html>');
const body = (await d3).select(fakeDom.window.document).select('body');
// + 1 is needed or else the bottom axis appears hidden
let svg = body
.append('svg')
.attr('width', WIDTH)
.attr('height', HEIGHT + 1);
svg = addBranchPoints(svg);
svg = addGemVersions(svg);
svg = addTimeAxis(svg);
svg = addGemsAxis(svg);
if (!fs.existsSync(`${outputDir}`)) {
fs.mkdirSync(`${outputDir}`);
}
fs.writeFileSync(outputPath, body.html());
};
const setGems = (integratedModelsPath) => {
const gems = [];
const intputDirFiles = fs.readdirSync(integratedModelsPath);
for (let i = 0; i < intputDirFiles.length; i++) {
const filePath = path.join(integratedModelsPath, intputDirFiles[i]);
const stat = fs.lstatSync(filePath);
if (stat.isDirectory() && intputDirFiles[i][0] != '.') {
const rawJson = fs.readFileSync(`${filePath}/gemRepository.json`, 'utf8');
gems.push(JSON.parse(rawJson));
}
}
return gems.map((versions) =>
versions.filter((v) => v.id.split('.')[2] === '0'),
);
};
const getHeight = () => GEMS.length * 50 + 100;
const getModelFromId = (id) => id.match(/\w+-\w+/)[0];
const getGemPositions = () =>
GEMS.reduce(
(positions, [v], i) => ({
...positions,
[getModelFromId(v.id)]: HEIGHT - (i + 1) * 50,
}),
{},
);
const getBranchPoints = () => {
BRANCH_POINTS = {};
Array.from(GEMS).forEach(([v]) => {
if (v.externalParentId.length > 0) {
BRANCH_POINTS[v.id] = v.externalParentId;
}
});
return BRANCH_POINTS;
};
const createTimeScale = () => {
const allVersionDates = GEMS.flat().map((v) => new Date(v.releaseDate));
const earliestDate = allVersionDates.sort((a, b) => a - b)[0];
const timePadding = 60 * 60 * 24 * 30 * 3 * 1000; // 3 months
const startDate = new Date(earliestDate - 2 * timePadding);
const currentDate = new Date(Date.now() + timePadding);
return d3.scaleTime().domain([startDate, currentDate]).range([0, WIDTH]);
};
const addTimeAxis = (svg) => {
const xAxis = d3.axisTop().scale(TIME_SCALE);
svg
.append('g')
.attr('transform', `translate(0, ${HEIGHT})`)
.style('font-size', '14px')
.style('font-weight', 'bold')
.call(xAxis);
return svg;
};
const addGemsAxis = (svg) => {
const kvFlippedGemPositions = Object.fromEntries(
Object.entries(GEM_POSITIONS).map(([k, v]) => [v, k]),
);
const yScaleLeft = d3.scaleLinear().domain([0, HEIGHT]).range([HEIGHT, 0]);
const yAxisLeft = d3
.axisRight(yScaleLeft)
.tickValues(Object.keys(kvFlippedGemPositions))
.tickFormat((x) => kvFlippedGemPositions[x]);
svg
.append('g')
.style('font-size', '14px')
.style('font-weight', 'bold')
.call(yAxisLeft);
// The following commented out code adds a vertical axis on the right side of
// the chart. It does not seem like an improvment to include it at the moment
// (2022-05-05). The code is kept for convenience in case the opinion would
// change in the future.
//
// const yScaleRight = d3.scaleLinear().domain([0, HEIGHT]).range([HEIGHT, 0]);
// const yAxisRight = d3
// .axisLeft(yScaleRight)
// .tickValues(Object.keys(kvFlippedGemPositions))
// .tickFormat(x => kvFlippedGemPositions[x]);
// svg.append('g').attr('transform', `translate(${WIDTH}, 0)`).call(yAxisRight);
return svg;
};
const addBranchPoints = (svg) => {
const allGemVersions = GEMS.flat();
Object.entries(BRANCH_POINTS).forEach(([childId, parentIds]) => {
const childVersion = allGemVersions.find((v) => v.id === childId);
const childModel = getModelFromId(childVersion.id);
parentIds.forEach(({ id, citLink }) => {
const parentVersion = allGemVersions.find((v) => v.id === id);
const parentModel = getModelFromId(parentVersion.id);
const curve = [
[
TIME_SCALE(new Date(parentVersion.releaseDate)),
HEIGHT - GEM_POSITIONS[parentModel],
],
[
TIME_SCALE(new Date(parentVersion.releaseDate)) +
(TIME_SCALE(new Date(childVersion.releaseDate)) -
TIME_SCALE(new Date(parentVersion.releaseDate))) /
5,
HEIGHT -
GEM_POSITIONS[childModel] -
(HEIGHT -
GEM_POSITIONS[childModel] -
(HEIGHT - GEM_POSITIONS[parentModel])) /
5,
],
[
TIME_SCALE(new Date(childVersion.releaseDate)),
HEIGHT - GEM_POSITIONS[childModel],
],
];
svg
.append('path')
.attr('d', d3Curve(curve))
.attr('stroke', 'lightgray')
.attr('fill', 'none');
});
});
return svg;
};
const addGemVersions = (svg) => {
GEMS.forEach((versions) => {
const model = getModelFromId(versions[0].id);
const y = HEIGHT - GEM_POSITIONS[model];
// add connecting lines
svg
.append('g')
.selectAll('line')
.data(versions.slice(0, -1).map((v) => new Date(v.releaseDate)))
.enter()
.append('line')
.style('stroke', 'lightgray')
.attr('x1', (d) => TIME_SCALE(d))
.attr('y1', y)
.attr('x2', (_d, i) => {
const nextD = new Date(versions[i + 1].releaseDate);
return TIME_SCALE(nextD);
})
.attr('y2', y);
// add circles
svg
.append('g')
.selectAll('circle')
.data(versions.map((v) => new Date(v.releaseDate)))
.join('circle')
.classed('circle', true)
.attr('r', (_d, i) => {
const idParts = versions[i].id.split('.');
const isMajor = idParts[1] === '0' && idParts[2] === '0';
return isMajor ? 24 : 18;
})
.attr('cy', y)
.attr('cx', (d) => TIME_SCALE(d))
.attr('data-model', (_d, i) => getModelFromId(versions[i].id))
.attr('data-version', (_d, i) => versions[i].id.split('-')[2])
.attr('data-release-date', (_d, i) => versions[i].releaseDate)
.attr('data-release-link', (_d, i) => versions[i].releaseLink)
.attr('data-pmid', (_d, i) => versions[i].PMID)
.attr('data-external-parent-ids', (_d, i) =>
JSON.stringify(versions[i].externalParentId),
);
svg
.append('g')
.selectAll('text')
.data(versions.map((v) => new Date(v.releaseDate)))
.join('text')
.classed('label', true)
.style('fill', 'white')
.style('font-size', '10px')
.style('font-weight', 'bold')
.attr('y', y + 3)
.attr(
'x',
(d, i) => TIME_SCALE(d) - (versions[i].id.split('.')[1] > 9 ? 5 : 2),
)
.attr('transform', 'translate(-10, 0)')
.text((_d, i) => versions[i].id.split('-')[2]);
});
return svg;
};
export { createTimelineChart };