import {
IgxSummaryResult,
IgxSummaryOperand,
IgxNumberSummaryOperand,
IgxDateSummaryOperand,
} from "@infragistics/igniteui-angular";
export interface CustomSummary {
name?: string;
label?: string;
value?: any;
sign?: string;
parameter?: string;
}
export interface ColumnSummaries {
fieldName?: string;
customSummary?: any;
}
import { formatNumber } from "@angular/common";
export class ExtendedSummary {
public result: any = [];
constructor() {}
operate(columnData: any[], allGridData = [], fieldName?): IgxSummaryResult[] {
if (!fieldName) {
return this.result;
}
let columnSummaryField = fieldName + "CustomSummary";
if (!allGridData || allGridData.length == 0) {
return this.result;
}
let record = allGridData[0];
let customSummaries = record[columnSummaryField];
if (!customSummaries) {
return this.result;
}
let selectedLocale = record.locale;
let parameter = record.parameter;
for (let summary of customSummaries) {
let summaryFunctionKey = summary.name;
if (!summaryFunctionKey || !summaryFunctions[summaryFunctionKey]) {
continue;
}
let summaryGenerated = summaryFunctions[summaryFunctionKey](
columnData,
summary,
selectedLocale,
parameter
);
let summaryExists = this.result.some((elem) => {
return JSON.stringify(summaryGenerated) === JSON.stringify(elem);
});
if (!summaryExists) {
this.result.push(summaryGenerated);
}
}
return this.result;
}
}
const summaryFunctions = {
count: (data, customSummary?, selectedLocale?, parameter?) => {
let result = getsummaryResult(
selectedLocale,
IgxSummaryOperand.count(data),
customSummary && customSummary.parameter
? customSummary.parameter
: parameter,
customSummary ? customSummary.sign : null
);
return {
key: "count",
label: customLabelExists(customSummary) ? customSummary.label : "Count",
summaryResult: result,
};
},
avg: (data, customSummary?, selectedLocale?, parameter?) => {
let result = getsummaryResult(
selectedLocale,
IgxNumberSummaryOperand.average(data),
customSummary && customSummary.parameter
? customSummary.parameter
: parameter,
customSummary ? customSummary.sign : null
);
return {
key: "avg",
label: customLabelExists(customSummary) ? customSummary.label : "Avg",
summaryResult: result,
};
},
max: (data, customSummary?, selectedLocale?, parameter?) => {
let result = getsummaryResult(
selectedLocale,
IgxNumberSummaryOperand.max(data),
customSummary && customSummary.parameter
? customSummary.parameter
: parameter,
customSummary ? customSummary.sign : null
);
return {
key: "max",
label: customLabelExists(customSummary) ? customSummary.label : "Max",
summaryResult: result,
};
},
min: (data, customSummary?, selectedLocale?, parameter?) => {
let result = getsummaryResult(
selectedLocale,
IgxNumberSummaryOperand.min(data),
customSummary && customSummary.parameter
? customSummary.parameter
: parameter,
customSummary ? customSummary.sign : null
);
return {
key: "min",
label: customLabelExists(customSummary) ? customSummary.label : "Min",
summaryResult: result,
};
},
sum: (data, customSummary?, selectedLocale?, parameter?) => {
let result = getsummaryResult(
selectedLocale,
IgxNumberSummaryOperand.sum(data),
customSummary && customSummary.parameter
? customSummary.parameter
: parameter,
customSummary ? customSummary.sign : null
);
return {
key: "sum",
label: customLabelExists(customSummary) ? customSummary.label : "Sum",
summaryResult: result,
};
},
earliest: (data, customSummary?) => {
return {
key: "earliest",
label: customLabelExists(customSummary)
? customSummary.label
: "Earliest",
summaryResult: IgxDateSummaryOperand.earliest(data),
};
},
latest: (data, customSummary?) => {
return {
key: "latest",
label: customLabelExists(customSummary) ? customSummary.label : "Latest",
summaryResult: IgxDateSummaryOperand.latest(data),
};
},
fromData: (data, customSummary, selectedLocale, parameter) => {
let result = getsummaryResult(
selectedLocale,
customSummary.value,
customSummary.parameter ? customSummary.parameter : parameter,
customSummary.sign
);
return {
key: "fromData",
label: customSummary.label,
summaryResult: result,
};
},
};
function customLabelExists(customSummary: any) {
return customSummary && customSummary.label;
}
function getsummaryResult(
selectedLocale: any,
data: any,
parameter: any,
sign: any
) {
if ((data || data === 0) && selectedLocale) {
data = formatNumber(data, selectedLocale, parameter);
}
if (sign) {
data = data + " " + sign;
}
return data;
}