Skip to content

Commit 2a65535

Browse files
committed
feat(core/reports): xlsreport improvements for social balance
1 parent dbe8f30 commit 2a65535

File tree

5 files changed

+83
-16
lines changed

5 files changed

+83
-16
lines changed

projects/core/models/src/utils/expression-utils.md

+4
Original file line numberDiff line numberDiff line change
@@ -121,6 +121,10 @@ An optional expression can be added to filter which forms to consider in the com
121121
Computes the mean of the values of the specified field.
122122
An optional expression can be added to filter which forms to consider in the computation.
123123

124+
`MIN(forms, $field, expression?)`
125+
Computes the min value of the field.
126+
An optional expression can be added to filter which forms to consider in the computation.
127+
124128
`MAX(forms, $field, expression?)`
125129
Computes the max value of the field.
126130
An optional expression can be added to filter which forms to consider in the computation.

projects/core/models/src/utils/expression-utils.ts

+19
Original file line numberDiff line numberDiff line change
@@ -162,6 +162,7 @@ export class AjfExpressionUtils {
162162
LAST: {fn: LAST},
163163
LEN: {fn: LEN},
164164
MAP: {fn: MAP},
165+
MIN: {fn: MIN},
165166
MAX: {fn: MAX},
166167
MEAN: {fn: MEAN},
167168
MEDIAN: {fn: MEDIAN},
@@ -1007,6 +1008,24 @@ export function LAST(
10071008
return expression(form);
10081009
}
10091010

1011+
/**
1012+
* Computes the min value of the field.
1013+
*/
1014+
export function MIN(
1015+
forms: (Form | MainForm)[],
1016+
field: string,
1017+
filter: Func | string = 'true',
1018+
): number {
1019+
const values = getNumericValues(forms, field, filter);
1020+
let min = +Infinity;
1021+
for (const val of values) {
1022+
if (val < min) {
1023+
min = val;
1024+
}
1025+
}
1026+
return min;
1027+
}
1028+
10101029
/**
10111030
* Computes the max value of the field.
10121031
*/

projects/core/reports/src/widget-export.ts

+14-1
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,19 @@ export function exportReportXlsx(
4949
return exportAllWidgets(widgetInstances, iconsMap);
5050
}
5151

52+
/**
53+
* Checks if p is a {x, y, r?} point and eventually formats it as string.
54+
*/
55+
function formatPointData(p: any): any {
56+
if (typeof p === 'object' && p !== null && p.x !== undefined && p.y !== undefined) {
57+
if (p.r === undefined) {
58+
return `(${p.x}, ${p.y})`;
59+
}
60+
return `(${p.x}, ${p.y}, ${p.r})`;
61+
}
62+
return p;
63+
}
64+
5265
/**
5366
* Build xlsx data for export
5467
* @param widgetType
@@ -74,7 +87,7 @@ function buildXlsxData(
7487
const data = datasets[i].data || [];
7588
row.push(datasets[i].label);
7689
for (let j = 0; j < data.length; j++) {
77-
row.push(data[j]);
90+
row.push(formatPointData(data[j]));
7891
}
7992
xlsxData.push(row);
8093
}

projects/core/reports/src/xls-report/hindikit-parser.ts

+1
Original file line numberDiff line numberDiff line change
@@ -417,6 +417,7 @@ const functionArgs: {[name: string]: string[]} = {
417417
LAST: ["arg", "func(form)", "field?"],
418418
LEN: ["arg"],
419419
MAP: ["arg", "func(elem)"],
420+
MIN: ["arg", "field", "func(form)?"],
420421
MAX: ["arg", "field", "func(form)?"],
421422
MEAN: ["arg", "field", "func(form)?"],
422423
MEDIAN: ["arg", "field", "func(form)?"],

projects/core/reports/src/xls-report/xls-report.ts

+45-15
Original file line numberDiff line numberDiff line change
@@ -192,14 +192,6 @@ function alertAndThrow(err: string) {
192192
throw new Error(err);
193193
}
194194

195-
// converts to true|undefined an option from an excel cell
196-
function boolOption(cell: undefined | null | string | boolean): true | undefined {
197-
if (!cell || cell === 'false') {
198-
return undefined;
199-
}
200-
return true;
201-
}
202-
203195
function _buildChart(name: string, sheet: {[key: string]: string}[]): AjfWidget {
204196
if (sheet == null || sheet.length === 0) {
205197
alertAndThrow('Empty sheet for chart ' + name);
@@ -211,6 +203,12 @@ function _buildChart(name: string, sheet: {[key: string]: string}[]): AjfWidget
211203
'stacked',
212204
'beginAtZeroX',
213205
'beginAtZeroY',
206+
'axisLabelX',
207+
'axisLabelY',
208+
'axisMinX',
209+
'axisMinY',
210+
'axisMaxX',
211+
'axisMaxY',
214212
'removeZeroValues',
215213
'mainDataNumberThreshold',
216214
];
@@ -247,9 +245,15 @@ function _buildChart(name: string, sheet: {[key: string]: string}[]): AjfWidget
247245
labelsFormula = {formula: labelsJs};
248246
}
249247

250-
const stacked = boolOption(options['stacked']);
251-
const beginAtZeroX = boolOption(options['beginAtZeroX']);
252-
const beginAtZeroY = boolOption(options['beginAtZeroY']);
248+
const stacked = Boolean(options['stacked']);
249+
const beginAtZeroX = Boolean(options['beginAtZeroX']);
250+
const beginAtZeroY = Boolean(options['beginAtZeroY']);
251+
const axisLabelX = options['axisLabelX'];
252+
const axisLabelY = options['axisLabelY'];
253+
const axisMinX = options['axisMinX'];
254+
const axisMinY = options['axisMinY'];
255+
const axisMaxX = options['axisMaxX'];
256+
const axisMaxY = options['axisMaxY'];
253257
const removeZeroValues =
254258
options['removeZeroValues'] != null ? Boolean(options['removeZeroValues']) : true;
255259
const mainDataNumberThreshold =
@@ -310,11 +314,37 @@ function _buildChart(name: string, sheet: {[key: string]: string}[]): AjfWidget
310314
});
311315

312316
const scales: Chart.ChartScales = {};
313-
if (stacked || beginAtZeroX) {
314-
scales.xAxes = [{stacked, ticks: beginAtZeroX ? {beginAtZero: true} : undefined}];
317+
if (stacked || beginAtZeroX || axisMinX != null || axisMaxX != null || axisLabelX) {
318+
const axisX: Chart.ChartXAxe = {
319+
stacked,
320+
ticks: {beginAtZero: beginAtZeroX},
321+
};
322+
if (axisMinX != null) {
323+
axisX.ticks!.suggestedMin = Number(axisMinX);
324+
}
325+
if (axisMaxX != null) {
326+
axisX.ticks!.suggestedMax = Number(axisMaxX);
327+
}
328+
if (axisLabelX) {
329+
axisX.scaleLabel = {display: true, labelString: axisLabelX};
330+
}
331+
scales.xAxes =[axisX];
315332
}
316-
if (stacked || beginAtZeroY) {
317-
scales.yAxes = [{stacked, ticks: {beginAtZero: true}}];
333+
if (stacked || beginAtZeroY || axisMinY != null || axisMaxY != null || axisLabelY) {
334+
const axisY: Chart.ChartYAxe = {
335+
stacked,
336+
ticks: {beginAtZero: stacked || beginAtZeroY},
337+
};
338+
if (axisMinY != null) {
339+
axisY.ticks!.suggestedMin = Number(axisMinY);
340+
}
341+
if (axisMaxY != null) {
342+
axisY.ticks!.suggestedMax = Number(axisMaxY);
343+
}
344+
if (axisLabelY) {
345+
axisY.scaleLabel = {display: true, labelString: axisLabelY};
346+
}
347+
scales.yAxes = [axisY];
318348
}
319349
return createWidget({
320350
name,

0 commit comments

Comments
 (0)