This repository has been archived by the owner on Feb 21, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
/
utils.js
170 lines (150 loc) · 3.53 KB
/
utils.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
import { format } from 'date-fns';
import { pt } from 'date-fns/locale';
import fetchNode from 'node-fetch';
import { RESIZE_TRESHOLD } from './constants';
export const formatNumber = (number, isDate = true) => {
let numberFormatter = new Intl.NumberFormat('pt-PT', {
minimumIntegerDigits: isDate ? 2 : 1,
});
return numberFormatter.format(number).replace(/,/gm, ' ');
};
import { populacao } from './data/generic.json';
//https://stackoverflow.com/questions/5623838/rgb-to-hex-and-hex-to-rgb
export function hexToRgb(hex) {
var result = /^#?([a-f\d]{2})([a-f\d]{2})([a-f\d]{2})$/i.exec(hex);
return result
? {
r: parseInt(result[1], 16),
g: parseInt(result[2], 16),
b: parseInt(result[3], 16),
}
: null;
}
export function dateWithoutTimezone(unix) {
const dt = new Date(unix);
return new Date(dt.valueOf() + dt.getTimezoneOffset() * 60 * 1000);
}
export function downloadPNG(canvasElement, graphName) {
var link = document.createElement('a');
link.download = 'filename.png';
link.href = canvasElement.toDataURL();
link.click();
}
export function perHundred(total, populacaoTotal = populacao.valor) {
return (total / populacaoTotal) * 100;
}
export function isSameDay(dateA, dateB) {
let parsedDateA = new Date(dateA);
let parsedDateB = new Date(dateB);
let isSame = parsedDateA.toLocaleDateString() === parsedDateB.toLocaleDateString();
return isSame;
}
export function fetchWithLocalCache(url, options) {
let useCache = true;
let [path, cacheBuster] = url.split('?');
let items = JSON.parse(JSON.stringify(localStorage));
for (var k in items) {
let [lsPath, lsCacheBuster] = k.split('?');
if (lsPath === path && lsCacheBuster !== cacheBuster) {
useCache = false;
localStorage.removeItem(k);
}
}
if (window && localStorage.getItem(url) && useCache === true) {
let data = JSON.parse(localStorage.getItem(url));
return Promise.resolve(data);
} else {
return fetch(url, {
...options,
headers: {
'X-Request-Self': true,
},
})
.then((res) => res.json())
.then((data) => {
localStorage.setItem(url, JSON.stringify(data));
return data;
});
}
}
export function formatDateShort(date) {
return format(new Date(date), "dd'/'MM'/'yyyy", {
locale: pt,
});
}
export function makeAnnotations(annotationsArray) {
let annotationBoilerplate = {
type: 'line',
mode: 'horizontal',
scaleID: 'y',
value: null,
borderColor: '#0A9DD1',
borderWidth: 1,
borderDash: [5, 5],
label: {
font: {
style: 'normal',
},
backgroundColor: 'rgba(255,255,255,0.8)',
cornerRadius: 0,
drawTime: 'afterDraw',
color: '#0A9DD1',
rotation: 270,
xAdjust: 8,
//xAdjust: -8,
yAdjust: 0,
fontSize: '13px',
enabled: true,
content: '',
},
};
let arr = [];
annotationsArray.forEach((el) => {
let annotation = {
...annotationBoilerplate,
mode: el.mode,
scaleID: el.mode === 'horizontal' ? 'y' : 'x',
borderColor: el.color,
value: el.position,
display: el.display,
label: {
...annotationBoilerplate.label,
content: el.marcador,
color: el.color,
xAdjust: el.xAdjust ?? 0,
},
};
arr.push(annotation);
});
return arr;
}
export function calculateDims() {
if (window.innerWidth <= RESIZE_TRESHOLD) {
return {
width: 2000,
height: 350,
};
} else {
return {
width: 3000,
height: 500,
};
}
}
export function getColor(d) {
if (d >= 80) {
return '#01ae97';
}
if (d >= 60) {
return '#4dc6b6';
}
if (d >= 40) {
return '#80d7cb';
}
if (d >= 20) {
return '#b3e7e0';
}
if (d >= 0) {
return '#e6f7f5';
}
}