-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathchart-tools.js
136 lines (129 loc) · 4.53 KB
/
chart-tools.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
/*
By: Brendan Luke
Date: March 26, 2022
Scope: create chart.js plot from TLE data
*/
function createChart(outData) { // this is called after the TLE decoded data is written to a file and downloading
var Xselections = document.getElementById("X-Axis"); // get DOM access to x-axis selection
var Yselections = document.getElementById("Y-Axis"); // get DOM access to y-axis selection
var ctx = document.getElementById('chart').getContext('2d');
var myChart = new Chart(ctx, {
type: 'line',
data: {
datasets: [{
label: 'Semi-Major Axis Height (km)', // default
data: [], // initialize empty, populate later
backgroundColor: '#0D5198',
borderColor: '#0D5198',
borderWidth: 1.5,
}]
},
options: {
responsive: true,
maintainAspectRatio: false,
scales: {
x: {
type: 'time',
time: {
unit: 'day',
displayFormats: {
day: 'yyyy-MMM-dd HH:mm'
}
},
adapters: {
date: {
zone: 'UTC'
}
}
},
y: {
type: 'linear'
}
}
},
plugins: [plugin],
});
for (i = 0; i < outData[Xselections.value].length; i++) { // add data
myChart.data.datasets[0].data.push({x: luxon.DateTime.fromISO(outData.epoch[i]).ts, y: outData.SemiMajorAxisH[i]});
}
myChart.update(); // force update to display default data
Xselections.addEventListener("change", function() { // change X data and label
myChart.data.datasets[0].label = Xselections.options[Xselections.selectedIndex].text; // change label
for (i = 0; i < myChart.data.datasets[0].data.length; i++) { // change x data
myChart.data.datasets[0].data[i].x = outData[Xselections.value][i];
}
if (Xselections.value.toString() == 'epoch') {
timeFlag = true;
} else {
timeFlag = false;
}
axesConfig(myChart.options.scales,timeFlag,true);
myChart.update();
});
Yselections.addEventListener("change", function() { // change Y data and label
myChart.data.datasets[0].label = Yselections.options[Yselections.selectedIndex].text; // change label
for (i = 0; i < myChart.data.datasets[0].data.length; i++) { // change x data
myChart.data.datasets[0].data[i].y = outData[Yselections.value][i];
}
if (Yselections.value.toString() == 'epoch') {
timeFlag = true;
} else {
timeFlag = false;
}
axesConfig(myChart.options.scales,timeFlag,false)
myChart.update();
});
}
const plugin = {
id: 'custom_canvas_background_color',
beforeDraw: (chart) => {
const ctx = chart.canvas.getContext('2d');
ctx.save();
ctx.globalCompositeOperation = 'destination-over';
ctx.fillStyle = 'white';
ctx.fillRect(0, 0, chart.width, chart.height);
ctx.restore();
}
};
function axesConfig(scaleOption,timeFlag,xAxis) {
// configure axes (time or not)
if (timeFlag) { // is 'epoch' data
if (xAxis) { // is x-axis
scaleOption.x = {
type: 'time',
time: {
unit: 'day',
displayFormats: {
day: 'yyyy-MMM-dd HH:mm'
}
},
adapters: {
date: {
zone: 'UTC'
}
}
}
} else { // is y-axis
scaleOption.y = {
type: 'time',
time: {
unit: 'day',
displayFormats: {
day: 'yyyy-MMM-dd HH:mm'
}
},
adapters: {
date: {
zone: 'UTC'
}
}
}
}
} else { // any other data field
if (xAxis) { // is x-axis
scaleOption.x = {type: 'linear'}
} else { // is y-axis
scaleOption.y = {type: 'linear'}
}
}
}