-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
147 lines (115 loc) · 3.97 KB
/
script.js
File metadata and controls
147 lines (115 loc) · 3.97 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
var getXmlHttp = function() {
var xmlhttp;
try {
xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
} catch (e) {
try {
xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
} catch (E) {
xmlhttp = false;
}
}
if (!xmlhttp && typeof XMLHttpRequest!='undefined') {
xmlhttp = new XMLHttpRequest();
}
return xmlhttp;
};
var getCookie = function(name) {
var matches = document.cookie.match(new RegExp(
"(?:^|; )" + name.replace(/([\.$?*|{}\(\)\[\]\\\/\+^])/g, '\\$1') + "=([^;]*)"
));
return matches ? decodeURIComponent(matches[1]) : undefined;
};
var setCookie = function(name, value, options) {
options = options || {};
var expires = options.expires;
if (typeof expires == "number" && expires) {
var d = new Date();
d.setTime(d.getTime() + expires * 1000);
expires = options.expires = d;
}
if (expires && expires.toUTCString) {
options.expires = expires.toUTCString();
}
value = encodeURIComponent(value);
var updatedCookie = name + "=" + value;
for (var propName in options) {
updatedCookie += "; " + propName;
var propValue = options[propName];
if (propValue !== true) {
updatedCookie += "=" + propValue;
}
}
document.cookie = updatedCookie;
};
var initChart = function() {
var xhr = getXmlHttp();
var theme = getCookie('theme');
var body = document.getElementsByTagName('body')[0];
var metaColorsTags = document.getElementsByClassName('browser-theme-color');
var themeColors = {
'default': '#eeeeee',
'night': '#242f3e'
};
var selectedTheme = 'default';
var setTheme = function(theme) {
theme = theme || 'default';
switch (theme) {
case 'night':
selectedTheme = 'night';
body.className = 'dark-theme';
break;
case 'default':
selectedTheme = 'default';
body.className = 'default-theme';
break;
}
for (var k = 0; k < metaColorsTags.length; k++) {
metaColorsTags[k].setAttribute('content', themeColors[selectedTheme]);
}
setCookie('theme', theme, {expires: 86400});
};
setTheme(theme);
var switchTheme = function () {
switch (selectedTheme) {
case 'default':
setTheme('night');
break;
case 'night':
setTheme('default');
break;
}
};
var themeSwitcher = document.getElementById('theme-switcher');
themeSwitcher.addEventListener ?
themeSwitcher.addEventListener('click', switchTheme, false) :
themeSwitcher.attachEvent("onclick", switchTheme);
var charts = document.getElementById('charts-list');
xhr.open('GET', 'data.json', true);
xhr.send();
xhr.onreadystatechange = function() {
if (xhr.readyState != 4) return;
if (xhr.status != 200) {
console.log(xhr.status + ': ' + xhr.statusText);
} else {
setDataToCharts(JSON.parse(xhr.responseText));
}
};
var setDataToCharts = function(chartsData) {
chartsData.forEach(function(chartData, index) {
var chartDomContainer = document.createElement('div');
chartDomContainer.className = 'one-chart-block';
var chartDom = document.createElement('div');
chartDom.className = 'one-chart-container';
var chartTitle = document.createElement('div');
chartTitle.className = 'one-chart-title';
chartTitle.innerText = 'Chart #' + (index + 1);
chartDomContainer.appendChild(chartTitle);
chartDomContainer.appendChild(chartDom);
charts.appendChild(chartDomContainer);
var chart = new Chart(chartDom);
chart.setData(chartData);
});
};
};
window.onload = initChart;