-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscripts2.js
148 lines (143 loc) · 4.72 KB
/
scripts2.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
// Slugify a string
// https://lucidar.me/en/web-dev/how-to-slugify-a-string-in-javascript/
function slugify(str) {
str = str.replace(/^\s+|\s+$/g, '');
// Make the string lowercase
str = str.toLowerCase();
// Remove accents, swap ñ for n, etc
let from = "ÁÄÂÀÃÅČÇĆĎÉĚËÈÊẼĔȆÍÌÎÏŇÑÓÖÒÔÕØŘŔŠŤÚŮÜÙÛÝŸŽáäâàãåčçćďéěëèêẽĕȇíìîïňñóöòôõøðřŕšťúůüùûýÿžþÞĐđßÆa·/_,:;";
let to = "AAAAAACCCDEEEEEEEEIIIINNOOOOOORRSTUUUUUYYZaaaaaacccdeeeeeeeeiiiinnooooooorrstuuuuuyyzbBDdBAa------";
for (let i=0, l=from.length ; i<l ; i++) {
str = str.replace(new RegExp(from.charAt(i), 'g'), to.charAt(i));
}
// Remove invalid chars
str = str.replace(/[^a-z0-9 -]/g, '')
// Collapse whitespace and replace by -
.replace(/\s+/g, '-')
// Collapse dashes
.replace(/-+/g, '-');
return str;
}
function buildChart(e) {
const csv = document.getElementById('chart-csv').value;
const rows = csv.split('\n');
const headers = rows[0].split(',');
const title = document.getElementById('chart-title').value;
const description = document.getElementById('chart-description').value;
let caption = '';
let captionId = '';
let tableId = '';
if (title) {
tableId = slugify(title);
captionId = 'caption-' + tableId;
} else if (description) {
tableId = slugify(description);
captionId = 'caption-' + tableId;
}
let chartStyle = '';
let numericData = []
let pos = 0;
let neg = 0;
let min = 0;
let max = 0;
let minChars = 0;
let maxChars = 0;
for (let row = 1; row < rows.length; row++) {
let currentRow = rows[row].split(',');
for (let col = 1; col < currentRow.length; col++) {
let amount = Number(currentRow[col]);
if (typeof(amount) === 'number') {
numericData.push(amount);
if (amount >= 0) {
pos += 1;
} else {
neg += 1;
}
}
}
}
numericData.sort(function (a, b) { return a - b; });
if (pos >= 0 && neg === 0) {
chartStyle = ' sb-allpos';
min = 0;
max = numericData[numericData.length - 1];
minChars = 0;
maxChars = String(max).length + 1;
} else if (pos === 0 && neg >= 0) {
chartStyle = ' sb-allneg';
min = numericData[0];
max = 0;
minChars = String(min).length + 1;
maxChars = 0;
} else if (pos >= 0 && neg >= 0) {
chartStyle = ' sb-posneg';
min = numericData[0];
max = numericData[numericData.length - 1];
minChars = String(min).length + 1;
maxChars = String(max).length + 1;
}
console.log('chartStyle: ' + chartStyle);
console.log('numericData: ' + numericData);
console.log('pos: ' + pos);
console.log('neg: ' + neg);
console.log('min: ' + min);
console.log('max: ' + max);
console.log('minChars: ' + minChars);
console.log('maxChars: ' + maxChars);
let tableMarkup = '<table role="table"';
if (tableId) {
tableMarkup += ' id="' + tableId + '"';
}
if (captionId) {
tableMarkup += ' aria-describedby="' + captionId + '"'; // Add slugified title or description
caption = '<caption id="' + captionId + '" class="sb-caption">'; // Add slugified title or description
if (title) {
caption += '<p class="sb-title"><strong>' + title + '</strong></p>';
}
if (description) {
caption += '<p class="sb-description">' + description + '</p>';
}
caption += '</caption>';
}
tableMarkup += ' class="simplebars' + chartStyle + '" style="--abs-min:' + Math.abs(min) + ';--abs-max:' + max + ';--min-chars:' + minChars + 'ch;--max-chars:' + maxChars + 'ch;">';
// Add colors
tableMarkup += caption;
if (headers) {
tableMarkup += '<thead role="rowgroup" class="sb-legend"><tr role="row" class="sb-legend-items">';
for (let header = 0; header < headers.length; header++) {
tableMarkup += '<th role="columnheader" class="sb-legend-item">' + headers[header] + '</th>';
}
tableMarkup += '</tr></thead>';
}
if (rows) {
tableMarkup += '<tbody role="rowgroup" class="sb-data">';
for (let row = 1; row < rows.length; row++) {
let currentRow = rows[row].split(',');
tableMarkup += '<tr role="row" class="sb-datum">';
for (let col = 0; col < currentRow.length; col++) {
let cellType = 'sb-amount';
let amountType = '';
let style = ''
if (col === 0) {
cellType = 'sb-label';
} else {
style = ' style="--abs-val:' + Math.abs(currentRow[col]) + '"';
}
if (col > 0) {
if (currentRow[col] >= 0) {
amountType = ' sb-pos';
} else {
amountType = ' sb-neg';
}
}
tableMarkup += '<td role="cell" class="' + cellType + amountType + '"' + style + '>' + currentRow[col] + '</td>';
}
tableMarkup += '</tr>';
}
tableMarkup += '</tbody>';
}
tableMarkup += '</table>';
document.getElementById('output').innerHTML = tableMarkup;
e.preventDefault();
}
document.getElementById('generateEmbed').addEventListener('click', buildChart);