MCPcopy Create free account
hub / github.com/Make-md/makemd / aggregateForLineGraph

Function aggregateForLineGraph

src/core/utils/visualization/visualizationUtils.ts:648–763  ·  view source on GitHub ↗
(
  rows: any[],
  xField: string,
  yField: string,
  aggregationMethod: AggregationType = 'sum'
)

Source from the content-addressed store, hash-verified

646 * Line graphs always need data bucketing regardless of duplicates
647 */
648export const aggregateForLineGraph = (
649 rows: any[],
650 xField: string,
651 yField: string,
652 aggregationMethod: AggregationType = 'sum'
653): any[] => {
654 // Group all data points by x-axis value
655 const buckets = new Map<string, { sum: number, count: number, values: number[], min: number, max: number }>();
656
657 rows.forEach(row => {
658 const xValue = String(row[xField] || '');
659 const yValue = parseFloat(row[yField]) || 0;
660
661 if (!buckets.has(xValue)) {
662 buckets.set(xValue, {
663 sum: 0,
664 count: 0,
665 values: [],
666 min: Number.MAX_VALUE,
667 max: Number.MIN_VALUE
668 });
669 }
670
671 const bucket = buckets.get(xValue)!;
672 bucket.sum += yValue;
673 bucket.count += 1;
674 bucket.values.push(yValue);
675 bucket.min = Math.min(bucket.min, yValue);
676 bucket.max = Math.max(bucket.max, yValue);
677 });
678
679 // Convert buckets to array format based on aggregation method
680 const aggregatedData = Array.from(buckets.entries()).map(([xValue, bucket]) => {
681 let aggregatedValue: number;
682
683 switch (aggregationMethod) {
684 case 'count':
685 aggregatedValue = bucket.count;
686 break;
687 case 'sum':
688 aggregatedValue = bucket.sum;
689 break;
690 case 'average':
691 aggregatedValue = bucket.count > 0 ? bucket.sum / bucket.count : 0;
692 break;
693 case 'min':
694 aggregatedValue = bucket.min === Number.MAX_VALUE ? 0 : bucket.min;
695 break;
696 case 'max':
697 aggregatedValue = bucket.max === Number.MIN_VALUE ? 0 : bucket.max;
698 break;
699 case 'distinct':
700 aggregatedValue = new Set(bucket.values).size;
701 break;
702 default:
703 aggregatedValue = bucket.sum;
704 }
705

Callers 1

processDataAggregationFunction · 0.85

Calls 10

hasMethod · 0.80
pushMethod · 0.80
fromMethod · 0.80
entriesMethod · 0.80
sortMethod · 0.80
isDateLikeFunction · 0.70
forEachMethod · 0.45
setMethod · 0.45
getMethod · 0.45
mapMethod · 0.45

Tested by

no test coverage detected