@datarailsshared/dr_renderer 1.2.369 → 1.2.371

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.369",
3
+ "version": "1.2.371",
4
4
  "description": "DataRails charts and tables renderer",
5
5
  "keywords": [
6
6
  "datarails",
@@ -1547,7 +1547,7 @@ let getHighchartsRenderer = function ($, document, Highcharts, default_colors, h
1547
1547
  const chart_series = [];
1548
1548
  const row_n_keys = pivotData.getRowKeys();
1549
1549
  const col_n_keys = pivotData.getColKeys();
1550
- const rows_by_cols = pivotData.rowKeysByCols;
1550
+ const rows_by_cols = pivotData.getRowKeysByCols();
1551
1551
 
1552
1552
  let resultObject = {
1553
1553
  data: [],
@@ -3584,7 +3584,9 @@ let getHighchartsRenderer = function ($, document, Highcharts, default_colors, h
3584
3584
  ? opts.chartOptions
3585
3585
  : highchartsRenderer.getDefaultValueForChart(highchartsRenderer.CHART_TYPES.WATERFALL_BREAKDOWN);
3586
3586
 
3587
- pivotData.colKeys = lodash.map(lodash.keys(pivotData.colTotals), key => [key]);
3587
+ if (!highchartsRenderer.isSortingOnBackendEnabled) {
3588
+ pivotData.colKeys = lodash.map(lodash.keys(pivotData.colTotals), key => [key]);
3589
+ }
3588
3590
 
3589
3591
  chartOptions.chart = {
3590
3592
  type: 'waterfall',
@@ -5412,6 +5414,7 @@ let getHighchartsRenderer = function ($, document, Highcharts, default_colors, h
5412
5414
  widgetOptions.pivot.chartOptions = widgetOptions.options;
5413
5415
  widgetOptions.pivot.chartRender = highchartsRenderer.getChartRendererFunction(widgetOptions.pivot.chartType, drilldownFunc, drillDownListFunc, insightsTooltipFunc, trackUserInsightsTooltipFunc);
5414
5416
 
5417
+ if (highchartsRenderer.isSortingOnBackendEnabled) return;
5415
5418
  // TODO: remove this logic after BE sort is implemented
5416
5419
  // it is required to do sort by totals for comparative analysis - we need to change deltas if columns swaped vice versa
5417
5420
  const isTwoColumnComparisonWidget = widgetOptions.chart_type === highchartsRenderer.CHART_TYPES.WATERFALL_BREAKDOWN
@@ -8860,6 +8863,25 @@ let getHighchartsRenderer = function ($, document, Highcharts, default_colors, h
8860
8863
  return res;
8861
8864
  };
8862
8865
 
8866
+ highchartsRenderer.replaceSortingKeysWithMappedValues = function(keys, invertedDateStringMap, widgetFields) {
8867
+ if (!keys || !invertedDateStringMap) {
8868
+ return;
8869
+ }
8870
+ const replaceNestedKeys = (nestedKeys) => {
8871
+ for (let i = 0; i < nestedKeys.length; i++) {
8872
+ if (Array.isArray(nestedKeys[i])) {
8873
+ replaceNestedKeys(nestedKeys[i]);
8874
+ } else if (invertedDateStringMap[nestedKeys[i]] && widgetFields[i] && widgetFields[i].type === 'Date') {
8875
+ nestedKeys[i] = invertedDateStringMap[nestedKeys[i]];
8876
+ }
8877
+ }
8878
+ };
8879
+
8880
+ for (let i = 0; i < keys.length; i++) {
8881
+ replaceNestedKeys(keys[i]);
8882
+ }
8883
+ }
8884
+
8863
8885
  highchartsRenderer.getWidgetDataSorters = function (res, widget, defaultDateFormat) {
8864
8886
  const isFormattingDatesAsOtherAxisTypes = highchartsRenderer.isFormattingDatesAsOtherAxisTypes();
8865
8887
  let sorters;
@@ -8900,7 +8922,7 @@ let getHighchartsRenderer = function ($, document, Highcharts, default_colors, h
8900
8922
  row.val_not_convert = highchartsRenderer.check_values_not_for_convert(widget, row.name);
8901
8923
  });
8902
8924
  }
8903
-
8925
+
8904
8926
  if (datesFields.length > 0) {
8905
8927
  const invertedDateStringMap = {};
8906
8928
  lodash.forEach(res, function (element) {
@@ -8924,25 +8946,15 @@ let getHighchartsRenderer = function ($, document, Highcharts, default_colors, h
8924
8946
  }
8925
8947
  }
8926
8948
  }
8927
- });
8949
+ if (highchartsRenderer.sortHasBeenDoneOnBE(lodash.get(widget, 'pivot.keysObject')) && !isFormattingDatesAsOtherAxisTypes) {
8950
+ const keysObject = lodash.get(widget, 'pivot.keysObject');
8928
8951
 
8929
- if (highchartsRenderer.sortHasBeenDoneOnBE(lodash.get(widget, 'pivot.keysObject')) && !isFormattingDatesAsOtherAxisTypes) {
8930
- lodash.forEach(['col_keys', 'row_keys'], (keysListName, index) => {
8931
- const widgetFields = index ? widget.rows : widget.cols;
8932
- const uniqueFormattedKeysList = [];
8933
- lodash.forEach(widget.pivot.keysObject[keysListName], subKeysList => {
8934
- lodash.forEach(subKeysList, (key, index) => {
8935
- if (widgetFields[index] && widgetFields[index].type === 'Date') {
8936
- subKeysList[index] = invertedDateStringMap[key] || key;
8937
- }
8938
- });
8939
- if (!lodash.find(uniqueFormattedKeysList, formattedSubKeys => lodash.isEqual(formattedSubKeys, subKeysList))) {
8940
- uniqueFormattedKeysList.push(subKeysList);
8941
- }
8952
+ lodash.forEach(['col_keys', 'row_keys', 'row_keys_by_cols'], (keysListName) => {
8953
+ const widgetFields = keysListName === 'col_keys' ? widget.cols : widget.rows;
8954
+ highchartsRenderer.replaceSortingKeysWithMappedValues(lodash.get(keysObject, keysListName), invertedDateStringMap, widgetFields);
8942
8955
  });
8943
- widget.pivot.keysObject[keysListName] = uniqueFormattedKeysList;
8944
- });
8945
- }
8956
+ }
8957
+ });
8946
8958
  }
8947
8959
 
8948
8960
  if (!highchartsRenderer.sortHasBeenDoneOnBE(lodash.get(widget, 'pivot.keysObject'))) {
@@ -8980,7 +8992,7 @@ let getHighchartsRenderer = function ($, document, Highcharts, default_colors, h
8980
8992
  var rowsAndCols = [];
8981
8993
  rowsAndCols = widget.rows.concat(widget.cols);
8982
8994
 
8983
- if (widget.chart_type === highchartsRenderer.CHART_TYPES.WATERFALL_BREAKDOWN) {
8995
+ if (widget.chart_type === highchartsRenderer.CHART_TYPES.WATERFALL_BREAKDOWN && !highchartsRenderer.sortHasBeenDoneOnBE(lodash.get(widget, 'pivot.keysObject'))) {
8984
8996
 
8985
8997
  // if it is breakdown widget - redefine sorting according to breakdown_options
8986
8998
  // TODO: remove this when BE sort will be implemented (FE subtickets for story DR-18469)
@@ -9088,7 +9100,7 @@ let getHighchartsRenderer = function ($, document, Highcharts, default_colors, h
9088
9100
 
9089
9101
  let keysObject;
9090
9102
  if (highchartsRenderer.isSortingOnBackendEnabled) {
9091
- keysObject = row_data.pop();
9103
+ keysObject = highchartsRenderer.getAggregatedSortingObjects(row_data);
9092
9104
  }
9093
9105
 
9094
9106
  let res = highchartsRenderer.updateSelectedOverrideValues(widget_obj, override_values, row_data);
@@ -9688,6 +9700,30 @@ let getHighchartsRenderer = function ($, document, Highcharts, default_colors, h
9688
9700
  return res;
9689
9701
  }
9690
9702
 
9703
+ // TODO: move this logic to BE (the problem it is solving that BE returns several sorting objects for more than 1 field in Values)
9704
+ highchartsRenderer.getAggregatedSortingObjects = function(res) {
9705
+ let sortingObjectToReturn = null;
9706
+
9707
+ lodash.forEach(res, (dataRow) => {
9708
+ if (!lodash.isNil(dataRow.row_keys) || !lodash.isNil(dataRow.col_keys)) {
9709
+ if (!sortingObjectToReturn) {
9710
+ sortingObjectToReturn = Object.assign({}, dataRow);
9711
+ } else {
9712
+ sortingObjectToReturn.row_keys = lodash.concat(sortingObjectToReturn.row_keys, dataRow.row_keys || []);
9713
+ }
9714
+ dataRow.isSortingObject = true;
9715
+ }
9716
+ });
9717
+
9718
+ if (sortingObjectToReturn) {
9719
+ const resWithoutSortObjects = lodash.filter(res, (dataRow) => !dataRow.isSortingObject);
9720
+ res.length = 0;
9721
+ Object.assign(res, resWithoutSortObjects);
9722
+ }
9723
+
9724
+ return sortingObjectToReturn;
9725
+ }
9726
+
9691
9727
  return highchartsRenderer;
9692
9728
  };
9693
9729
 
package/src/pivottable.js CHANGED
@@ -671,6 +671,7 @@ let initPivotTable = function($, window, document) {
671
671
  this.getAggregator = bind(this.getAggregator, this);
672
672
  this.getRowKeys = bind(this.getRowKeys, this);
673
673
  this.getColKeys = bind(this.getColKeys, this);
674
+ this.getRowKeysByCols = bind(this.getRowKeysByCols, this);
674
675
  this.sortKeys = bind(this.sortKeys, this);
675
676
  this.arrSort = bind(this.arrSort, this);
676
677
  this.input = input;
@@ -693,7 +694,7 @@ let initPivotTable = function($, window, document) {
693
694
  if (this.isKeysSortingDoneOnBackendSide) {
694
695
  this.rowKeys = opts.keysObject.row_keys;
695
696
  this.colKeys = opts.keysObject.col_keys;
696
- // TODO: add also for breakdown sort object when BE story is ready.
697
+ this.rowKeysByCols = opts.keysObject.row_keys_by_cols;
697
698
  } else {
698
699
  this.rowKeys = [];
699
700
  this.colKeys = [];
@@ -883,6 +884,10 @@ let initPivotTable = function($, window, document) {
883
884
  return this.rowKeys;
884
885
  };
885
886
 
887
+ PivotData.prototype.getRowKeysByCols = function() {
888
+ return this.rowKeysByCols;
889
+ };
890
+
886
891
  PivotData.prototype.processRecord = function(record) {
887
892
  var colKey, flatColKey, flatRowKey, l, len1, len2, n, ref, ref1, ref2, ref3, rowKey, x;
888
893
  colKey = [];
@@ -5521,4 +5521,192 @@ describe('highcharts_renderer', () => {
5521
5521
  expect(highchartsRenderer.getColsInFormatterContext(context, pivotData)).toEqual(name2);
5522
5522
  });
5523
5523
  });
5524
+
5525
+
5526
+ describe('Function replaceSortingKeysWithMappedValues', () => {
5527
+ it('should replace sorting keys with mapped values for Date fields', () => {
5528
+ const keysObject = {
5529
+ col_keys: [
5530
+ [1640995200],
5531
+ [1648684800],
5532
+ [1667174400]
5533
+ ],
5534
+ row_keys: [],
5535
+ row_keys_by_cols: [
5536
+ [
5537
+ ["Other Income"],
5538
+ ["Other Expense"],
5539
+ ["Income"],
5540
+ ["Expenses"],
5541
+ ["Cost of Goods Sold"]
5542
+ ]
5543
+ ]
5544
+ };
5545
+ const invertedDateStringMap = {
5546
+ 1640995200: '2022-01-01',
5547
+ 1648684800: '2022-03-29',
5548
+ 1667174400: '2023-03-01'
5549
+ };
5550
+ const widget = {
5551
+ cols: [{ type: 'Date' }],
5552
+ rows: [{ type: 'NotDate' }],
5553
+ pivot: {
5554
+ keysObject: keysObject
5555
+ }
5556
+ };
5557
+
5558
+ highchartsRenderer.replaceSortingKeysWithMappedValues(keysObject.col_keys, invertedDateStringMap, widget.cols);
5559
+ highchartsRenderer.replaceSortingKeysWithMappedValues(keysObject.row_keys, invertedDateStringMap, widget.rows);
5560
+ highchartsRenderer.replaceSortingKeysWithMappedValues(keysObject.row_keys_by_cols, invertedDateStringMap, widget.rows);
5561
+
5562
+ expect(keysObject).toEqual({
5563
+ col_keys: [
5564
+ ['2022-01-01'],
5565
+ ['2022-03-29'],
5566
+ ['2023-03-01']
5567
+ ],
5568
+ row_keys: [],
5569
+ row_keys_by_cols: [
5570
+ [
5571
+ ['Other Income'],
5572
+ ['Other Expense'],
5573
+ ['Income'],
5574
+ ['Expenses'],
5575
+ ['Cost of Goods Sold']
5576
+ ]
5577
+ ]
5578
+ });
5579
+ });
5580
+
5581
+ it('should not replace sorting keys for non-Date fields', () => {
5582
+ const keysObject = {
5583
+ col_keys: [
5584
+ [1640995200],
5585
+ [1648684800],
5586
+ [1667174400]
5587
+ ],
5588
+ row_keys: [],
5589
+ row_keys_by_cols: [
5590
+ [
5591
+ ["Other Income"],
5592
+ ["Other Expense"],
5593
+ ["Income"],
5594
+ ["Expenses"],
5595
+ ["Cost of Goods Sold"]
5596
+ ]
5597
+ ]
5598
+ };
5599
+ const invertedDateStringMap = {
5600
+ 1640995200: '2022-01-01',
5601
+ 1648684800: '2022-03-29',
5602
+ 1667174400: '2023-03-01'
5603
+ };
5604
+ const widget = {
5605
+ cols: [{ type: 'NotDate' }],
5606
+ rows: [{ type: 'NotDate' }],
5607
+ pivot: {
5608
+ keysObject: keysObject
5609
+ }
5610
+ };
5611
+
5612
+ highchartsRenderer.replaceSortingKeysWithMappedValues(keysObject.col_keys, invertedDateStringMap, widget.cols);
5613
+ highchartsRenderer.replaceSortingKeysWithMappedValues(keysObject.row_keys, invertedDateStringMap, widget.rows);
5614
+ highchartsRenderer.replaceSortingKeysWithMappedValues(keysObject.row_keys_by_cols, invertedDateStringMap, widget.rows);
5615
+
5616
+ expect(keysObject).toEqual({
5617
+ col_keys: [
5618
+ [1640995200],
5619
+ [1648684800],
5620
+ [1667174400]
5621
+ ],
5622
+ row_keys: [],
5623
+ row_keys_by_cols: [
5624
+ [
5625
+ ["Other Income"],
5626
+ ["Other Expense"],
5627
+ ["Income"],
5628
+ ["Expenses"],
5629
+ ["Cost of Goods Sold"]
5630
+ ]
5631
+ ]
5632
+ });
5633
+ });
5634
+
5635
+ it('should handle null or undefined keysObject', () => {
5636
+ const invertedDateStringMap = {
5637
+ 1640995200: '2022-01-01',
5638
+ 1648684800: '2022-03-29',
5639
+ 1667174400: '2023-03-01'
5640
+ };
5641
+ const widget = {
5642
+ cols: [{ type: 'Date' }],
5643
+ rows: [{ type: 'NotDate' }],
5644
+ pivot: {
5645
+ keysObject: null
5646
+ }
5647
+ };
5648
+
5649
+ highchartsRenderer.replaceSortingKeysWithMappedValues(null, invertedDateStringMap, widget.cols);
5650
+ highchartsRenderer.replaceSortingKeysWithMappedValues(null, invertedDateStringMap, widget.rows);
5651
+ highchartsRenderer.replaceSortingKeysWithMappedValues(null, invertedDateStringMap, widget.rows);
5652
+ });
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
+ });
5524
5712
  });