-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathscript.js
More file actions
909 lines (772 loc) · 29.4 KB
/
Copy pathscript.js
File metadata and controls
909 lines (772 loc) · 29.4 KB
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
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
// Constants
const PIXELS_PER_DIV = 8;
// State
let regressionLines = [];
let referenceLines = [];
let showDataPoints = true;
let showConnectingLines = true;
let showRegressionLines = true;
let showReferenceLines = true;
let graphCollapsed = false;
// Rounding function - always rounds 0.5 and above up
function roundHalfUp(num) {
return Math.floor(num + 0.5);
}
// Demo Data Loaders
function loadDemo1() {
// Demo 1: Conductivity vs Concentration
document.getElementById('xBigDivisions').value = 12;
document.getElementById('xSmallPerBig').value = 10;
document.getElementById('xMinValue').value = 0;
document.getElementById('xMaxValue').value = 60;
document.getElementById('yBigDivisions').value = 18;
document.getElementById('ySmallPerBig').value = 10;
document.getElementById('yMinValue').value = 0;
document.getElementById('yMaxValue').value = 180;
document.getElementById('xAxisLabel').value = 'Concentration (mM)';
document.getElementById('yAxisLabel').value = 'Conductivity (μS/cm)';
document.getElementById('graphTitle').value = 'Conductivity vs Concentration';
const demoData = [
[0, 0], [10, 30], [20, 60], [30, 90], [40, 120], [50, 150], [60, 180]
];
loadDemoData(demoData);
updateCalculatedParameters();
}
function loadDemo2() {
// Demo 2: Pressure vs Volume (Boyle's Law)
document.getElementById('xBigDivisions').value = 20;
document.getElementById('xSmallPerBig').value = 10;
document.getElementById('xMinValue').value = 20;
document.getElementById('xMaxValue').value = 100;
document.getElementById('yBigDivisions').value = 16;
document.getElementById('ySmallPerBig').value = 10;
document.getElementById('yMinValue').value = 0;
document.getElementById('yMaxValue').value = 80;
document.getElementById('xAxisLabel').value = 'Volume (mL)';
document.getElementById('yAxisLabel').value = 'Pressure (kPa)';
document.getElementById('graphTitle').value = 'Pressure vs Volume (Boyle\'s Law)';
const demoData = [
[20, 80], [30, 53.3], [40, 40], [50, 32], [60, 26.7], [80, 20], [100, 16]
];
loadDemoData(demoData);
updateCalculatedParameters();
}
function loadDemoData(data) {
const tbody = document.getElementById('dataTable').getElementsByTagName('tbody')[0];
const rows = tbody.rows;
for (let i = 0; i < data.length && i < rows.length; i++) {
rows[i].cells[1].children[0].value = data[i][0];
rows[i].cells[2].children[0].value = data[i][1];
rows[i].cells[3].textContent = '';
rows[i].cells[4].textContent = '';
}
// Clear remaining rows
for (let i = data.length; i < rows.length; i++) {
rows[i].cells[1].children[0].value = '';
rows[i].cells[2].children[0].value = '';
rows[i].cells[3].textContent = '';
rows[i].cells[4].textContent = '';
}
}
// Get parameter values with smart defaults
function getParameters() {
let xBigDivisions = parseInt(document.getElementById('xBigDivisions').value);
let xSmallPerBig = parseInt(document.getElementById('xSmallPerBig').value);
let xMinValue = parseFloat(document.getElementById('xMinValue').value);
let xMaxValue = parseFloat(document.getElementById('xMaxValue').value);
let yBigDivisions = parseInt(document.getElementById('yBigDivisions').value);
let ySmallPerBig = parseInt(document.getElementById('ySmallPerBig').value);
let yMinValue = parseFloat(document.getElementById('yMinValue').value);
let yMaxValue = parseFloat(document.getElementById('yMaxValue').value);
// Smart defaults
if (isNaN(xBigDivisions)) xBigDivisions = 1;
if (isNaN(xSmallPerBig)) xSmallPerBig = 1;
if (isNaN(xMinValue)) xMinValue = 0;
if (isNaN(xMaxValue)) xMaxValue = xMinValue + 100;
if (isNaN(yBigDivisions)) yBigDivisions = 1;
if (isNaN(ySmallPerBig)) ySmallPerBig = 1;
if (isNaN(yMinValue)) yMinValue = 0;
if (isNaN(yMaxValue)) yMaxValue = yMinValue + 100;
// Validation
if (xMaxValue <= xMinValue) {
xMaxValue = xMinValue + 100;
}
if (yMaxValue <= yMinValue) {
yMaxValue = yMinValue + 100;
}
// Calculate divisions
const xTotalDivisions = xBigDivisions * xSmallPerBig;
const yTotalDivisions = yBigDivisions * ySmallPerBig;
const xRange = xMaxValue - xMinValue;
const yRange = yMaxValue - yMinValue;
const xSmallestDivCalc = xRange / xTotalDivisions;
const ySmallestDivCalc = yRange / yTotalDivisions;
// Check for overrides
const xOverride = parseFloat(document.getElementById('xSmallestDivOverride').value);
const yOverride = parseFloat(document.getElementById('ySmallestDivOverride').value);
const xSmallestDiv = (xOverride && xOverride >= xSmallestDivCalc) ? xOverride : xSmallestDivCalc;
const ySmallestDiv = (yOverride && yOverride >= ySmallestDivCalc) ? yOverride : ySmallestDivCalc;
return {
xBigDivisions, xSmallPerBig, xMinValue, xMaxValue, xTotalDivisions, xSmallestDiv, xSmallestDivCalc,
yBigDivisions, ySmallPerBig, yMinValue, yMaxValue, yTotalDivisions, ySmallestDiv, ySmallestDivCalc
};
}
// Update calculated parameters display
function updateCalculatedParameters() {
const params = getParameters();
const resultBox = document.getElementById('calculatedResults');
const resultText = document.getElementById('calculatedText');
resultText.innerHTML = `
<div style="margin-bottom:10px;">
<strong>X-Axis:</strong> ${params.xTotalDivisions} total divisions
(${params.xBigDivisions} × ${params.xSmallPerBig}) |
<strong>Smallest division = <span class="calc-highlight">${params.xSmallestDivCalc.toFixed(4)}</span> units</strong>
</div>
<div>
<strong>Y-Axis:</strong> ${params.yTotalDivisions} total divisions
(${params.yBigDivisions} × ${params.ySmallPerBig}) |
<strong>Smallest division = <span class="calc-highlight">${params.ySmallestDivCalc.toFixed(4)}</span> units</strong>
</div>
`;
resultBox.classList.add('show');
// Update placeholders
document.getElementById('xSmallestDivOverride').placeholder = `Auto: ${params.xSmallestDivCalc.toFixed(4)}`;
document.getElementById('ySmallestDivOverride').placeholder = `Auto: ${params.ySmallestDivCalc.toFixed(4)}`;
}
// Clear calculation when data changes
function clearCalculation(input) {
const row = input.parentNode.parentNode;
row.cells[3].textContent = '';
row.cells[4].textContent = '';
}
// Toggle all point selection
function toggleAllSelection() {
const selectAll = document.getElementById('selectAllPoints').checked;
const checkboxes = document.querySelectorAll('.point-select');
checkboxes.forEach(cb => cb.checked = selectAll);
}
// Add new data row
function addDataRow() {
const table = document.getElementById('dataTable').getElementsByTagName('tbody')[0];
const row = table.insertRow();
row.innerHTML = `
<td><input type="checkbox" class="point-select"></td>
<td><input type="number" step="any" oninput="clearCalculation(this)"></td>
<td><input type="number" step="any" oninput="clearCalculation(this)"></td>
<td></td>
<td></td>
<td><button class="btn-icon" onclick="deleteRow(this)"><i class="fas fa-trash-alt"></i></button></td>
`;
}
// Delete row
function deleteRow(btn) {
const row = btn.parentNode.parentNode;
const tbody = row.parentNode;
if (tbody.rows.length > 1) {
tbody.removeChild(row);
} else {
alert('At least one data row is required');
}
}
// Calculate divisions for all data points
function calculateDivisions() {
const params = getParameters();
const tableRows = document.getElementById('dataTable').getElementsByTagName('tbody')[0].rows;
let validPointCount = 0;
for (let i = 0; i < tableRows.length; i++) {
const xValue = parseFloat(tableRows[i].cells[1].children[0].value);
const yValue = parseFloat(tableRows[i].cells[2].children[0].value);
if (!isNaN(xValue) && !isNaN(yValue)) {
const xDivision = roundHalfUp((xValue - params.xMinValue) / params.xSmallestDiv);
const yDivision = roundHalfUp((yValue - params.yMinValue) / params.ySmallestDiv);
tableRows[i].cells[3].textContent = xDivision;
tableRows[i].cells[4].textContent = yDivision;
validPointCount++;
}
}
if (validPointCount === 0) {
alert('Please enter at least one valid data point');
return;
}
document.getElementById('graphSettingsSection').style.display = 'block';
// Scroll to graph settings
document.getElementById('graphSettingsSection').scrollIntoView({ behavior: 'smooth', block: 'start' });
}
// Toggle graph collapse
function toggleGraphCollapse() {
graphCollapsed = !graphCollapsed;
const container = document.getElementById('graphContainer');
const icon = document.getElementById('collapseIcon');
const text = document.getElementById('collapseText');
if (graphCollapsed) {
container.classList.add('collapsed');
icon.className = 'fas fa-chevron-down';
text.textContent = 'Expand';
} else {
container.classList.remove('collapsed');
icon.className = 'fas fa-chevron-up';
text.textContent = 'Collapse';
}
}
// Toggle display options
function toggleDisplay() {
showDataPoints = document.getElementById('showPoints').checked;
showConnectingLines = document.getElementById('showLines').checked;
showRegressionLines = document.getElementById('showRegressions').checked;
showReferenceLines = document.getElementById('showReferences').checked;
if (document.getElementById('plotlyGraph').children.length > 0) {
regenerateGraph();
}
}
// Generate graph
function generateGraph() {
const params = getParameters();
const tableRows = document.getElementById('dataTable').getElementsByTagName('tbody')[0].rows;
const xDivisions = [];
const yDivisions = [];
const xOriginalValues = []; // NEW: Store original values
const yOriginalValues = []; // NEW: Store original values
const labels = [];
for (let i = 0; i < tableRows.length; i++) {
const xValue = parseFloat(tableRows[i].cells[1].children[0].value);
const yValue = parseFloat(tableRows[i].cells[2].children[0].value);
if (!isNaN(xValue) && !isNaN(yValue)) {
const xDiv = roundHalfUp((xValue - params.xMinValue) / params.xSmallestDiv);
const yDiv = roundHalfUp((yValue - params.yMinValue) / params.ySmallestDiv);
xDivisions.push(xDiv);
yDivisions.push(yDiv);
xOriginalValues.push(xValue); // NEW: Store original
yOriginalValues.push(yValue); // NEW: Store original
labels.push(`(${xValue}, ${yValue})`);
}
}
if (xDivisions.length === 0) {
alert('No valid data points to plot');
return;
}
const tickInterval = parseInt(document.getElementById('tickInterval').value) || 10;
const xLabel = document.getElementById('xAxisLabel').value || 'X Axis';
const yLabel = document.getElementById('yAxisLabel').value || 'Y Axis';
const graphTitle = document.getElementById('graphTitle').value || 'Graph';
// Generate tick positions and labels
const xTickPositions = [];
const xTickLabels = [];
for (let i = 0; i <= params.xTotalDivisions; i += tickInterval) {
xTickPositions.push(i);
xTickLabels.push((params.xMinValue + (i * params.xSmallestDiv)).toFixed(2));
}
const yTickPositions = [];
const yTickLabels = [];
for (let i = 0; i <= params.yTotalDivisions; i += tickInterval) {
yTickPositions.push(i);
yTickLabels.push((params.yMinValue + (i * params.ySmallestDiv)).toFixed(2));
}
// Division box annotation
const divBoxPosition = document.getElementById('divBoxPosition').value;
let annotationX, annotationY, xAnchor, yAnchor;
switch(divBoxPosition) {
case 'top-left':
annotationX = 0.02; annotationY = 0.98; xAnchor = 'left'; yAnchor = 'top';
break;
case 'top-right':
annotationX = 0.98; annotationY = 0.98; xAnchor = 'right'; yAnchor = 'top';
break;
case 'bottom-left':
annotationX = 0.02; annotationY = 0.02; xAnchor = 'left'; yAnchor = 'bottom';
break;
case 'bottom-right':
annotationX = 0.98; annotationY = 0.02; xAnchor = 'right'; yAnchor = 'bottom';
break;
default:
annotationX = 0.02; annotationY = 0.98; xAnchor = 'left'; yAnchor = 'top';
}
const annotations = [{
text: `<b>1 smallest division:</b><br>X = ${params.xSmallestDiv.toFixed(4)}<br>Y = ${params.ySmallestDiv.toFixed(4)}`,
xref: 'paper',
yref: 'paper',
x: annotationX,
y: annotationY,
xanchor: xAnchor,
yanchor: yAnchor,
showarrow: false,
bgcolor: '#fffaed',
bordercolor: '#ffd54f',
borderwidth: 2,
borderpad: 8,
font: {family: 'Cambria, Georgia, serif', size: 11, color: '#5d4037'}
}];
// Reference lines as shapes
const shapes = [];
if (showReferenceLines) {
referenceLines.forEach(refLine => {
if (refLine.type === 'vertical') {
shapes.push({
type: 'line',
xref: 'x',
yref: 'paper',
x0: refLine.position,
y0: 0,
x1: refLine.position,
y1: 1,
line: { dash: refLine.style, color: refLine.color, width: refLine.width },
editable: true
});
} else {
shapes.push({
type: 'line',
xref: 'paper',
yref: 'y',
x0: 0,
y0: refLine.position,
x1: 1,
y1: refLine.position,
line: { dash: refLine.style, color: refLine.color, width: refLine.width },
editable: true
});
}
});
}
const graphWidth = params.xTotalDivisions * PIXELS_PER_DIV;
const graphHeight = params.yTotalDivisions * PIXELS_PER_DIV;
const layout = {
title: {text: graphTitle, font: {family: 'Cambria, Georgia, serif', size: 18, color: '#003366'}},
xaxis: {
title: {text: `──────── ${xLabel} ────────➤`, font: {family: 'Cambria, Georgia, serif', size: 14}},
range: [0, params.xTotalDivisions],
tickmode: 'array',
tickvals: xTickPositions,
ticktext: xTickLabels,
showgrid: true,
gridcolor: '#2d8659',
gridwidth: 2,
zeroline: false,
tickfont: {family: 'Cambria, Georgia, serif', size: 11},
minor: {dtick: 1, showgrid: true, gridcolor: '#90d9b8', gridwidth: 0.5},
scaleanchor: 'y',
scaleratio: 1
},
yaxis: {
title: {text: `──────── ${yLabel} ────────➤`, font: {family: 'Cambria, Georgia, serif', size: 14}},
range: [0, params.yTotalDivisions],
tickmode: 'array',
tickvals: yTickPositions,
ticktext: yTickLabels,
showgrid: true,
gridcolor: '#2d8659',
gridwidth: 2,
zeroline: false,
tickfont: {family: 'Cambria, Georgia, serif', size: 11},
minor: {dtick: 1, showgrid: true, gridcolor: '#90d9b8', gridwidth: 0.5}
},
width: graphWidth + 120,
height: graphHeight + 100,
margin: {l: 80, r: 50, t: 80, b: 70},
font: {family: 'Cambria, Georgia, serif', size: 12},
plot_bgcolor: '#fefef8',
paper_bgcolor: '#ffffff',
autosize: false,
annotations: annotations,
shapes: shapes
};
const traces = [];
// Data points trace
if (showDataPoints || showConnectingLines) {
let mode = [];
if (showDataPoints) mode.push('markers');
if (showConnectingLines) mode.push('lines');
mode.push('text');
traces.push({
x: xDivisions,
y: yDivisions,
mode: mode.join('+'),
type: 'scatter',
marker: showDataPoints ? {size: 10, color: '#c41e3a', symbol: 'circle', line: {width: 2, color: '#8b0000'}} : undefined,
line: showConnectingLines ? {color: '#003366', width: 2.5} : undefined,
text: labels,
textposition: 'top center',
textfont: {family: 'Cambria, Georgia, serif', size: 10, color: '#000'},
name: 'Data',
// Custom data for hover
customdata: xOriginalValues.map((xVal, i) => [xVal, yOriginalValues[i], xDivisions[i], yDivisions[i]]),
// Custom hover template
hovertemplate:
'Values: (%{customdata[0]}, %{customdata[1]})<br>' +
'Divisions: (%{customdata[2]}, %{customdata[3]})' +
'<extra></extra>',
showlegend: true
});
}
// Regression lines
if (showRegressionLines) {
regressionLines.forEach((reg, index) => {
traces.push({
x: reg.xValues,
y: reg.yValues,
mode: 'lines',
type: 'scatter',
line: {color: '#ff6b35', width: 2.5, dash: 'dash'},
name: `Regression ${index + 1}`,
showlegend: true
});
});
}
const config = {
displayModeBar: true,
modeBarButtonsToRemove: ['pan2d', 'lasso2d', 'select2d'],
toImageButtonOptions: {
format: 'jpeg',
filename: 'graph_plot',
width: graphWidth + 120,
height: graphHeight + 100,
scale: 10
},
responsive: true,
edits: { shapePosition: true }
};
Plotly.newPlot('plotlyGraph', traces, layout, config);
// Show sections
document.getElementById('graphDisplaySection').style.display = 'block';
document.getElementById('reportSection').style.display = 'block';
// Scroll to graph
document.getElementById('graphDisplaySection').scrollIntoView({ behavior: 'smooth', block: 'start' });
}
function regenerateGraph() {
if (document.getElementById('plotlyGraph').children.length > 0) {
generateGraph();
}
}
// Download graph as high-quality image
function downloadGraphImage() {
const params = getParameters();
const graphWidth = params.xTotalDivisions * PIXELS_PER_DIV + 120;
const graphHeight = params.yTotalDivisions * PIXELS_PER_DIV + 100;
const graphDiv = document.getElementById('plotlyGraph');
Plotly.downloadImage(graphDiv, {
format: 'jpeg',
width: graphWidth,
height: graphHeight,
scale: 10,
filename: 'graph_plot'
});
}
// Regression Analysis
function calculateRegressionLine(xValues, yValues) {
const n = xValues.length;
if (n < 2) return null;
const sumX = xValues.reduce((a, b) => a + b, 0);
const sumY = yValues.reduce((a, b) => a + b, 0);
const sumXY = xValues.reduce((sum, x, i) => sum + x * yValues[i], 0);
const sumX2 = xValues.reduce((sum, x) => sum + x * x, 0);
const slope = (n * sumXY - sumX * sumY) / (n * sumX2 - sumX * sumX);
const intercept = (sumY - slope * sumX) / n;
const yMean = sumY / n;
const ssTotal = yValues.reduce((sum, y) => sum + Math.pow(y - yMean, 2), 0);
const ssResidual = yValues.reduce((sum, y, i) => {
const yPred = slope * xValues[i] + intercept;
return sum + Math.pow(y - yPred, 2);
}, 0);
const r2 = 1 - (ssResidual / ssTotal);
return {
slope,
intercept,
r2,
equation: `y = ${slope.toFixed(4)}x + ${intercept.toFixed(4)}`
};
}
function addRegressionLine() {
const params = getParameters();
const tableRows = document.getElementById('dataTable').getElementsByTagName('tbody')[0].rows;
const selectedXDiv = [];
const selectedYDiv = [];
for (let i = 0; i < tableRows.length; i++) {
if (tableRows[i].cells[0].children[0].checked) {
const xDiv = parseInt(tableRows[i].cells[3].textContent);
const yDiv = parseInt(tableRows[i].cells[4].textContent);
if (!isNaN(xDiv) && !isNaN(yDiv)) {
selectedXDiv.push(xDiv);
selectedYDiv.push(yDiv);
}
}
}
if (selectedXDiv.length < 2) {
alert('Please select at least 2 points for regression analysis');
return;
}
const regression = calculateRegressionLine(selectedXDiv, selectedYDiv);
if (!regression) return;
const xLineValues = [0, params.xTotalDivisions];
const yLineValues = xLineValues.map(x => regression.slope * x + regression.intercept);
regressionLines.push({
id: regressionLines.length,
xValues: xLineValues,
yValues: yLineValues,
equation: regression.equation,
r2: regression.r2,
pointCount: selectedXDiv.length
});
updateRegressionList();
regenerateGraph();
}
function updateRegressionList() {
const listDiv = document.getElementById('regressionList');
listDiv.innerHTML = '';
regressionLines.forEach((reg, index) => {
const item = document.createElement('div');
item.className = 'item-list-item';
item.innerHTML = `
<div>
<strong>Regression ${index + 1}:</strong> ${reg.equation}<br>
<small>R² = ${reg.r2.toFixed(4)} | ${reg.pointCount} points</small>
</div>
<button class="btn btn-red btn-sm" onclick="removeRegressionLine(${index})">
<i class="fas fa-times"></i> Remove
</button>
`;
listDiv.appendChild(item);
});
}
function removeRegressionLine(index) {
regressionLines.splice(index, 1);
updateRegressionList();
regenerateGraph();
}
function clearAllRegressions() {
if (regressionLines.length === 0) return;
if (confirm('Clear all regression lines?')) {
regressionLines = [];
updateRegressionList();
regenerateGraph();
}
}
// Reference Lines
function addReferenceLine() {
const params = getParameters();
const type = document.getElementById('refLineType').value;
const position = parseFloat(document.getElementById('refLinePosition').value);
const style = document.getElementById('refLineStyle').value;
const width = parseFloat(document.getElementById('refLineWidth').value);
const color = document.getElementById('refLineColor').value;
if (isNaN(position)) {
alert('Please enter a valid position');
return;
}
if (type === 'vertical' && (position < 0 || position > params.xTotalDivisions)) {
alert(`Position must be between 0 and ${params.xTotalDivisions}`);
return;
}
if (type === 'horizontal' && (position < 0 || position > params.yTotalDivisions)) {
alert(`Position must be between 0 and ${params.yTotalDivisions}`);
return;
}
referenceLines.push({ type, position, style, width, color });
updateReferenceList();
regenerateGraph();
}
function updateReferenceList() {
const listDiv = document.getElementById('referenceList');
listDiv.innerHTML = '';
referenceLines.forEach((line, index) => {
const item = document.createElement('div');
item.className = 'item-list-item';
item.innerHTML = `
<div>
<strong>${line.type === 'vertical' ? 'Vertical' : 'Horizontal'} Line ${index + 1}:</strong>
Position = ${line.position}<br>
<small>Style: ${line.style} | Width: ${line.width}px | Color: ${line.color}</small>
</div>
<button class="btn btn-red btn-sm" onclick="removeReferenceLine(${index})">
<i class="fas fa-times"></i> Remove
</button>
`;
listDiv.appendChild(item);
});
}
function removeReferenceLine(index) {
referenceLines.splice(index, 1);
updateReferenceList();
regenerateGraph();
}
function clearAllReferenceLines() {
if (referenceLines.length === 0) return;
if (confirm('Clear all reference lines?')) {
referenceLines = [];
updateReferenceList();
regenerateGraph();
}
}
// PDF Report Generation
async function generatePDFReport() {
const experimentName = document.getElementById('experimentName').value;
const studentName = document.getElementById('studentName').value;
const rollNumber = document.getElementById('rollNumber').value;
const department = document.getElementById('department').value;
const subjectCode = document.getElementById('subjectCode').value;
const experimentDate = document.getElementById('experimentDate').value;
if (!experimentName || !studentName || !rollNumber || !department || !experimentDate) {
alert('Please fill all required fields');
return;
}
document.getElementById('loadingOverlay').classList.add('show');
try {
const { jsPDF } = window.jspdf;
const pdf = new jsPDF('p', 'mm', 'a4');
const params = getParameters();
// Page 1: Report Details
pdf.setFont('times', 'bold');
pdf.setFontSize(14);
const splitTitle = pdf.splitTextToSize(experimentName, 170);
pdf.text(splitTitle, 105, 20, { align: 'center' });
let yPos = 20 + (splitTitle.length * 5) + 10;
pdf.setFontSize(10);
pdf.setFont('times', 'normal');
pdf.text(`Name: ${studentName}`, 20, yPos);
yPos += 6;
pdf.text(`Roll Number: ${rollNumber}`, 20, yPos);
yPos += 6;
pdf.text(`Department: ${department}`, 20, yPos);
yPos += 6;
pdf.text(`Subject: ${subjectCode}`, 20, yPos);
yPos += 6;
// Format date as DD-MM-YYYY
const dateObj = new Date(experimentDate);
const formattedExpDate = `${String(dateObj.getDate()).padStart(2, '0')}-${String(dateObj.getMonth() + 1).padStart(2, '0')}-${dateObj.getFullYear()}`;
pdf.text(`Date of Experiment: ${formattedExpDate}`, 20, yPos);
yPos += 6;
const now = new Date();
const dateTime = now.toLocaleString('en-IN', {
timeZone: 'Asia/Kolkata',
year: 'numeric',
month: '2-digit',
day: '2-digit',
hour: '2-digit',
minute: '2-digit',
second: '2-digit',
hour12: false
});
pdf.text(`Report Generated: ${dateTime}`, 20, yPos);
yPos += 12;
// Smallest division
pdf.text('Smallest division along X: ', 20, yPos);
pdf.setFont('times', 'bold');
pdf.text(`${params.xSmallestDiv.toFixed(4)}`, 75, yPos);
yPos += 6;
pdf.setFont('times', 'normal');
pdf.text('Smallest division along Y: ', 20, yPos);
pdf.setFont('times', 'bold');
pdf.text(`${params.ySmallestDiv.toFixed(4)}`, 75, yPos);
yPos += 12;
pdf.setFont('times', 'normal');
// Data table
const tableRows = document.getElementById('dataTable').getElementsByTagName('tbody')[0].rows;
const colWidths = [42.5, 42.5, 42.5, 42.5];
const startX = 20;
pdf.setLineWidth(0.1);
pdf.setFont('times', 'bold');
let xOffset = startX;
pdf.rect(xOffset, yPos - 5, colWidths[0], 7);
pdf.text('X Value', xOffset + colWidths[0]/2, yPos, { align: 'center' });
xOffset += colWidths[0];
pdf.rect(xOffset, yPos - 5, colWidths[1], 7);
pdf.text('Y Value', xOffset + colWidths[1]/2, yPos, { align: 'center' });
xOffset += colWidths[1];
pdf.rect(xOffset, yPos - 5, colWidths[2], 7);
pdf.text('X Division', xOffset + colWidths[2]/2, yPos, { align: 'center' });
xOffset += colWidths[2];
pdf.rect(xOffset, yPos - 5, colWidths[3], 7);
pdf.text('Y Division', xOffset + colWidths[3]/2, yPos, { align: 'center' });
yPos += 7;
pdf.setFont('times', 'normal');
for (let i = 0; i < tableRows.length; i++) {
const xValue = tableRows[i].cells[1].children[0].value;
const yValue = tableRows[i].cells[2].children[0].value;
const xDiv = tableRows[i].cells[3].textContent;
const yDiv = tableRows[i].cells[4].textContent;
if (xValue && yValue && xDiv && yDiv) {
if (yPos > 270) {
pdf.addPage();
yPos = 20;
}
xOffset = startX;
pdf.rect(xOffset, yPos - 5, colWidths[0], 7);
pdf.text(xValue, xOffset + colWidths[0]/2, yPos, { align: 'center' });
xOffset += colWidths[0];
pdf.rect(xOffset, yPos - 5, colWidths[1], 7);
pdf.text(yValue, xOffset + colWidths[1]/2, yPos, { align: 'center' });
xOffset += colWidths[1];
pdf.rect(xOffset, yPos - 5, colWidths[2], 7);
pdf.text(xDiv, xOffset + colWidths[2]/2, yPos, { align: 'center' });
xOffset += colWidths[2];
pdf.rect(xOffset, yPos - 5, colWidths[3], 7);
pdf.text(yDiv, xOffset + colWidths[3]/2, yPos, { align: 'center' });
yPos += 7;
}
}
// Regression lines info
if (regressionLines.length > 0) {
yPos += 8;
if (yPos > 260) {
pdf.addPage();
yPos = 20;
}
pdf.setFont('times', 'bold');
pdf.text('Regression Lines:', 20, yPos);
yPos += 6;
pdf.setFont('times', 'normal');
regressionLines.forEach((reg, index) => {
if (yPos > 270) {
pdf.addPage();
yPos = 20;
}
pdf.text(`${index + 1}. ${reg.equation} | R² = ${reg.r2.toFixed(4)}`, 20, yPos);
yPos += 5;
});
}
// Signature section
yPos += 15;
if (yPos > 250) {
pdf.addPage();
yPos = 20;
}
pdf.setFont('times', 'normal');
pdf.text('Teacher Signature: ___________________________', 20, yPos);
yPos += 10;
pdf.text('Remarks:', 20, yPos);
yPos += 6;
pdf.text('________________________________________________________________________', 20, yPos);
yPos += 6;
pdf.text('________________________________________________________________________', 20, yPos);
yPos += 6;
pdf.text('________________________________________________________________________', 20, yPos);
// Page 2: Graph
pdf.addPage();
const graphDiv = document.getElementById('plotlyGraph');
const graphWidth = params.xTotalDivisions * PIXELS_PER_DIV + 120;
const graphHeight = params.yTotalDivisions * PIXELS_PER_DIV + 100;
const imgData = await Plotly.toImage(graphDiv, {
format: 'jpeg',
width: graphWidth,
height: graphHeight,
scale: 6
});
const pdfWidth = 190;
const pdfHeight = (graphHeight * pdfWidth) / graphWidth;
pdf.addImage(imgData, 'JPEG', 10, 10, pdfWidth, Math.min(pdfHeight, 277));
pdf.save(`${experimentName.replace(/\s+/g, '_')}_report.pdf`);
} catch (error) {
console.error('Error generating PDF:', error);
alert('Error generating PDF. Please try again.');
}
document.getElementById('loadingOverlay').classList.remove('show');
}
// Event listeners for parameter updates
['xBigDivisions', 'xSmallPerBig', 'xMinValue', 'xMaxValue',
'yBigDivisions', 'ySmallPerBig', 'yMinValue', 'yMaxValue'].forEach(id => {
const element = document.getElementById(id);
if (element) {
element.addEventListener('input', updateCalculatedParameters);
}
});
// Initialize on load
window.onload = function() {
updateCalculatedParameters();
};