-
Notifications
You must be signed in to change notification settings - Fork 0
/
Charts.ts
382 lines (337 loc) · 12.1 KB
/
Charts.ts
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
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
declare class Chart {
constructor(
context: string | CanvasRenderingContext2D | HTMLCanvasElement | ArrayLike<CanvasRenderingContext2D | HTMLCanvasElement>,
options: any
);
}
var GlobalScope: any = {};
type fmapper = (x:number)=>number;
type fmapstr = (x:number)=>string;
type TChartsOptions = {
xlabelcallback: fmapper,
ylabelcallback: fmapper,
legend: string,
title: string,
subtitle: string,
xlabel: string,
ylabel: string,
proportion: string,
tooltip: Function
};
abstract class Charts {
static buildFunction(code: string, variable: string, env?: string): fmapper {
let result: fmapper = eval(
"(" +
variable +
") => { " + (env?(env+";"):"") + "return (" +
code +
"); }"
);
return result;
}
static buildDatasets(n: number) {
let result = new Array(n);
for (let i=0;i<n;i++) {
result[i] = {
label: undefined as unknown as string,
backgroundColor: 'rgb(132, 99, 255)',
borderColor: 'rgb(132, 99, 255)',
pointRadius: 3,
data: [] as Array<any>,
};
}
return result;
}
static buildData(ndataset: number) {
return {
labels: [] as Array<any>,
datasets: Charts.buildDatasets(ndataset)
};
}
static buildOption(): {[k: string]: any} {
return {
plugins: {
legend: {
display: false,
},
title: {
display: false,
text: undefined as unknown as string
},
subtitle: {
display: false,
text: undefined as unknown as string
}
},
scales: {
x: {
title: {
display: false,
text: undefined as unknown as string
},
ticks: {}
},
y: {
title: {
display: false,
text: undefined as unknown as string
},
ticks: {}
}
},
animation: {
duration: 0 // general animation time
},
hover: {
animationDuration: 0 // duration of animations when hovering an item
},
responsiveAnimationDuration: 0 // animation duration after a resize
}
}
static functionXY(
container: string | HTMLElement,
codeX: string,
variable: string,
start: string | number,
step: string | number,
end: string | number,
options: TChartsOptions
) {
if (typeof container == 'string') {
container = document.getElementById(container) as HTMLElement;
}
let CanvasChart = document.createElement('canvas');
container.append(CanvasChart);
const data = Charts.buildData(1);
const opt = Charts.buildOption();
start = eval(start as string) as number;
step = eval(step as string) as number;
end = eval(end as string) as number;
let xlabelcallback : fmapper | undefined;
if (options!==undefined) {
if (options.xlabelcallback!==undefined) {
xlabelcallback = options.xlabelcallback;
}
if (options.ylabelcallback!==undefined) {
opt.scales.y.ticks.callback = options.ylabelcallback
}
if (options.legend!==undefined) {
data.datasets[0].label = options.legend;
opt.plugins.legend.display = true;
}
if (options.title!==undefined) {
opt.plugins.title.text = options.title;
opt.plugins.title.display = true;
}
if (options.subtitle!==undefined) {
opt.plugins.subtitle.text = options.subtitle;
opt.plugins.subtitle.display = true;
}
if (options.xlabel!==undefined) {
opt.scales.x.title.text = options.xlabel;
opt.scales.x.title.display = true;
}
if (options.ylabel!==undefined) {
opt.scales.y.title.text = options.ylabel;
opt.scales.y.title.display = true;
}
if (options.tooltip!==undefined) {
opt.plugins.tooltip = options.tooltip;
}
}
let fxy = this.buildFunction(codeX,variable);
let n = Math.trunc((end - start)/step) + 1;
for (let i=0;i<n;i++) {
let x_data = start + i * step;
let y_data = fxy(x_data);
data.labels.push((xlabelcallback!==undefined)?(xlabelcallback(x_data)):(x_data));
data.datasets[0].data.push(y_data);
}
const config = {
type: 'line',
data: data,
options: opt
};
const theChart = new Chart(
CanvasChart,
config
);
}
static parametricXY(
container: string | HTMLElement,
codeX: string,
codeY: string,
parameter: string,
start: string | number,
step: string | number,
end: string | number,
options: TChartsOptions,
env: string
) {
if (typeof container == 'string') {
container = document.getElementById(container) as HTMLElement;
}
let CanvasChart = document.createElement('canvas');
CanvasChart.setAttribute('style','width:600px; height:600px');
container.append(CanvasChart);
const data = Charts.buildData(1);
const opt = Charts.buildOption();
start = eval(env + ";" + start as string) as number;
step = eval(env + ";" + step as string) as number;
end = eval(env + ";" + end as string) as number;
if (options!==undefined) {
if (options.xlabelcallback!==undefined) {
opt.scales.x.ticks.callback = options.xlabelcallback
}
if (options.ylabelcallback!==undefined) {
opt.scales.y.ticks.callback = options.ylabelcallback
}
if (options.legend!==undefined) {
data.datasets[0].label = options.legend;
opt.plugins.legend.display = true;
}
if (options.title!==undefined) {
opt.plugins.title.text = options.title;
opt.plugins.title.display = true;
}
if (options.subtitle!==undefined) {
opt.plugins.subtitle.text = options.subtitle;
opt.plugins.subtitle.display = true;
}
if (options.xlabel!==undefined) {
opt.scales.x.title.text = options.xlabel;
opt.scales.x.title.display = true;
}
if (options.ylabel!==undefined) {
opt.scales.y.title.text = options.ylabel;
opt.scales.y.title.display = true;
}
if (options.tooltip!==undefined) {
opt.plugins.tooltip = options.tooltip;
}
}
opt.scales.x.type = 'linear';
opt.scales.x.position = 'bottom';
let fx = Charts.buildFunction(codeX,parameter,env);
let fy = Charts.buildFunction(codeY,parameter,env);
GlobalScope.fx = fx;
GlobalScope.fy = fy;
let n = Math.trunc((end - start)/step) + 1;
for (let i=0;i<n;i++) {
let p_data = start + i * step;
let x_data = GlobalScope.fx(p_data);
let y_data = GlobalScope.fy(p_data);
data.datasets[0].data.push({x: x_data, y: y_data});
}
const config = {
type: 'scatter',
data: data,
options: opt
};
const theChart = new Chart(
CanvasChart,
config
);
return theChart;
}
static sunChart(
container: string | HTMLElement,
year: number,
lat: number,
lng: number,
tz: number,
options: TChartsOptions,
) {
if (typeof container == 'string') {
container = document.getElementById(container) as HTMLElement;
}
let CanvasChart = document.createElement('canvas');
CanvasChart.setAttribute('style','width:600px; height:600px');
container.append(CanvasChart);
const data = Charts.buildData(13);
const opt = Charts.buildOption();
let start = 0;
let step = 1;
let end = Computus.yeardays(year)-1;
if (options!==undefined) {
if (options.legend!==undefined) {
data.datasets[0].label = options.legend;
opt.plugins.legend.display = true;
}
if (options.title!==undefined) {
opt.plugins.title.text = options.title;
opt.plugins.title.display = true;
}
if (options.subtitle!==undefined) {
opt.plugins.subtitle.text = options.subtitle;
opt.plugins.subtitle.display = true;
}
if (options.xlabel!==undefined) {
opt.scales.x.title.text = options.xlabel;
opt.scales.x.title.display = true;
}
if (options.ylabel!==undefined) {
opt.scales.y.title.text = options.ylabel;
opt.scales.y.title.display = true;
}
}
opt.plugins.tooltip = {
callbacks: {
label: function(context: any) {
let d = context.dataIndex+1;
let date = Computus.doy2cal(year,d);
return (
Computus.monthTable['PT'][1][date[1] ] + " " + date[2] +
", el: " + context.dataset.data[context.dataIndex].y +
", " + (context.datasetIndex+7) + "h"
);
}
}
}
opt.scales.x.type = 'linear';
opt.scales.x.position = 'bottom';
// opt.scales.x.min = -3.25;
// opt.scales.x.max = 3.25;
opt.scales.x.ticks.callback = function(value: number, index: number, ticks: any) {
return value;//Computus.az2cardinal(value*dtan(analemma[index].el),270);
}
// opt.scales.y.min = -1;
// opt.scales.y.max = 2.2;
opt.scales.y.ticks.callback = function(value: number) {
return value;// + "º";
}
const analemma = Computus.sunAnalemma(year,12,lat,lng,tz);
const sunchart = Computus.sunChart(year,lat,lng,tz);
GlobalScope.fx =
function (d: number) { return dcos(90-sunchart[d][12].az)*dcos(sunchart[d][12].el)/dsin(sunchart[d][12].el); }
GlobalScope.fy =
// function (d: number) { return dsin(90-sunchart[d][12].az)*dcos(sunchart[d][12].el)/dsin(sunchart[d][12].el); };
function (d: number) { return sunchart[d][12].el; };
let n = Math.trunc((end - start)/step) + 1;
for (let h=7; h<18; h++) {
data.datasets[h-7].pointRadius = 0.6;
for (let d=0;d<n;d++) {
data.datasets[h-7].data.push({
x: dcos(90-sunchart[d][h].az)*dcos(sunchart[d][h].el)/dsin(sunchart[d][h].el),
// x: dcos(dmod(90-sunchart[d][h].az)),
y: dsin(90-sunchart[d][h].az)*dcos(sunchart[d][h].el)/dsin(sunchart[d][h].el),
// y: sunchart[d][h].el,
});
}
}
const config = {
type: 'scatter',
data: data,
options: opt
};
const theChart = new Chart(
CanvasChart,
config
);
return theChart;
}
}
// How to Customize Tooltip Each Scatter Chart Data Point in Chart JS
// https://www.youtube.com/watch?v=80w6gWWDJM0
// How to Add More Information in the Tooltips in Chart JS
// https://www.youtube.com/watch?v=UxJ5d-HGhJA