-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.js
259 lines (235 loc) · 6.88 KB
/
main.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
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
if ("serviceWorker" in navigator) {
navigator.serviceWorker.register("sw.js");
}
// Variables and Lists
function present() {
// document.querySelector('#databutton').style.display = 'none'
document.querySelector('#calcbutton').style.display = 'none'
}
function dataentry() {
// Check what variables exist and calculations needed to do
if (localStorage.getItem('price')) {
price = localStorage.price;
let price2 = prompt("What is the price/cost?", price);
localStorage.price = price2;
mprice = '<p><b>Price:</b> $' + price2 + '</p>'
document.getElementById("price").innerHTML = mprice;
} else {
// price();
price();
}
if (localStorage.getItem('period')) {
period = localStorage.period;
// alert(period);
let period2 = prompt("How many time periods (months/days/years)?", period);
localStorage.period = period2;
// time = '<p><b>Time period:</b> ' + period2 + ' <select id="timeunits"><option value = "Month" selected>months</option><option value = "Day">days</option><option value = "Year">years</option></select></p>'
time = '<p><b>Time period:</b> ' + period + ' time periods</p>'
document.getElementById('period').innerHTML = time;
} else {
period();
}
if (localStorage.getItem('uses')) {
uses = localStorage.uses;
// alert(uses);
let uses2 = prompt("How often would you use it per time period?", uses);
localStorage.uses = uses2;
ownership = '<p><b>Uses per time period:</b> ' + uses2 + ' / period</p>'
document.getElementById('uses').innerHTML = ownership;
} else {
uses();
}
document.querySelector('#databutton').style.display = 'none'
document.querySelector('#calcbutton').style.display = 'block'
};
function price() {
let price = prompt("What is the price/cost?");
localStorage.price = price;
mprice = '<p><b>Price:</b> $' + price + '</p>'
document.getElementById("price").innerHTML = mprice;
};
function period() {
let period = prompt("How many time periods (months/days/years)?");
localStorage.period = period;
// time = '<p><b>Time period:</b> ' + period + ' <select name = "timeunits"><option value = "months" selected>months</option><option value = "days">days</option><option value = "years">years</option></select></p>'
time = '<p><b>Time period:</b> ' + period + ' time periods</p>'
document.getElementById('period').innerHTML = time;
}
function uses() {
let uses = prompt("How often would you use it per time period?");
localStorage.uses = uses;
ownership = '<p><b>Expected uses per time period:</b> ' + uses + '/period</p>'
document.getElementById('uses').innerHTML = ownership;
}
function calculations() {
price = localStorage.price;
period = localStorage.period;
uses = localStorage.uses;
// if (document.getElementById('timeunits') {
// timeunits = document.getElementById('timeunits').value;
// } else {
timeunits = "Time Periods"
// }
// labels();
// data();
// Actual chart using chart.js
new Chart(document.getElementById("costovertime"), {
type: 'line',
data: {
labels: labels(), // x-axis
datasets: [{
data: data(), // y-axis
label: "Cost per " + timeunits,
borderColor: "#3e95cd",
fill: false,
},
{
data: eightytwenty(),
label: "93% breakpoint",
borderColor: "#8e5ea2",
fill: true,
}]
},
options: {
responsive: true,
title: {
display: true,
text: 'Purchase Evaluation: Cost over time'
},
scales: {
xAxes: [{
display: true,
scaleLabel: {
display: true,
labelString: timeunits,
}
}],
yAxes: [{
display: true,
scaleLabel: {
display: true,
labelString: 'Cost ($)',
}
}],
},
}
});
//costperuse
new Chart(document.getElementById("costperuse"), {
type: 'line',
data: {
labels: uselabels(), // x-axis
datasets: [{
data: usedata(), // y-axis
label: "Cost per use",
borderColor: "#3e95cd",
fill: false,
// },
// {
// data: eightytwenty(),
// label: "93% breakpoint",
// borderColor: "#8e5ea2",
// fill: true,
}]
},
options: {
responsive: true,
title: {
display: true,
text: 'Purchase Evaluation: Per use cost'
},
scales: {
xAxes: [{
display: true,
scaleLabel: {
display: true,
labelString: 'Cost per use',
}
}],
yAxes: [{
display: true,
scaleLabel: {
display: true,
labelString: 'Cost ($)',
}
}],
},
}
});
resetpage();
document.querySelector('#calcbutton').style.display = 'none'
}
// labels for chart.js
function labels() {
var periods = [];
for (let i = 1; i <= period; i++) {
// alert("The number is " + i);
periods.push(i);
}
return periods;
// alert(periods);
}
// data for chart.js
function data() {
var costs = [];
for (let i=0; i <=period; i++) {
if (i<1) {
divide = price;
} else {
divide = ((price/i).toFixed(2));
// alert(divide);
costs.push(divide);
}
}
return costs;
// alert(costs);
}
// use data
function uselabels() {
var usetimes = [];
usetime = uses*period;
for (let i=1; i <=usetime; i++) {
// alert(usetimes);
usetimes.push(i);
}
return usetimes;
}
function usedata() {
var usecosts = [];
usetimes = uses*period
for (let i=0; i <=usetimes; i++) {
if (i<1) {
divide = price;
} else {
divide = ((price/i).toFixed(2));
usecosts.push(divide);
}
}
return usecosts;
}
function eightytwenty() {
let et = (0.07*price) // 7% per time period (93% breakpoint)
var eighty = [];
for (let i=0; i <= period; i++) {
if (i<1) {
divide = price;
} else {
divide = ((price/i).toFixed(2));
// alert('1) et =' + et + ', divide = ' + divide);
// divideet = divide;
if (divide >=et) {
// alert('2)et =' + et + ', divide = ' + divide); // + ', divide-et: ' + divideet);
eighty.push(divide);
}
}
}
return eighty;
}
function resetpage() {
// window.Location.reload();
// localStorage.price = '';
localStorage.removeItem('price');
localStorage.removeItem('period');
localStorage.removeItem('uses');
// localStorage.removeItem('uses');
}