@datarailsshared/dr_renderer 1.2.370 → 1.2.372

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@datarailsshared/dr_renderer",
3
- "version": "1.2.370",
3
+ "version": "1.2.372",
4
4
  "description": "DataRails charts and tables renderer",
5
5
  "keywords": [
6
6
  "datarails",
@@ -3184,40 +3184,30 @@ let getHighchartsRenderer = function ($, document, Highcharts, default_colors, h
3184
3184
  stackLabels: {
3185
3185
  enabled: additionOptions && additionOptions.label ? additionOptions.label.show : true,
3186
3186
  formatter: function () {
3187
- var presentTotal = additionOptions && additionOptions.label && additionOptions.label.present_total;
3188
- var hideSubtotals = additionOptions && additionOptions.label && additionOptions.label.hide_subtotals;
3189
- var showTotalsPercentage = additionOptions && additionOptions.label && additionOptions.label.show_percentage;
3187
+ const showTotal = additionOptions && additionOptions.label && additionOptions.label.present_total;
3188
+ const hideSubtotals = additionOptions && additionOptions.label && additionOptions.label.hide_subtotals;
3189
+ const showTotalsPercentage = additionOptions && additionOptions.label && additionOptions.label.show_percentage;
3190
3190
 
3191
- if (hideSubtotals && !presentTotal) {
3192
- return '';
3193
- }
3191
+ if (!hideSubtotals || showTotal) {
3192
+ const agg = pivotData.getAggregator([], pivotData.colKeys[this.x]);
3194
3193
 
3195
- if (this.total) {
3196
- var total = parseFloat(this.total);
3197
- var agg = pivotData.getAggregator([], presentTotal ? pivotData.colKeys[this.x] : []);
3194
+ if (agg && agg.sum) {
3195
+ const isNeedToDisplayLabel = agg.sum > 0 && !this.isNegative || agg.sum < 0 && this.isNegative;
3198
3196
 
3199
- let result = '';
3200
- if (agg) {
3201
- let percentages = '';
3202
- result = presentTotal ? this.total > 0 ? agg.sum : '' : total;
3197
+ if (isNeedToDisplayLabel) {
3198
+ const subtotalValue = agg.sum;
3199
+ let percentages = '';
3203
3200
 
3204
- if (!result) {
3205
- return '';
3206
- }
3207
-
3208
- if (showTotalsPercentage) {
3209
- let aggTotal = pivotData.getAggregator([], []);
3210
- let aggTotalValue = aggTotal.value();
3211
- percentages = aggTotalValue ? ' (' + Math.round(result/aggTotalValue * 100) + '%)' : '';
3201
+ if (showTotalsPercentage) {
3202
+ const aggTotal = pivotData.getAggregator([], []);
3203
+ const aggTotalValue = aggTotal.value();
3204
+ percentages = aggTotalValue ? ' (' + Math.round(subtotalValue/aggTotalValue * 100) + '%)' : '';
3205
+ }
3206
+ return (showTotal ? agg.format(subtotalValue, true) : '') + percentages;
3212
3207
  }
3213
-
3214
- result = (presentTotal ? agg.format(result, true) : '') + percentages;
3215
- } else {
3216
- result = total.toLocaleString();
3217
3208
  }
3218
- return result;
3219
3209
  }
3220
- return "";
3210
+ return '';
3221
3211
  },
3222
3212
  style: highchartsRenderer.getDataLabelsStyle(additionOptions),
3223
3213
  },
@@ -9100,7 +9090,7 @@ let getHighchartsRenderer = function ($, document, Highcharts, default_colors, h
9100
9090
 
9101
9091
  let keysObject;
9102
9092
  if (highchartsRenderer.isSortingOnBackendEnabled) {
9103
- keysObject = row_data.pop();
9093
+ keysObject = highchartsRenderer.getAggregatedSortingObjects(row_data);
9104
9094
  }
9105
9095
 
9106
9096
  let res = highchartsRenderer.updateSelectedOverrideValues(widget_obj, override_values, row_data);
@@ -9700,6 +9690,30 @@ let getHighchartsRenderer = function ($, document, Highcharts, default_colors, h
9700
9690
  return res;
9701
9691
  }
9702
9692
 
9693
+ // TODO: move this logic to BE (the problem it is solving that BE returns several sorting objects for more than 1 field in Values)
9694
+ highchartsRenderer.getAggregatedSortingObjects = function(res) {
9695
+ let sortingObjectToReturn = null;
9696
+
9697
+ lodash.forEach(res, (dataRow) => {
9698
+ if (!lodash.isNil(dataRow.row_keys) || !lodash.isNil(dataRow.col_keys)) {
9699
+ if (!sortingObjectToReturn) {
9700
+ sortingObjectToReturn = Object.assign({}, dataRow);
9701
+ } else {
9702
+ sortingObjectToReturn.row_keys = lodash.concat(sortingObjectToReturn.row_keys, dataRow.row_keys || []);
9703
+ }
9704
+ dataRow.isSortingObject = true;
9705
+ }
9706
+ });
9707
+
9708
+ if (sortingObjectToReturn) {
9709
+ const resWithoutSortObjects = lodash.filter(res, (dataRow) => !dataRow.isSortingObject);
9710
+ res.length = 0;
9711
+ Object.assign(res, resWithoutSortObjects);
9712
+ }
9713
+
9714
+ return sortingObjectToReturn;
9715
+ }
9716
+
9703
9717
  return highchartsRenderer;
9704
9718
  };
9705
9719
 
@@ -5577,7 +5577,7 @@ describe('highcharts_renderer', () => {
5577
5577
  ]
5578
5578
  });
5579
5579
  });
5580
-
5580
+
5581
5581
  it('should not replace sorting keys for non-Date fields', () => {
5582
5582
  const keysObject = {
5583
5583
  col_keys: [
@@ -5651,4 +5651,62 @@ describe('highcharts_renderer', () => {
5651
5651
  highchartsRenderer.replaceSortingKeysWithMappedValues(null, invertedDateStringMap, widget.rows);
5652
5652
  });
5653
5653
  });
5654
+
5655
+
5656
+ describe('Functions getAggregatedSortingObjects', () => {
5657
+
5658
+ const simpleRowData = [{ someField1: 'someValue1' }, { someField2: 'someValue2' }];
5659
+
5660
+ const generalTestCases = [
5661
+ {
5662
+ description: 'should get sort object and remove from data rows (case with all fields)',
5663
+ expectedObj: { col_keys: ['colKey1', 'colKey2'], row_keys: ['rowKey1', 'rowKey2'], row_keys_by_cols: [['1', '2'], ['2', '1']] },
5664
+ },
5665
+ {
5666
+ description: 'should get sort object and remove from data rows (case with only colkeys)',
5667
+ expectedObj: { row_keys: [], col_keys: ['colKey1', 'colKey2'] },
5668
+ },
5669
+ {
5670
+ description: 'should get sort object and remove from data rows (case with only rowkeys)',
5671
+ expectedObj: { row_keys: ['rowKey1', 'rowKey2'], col_keys: [] },
5672
+ },
5673
+ {
5674
+ description: 'should get null sort object and remove from data rows (case with no sorting object)',
5675
+ expectedObj: null,
5676
+ }
5677
+ ];
5678
+
5679
+ lodash.forEach(generalTestCases, testCase => {
5680
+ it(testCase.description, () => {
5681
+ const rowData = lodash.concat(simpleRowData, testCase.expectedObj ? lodash.clone(testCase.expectedObj) : []);
5682
+ const sortObj = highchartsRenderer.getAggregatedSortingObjects(rowData);
5683
+
5684
+ expect(rowData).toEqual(simpleRowData);
5685
+ expect(sortObj).toEqual(testCase.expectedObj);
5686
+ });
5687
+ });
5688
+
5689
+ it('should get sort object and remove from data rows (several object encounters - for more than one Values field)', () => {
5690
+
5691
+ const expectedObj = { col_keys: ['colKey1', 'colKey2'], row_keys: ['rowKey1', 'rowKey2'] };
5692
+ const rowData = [
5693
+ { someField1: 'someValue1' },
5694
+ { col_keys: ['colKey1', 'colKey2'], row_keys: ['rowKey1'] },
5695
+ { someField2: 'someValue2' },
5696
+ { someField3: 'someValue3' },
5697
+ { someField4: 'someValue4' },
5698
+ { col_keys: ['colKey1', 'colKey2'], row_keys: ['rowKey2'] },
5699
+ ];
5700
+
5701
+ const sortObj = highchartsRenderer.getAggregatedSortingObjects(rowData);
5702
+
5703
+ expect(rowData).toEqual([
5704
+ { someField1: 'someValue1' },
5705
+ { someField2: 'someValue2' },
5706
+ { someField3: 'someValue3' },
5707
+ { someField4: 'someValue4' },
5708
+ ]);
5709
+ expect(sortObj).toEqual(expectedObj);
5710
+ });
5711
+ });
5654
5712
  });