-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathanalysis.js
More file actions
411 lines (373 loc) · 12.6 KB
/
Copy pathanalysis.js
File metadata and controls
411 lines (373 loc) · 12.6 KB
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
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
/* ============================================================
AtlasNLP — analysis.js
Five finding charts via Chart.js, data from expanded CSV
(one row per dataset × content_country pair)
============================================================ */
const CHART_DEFAULTS = {
font: { family: 'system-ui, -apple-system, sans-serif', size: 12 },
colors: {
primary: '#2A7088',
ocean: '#0C3340',
coral: '#FF7A5C',
gold: '#F39C12',
kelp: '#1E8449',
slate: '#5A8894',
foam: '#DFF1EC',
mist: '#CAE9E1',
},
};
Chart.defaults.font.family = CHART_DEFAULTS.font.family;
Chart.defaults.font.size = CHART_DEFAULTS.font.size;
Chart.defaults.color = '#5A8894';
document.addEventListener('DOMContentLoaded', async () => {
const ids = ['chart1','chart2','chart3','chart4','chart5','chart6','chart7'];
ids.forEach(id => {
const el = document.getElementById(id + '-wrap');
if (el) showSpinner(el);
});
try {
const rows = await loadExpandedCols();
drawChart1(rows);
drawChart2(rows);
drawChart3(rows);
drawChart4(rows);
drawChart5(rows);
drawTaskPortfolio(rows);
drawLanguageConcentration(rows);
} catch(e) {
ids.forEach(id => {
const el = document.getElementById(id + '-wrap');
if (el) showError(el, e.message);
});
}
});
/* ---- Chart 1: Top 20 content countries by dataset count -- */
function drawChart1(rows) {
const wrap = document.getElementById('chart1-wrap');
wrap.innerHTML = '<canvas id="chart1"></canvas>';
const countMap = {};
rows.forEach(r => {
const c = safeStr(r['content_country']);
if (c) countMap[c] = (countMap[c] || 0) + 1;
});
const sorted = Object.entries(countMap).sort((a, b) => b[1] - a[1]).slice(0, 20);
const labels = sorted.map(([k]) => k);
const data = sorted.map(([, v]) => v);
const maxVal = Math.max(...data);
const colors = data.map(v => {
const t = v / maxVal;
const r = Math.round(27 + t * (11 - 27));
const g = Math.round(79 + t * (29 - 79));
const b = Math.round(114 + t * (46 - 114));
return `rgba(${r+30},${g+30},${b+30},0.85)`;
});
new Chart(document.getElementById('chart1'), {
type: 'bar',
data: { labels, datasets: [{ data, backgroundColor: colors, borderRadius: 4, borderSkipped: false }] },
options: {
indexAxis: 'y',
plugins: { legend: { display: false }, tooltip: { callbacks: {
label: ctx => ` ${ctx.raw.toLocaleString()} datasets`
}}},
scales: {
x: { grid: { color: 'rgba(0,0,0,.06)' }, ticks: { callback: v => v.toLocaleString() } },
y: { grid: { display: false } },
},
responsive: true,
maintainAspectRatio: false,
}
});
}
/* ---- Chart 2: Task distribution (top 15 tasks, deduped) -- */
function drawChart2(rows) {
const wrap = document.getElementById('chart2-wrap');
wrap.innerHTML = '<canvas id="chart2"></canvas>';
const taskMap = {};
const seen = new Set();
rows.forEach(r => {
const name = safeStr(r['Dataset name']);
if (!name || seen.has(name)) return;
seen.add(name);
const t = safeStr(r['Task Category']);
if (t) taskMap[t] = (taskMap[t] || 0) + 1;
});
const sorted = Object.entries(taskMap).sort((a, b) => b[1] - a[1]).slice(0, 15);
const labels = sorted.map(([k]) => truncate(k, 45));
const data = sorted.map(([, v]) => v);
new Chart(document.getElementById('chart2'), {
type: 'bar',
data: {
labels,
datasets: [{
data,
backgroundColor: CHART_DEFAULTS.colors.primary,
borderRadius: 4,
borderSkipped: false,
}]
},
options: {
indexAxis: 'y',
plugins: { legend: { display: false }, tooltip: { callbacks: {
label: ctx => ` ${ctx.raw.toLocaleString()} datasets`
}}},
scales: {
x: { grid: { color: 'rgba(0,0,0,.06)' }, ticks: { callback: v => v.toLocaleString() } },
y: { grid: { display: false }, ticks: { font: { size: 11 } } },
},
responsive: true,
maintainAspectRatio: false,
}
});
}
/* ---- Chart 3: Producer vs. content countries (top 15) ---- */
function drawChart3(rows) {
const wrap = document.getElementById('chart3-wrap');
wrap.innerHTML = '<canvas id="chart3"></canvas>';
const contentMap = {};
const producerMap = {};
rows.forEach(r => {
const c = safeStr(r['content_country']);
if (c) contentMap[c] = (contentMap[c] || 0) + 1;
parseListField(safeStr(r['producer_countries'])).forEach(p => {
producerMap[p] = (producerMap[p] || 0) + 1;
});
});
const topContent = Object.entries(contentMap).sort((a, b) => b[1] - a[1]).slice(0, 15).map(([k]) => k);
new Chart(document.getElementById('chart3'), {
type: 'bar',
data: {
labels: topContent,
datasets: [
{
label: 'Content country',
data: topContent.map(c => contentMap[c] || 0),
backgroundColor: CHART_DEFAULTS.colors.primary,
borderRadius: 3,
},
{
label: 'Producer country',
data: topContent.map(c => producerMap[c] || 0),
backgroundColor: CHART_DEFAULTS.colors.coral,
borderRadius: 3,
}
]
},
options: {
plugins: {
legend: { position: 'top' },
tooltip: { callbacks: { label: ctx => ` ${ctx.dataset.label}: ${ctx.raw.toLocaleString()}` } }
},
scales: {
x: { grid: { display: false } },
y: { grid: { color: 'rgba(0,0,0,.06)' }, ticks: { callback: v => v.toLocaleString() } },
},
responsive: true,
maintainAspectRatio: false,
}
});
}
/* ---- Chart 4: Language coverage type (deduped) ----------- */
function drawChart4(rows) {
const wrap = document.getElementById('chart4-wrap');
wrap.innerHTML = '<canvas id="chart4"></canvas>';
const covMap = {};
const seen = new Set();
rows.forEach(r => {
const name = safeStr(r['Dataset name']);
if (!name || seen.has(name)) return;
seen.add(name);
const lv = safeStr(r['Language coverage type']);
if (lv) covMap[lv] = (covMap[lv] || 0) + 1;
});
const labels = Object.keys(covMap).sort();
const data = labels.map(l => covMap[l]);
const total = data.reduce((a, b) => a + b, 0);
const palette = {
'Monolingual': CHART_DEFAULTS.colors.primary,
'Multilingual': CHART_DEFAULTS.colors.coral,
};
const colors = labels.map(l => palette[l] || CHART_DEFAULTS.colors.slate);
new Chart(document.getElementById('chart4'), {
type: 'doughnut',
data: {
labels,
datasets: [{
data,
backgroundColor: colors,
borderWidth: 2,
borderColor: '#fff',
hoverOffset: 8,
}]
},
options: {
plugins: {
legend: { position: 'right' },
tooltip: { callbacks: {
label: ctx => ` ${ctx.label}: ${ctx.raw.toLocaleString()} (${((ctx.raw / total) * 100).toFixed(1)}%)`
}}
},
responsive: true,
maintainAspectRatio: false,
}
});
}
/* ---- Chart 5: Attribution method breakdown (deduped) ----- */
function drawChart5(rows) {
const wrap = document.getElementById('chart5-wrap');
wrap.innerHTML = '<canvas id="chart5"></canvas>';
const methodMap = {};
const seen = new Set();
rows.forEach(r => {
const name = safeStr(r['Dataset name']);
if (!name || seen.has(name)) return;
seen.add(name);
const m = safeStr(r['Country Attribution Method']) || 'Not stated';
methodMap[m] = (methodMap[m] || 0) + 1;
});
const sorted = Object.entries(methodMap).sort((a, b) => b[1] - a[1]);
const labels = sorted.map(([k]) => truncate(k, 50));
const data = sorted.map(([, v]) => v);
const total = data.reduce((a, b) => a + b, 0);
const colors = [
CHART_DEFAULTS.colors.primary,
CHART_DEFAULTS.colors.kelp,
CHART_DEFAULTS.colors.gold,
CHART_DEFAULTS.colors.coral,
CHART_DEFAULTS.colors.slate,
'#8E44AD',
'#16A085',
];
new Chart(document.getElementById('chart5'), {
type: 'bar',
data: {
labels,
datasets: [{
data,
backgroundColor: data.map((_, i) => colors[i % colors.length]),
borderRadius: 4,
borderSkipped: false,
}]
},
options: {
indexAxis: 'y',
plugins: {
legend: { display: false },
tooltip: { callbacks: {
label: ctx => ` ${ctx.raw.toLocaleString()} (${((ctx.raw / total) * 100).toFixed(1)}%)`
}}
},
scales: {
x: { grid: { color: 'rgba(0,0,0,.06)' }, ticks: { callback: v => v.toLocaleString() } },
y: { grid: { display: false }, ticks: { font: { size: 11 } } },
},
responsive: true,
maintainAspectRatio: false,
}
});
}
/* ---- Chart 6: Task breadth by country (top 20) ----------- */
function drawTaskPortfolio(rows) {
const wrap = document.getElementById('chart6-wrap');
if (!wrap) return;
wrap.innerHTML = '';
wrap.style.height = 'auto';
const countryData = {};
rows.forEach(r => {
const c = safeStr(r['content_country']);
const task = safeStr(r['Task Category']) || 'Unknown';
if (!c) return;
if (!countryData[c]) countryData[c] = { total: 0, tasks: {} };
countryData[c].total++;
countryData[c].tasks[task] = (countryData[c].tasks[task] || 0) + 1;
});
const top20 = Object.entries(countryData)
.sort((a, b) => b[1].total - a[1].total)
.slice(0, 20)
.map(([country, d]) => {
const taskEntries = Object.entries(d.tasks).sort((a, b) => b[1] - a[1]);
const top3Sum = taskEntries.slice(0, 3).reduce((s, [, v]) => s + v, 0);
const top3Names = taskEntries.slice(0, 3).map(([k]) => truncate(k, 22)).join(', ');
return { country, total: d.total, breadth: taskEntries.length, top3Share: ((top3Sum / d.total) * 100).toFixed(0) + '%', top3Names };
});
wrap.innerHTML = `
<div class="table-wrap" style="max-height:440px;overflow-y:auto;">
<table>
<thead><tr><th>Country</th><th>Datasets</th><th>Task breadth</th><th>Top-3 share</th><th>Top 3 tasks</th></tr></thead>
<tbody>
${top20.map(d => `<tr>
<td>${d.country}</td>
<td>${d.total.toLocaleString()}</td>
<td>${d.breadth}</td>
<td>${d.top3Share}</td>
<td style="font-size:12px;color:var(--muted)">${d.top3Names}</td>
</tr>`).join('')}
</tbody>
</table>
</div>`;
}
/* ---- Chart 7: Language concentration (top 12 languages) -- */
function drawLanguageConcentration(rows) {
const wrap = document.getElementById('chart7-wrap');
if (!wrap) return;
wrap.innerHTML = '<canvas id="chart7"></canvas>';
const langCountry = {};
const langTotal = {};
rows.forEach(r => {
const country = safeStr(r['content_country']);
if (!country) return;
parseListField(safeStr(r['languages_in_dataset']), /;/).forEach(lang => {
if (!lang) return;
if (!langCountry[lang]) langCountry[lang] = {};
langTotal[lang] = (langTotal[lang] || 0) + 1;
langCountry[lang][country] = (langCountry[lang][country] || 0) + 1;
});
});
const top12 = Object.entries(langTotal).sort((a, b) => b[1] - a[1]).slice(0, 12).map(([k]) => k);
const allCounts = {};
top12.forEach(lang => {
Object.entries(langCountry[lang] || {}).forEach(([c, v]) => { allCounts[c] = (allCounts[c] || 0) + v; });
});
const top5Countries = Object.entries(allCounts).sort((a, b) => b[1] - a[1]).slice(0, 5).map(([k]) => k);
const palette = ['#0C3340','#2A7088','#5AB4B8','#FF7A5C','#CAE9E1'];
const datasets = [
...top5Countries.map((country, i) => ({
label: country,
data: top12.map(lang => {
const t = langTotal[lang];
return t > 0 ? Math.round(((langCountry[lang][country] || 0) / t) * 100) : 0;
}),
backgroundColor: palette[i],
borderRadius: 2,
borderSkipped: false,
})),
{
label: 'Others',
data: top12.map(lang => {
const t = langTotal[lang];
if (!t) return 0;
const top5Sum = top5Countries.reduce((s, c) => s + (langCountry[lang][c] || 0), 0);
return Math.round(((t - top5Sum) / t) * 100);
}),
backgroundColor: '#EAECEE',
borderRadius: 2,
borderSkipped: false,
}
];
new Chart(document.getElementById('chart7'), {
type: 'bar',
data: { labels: top12, datasets },
options: {
indexAxis: 'y',
plugins: {
legend: { position: 'bottom' },
tooltip: { callbacks: { label: ctx => ' ' + ctx.dataset.label + ': ' + ctx.raw + '%' } }
},
scales: {
x: { stacked: true, max: 100, grid: { color: 'rgba(0,0,0,.06)' }, ticks: { callback: v => v + '%' } },
y: { stacked: true, grid: { display: false } },
},
responsive: true,
maintainAspectRatio: false,
}
});
}