-
Notifications
You must be signed in to change notification settings - Fork 0
/
graphScript.js
153 lines (136 loc) · 3.61 KB
/
graphScript.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
//import Chart from 'chart.js/auto';
const e=2.7182818;
function fSat(alpha,gamma){
function f(x){
return x**alpha/(x**alpha+gamma**alpha);
}
return f;
}
function fGeoAdStock(theta){
function decay(t){
return 1/e**(theta*t)
}
/*
function f(actual,previous){
return actual + theta*previous;
}
*/
return decay;
}
function fWeibullAdStock(shape,scale,type='pdf'){
function pdf(t){
return scale*shape*(scale*t)**(shape-1)*e**((-1)*(scale*t)**shape);
}
function cdf(t){
return 1-e**((-1)*(scale*t)**shape)
}
let funMap = {
'pdf':pdf,
'cdf':cdf
}
/*
function f(actual,previous,t){
return actual + funMap[type](t)*previous;
}
*/
return funMap[type];
}
function getChart(){
return document.getElementById('Grafica');
}
function genXRange(min,max,step){
let xValues = [];
for (let x=min; x<=max; x+=step){xValues.push(Math.round(x*100)/100);}
return xValues;
}
function genRevXRange(max,min,step){
let xValues = [];
for (let x=max; x>=min; x+=step){xValues.push(Math.round(x*100)/100);}
return xValues;
}
function getGeoGraphData(xValues,yFunc){
let dataList = [];
let previous = 0;
for (let i=1; i<xValues.length; i++){
let actual = xValues[i]
data = {'x':actual,'y':yFunc(previous,actual)};
dataList.push(data);
previous = actual
}
return {'dataset':dataList}
}
function getGeoYValues(xValues,yFunc){
let yList = []
let previous = 0;
for (let i=1; i<xValues.length; i++){
let actual = xValues[i]
yList.push(yFunc(actual,previous))
previous = actual
}
return yList;
}
function getYValues(xValues,yFunc){
let yList = [];
for (let i=1; i<xValues.length; i++){
yList.push(yFunc(xValues[i]));
}
return yList;
}
function removeCanvas(){
let chart = getChart();
chart.remove();
return undefined;
}
function createCanvas(){
let chart = document.createElement('canvas');
chart.id='Grafica';
document.getElementById('canvasContainer').appendChild(chart);
return undefined;
}
function getConfig(XData,YData,varName,adStock){
return {
'type':'line',
'data': {
'labels':XData,
'datasets': [
{
'label':adStock +' '+varName,
'data':YData,
'backgroundColor': 'rgba(153, 0, 153, 0.2)', // Color de fondo
'borderColor': 'rgba(204, 0, 204, 1)', // Color de borde
'borderWidth': 1 // Ancho del borde
}
]
},
}
}
async function plotGeometric(theta,varName){
removeCanvas();
createCanvas();
if (theta === ''){return undefined;}
X = genXRange(0,10,0.1);
f = fGeoAdStock(theta);
Y = getYValues(X,f);
let config = getConfig(X,Y,varName,'geometric');
let ctx = getChart().getContext('2d');
new Chart(
ctx,
config
);
return undefined;
}
async function plotWeibull(shape,scale,type,varName){
removeCanvas();
createCanvas();
if (shape === '' || scale === '' || type===''){return undefined;}
X = genXRange(0,2,0.01);
f = fWeibullAdStock(shape,scale,type);
Y = getYValues(X,f);
let config = getConfig(X,Y,varName,'weibull_'+type);
let ctx = getChart().getContext('2d');
new Chart(
ctx,
config
);
return undefined;
}