-
Notifications
You must be signed in to change notification settings - Fork 0
/
plots.js
executable file
·264 lines (199 loc) · 6.36 KB
/
plots.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
260
261
262
263
264
/*
* This file is subject to the terms and conditions defined in
* file 'LICENSE', which is part of this source code package.
*/
var thisData = null;
var thisLabels = null;
var thisSubject = null;
var canvasCreated = false;
var thisLabsLabel = null;
var CHART;
var lineChart;
var chartType = null;
var thisChartType;
var dataVals, dataArr,labelVals, labelArr ,subjVals, subject, labsVal, oPercent;
var thisOverallPercentage;
var toggleLabel = 0;
Chart.defaults.scale.ticks.beginAtZero = true;
Chart.defaults.scale.ticks.max = 100;
Chart.defaults.global.animation.duration = 2000;
Chart.defaults.global.defaultFontColor = "#000";
//Chart.defaults.global.maintainAspectRatio = false;
//Chart.defaults.global.responsive = true;
Chart.defaults.global.legend.position = 'bottom';
function graphThis(type){
lineChart = new Chart (CHART, {
type: type,
data: data = {
axisY: {
title: "percentage"
},
labels: labsVal,
datasets: [
{
label: subject,
fill: true,
lineTension: 0.1,
backgroundColor: "rgba(75,192,192,0.4)",
borderColor: "rgba(75,192,192,1)",
borderCapStyle: 'butt',
borderDash: [],
borderDashOffset: 0.0,
borderJoinStyle: 'miter',
pointBorderColor: "rgba(75,192,192,1)",
pointBackgroundColor: "#fff",
pointBorderWidth: 1,
pointHoverRadius: 5,
pointHoverBackgroundColor: "rgba(75,192,192,1)",
pointHoverBorderColor: "rgba(0,0,0)",
pointHoverBorderWidth: 10,
pointRadius: 1,
pointHitRadius: 4,
data: dataArr,
spanGaps: false,
}
]
},
options:{
tooltips : {
enabled: true,
tooltipTemplate: "<%if (labels){%><%=labels%>: <%}%><%= value %>"
},
scales: {
yAxes: [{
scaleLabel: {
display: true,
labelString: '%'
}
}]
},
maintainAspectRatio: true,
bezierCurve: false,
//when animation is completed
animation:{
onComplete : function(){
var ctx = this.chart.ctx;
var chartInstance = this.chart;
ctx.textAlign = "center";
ctx.textBaseline = "bottom";
ctx.fillStyle = "#000";
var x_offset=0;
var y_offset=10;
// if(chartType == 'bar'){
// x_offset = 10;
// y_offset = 5;
// }
//
// if(chartType == 'line'){
// x_offset = 10;
// y_offset = 10;
// }
ctx.textAlign = "center";
//convert canvas object to a a png image
var url_base64 = document.getElementById("lineChart").toDataURL("image/png");
link1.href = url_base64;
var url = link1.href.replace(/^data:image\/[^;]/, 'data:application/octet-stream');
}
}
}
});
}
chrome.runtime.onMessage.addListener(function(message, sender, sendResponse){
//extract the messages passed from getPageSource.JS
if(message.type == "data") {
console.log("First message: ", message.content);
thisData = message;
}
if(message.type == "label") {
console.log("Second message: ", message.content);
thisLabels = message;
}
if(message.type == "subject") {
console.log("third message: ", message.content);
thisSubject = message;
}
if(message.type== "chartType"){
thisChartType = message;
console.log(message);
}
if(message.type == "labsLabel"){
thisLabsLabel = message;
}
if(message.type == "overallPercentage"){
thisOverallPercentage = message;
//console.log("OV per message:")
//console.log(message)
}
//check if all the necessary messages are passed to graph the grades.
if(thisData != null & thisLabels != null & thisSubject !=null & thisChartType !=null & thisLabsLabel!=null){
//check if canvas has been created already.
if(canvasCreated == false){
CHART = document.getElementById("lineChart");
console.log(CHART);
canvasCreated = true;
}
//if already created, have to clear the old one and draw a new graph on same canvas
else {
lineChart.destroy();
CHART = document.getElementById("lineChart");
}
//get the values from the objects passed as a message from getPageSource.js
//thisOverallPercentage can be null sometimes (This is not an error state)
if(thisOverallPercentage != null) {
oPercent = Object.values(thisOverallPercentage);
$('#overall_div').text("Overall: " + oPercent[0] + "%");
console.log(oPercent)
}
dataVals = Object.values(thisData);
dataArr =dataVals[0];
console.log(dataArr);
labelVals = Object.values(thisLabels);
labelArr = labelVals[0];
console.log(labelArr);
subjVals = Object.values(thisSubject);
subject = subjVals[0];
var chartTypeVals = Object.values(thisChartType);
chartType = chartTypeVals[0];
var labsObject = Object.values(thisLabsLabel);
labsVal = labsObject[0];
graphThis(chartType);
//make all the messages passed from getPageSource to null so they dont corrupt the next messages passed.
thisData = null;
thisLabels= null;
thisSubject = null;
thisChartType = null;
thisLabsLabel = null;
thisOverallPercentage = null;
console.log("graphed!");
}
});
//functionto change from line graph to a bar chart
$('#barChartIt').click(function do_something(){
lineChart.destroy();
CHART = document.getElementById("lineChart");
document.getElementById("barChartIt").disabled = true;
graphThis('bar');
document.getElementById("lineChartIt").disabled = false;
});
//function to change bar graph to line graph
$('#lineChartIt').click(function do_something(){
console.log("line it");
lineChart.destroy();
CHART = document.getElementById("lineChart");
document.getElementById("lineChartIt").disabled = true;
graphThis('line');
document.getElementById("barChartIt").disabled= false;
});
//function to change y axis/label
$('#change_Labels').click(function change_Labels(){
//check current state
if(toggleLabel ==1 ){
lineChart.config.data.labels = labsVal;
toggleLabel = 0;
}
else{
lineChart.config.data.labels = labelArr;
toggleLabel = 1;
}
lineChart.update()
});