forked from parse-community/parse-dashboard
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGraphDataUtils.js
More file actions
919 lines (797 loc) · 29.9 KB
/
GraphDataUtils.js
File metadata and controls
919 lines (797 loc) · 29.9 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
910
911
912
913
914
915
916
917
918
919
/*
* Copyright (c) 2016-present, Parse, LLC
* All rights reserved.
*
* This source code is licensed under the license found in the LICENSE file in
* the root directory of this source tree.
*/
import { evaluateFormula, buildVariables } from './FormulaEvaluator';
/**
* Utility functions for processing Parse data into chart-compatible formats
*/
/**
* Get nested value from object using dot notation path
* @param {Object} obj - The object to extract value from
* @param {string} path - Dot notation path (e.g., 'user.name')
* @returns {*} The value at the path
*/
export function getNestedValue(obj, path) {
if (!path || !obj) {return null;}
// Handle Parse object attributes vs raw object
const data = obj.attributes || obj;
const result = path.split('.').reduce((current, key) => {
if (current && typeof current === 'object') {
return current[key];
}
return null;
}, data);
// Convert undefined to null for consistency
return result === undefined ? null : result;
}
/**
* Check if a value is numeric
* @param {*} value - Value to check
* @returns {boolean} True if numeric
*/
export function isNumeric(value) {
return typeof value === 'number' && !isNaN(value);
}
/**
* Extract a numeric value from various types (number, pointer objectId, etc.)
* For pointers, we hash the objectId to get a consistent numeric value
* @param {*} value - Value to extract number from
* @returns {number|null} Numeric value or null
*/
export function extractNumericValue(value) {
// Direct numeric value
if (isNumeric(value)) {
return value;
}
// Handle Parse Pointer objects - use objectId as string for counting
if (value && typeof value === 'object') {
// Parse pointer has objectId or id
const id = value.objectId || value.id;
if (id) {
// For counting purposes, we'll use a hash of the objectId
// This allows us to count unique pointers
return simpleHash(String(id));
}
}
return null;
}
/**
* Simple hash function to convert strings to numbers
* @param {string} str - String to hash
* @returns {number} Hash value
*/
function simpleHash(str) {
let hash = 0;
for (let i = 0; i < str.length; i++) {
const char = str.charCodeAt(i);
hash = ((hash << 5) - hash) + char;
hash = hash & hash; // Convert to 32-bit integer
}
return Math.abs(hash);
}
/**
* Check if a value is a valid date
* @param {*} value - Value to check
* @returns {boolean} True if valid date
*/
export function isValidDate(value) {
return value instanceof Date && !isNaN(value.getTime());
}
/**
* Convert various date formats to a standard format
* @param {*} value - Date value to convert
* @returns {Date|null} Converted date or null
*/
export function normalizeDate(value) {
if (!value) {return null;}
// Handle Parse Date objects
if (value && value.iso) {
return new Date(value.iso);
}
// Handle Date objects
if (value instanceof Date) {
return value;
}
// Handle string dates
if (typeof value === 'string') {
const date = new Date(value);
return isNaN(date.getTime()) ? null : date;
}
return null;
}
/**
* Format a date in compact format: YYYY-MM-DD HH:mm
* @param {Date} date - Date to format
* @returns {string} Formatted date string
*/
export function formatDateCompact(date) {
if (!date || !(date instanceof Date) || isNaN(date.getTime())) {
return '';
}
const year = date.getUTCFullYear();
const month = String(date.getUTCMonth() + 1).padStart(2, '0');
const day = String(date.getUTCDate()).padStart(2, '0');
const hours = String(date.getUTCHours()).padStart(2, '0');
const minutes = String(date.getUTCMinutes()).padStart(2, '0');
return `${year}-${month}-${day} ${hours}:${minutes}`;
}
/**
* Filter data based on column type and chart requirements
* @param {Array} data - Array of Parse objects
* @param {string} column - Column name to filter by
* @param {string} requiredType - Required data type ('number', 'string', 'date')
* @returns {Array} Filtered data
*/
export function filterDataByType(data, column, requiredType) {
return data.filter(item => {
const value = getNestedValue(item, column);
switch (requiredType) {
case 'number':
return isNumeric(value);
case 'string':
return typeof value === 'string' && value.trim().length > 0;
case 'date':
return normalizeDate(value) !== null;
default:
return value != null;
}
});
}
/**
* Aggregate values based on aggregation type
* @param {Array<number>} values - Array of numeric values
* @param {string} aggregationType - Type of aggregation ('count', 'sum', 'avg', 'min', 'max')
* @returns {number} Aggregated value
*/
export function aggregateValues(values, aggregationType = 'count') {
if (!Array.isArray(values) || values.length === 0) {
return 0;
}
// Filter out non-numeric values
const numericValues = values.filter(val => isNumeric(val));
if (numericValues.length === 0) {
return 0;
}
switch (aggregationType) {
case 'sum':
return numericValues.reduce((sum, val) => sum + val, 0);
case 'avg':
case 'mean':
return numericValues.reduce((sum, val) => sum + val, 0) / numericValues.length;
case 'min':
return Math.min(...numericValues);
case 'max':
return Math.max(...numericValues);
case 'count':
default:
return numericValues.length;
}
}
/**
* Calculate a value based on operator and fields
* @param {Object} item - Data item
* @param {Array<string>} fields - Fields to use in calculation
* @param {string} operator - Calculation operator (sum, percent, average, difference, ratio, formula)
* @param {string} formula - Formula string (only used when operator is 'formula')
* @param {Array<string>} availableFields - All available field names for formula evaluation
* @returns {number|null} Calculated value or null
*/
function calculateValue(item, fields, operator, formula = null, availableFields = []) {
// Handle formula operator separately
if (operator === 'formula') {
if (!formula || typeof formula !== 'string' || formula.trim() === '') {
return null;
}
// Build variables from all available fields
const fieldValues = {};
for (const field of availableFields) {
const rawValue = getNestedValue(item, field);
const numericValue = extractNumericValue(rawValue);
if (field) {
fieldValues[field] = numericValue !== null ? numericValue : 0;
}
}
// Add item attributes directly (for previous calculated values)
const data = item.attributes || item;
for (const key of Object.keys(data)) {
if (key && !(key in fieldValues)) {
const value = data[key];
if (typeof value === 'number' && isFinite(value)) {
fieldValues[key] = value;
}
}
}
// Build variables with both plain and $-prefixed versions
return evaluateFormula(formula, buildVariables(fieldValues));
}
// Standard operators require fields
if (!fields || fields.length === 0) {
return null;
}
// Extract numeric values from all fields
const values = fields
.map(field => {
const rawValue = getNestedValue(item, field);
return extractNumericValue(rawValue);
})
.filter(val => val !== null);
if (values.length === 0) {
return null;
}
switch (operator) {
case 'sum':
return values.reduce((acc, val) => acc + val, 0);
case 'average':
return values.reduce((acc, val) => acc + val, 0) / values.length;
case 'difference':
// Subtract all subsequent values from the first
return values.reduce((acc, val, index) => index === 0 ? val : acc - val, 0);
case 'ratio': {
// Divide first value by second (or product of all others)
if (values.length < 2) {return null;}
const denominator = values.slice(1).reduce((acc, val) => acc * val, 1);
return denominator !== 0 ? values[0] / denominator : null;
}
case 'percent': {
// Calculate percentage: (numerator / denominator) * 100
if (values.length < 2) {return null;}
const numerator = values[0];
const denominator = values[1];
return denominator !== 0 ? (numerator / denominator) * 100 : null;
}
default:
return null;
}
}
/**
* Create a composite group key from multiple group-by columns
* @param {Object} item - Data item
* @param {string|Array<string>} groupByColumns - Column(s) to group by
* @returns {string} Composite group key
*/
function createGroupKey(item, groupByColumns) {
const columns = Array.isArray(groupByColumns) ? groupByColumns : [groupByColumns];
const parts = columns.map(col => {
const rawValue = getNestedValue(item, col);
// Handle pointer objects - use objectId
if (rawValue && typeof rawValue === 'object') {
return rawValue.objectId || rawValue.id || 'Other';
}
return rawValue != null ? String(rawValue) : 'Other';
});
return parts.join(' | ');
}
/**
* Group data by a column and apply aggregation
* @param {Array} data - Array of Parse objects
* @param {string|Array<string>} groupByColumn - Column(s) to group by
* @param {string} valueColumn - Column to aggregate
* @param {string} aggregationType - Aggregation type
* @returns {Object} Grouped and aggregated data
*/
export function groupAndAggregate(data, groupByColumn, valueColumn, aggregationType = 'count') {
const groups = {};
data.forEach(item => {
const rawValue = getNestedValue(item, valueColumn);
const value = extractNumericValue(rawValue);
// Create composite group key from potentially multiple columns
const groupKey = createGroupKey(item, groupByColumn);
if (!groups[groupKey]) {
groups[groupKey] = [];
}
// Only add numeric values for aggregation
if (value !== null) {
groups[groupKey].push(value);
}
});
// Apply aggregation to each group
const result = {};
Object.keys(groups).forEach(groupKey => {
result[groupKey] = aggregateValues(groups[groupKey], aggregationType);
});
return result;
}
/**
* Process data for scatter plots
* @param {Array} data - Array of Parse objects
* @param {string} xColumn - X-axis column
* @param {string} yColumn - Y-axis column
* @param {number} maxPoints - Maximum number of points to include
* @returns {Object} Chart.js compatible data
*/
export function processScatterData(data, xColumn, yColumn, maxPoints = 1000) {
if (!xColumn || !yColumn || !Array.isArray(data)) {
return null;
}
const points = data
.filter(item => {
const xVal = getNestedValue(item, xColumn);
const yVal = getNestedValue(item, yColumn);
return isNumeric(xVal) && isNumeric(yVal);
})
.slice(0, maxPoints)
.map(item => ({
x: getNestedValue(item, xColumn),
y: getNestedValue(item, yColumn),
}));
if (points.length === 0) {
return null;
}
return {
datasets: [{
label: `${xColumn} vs ${yColumn}`,
data: points,
backgroundColor: 'rgba(54, 162, 235, 0.6)',
borderColor: 'rgba(54, 162, 235, 1)',
borderWidth: 1,
pointRadius: 4,
pointHoverRadius: 6,
}],
};
}
/**
* Process data for pie/doughnut charts
* @param {Array} data - Array of Parse objects
* @param {string|Array<string>} valueColumn - Value column(s) for aggregation
* @param {string|Array<string>} groupByColumn - Column(s) to group by (optional)
* @param {string} aggregationType - Aggregation type
* @param {Array} calculatedValues - Calculated value definitions (optional)
* @returns {Object} Chart.js compatible data
*/
export function processPieData(data, valueColumn, groupByColumn, aggregationType = 'count', calculatedValues = null) {
if (!Array.isArray(data)) {
return null;
}
// Convert single valueColumn to array for uniform handling
const valueColumns = Array.isArray(valueColumn) ? valueColumn : (valueColumn ? [valueColumn] : []);
const hasCalculatedValues = calculatedValues && Array.isArray(calculatedValues) && calculatedValues.length > 0;
// Must have at least one value column or calculated value
if (valueColumns.length === 0 && !hasCalculatedValues) {
return null;
}
let aggregatedData = {};
if (groupByColumn) {
// Group by column and aggregate for each value column
valueColumns.forEach(valCol => {
const columnData = groupAndAggregate(data, groupByColumn, valCol, aggregationType);
// Prefix keys with column name if multiple columns
if (valueColumns.length > 1) {
Object.keys(columnData).forEach(key => {
aggregatedData[`${valCol} (${key})`] = columnData[key];
});
} else {
aggregatedData = { ...aggregatedData, ...columnData };
}
});
} else {
// Aggregate each value column separately
valueColumns.forEach(valCol => {
const values = data
.map(item => {
const rawValue = getNestedValue(item, valCol);
return extractNumericValue(rawValue);
})
.filter(val => val !== null);
aggregatedData[valCol] = aggregateValues(values, aggregationType);
});
}
// Process calculated values - with support for referencing other calculated values
if (calculatedValues && Array.isArray(calculatedValues)) {
// First, we need to compute all calculated values for each row
const rowsWithCalcValues = data.map(item => {
const calculatedValuesForRow = {};
calculatedValues.forEach(calc => {
// Formula operator doesn't require fields, other operators do
const hasRequiredConfig = calc.operator === 'formula'
? (calc.formula && calc.name)
: (calc.fields && calc.fields.length > 0 && calc.name);
if (hasRequiredConfig) {
// Create an enhanced item that includes previously calculated values
const enhancedItem = { ...item };
if (item.attributes) {
enhancedItem.attributes = { ...item.attributes, ...calculatedValuesForRow };
} else {
Object.assign(enhancedItem, calculatedValuesForRow);
}
// Build available fields for formula evaluation
const availableFields = [...valueColumns];
// Add previously calculated value names
Object.keys(calculatedValuesForRow).forEach(name => {
if (!availableFields.includes(name)) {
availableFields.push(name);
}
});
const calcValue = calculateValue(enhancedItem, calc.fields, calc.operator, calc.formula, availableFields);
calculatedValuesForRow[calc.name] = calcValue;
}
});
return { item, calculatedValues: calculatedValuesForRow };
});
// Now process each calculated value with grouping
calculatedValues.forEach(calc => {
// Formula operator doesn't require fields, other operators do
const hasRequiredConfig = calc.operator === 'formula'
? (calc.formula && calc.name)
: (calc.fields && calc.fields.length > 0 && calc.name);
if (hasRequiredConfig) {
if (groupByColumn) {
// Group calculated values by the same groupByColumn
const groups = {};
rowsWithCalcValues.forEach(({ item, calculatedValues: calcVals }) => {
const calcValue = calcVals[calc.name];
if (calcValue !== null) {
const groupKey = createGroupKey(item, groupByColumn);
if (!groups[groupKey]) {
groups[groupKey] = [];
}
groups[groupKey].push(calcValue);
}
});
// Aggregate each group
Object.keys(groups).forEach(groupKey => {
const labelKey = valueColumns.length > 1 || calculatedValues.length > 1
? `${calc.name} (${groupKey})`
: groupKey;
// For ratio-based operators (percent, ratio, formula), average the results
// For other operators, sum the results
let aggType = 'sum';
if (calc.operator === 'percent' || calc.operator === 'ratio' || calc.operator === 'formula') {
aggType = 'avg';
} else if (calc.operator === 'average') {
aggType = 'avg';
}
aggregatedData[labelKey] = aggregateValues(groups[groupKey], aggType);
});
} else {
// No grouping - aggregate all calculated values together
const calcValues = rowsWithCalcValues
.map(({ calculatedValues: calcVals }) => calcVals[calc.name])
.filter(val => val !== null);
if (calcValues.length > 0) {
// For ratio-based operators (percent, ratio, formula), average the results
// For other operators, sum the results
let aggType = 'sum';
if (calc.operator === 'percent' || calc.operator === 'ratio' || calc.operator === 'formula') {
aggType = 'avg';
} else if (calc.operator === 'average') {
aggType = 'avg';
}
aggregatedData[calc.name] = aggregateValues(calcValues, aggType);
}
}
}
});
}
const labels = Object.keys(aggregatedData);
const values = Object.values(aggregatedData);
if (labels.length === 0 || values.every(v => v === 0)) {
return null;
}
const colors = generateColors(labels.length);
return {
labels,
datasets: [{
data: values,
backgroundColor: colors,
borderColor: colors.map(color => color.replace('0.8', '1')),
borderWidth: 1,
}],
};
}
/**
* Process data for bar/line/radar charts
* @param {Array} data - Array of Parse objects
* @param {string} xColumn - X-axis column
* @param {string|Array<string>} valueColumn - Value column(s)
* @param {string|Array<string>} groupByColumn - Column(s) to group by (optional)
* @param {string} aggregationType - Aggregation type
* @param {Array} calculatedValues - Calculated value definitions (optional)
* @returns {Object} Chart.js compatible data
*/
export function processBarLineData(data, xColumn, valueColumn, groupByColumn, aggregationType = 'count', calculatedValues = null) {
if (!xColumn || !Array.isArray(data)) {
return null;
}
// Convert single valueColumn to array for uniform handling
const valueColumns = Array.isArray(valueColumn) ? valueColumn : (valueColumn ? [valueColumn] : []);
const hasCalculatedValues = calculatedValues && Array.isArray(calculatedValues) && calculatedValues.length > 0;
// Must have at least one value column or calculated value
if (valueColumns.length === 0 && !hasCalculatedValues) {
return null;
}
// Collect unique x-axis values and group data
const xValues = new Map(); // Use Map to store both raw value and formatted label
const groups = {};
let isDateAxis = false;
let hasNonDateAxisValue = false;
data.forEach(item => {
const xVal = getNestedValue(item, xColumn);
if (xVal == null) {return;}
// Check if x-axis value is a date
const normalizedDate = normalizeDate(xVal);
let xKey, xLabel;
if (normalizedDate) {
isDateAxis = true;
xKey = normalizedDate.getTime(); // Use timestamp as key for sorting
xLabel = formatDateCompact(normalizedDate);
} else {
hasNonDateAxisValue = true;
xKey = String(xVal);
xLabel = String(xVal);
}
xValues.set(xKey, xLabel);
// Create an extended item that will hold calculated values for this row
const calculatedValuesForRow = {};
// Process each value column
valueColumns.forEach(valCol => {
const rawValue = getNestedValue(item, valCol);
const value = extractNumericValue(rawValue);
if (value === null) {return;}
// Handle groupBy column(s) - create composite key if multiple columns
let groupKeyValue = valCol; // Use column name as default group
if (groupByColumn && (Array.isArray(groupByColumn) ? groupByColumn.length > 0 : true)) {
const compositeKey = createGroupKey(item, groupByColumn);
groupKeyValue = compositeKey;
// When groupBy is specified, combine with column name for unique series
if (valueColumns.length > 1) {
groupKeyValue = `${valCol} (${compositeKey})`;
}
}
const groupKey = groupKeyValue;
if (!groups[groupKey]) {
groups[groupKey] = {};
}
if (!groups[groupKey][xKey]) {
groups[groupKey][xKey] = [];
}
groups[groupKey][xKey].push(value);
});
// Process calculated values - with support for referencing other calculated values
if (calculatedValues && Array.isArray(calculatedValues)) {
calculatedValues.forEach(calc => {
// Formula operator doesn't require fields, other operators do
const hasRequiredConfig = calc.operator === 'formula'
? (calc.formula && calc.name)
: (calc.fields && calc.fields.length > 0 && calc.name);
if (hasRequiredConfig) {
// Create an enhanced item that includes previously calculated values
const enhancedItem = { ...item };
if (item.attributes) {
enhancedItem.attributes = { ...item.attributes, ...calculatedValuesForRow };
} else {
Object.assign(enhancedItem, calculatedValuesForRow);
}
// Build available fields for formula evaluation
const availableFields = [...valueColumns];
// Add previously calculated value names
Object.keys(calculatedValuesForRow).forEach(name => {
if (!availableFields.includes(name)) {
availableFields.push(name);
}
});
const calcValue = calculateValue(enhancedItem, calc.fields, calc.operator, calc.formula, availableFields);
// Store this calculated value so it can be referenced by subsequent calculations
calculatedValuesForRow[calc.name] = calcValue;
if (calcValue !== null) {
// Apply groupBy logic to calculated values, same as regular values
let groupKeyValue = calc.name;
if (groupByColumn && (Array.isArray(groupByColumn) ? groupByColumn.length > 0 : true)) {
const compositeKey = createGroupKey(item, groupByColumn);
groupKeyValue = compositeKey;
// When groupBy is specified, combine with calc name for unique series
if (calculatedValues.length > 1 || valueColumns.length > 0) {
groupKeyValue = `${calc.name} (${compositeKey})`;
}
}
const groupKey = groupKeyValue;
if (!groups[groupKey]) {
groups[groupKey] = {};
}
if (!groups[groupKey][xKey]) {
groups[groupKey][xKey] = [];
}
groups[groupKey][xKey].push(calcValue);
}
}
});
}
});
if (xValues.size === 0) {
return null;
}
// Sort x-axis values in ascending order
// For dates, keys are timestamps; for strings/numbers, lexicographic sort
const sortedXKeys = Array.from(xValues.keys()).sort((a, b) => {
if (isDateAxis && !hasNonDateAxisValue) {
return a - b; // Numeric sort for timestamps (ascending)
}
// Try numeric comparison first
const numA = Number(a);
const numB = Number(b);
if (!isNaN(numA) && !isNaN(numB)) {
return numA - numB;
}
// Fall back to string comparison
return String(a).localeCompare(String(b));
});
const sortedXLabels = sortedXKeys.map(key => xValues.get(key));
const groupKeys = Object.keys(groups);
// Create maps for calculated value properties (operator, secondary Y axis, line style, bar style)
// This handles both simple calc names and grouped calc names like "CalcName (GroupValue)"
const calcValueOperatorMap = new Map();
const calcValueSecondaryYAxisMap = new Map();
const calcValueLineStyleMap = new Map();
const calcValueBarStyleMap = new Map();
if (calculatedValues && Array.isArray(calculatedValues)) {
calculatedValues.forEach(calc => {
if (calc.name && calc.operator) {
// Map all possible variations of this calculated value's group keys
groupKeys.forEach(groupKey => {
// Check if this groupKey is for this calculated value
// It either matches exactly, or starts with "CalcName ("
if (groupKey === calc.name || groupKey.startsWith(`${calc.name} (`)) {
calcValueOperatorMap.set(groupKey, calc.operator);
if (calc.useSecondaryYAxis) {
calcValueSecondaryYAxisMap.set(groupKey, true);
}
if (calc.lineStyle) {
calcValueLineStyleMap.set(groupKey, calc.lineStyle);
}
if (calc.barStyle) {
calcValueBarStyleMap.set(groupKey, calc.barStyle);
}
}
});
}
});
}
// Generate colors once for all datasets
const colors = generateColors(groupKeys.length);
const datasets = groupKeys.map((groupKey, index) => {
const groupData = groups[groupKey];
const values = sortedXKeys.map(xKey => {
const groupValues = groupData[xKey] || [];
// Check if this is a calculated value
const calcOperator = calcValueOperatorMap.get(groupKey);
if (calcOperator) {
// For calculated values, the operator has already been applied at row level
// For ratio-based operators (percent, ratio, formula), we should average the results
// For other operators (sum, average, difference), we sum the results
let aggregationType = 'sum';
if (calcOperator === 'percent' || calcOperator === 'ratio' || calcOperator === 'formula') {
aggregationType = 'avg';
} else if (calcOperator === 'average') {
aggregationType = 'avg';
}
return groupValues.length > 0 ? aggregateValues(groupValues, aggregationType) : 0;
} else {
// For regular values, use the selected aggregationType
return groupValues.length > 0 ? aggregateValues(groupValues, aggregationType) : 0;
}
});
const dataset = {
label: groupKey,
data: values,
backgroundColor: colors[index],
borderColor: colors[index].replace('0.8', '1'),
borderWidth: 1,
yAxisID: calcValueSecondaryYAxisMap.get(groupKey) ? 'y1' : 'y',
};
// Add line style if specified
const lineStyle = calcValueLineStyleMap.get(groupKey);
if (lineStyle) {
dataset.lineStyle = lineStyle;
}
// Add bar style if specified
const barStyle = calcValueBarStyleMap.get(groupKey);
if (barStyle) {
dataset.barStyle = barStyle;
}
return dataset;
});
return {
labels: sortedXLabels,
datasets,
};
}
/**
* Generate a color palette for charts
* @param {number} count - Number of colors needed
* @returns {Array<string>} Array of RGBA color strings
*/
export function generateColors(count) {
const baseColors = [
'rgba(255, 99, 132, 0.8)', // Red
'rgba(54, 162, 235, 0.8)', // Blue
'rgba(255, 205, 86, 0.8)', // Yellow
'rgba(75, 192, 192, 0.8)', // Teal
'rgba(153, 102, 255, 0.8)', // Purple
'rgba(255, 159, 64, 0.8)', // Orange
'rgba(201, 203, 207, 0.8)', // Grey
'rgba(255, 87, 51, 0.8)', // Coral
'rgba(51, 255, 87, 0.8)', // Green
'rgba(87, 51, 255, 0.8)', // Indigo
];
if (count <= baseColors.length) {
return baseColors.slice(0, count);
}
// Generate additional colors using HSL
const additionalColors = [];
for (let i = baseColors.length; i < count; i++) {
const hue = (i * 137.5) % 360; // Golden angle approximation
additionalColors.push(`hsla(${hue}, 70%, 50%, 0.8)`);
}
return [...baseColors, ...additionalColors];
}
/**
* Validate graph configuration
* @param {Object} config - Graph configuration object
* @param {Object} columns - Available columns with types
* @returns {Object} Validation result with isValid boolean and error message
*/
export function validateGraphConfig(config, columns) {
if (!config) {
return { isValid: false, error: 'No configuration provided' };
}
const { chartType, xColumn, yColumn, valueColumn, calculatedValues } = config;
if (!chartType) {
return { isValid: false, error: 'Chart type is required' };
}
const hasValueColumn = valueColumn && (!Array.isArray(valueColumn) || valueColumn.length > 0);
const hasCalculatedValues = calculatedValues && Array.isArray(calculatedValues) && calculatedValues.length > 0;
const hasValuesToDisplay = hasValueColumn || hasCalculatedValues;
// Check required columns based on chart type
switch (chartType) {
case 'scatter':
if (!xColumn || !yColumn) {
return { isValid: false, error: 'Scatter plots require both X and Y axis columns' };
}
if (!columns || !columns[xColumn] || !columns[yColumn]) {
return { isValid: false, error: 'Selected columns do not exist' };
}
break;
case 'pie':
case 'doughnut': {
if (!hasValuesToDisplay) {
return { isValid: false, error: 'Pie charts require at least one value column or calculated value' };
}
// Validate all value columns exist (only if valueColumn is specified)
if (hasValueColumn) {
const pieValueCols = Array.isArray(valueColumn) ? valueColumn : [valueColumn];
for (const col of pieValueCols) {
if (!columns || !columns[col]) {
return { isValid: false, error: `Value column '${col}' does not exist` };
}
}
}
break;
}
case 'bar':
case 'line':
case 'radar': {
if (!xColumn || !hasValuesToDisplay) {
return { isValid: false, error: 'Bar/line charts require both X axis and at least one value column or calculated value' };
}
if (!columns || !columns[xColumn]) {
return { isValid: false, error: 'X column does not exist' };
}
// Validate all value columns exist (only if valueColumn is specified)
if (hasValueColumn) {
const barValueCols = Array.isArray(valueColumn) ? valueColumn : [valueColumn];
for (const col of barValueCols) {
if (!columns || !columns[col]) {
return { isValid: false, error: `Value column '${col}' does not exist` };
}
}
}
break;
}
default:
return { isValid: false, error: 'Unsupported chart type' };
}
return { isValid: true };
}