-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsdeCharts.js
1688 lines (1597 loc) · 66.4 KB
/
sdeCharts.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
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
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
function updateOptions() {
//Recheck available samples to make sure all the correct options are available
/* for (group in sortButtonGroups) {
disableRadioButtons(sortButtonGroups[group], false);
}
dataSheetNames.forEach(sheetName => {
disableRadioButtons(sortButtonGroups[sheetName], false);
completeSheet[sheetName] = true;
})*/
dataSheetNames.forEach(sheetName => {
disableRadioButtons(sortButtonGroups[sheetName], false);
completeSheet[sheetName] = true;
for(dateSampled in selectedSampleMeasurements) {
if (!selectedSampleMeasurements[dateSampled][sheetName]){
disableRadioButtons(sortButtonGroups[sheetName], true);
completeSheet[sheetName] = false;
break;
}
}
})
for (i = 0; i < dataSheetNames.length; i++) {
sheetName = dataSheetNames[i];
sheetsToDisplay[dataSheetNames[i]] = document.getElementById(dataSheetNamesCheckboxes[i]).checked ? true : false; // Check the checkbox state
}
for (i = 0; i < subChartNames.length; i++) {
subName = subChartNames[i];
subsToDisplay[subName] = document.getElementById(subName).checked ? true : false; // Check the checkbox state
}
xAxisSort = document.querySelector('input[name="sorting"]:checked').value;
resuspensionSize = parseFloat(document.getElementById('resuspensionsize').value);
if (isNaN(resuspensionSize)) {
resuspensionSize = 0;
} else {
if (resuspensionSize > 0 ) {
resuspensionSize = resuspensionSize / 1000000;
}
}
}
function updateChart(){
updateOptions();
wrangleData();
//console.log('UPDATECHART*******************');
if (lastInstanceNo > 0) {
const canvas = [];
for (i = 1; i < lastInstanceNo + 1; i++) {
canvas[i] = document.getElementById('chart' + i);
clearCanvasAndChart(canvas[i], i);
}
}
lastInstanceNo = 0;
blankSheets = {};
setBlanksForCharting();
//console.log(sheetsToDisplay);
for (sheetName in sheetsToDisplay) {
if (sheetsToDisplay[sheetName] && chemicalTypeHasData(sheetName)) {
lastInstanceNo = displayCharts(sheetName, lastInstanceNo);
}
}
if (!(radarPlot === "None")) {
retData = dataForCharting(radarPlot);
unitTitle = retData['unitTitle'];
selectedMeas = retData['measChart'];
createRadarPlot(selectedMeas, radarPlot);
}
//console.log('lastInstanceNo ',lastInstanceNo);
//console.log(selectedMeas);
sampleMap(selectedMeas);
filenameDisplay();
}
function displayPsdSplits(sums, sheetName, instanceNo, unitTitle, subTitle) {
// console.log(sums);
createCanvas(instanceNo);
const convas = document.getElementById("chart" + instanceNo);
convas.style.display = "block";
instanceType[instanceNo] = 'PSD splits by ' + subTitle;
instanceSheet[instanceNo] = sheetName;
const allSamples = Object.keys(sums);
const allParticles = Object.keys(sums[allSamples[0]]); // Assuming all samples have the particles
const datasets = allParticles.map((particle, index) => {
const data = allSamples.map(sample => sums[sample][particle]);
return {
label: particle,
data: data,
borderWidth: 1,
yAxisID: 'y',
};
});
displayAnySampleChart(sums, allSamples, datasets, instanceNo, sheetName + ': PSD splits by ' + subTitle, unitTitle + ' by ' + subTitle, true);
y1Title = 'PSD Split by ' + subTitle;
chartInstance[instanceNo].options.plugins.annotation.annotations = {};
chartInstance[instanceNo].options.plugins.legend.display = true;
legends[instanceNo] = true;
// Update the chart
chartInstance[instanceNo].options.scales.x.stacked = true;
chartInstance[instanceNo].options.scales.y.stacked = true;
chartInstance[instanceNo].update();
}
function displayCharts(sheetName, instanceNo) {
// totalAreasAvailable = true;
if(sheetName === 'Physical Data') {
retData = dataForPSDCharting(sheetName);
unitTitle = retData['unitTitle'];
//console.log('unitTitle displayCharts ',unitTitle);
sizes = retData['ptsSizes'];
selectedMeas = retData['measChart'];
fred=selectedMeas;
selectedMeasRelativeArea = retData['measChartRelativeArea'];
selectedMeasArea = retData['measChartArea'];
splitWeights = retData['splitWeights'];
splitRelativeAreas = retData['splitRelativeAreas'];
splitAreas = retData['splitAreas'];
cumWeights = retData['cumWeights'];
cumAreas = retData['cumAreas'];
//console.log(sizes);
//console.log('selectedMeas ', selectedMeas);
instanceNo += 1;
displayPSDChart(sizes, selectedMeas, sheetName, instanceNo, unitTitle, 'Relative Weight');
instanceNo += 1;
displayPSDChart(sizes, selectedMeasRelativeArea, sheetName, instanceNo, unitTitle, 'Relative Area');
instanceNo += 1;
displayPSDChart(sizes, selectedMeasArea, sheetName, instanceNo, unitTitle, 'Absolute Area');
instanceNo += 1;
displayPSDChart(sizes, cumWeights, sheetName, instanceNo, unitTitle, 'Cumlative by Weight');
instanceNo += 1;
displayPSDChart(sizes, cumAreas, sheetName, instanceNo, unitTitle, 'Cumulative by Area');
instanceNo += 1;
displayPsdSplits(splitWeights, sheetName, instanceNo, unitTitle, 'Weight');
instanceNo += 1;
displayPsdSplits(splitRelativeAreas, sheetName, instanceNo, unitTitle, 'Relative Area');
instanceNo += 1;
displayPsdSplits(splitAreas, sheetName, instanceNo, unitTitle, 'Absolute Area');
if (resuspensionSize>0) {
instanceNo += 1;
displayResuspensionFractions(sizes, cumWeights, cumAreas, sheetName, instanceNo, unitTitle, 'Fractions');
}
} else {
retData = dataForCharting(sheetName);
unitTitle = retData['unitTitle'];
//console.log('unitTitle displayCharts ',unitTitle);
selectedMeas = retData['measChart'];
//console.log('dataForCharting - selectedMeas ', selectedMeas);
selectedMeasArea = {};
concentrateMeas = {};
concentrateFactor = {};
// totalAreasAvailable = true;
if (completeSheet['Physical Data']) {
if (resuspensionSize > 0) {
retData= recalculateConcentration(selectedMeas);
concentrateMeas = retData['concentrateMeas'];
concentrateFactor = retData['concentrateFactor'];
}
if (subsToDisplay['relationareadensity']) {
for (chemical in selectedMeas) {
selectedMeasArea[chemical] = {};
for (sample in selectedMeas[chemical]) {
let parts = sample.split(": ");
if (parts.length>2) {
parts[1] = parts[1] + ': ' + parts[2];
}
if (selectedSampleMeasurements?.[parts[0]]?.['Physical Data']?.samples[parts[1]]?.totalArea !== undefined) {
if (selectedSampleMeasurements[parts[0]]['Physical Data'].samples[parts[1]].totalArea > 0) {
totalArea = selectedSampleMeasurements[parts[0]]['Physical Data'].samples[parts[1]].totalArea;
selectedMeasArea[chemical][sample] = selectedMeas[chemical][sample] / totalArea;
}
}
}
}
}
}
if (subsToDisplay['samplegroup']) {
instanceNo += 1;
displaySampleChart(selectedMeas, sheetName, instanceNo, unitTitle);
if (completeSheet['Physical Data']) {
if (resuspensionSize > 0) {
instanceNo += 1;
displaySampleChart(concentrateMeas, sheetName, instanceNo, unitTitle + ' < ' + resuspensionSize * 1000000 + 'µm');
}
if (subsToDisplay['relationareadensity']) {
instanceNo += 1;
displaySampleChart(selectedMeasArea, sheetName, instanceNo, unitTitle + ' / Area');
}
}
}
if (subsToDisplay['chemicalgroup']) {
instanceNo += 1;
displayChemicalChart(selectedMeas, sheetName, instanceNo, unitTitle,true);
if (completeSheet['Physical Data']) {
if (resuspensionSize > 0) {
instanceNo += 1;
displayChemicalChart(concentrateMeas, sheetName, instanceNo, unitTitle + ' < ' + resuspensionSize * 1000000 + 'µm', true);
}
if (subsToDisplay['relationareadensity']) {
instanceNo += 1;
displayChemicalChart(selectedMeas, sheetName, instanceNo, unitTitle + ' / Area', false);
}
}
}
largeInstanceNo = -1;
if (subsToDisplay['positionplace']) {
retData = dataForScatterCharting(sheetName);
unitTitle = retData['unitTitle'];
//console.log('unitTitle displayCharts ',unitTitle);
scatterData = retData['scatterData'];
chemicalData = retData['chemicalData'];
//console.log('dataForScatterCharting scatterData, chemicalData',scatterData,chemicalData);
const allChemicals = Object.keys(chemicalData);
instanceNo += 1;
displayCombinedScatterChart(scatterData, sheetName, instanceNo, 'fred');
largeInstanceNo = instanceNo;
// Step 1: Create a table element
const scatterTable = document.createElement('table');
const noCharts = allChemicals.length;
const startInstanceNo = instanceNo;
// Step 2: Loop to create rows and cells for the table
for (let i = 0; i < noCharts; i++) {
instanceNo += 1;
if (i % 4 === 0) {
// console.log('created row');
row = scatterTable.insertRow(); // Create a row
// console.log(row);
}
const cell = row.insertCell(); // Create a cell
const canvas = document.createElement('canvas'); // Create a canvas for the chart
canvas.id = 'chart' + instanceNo;; // Unique id for each chart canvas
cell.appendChild(canvas); // Append the canvas to the cell
// }
}
// Step 3: Append the table to a container element in your HTML (e.g., <div id="container"></div>)
const chartContainer = document.getElementById('chartContainer');
// first add a div for the buttons
if (largeInstanceNo > 1) {
const divContainer = document.createElement('div');
divContainer.id = 'chartButtons';
chartContainer.appendChild(divContainer);
}
chartContainer.appendChild(scatterTable);
instanceNo = startInstanceNo;
i = 0;
for (const c in chemicalData) {
instanceNo += 1;
//console.log(c,scatterData, chemicalData[c]);
displayScatterChart(scatterData, chemicalData[c], sheetName, instanceNo, c, 'Longitude', 'Latitude', largeInstanceNo);
i += 1;
}
}
// Usage
instanceNo = displayScatterCharts(sheetName,
{ key: 'totalArea', sheetKey: 'Physical Data' },
'relationareadensity',
'Total Area',
'Concentration',
'chartContainer',
instanceNo
);
instanceNo = displayScatterCharts(sheetName,
{ key: 'totalHC', sheetKey: 'PAH data' },
'relationhc',
'Total Hydrocarbon',
'Concentration',
'chartContainer',
instanceNo
);
instanceNo = displayScatterCharts(sheetName,
{ key: 'totalSolids', sheetKey: 'Physical Data' },
'relationtotalsolids',
'Total Solids %',
'Concentration',
'chartContainer',
instanceNo
);
// }
if (sheetName == 'PAH data' && Object.keys(chemInfo).length != 0) {
const chemicalNames = Object.keys(chemInfo);
const properties = Object.keys(chemInfo[chemicalNames[0]]);
for (i = 0; i<14 ; i++) {
// Step 2: Sort the chemical names based on the property (e.g., molWeight)
chemicalNames.sort((a, b) => chemInfo[a][properties[i]] - chemInfo[b][properties[i]]);
// Step 3-6: Iterate through the sorted chemical names and populate selectedMeas
const sortedSelectedMeas = {};
chemicalNames.forEach((chemical) => {
if (selectedMeas[chemical]) {
sortedSelectedMeas[chemical] = selectedMeas[chemical];
}
});
// console.log(sortedSelectedMeas);
instanceNo += 1;
displaySampleChart(sortedSelectedMeas, sheetName + ': Sorted by ' + properties[i], instanceNo, unitTitle);
instanceNo += 1;
displayChemicalChart(sortedSelectedMeas, sheetName + ': Sorted by ' + properties[i], instanceNo, unitTitle);
}
}
if (sheetName === 'PAH data' && subsToDisplay['gorhamtest']) {
unitTitle = retData['unitTitle'];
selectedSums = sumsForGorhamCharting();
//console.log('sumsForGorhamCharting selectedSums',selectedSums);
//console.log(selectedSums);
instanceNo += 1;
displayGorhamTest(selectedSums, sheetName, instanceNo, unitTitle);
if (resuspensionSize > 0 && completeSheet['Physical Data']) {
retData = recalculateConcentrationComplex(selectedSums);
concentrateSums = retData['concentrateMeas'];
concentrateFactor = retData['concentrateFactor'];
instanceNo += 1;
displayGorhamTest(concentrateSums, sheetName, instanceNo, unitTitle + ' < ' + resuspensionSize * 1000000 + 'µm');
}
// retData = null;
}
if (sheetName === 'PAH data' && subsToDisplay['totalhc']) {
instanceNo += 1;
retData = sumsForTotalHCCharting();
unitTitle = retData['unitTitle'];
selectedSums = retData['measChart'];
//console.log('sumsForTotalHCCharting ',selectedSums);
//console.log(Object.keys(selectedSums));
displayTotalHC(selectedSums, sheetName, instanceNo, unitTitle);
}
if (sheetName === 'PAH data' && subsToDisplay['pahratios']) {
instanceNo += 1;
retData = ratiosForPAHs();
unitTitle = retData['unitTitle'];
selectedSums = retData['measChart'];
displayPAHRatios(selectedSums, sheetName, instanceNo, unitTitle);
}
if (sheetName === 'PAH data' && subsToDisplay['ringfractions']) {
instanceNo += 1;
retData = ringFractionsForPAHs();
unitTitle = retData['unitTitle'];
selectedSums = retData['measChart'];
displayRingFractions(selectedSums, sheetName, instanceNo, unitTitle);
}
if (sheetName === 'PAH data' && subsToDisplay['eparatios']) {
instanceNo += 1;
retData = epaRatiosForPAHs();
unitTitle = retData['unitTitle'];
selectedSums = retData['measChart'];
displayEpaRatios(selectedSums, sheetName, instanceNo, unitTitle);
}
if (sheetName === 'PAH data' && subsToDisplay['simpleratios']) {
instanceNo += 1;
retData = simpleRatiosForPAHs();
unitTitle = retData['unitTitle'];
selectedSums = retData['measChart'];
displaySimpleRatios(selectedSums, sheetName, instanceNo, unitTitle);
}
if (sheetName === 'PCB data' && subsToDisplay['congenertest']) {
instanceNo += 1;
selectedSums = sumsForCongenerCharting();
displayCongener(selectedSums, sheetName, instanceNo, unitTitle);
if (resuspensionSize > 0 && completeSheet['Physical Data']) {
retData = recalculateConcentrationComplex(selectedSums);
concentrateSums = retData['concentrateMeas'];
concentrateFactor = retData['concentrateFactor'];
instanceNo += 1;
displayCongener(concentrateSums, sheetName, instanceNo, unitTitle + ' < ' + resuspensionSize * 1000000 + 'µm');
}
}
}
// Display the canvas
//console.log('Display the canvas ',instanceNo);
return instanceNo
}
function displayScatterCharts(sheetName, chartType, subsKey, xAxisLabel, yAxisLabel, containerId, instanceNo) {
//console.log('Sheet Name ',sheetName, 'chartType ', chartType, 'subsKey', subsKey, 'xAxisLael', xAxisLabel, 'yAxisLabel', yAxisLabel, 'containerId', containerId, 'instanceNo', instanceNo);
if (subsToDisplay[subsKey] && completeSheet[chartType.sheetKey]) {
// Fetch chart data
const retData = dataForTotalScatterCharting(sheetName, chartType.key);
const { unitTitle, scatterData, chemicalData, fitConcentration, fitPredictors } = retData;
//console.log('dataForTotalScatterCharting scatterData, chemicalData ',scatterData, chemicalData);
if (unitTitle === 'No data') {
return instanceNo
}
const allChemicals = Object.keys(chemicalData);
instanceNo += 1;
displayCombinedScatterChart(scatterData, sheetName, instanceNo, 'fred');
largeInstanceNo = instanceNo;
// Create a table element
const scatterTable = document.createElement('table');
const noCharts = allChemicals.length;
const startInstanceNo = instanceNo;
for (let i = 0; i < noCharts; i++) {
instanceNo += 1;
if (i % 4 === 0) scatterTable.insertRow();
const cell = scatterTable.rows[scatterTable.rows.length - 1].insertCell();
const canvas = document.createElement('canvas');
canvas.id = `chart${instanceNo}`;
cell.appendChild(canvas);
}
// Append table and buttons to the container
const chartContainer = document.getElementById(containerId);
if (largeInstanceNo > 1) {
const divContainer = document.createElement('div');
divContainer.id = 'chartButtons';
chartContainer.appendChild(divContainer);
}
chartContainer.appendChild(scatterTable);
instanceNo = startInstanceNo;
for (const c in chemicalData) {
const data = fitConcentration ?
concentrationFitter(fitConcentration[c], fitPredictors[c], 'Chart Analysis') :
{ beta: 0, R_squared: 0 }; // Default values for charts without fitting
instanceNo += 1;
displayScatterChart(
scatterData[c],
chemicalData[c],
sheetName,
instanceNo,
`${c} : ${data.R_squared.toFixed(4)}`,
xAxisLabel,
yAxisLabel,
largeInstanceNo
);
}
}
return instanceNo
}
function setBlanksForCharting() {
let datesSampled = Object.keys(selectedSampleMeasurements);
// Have to deal with samples without measurements set everything to zero
for (const ds in selectedSampleMeasurements) {
for (const ct in selectedSampleMeasurements[ds]) {
if (blankSheets[ct] == null || blankSheets[ct] == undefined) {
if (!(selectedSampleMeasurements[ds][ct] == undefined || selectedSampleMeasurements[ds][ct] == null)) {
blankSheets[ct] = selectedSampleMeasurements[ds][ct];
}
}
}
}
return
}
// Function to interpolate between two colors based on the value
function colorGradient(value, color1, color2) {
// Ensure the value is between 0 and 1
value = Math.max(0, Math.min(1, value));
// Convert colors from hex to RGB
const color1RGB = hexToRgb(color1);
const color2RGB = hexToRgb(color2);
// Calculate the interpolated color
const r = Math.round(color1RGB.r + value * (color2RGB.r - color1RGB.r));
const g = Math.round(color1RGB.g + value * (color2RGB.g - color1RGB.g));
const b = Math.round(color1RGB.b + value * (color2RGB.b - color1RGB.b));
// Return the color in 'rgb(r,g,b)' format
return `rgb(${r},${g},${b},0.5)`;
}
// Helper function to convert hex color to RGB
function hexToRgb(hex) {
// Remove the hash symbol if present
hex = hex.replace(/^#/, '');
// Parse the red, green, and blue components
const bigint = parseInt(hex, 16);
return {
r: (bigint >> 16) & 255,
g: (bigint >> 8) & 255,
b: bigint & 255
};
}
function resizeChart(chart) {
// Get the parent container of the chart
var container = chart.canvas.parentNode;
// Toggle the class to resize the chart
container.classList.toggle('large-chart');
// Update the chart to reflect the new size
chart.resize();
}
function displayCombinedScatterChart(meas, sheetName, instanceNo, unitTitle) {
legends[instanceNo] = true;
ylinlog[instanceNo] = false;
stacked[instanceNo] = false;
createCanvas(instanceNo);
const convas = document.getElementById("chart" + instanceNo);
convas.style.display = "block";
instanceType[instanceNo] = 'combinedscatter';
instanceSheet[instanceNo] = sheetName;
//console.log(meas);
//console.log(meas, sheetName, instanceNo, unitTitle);
const allChemicals = Object.keys(meas);
const allSamples = Object.keys(meas[allChemicals[0]]); // Assuming all samples have the same chemicals // Using the first concentration value for simplicity
const datasets = allChemicals.map((chemical, index) => {
const data = meas[chemical];
return {
label: chemical,
data: data,
borderWidth: 1,
yAxisID: 'y',
};
});
//console.log(datasets);
const chartConfig = {
type: 'scatter',
data:
{
datasets: datasets
}
};
const ctx = document.getElementById('chart' + instanceNo).getContext('2d');
//console.log(instanceNo,ctx,chartConfig);
chartInstance[instanceNo] = new Chart(ctx, chartConfig);
createResetZoomButton(chartInstance[instanceNo], instanceNo);
createToggleLegendButton(chartInstance[instanceNo], instanceNo);
createToggleLinLogButton(chartInstance[instanceNo], instanceNo);
createStackedButton(chartInstance[instanceNo], instanceNo);
createExportButton(chartInstance[instanceNo], instanceNo);
// console.log(ddatasets);
}
function displayScatterChart(scatterData, oneChemical, sheetName, instanceNo, unitTitle, xAxisTitle, yAxisTitle, largeInstanceNo) {
//console.log(scatterData, oneChemical, sheetName, instanceNo, unitTitle, xAxisTitle, yAxisTitle, largeInstanceNo);
lastScatterInstanceNo = instanceNo;
legends[instanceNo] = false;
ylinlog[instanceNo] = false;
stacked[instanceNo] = false;
const convas = document.getElementById("chart" + instanceNo);
convas.style.display = "block";
instanceType[instanceNo] = 'Scatter ' + unitTitle;
instanceSheet[instanceNo] = sheetName;
const color1 = '#00ff00'; // Start of gradient (red)
const color2 = '#ff0000'; // End of gradient (green)
allSamples = Object.keys(oneChemical);
allConcs = Object.values(oneChemical);
//console.log(allConcs);
minConc = Math.min(...allConcs);
maxConc = Math.max(...allConcs);
//console.log(minConc,maxConc);
//console.log(oneChemical);
// oneChemical = (oneChemical - minConc) / (maxConc - minConc);
scaledChemical = {};
for (s in oneChemical) {
scaledChemical[s] = (oneChemical[s] - minConc) / (maxConc - minConc);
//console.log(oneChemical[s]);
}
//console.log(oneChemical);
// Chart configuration
const chartConfig = {
type: 'scatter',
data: {
datasets: [{
data: scatterData,
backgroundColor:
allSamples.map(sample => colorGradient(scaledChemical[sample], color1, color2)),
borderColor:
allSamples.map(sample => colorGradient(scaledChemical[sample], color1, color2)),
pointRadius: function(context) {
return convas.width / 70
}
}]
},
options: {
plugins: {
repsonsive: true,
title: {
display: true,
text: unitTitle,
onEvent: function() {
console.log('this is fed');
resizeChart(chartInstance[instanceNo]);
}
},
subtitle: {
display: true,
text: 'Min: ' + minConc + ' Max: ' + maxConc
},
legend: {
display: false,
position: 'bottom',
labels: {
font: { // Customize legend label font
size: 14,
weight: 'italic',
padding: 10
}
}
},
// Add a custom plugin for interactivity
selectSample: {
highlightedSample: null,
},
},
scales: {
x: {
position: 'bottom',
title: {
display: true,
text: xAxisTitle
}
},
y: {
title: {
display: true,
text: yAxisTitle
}
}
},
}
};
//console.log(chartConfig);
const ctx = document.getElementById('chart' + instanceNo).getContext('2d');
if (chartInstance[instanceNo]) {
chartInstance[instanceNo].destroy();
}
chartInstance[instanceNo] = new Chart(ctx, chartConfig);
// createToggleCanvasSize(convas, chartInstance[instanceNo], instanceNo, unitTitle);
//console.log(largeInstanceNo,oneChemical);
/* if (largeInstanceNo > 1) {
createToggleFocusChart(convas, chartInstance[instanceNo], instanceNo, oneChemical, scatterData, sheetName, unitTitle, xAxisTitle, yAxisTitle, largeInstanceNo);
}*/
document.getElementById('chart' + instanceNo).addEventListener('click', () => displayScatterChart(scatterData, oneChemical, sheetName, largeInstanceNo, unitTitle, xAxisTitle, yAxisTitle, -1));
Chart.register({
id: 'selectSample',
afterDraw: function (chart, args, options) {
const highlightedSample = chart.options.plugins.selectSample.highlightedSample;
if (highlightedSample) {
//console.log('highlightedSample ', highlightedSample);
const datasetIndex = chart.data.datasets.findIndex(dataset => dataset.label === highlightedSample);
if (datasetIndex !== -1) {
const dataset = chart.data.datasets[datasetIndex];
dataset.borderWidth = 4;
dataset.borderColor = 'red';
}
}
},
});
}
function displayPSDChart(sizes, meas, sheetName, instanceNo, unitTitle, subTitle) {
//console.log(sizes, meas, sheetName, instanceNo, unitTitle, subTitle);
legends[instanceNo] = false;
ylinlog[instanceNo] = false;
stacked[instanceNo] = false;
createCanvas(instanceNo);
const convas = document.getElementById("chart" + instanceNo);
convas.style.display = "block";
instanceType[instanceNo] = 'PSD ' + subTitle;
instanceSheet[instanceNo] = sheetName;
// Extract sample names from the PSD data structure
const sampleNames = Object.keys(meas);
// Create datasets for each sample
const datasets = sampleNames.map((sampleName, index) => {
return {
label: sampleName,
data: meas[sampleName],
borderWidth: 2,
fill: false,
};
});
// console.log('sizes: ',sizes);
// console.log('datasets: ',datasets)
// Chart configuration
const chartConfig = {
type: 'line',
data: {
labels: sizes,
datasets: datasets,
},
options: {
plugins: {
title: {
display: true,
text: sheetName + ': PSD by ' + subTitle
},
legend: {
display: false,
position: 'bottom',
labels: {
font: { // Customize legend label font
size: 14,
weight: 'italic',
padding: 10
}
}
},
// Add a custom plugin for interactivity
selectSample: {
highlightedSample: null,
},
},
scales: {
x: {
type: 'logarithmic',
position: 'bottom',
title: {
display: true,
text: 'm'
}
},
y: {
beginAtZero: true,
title: {
display: true,
text: unitTitle + ' by ' + subTitle
}
}
},
autocolors: {
mode: 'label'
}
}
};
const ctx = document.getElementById('chart' + instanceNo).getContext('2d');
chartInstance[instanceNo] = new Chart(ctx, chartConfig);
createResetZoomButton(chartInstance[instanceNo], instanceNo);
createToggleLegendButton(chartInstance[instanceNo], instanceNo);
createToggleLinLogButton(chartInstance[instanceNo], instanceNo);
createStackedButton(chartInstance[instanceNo], instanceNo);
createExportButton(chartInstance[instanceNo], instanceNo);
Chart.register({
id: 'selectSample',
afterDraw: function (chart, args, options) {
const highlightedSample = chart.options.plugins.selectSample.highlightedSample;
if (highlightedSample) {
//console.log('highlightedSample ', highlightedSample);
const datasetIndex = chart.data.datasets.findIndex(dataset => dataset.label === highlightedSample);
if (datasetIndex !== -1) {
const dataset = chart.data.datasets[datasetIndex];
dataset.borderWidth = 4;
dataset.borderColor = 'red';
}
}
},
});
}
// Function to generate a random color
function getRandomColor() {
const letters = '0123456789ABCDEF';
let color = '#';
for (let i = 0; i < 6; i++) {
color += letters[Math.floor(Math.random() * 16)];
}
return color;
}
function displayPSDHighlight(meas, instanceNo, clickedMapSample) {
legends[instanceNo] = false;
ylinlog[instanceNo] = false;
stacked[instanceNo] = false;
clickedSamples = findSamplesInSameLocation(clickedMapSample);
const allChemicals = Object.keys(meas);
let clickedIndexes = [];
clickedSamples.forEach (clickedSample => {
index = -1;
for (const sample in meas[allChemicals[0]]) {
index += 1;
if (sample.includes(clickedSample)) {
clickedIndexes.push(index);
}
}
});
clickedIndexes.forEach(item => {
//console.log('displayPSDHighlight',clickedIndexes);
//console.log('item ',item);
chartInstance[instanceNo].options.plugins.selectSample.highlightedSample = item;
});
// Update the chart
chartInstance[instanceNo].update();
}
// Helper function to remove highlighting
function removePSDHighlight() {
chartInstance.options.plugins.selectSample.highlightedSample = null;
chartInstance.update();
}
function displaySampleChart(meas, sheetName, instanceNo, unitTitle) {
//console.log(meas, sheetName, instanceNo, unitTitle);
createCanvas(instanceNo);
const convas = document.getElementById("chart" + instanceNo);
convas.style.display = "block";
instanceType[instanceNo] = 'chemical';
instanceSheet[instanceNo] = sheetName;
//console.log(meas);
const allChemicals = Object.keys(meas);
const allSamples = Object.keys(meas[allChemicals[0]]); // Assuming all samples have the same chemicals
const datasets = allChemicals.map((chemical, index) => {
const data = allSamples.map(sample => meas[chemical][sample]); // Using the first concentration value for simplicity
return {
label: chemical,
data: data,
borderWidth: 1,
yAxisID: 'y',
};
});
displayAnySampleChart(meas, allSamples,datasets,instanceNo,sheetName,unitTitle,false);
}
function highlightMapLocation(clickedIndex) {
console.log(clickedIndex);
return
}
function isEmpty(obj) {
for (const prop in obj) {
if (Object.hasOwn(obj, prop)) {
return false;
}
}
return true;
}
function createRadarPlot(meas, sheetName) {
if (!isEmpty(popupInstance)) {
allPopupKeys = Object.keys(popupInstance);
allPopupKeys.forEach(popupKey => {
popupInstance[popupKey].destroy();
});
popupInstance = [];
}
const allChemicals = Object.keys(meas);
const allSamples = Object.keys(meas[allChemicals[0]]); // Assuming all samples have the same chemicals
let data = {};
const datasets = allSamples.map((sample, index) => {
data[sample] = allChemicals.map(chemical => meas[chemical][sample]); // Using the first concentration value for simplicity
});
console.log("datasets ",datasets);
const chartsForMapContainer = document.getElementById('chartsForMapContainer');
for (sample in data) {
const divId = `radar_${sample}`;
const divContainer = document.createElement('div');
divContainer.id = divId;
divContainer.style = "width:250px; height:300px;"
chartsForMapContainer.appendChild(divContainer);
const canvas = document.createElement('canvas');
canvas.id = `c_${divId}`; // Unique chart ID
canvas.style = "width:250px; height:300px;"
divContainer.appendChild(canvas); // Append the canvas to the container
const ctx = document.getElementById(canvas.id);
popupInstance[sample] = new Chart(ctx, {
type: 'radar',
data: {
labels: allChemicals,
datasets: [{
label: sample,
data: data[sample]
}]
},
options: {
scales: {
r: {
pointLabels: {
display: false //Hides the labels around the radar chart
}
},
}
}
});
}
}
function displayAnySampleChart(meas, all, datasets, instanceNo, title, yTitle, showLegend) {
let readableLabels = [];
for (i = 0; i < all.length; i++) {
let parts = all[i].split(": ");
if (parts.length>2) {
parts[1] = parts[1] + ': ' + parts[2];
}
//console.log(parts[0],parts[1]);
readableLabels[i] = selectedSampleInfo[parts[0]].label + ': ' + selectedSampleInfo[parts[0]].position[parts[1]].label;
}
//console.log(readableLabels,datasets);
displayAnyChart(meas, readableLabels, datasets, instanceNo, title, yTitle, showLegend);
}
function displayAnyChart(meas, all, datasets, instanceNo, title, yTitle, showLegend) {
legends[instanceNo] = showLegend;
ylinlog[instanceNo] = false;
stacked[instanceNo] = false;
const ctx = document.getElementById('chart' + instanceNo);
//console.log(sheetName, instanceNo);
stanGraph = {
type: 'bar',
data: {
labels: all,
datasets: datasets
},
options: {
interaction: {
mode: 'index',
axis: 'xy'
},
plugins: {
title: {
display: true,
text: title
},
legend: {
display: showLegend,
position: 'top',
labels: {
font: { // Customize legend label font
size: 14,
weight: 'italic',
padding: 10
}
}
},
zoom: {
pan: {
// pan options and/or events
enabled: true,
mode: 'xy',
modifierKey: 'shift',
},
limits: {
y: { min: 0 }
// axis limits
},
zoom: {
// zoom options and/or events
wheel: {
enabled: true,
},
drag: {
enabled: true,
},
mode: 'xy'
}
},
},
indexAxis: 'x',
scales: {
x: {
beginAtZero: true,
ticks: {
maxRotation: 90,
minRotation: 90,
autoSkip: false,
},
},
y: {
beginAtZero: true,
title: {
display: true,
text: yTitle,
position: 'left',
},
},
},
autocolors: {
mode: 'label'
}
}
};
if (title.includes('hydrocarbon')) {
stanGraph.options.scales.y1 = {
beginAtZero: true,
position: 'right',
title: {
display: true,
text: 'Total PAH content (mg/kg)',
position: 'right',
}
};
//console.log(stanGraph);
};
if (title.includes('factor')) {
stanGraph.options.scales.y1 = {
beginAtZero: true,
position: 'right',