@datarailsshared/dr_renderer 1.4.114 → 1.4.118

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.4.114",
3
+ "version": "1.4.118",
4
4
  "description": "DataRails charts and tables renderer",
5
5
  "keywords": [
6
6
  "datarails",
@@ -641,7 +641,7 @@ let getHighchartsRenderer = function ($, document, Highcharts, default_colors, h
641
641
  highchartsRenderer.persantageValueLabelsFormatter = function (pivotData, opts) {
642
642
  var func = function () {
643
643
  var value = parseFloat(this.value);
644
- return $.pivotUtilities.getFormattedNumber(value, null, null) + "%";
644
+ return $.pivotUtilities.getFormattedNumber(value * 100, null, null) + "%";
645
645
  };
646
646
  return func;
647
647
  };
@@ -702,11 +702,14 @@ let getHighchartsRenderer = function ($, document, Highcharts, default_colors, h
702
702
  var value = parseFloat(this.y);
703
703
 
704
704
  if (pivotData) {
705
+ const isChartWithMultiValues = highchartsRenderer.isChartWithMultiValues(pivotData);
705
706
  let series_name = highchartsRenderer.getSeriesNameInFormatterContext(this);
706
707
  var rows = series_name.split(highchartsRenderer.delimer);
708
+ var cols = highchartsRenderer.getColsInFormatterContext(this);
707
709
 
708
- if (is_drill_down_pie && highchartsRenderer.selfStartsWith(series_name, "Series ")) {
710
+ if (is_drill_down_pie && (highchartsRenderer.selfStartsWith(series_name, "Series ") || isChartWithMultiValues)) {
709
711
  rows = [];
712
+ cols = pivotData.getColKeys()[0];
710
713
  }
711
714
 
712
715
  if (pivotData.rowAttrs.length == 0
@@ -715,7 +718,6 @@ let getHighchartsRenderer = function ($, document, Highcharts, default_colors, h
715
718
  rows = [];
716
719
  }
717
720
 
718
- var cols = highchartsRenderer.getColsInFormatterContext(this);
719
721
  if (typeof (cols) == 'object' && cols && cols.name) {
720
722
  cols = cols.name;
721
723
  }
@@ -740,7 +742,7 @@ let getHighchartsRenderer = function ($, document, Highcharts, default_colors, h
740
742
  try {
741
743
  const percentageArray = [];
742
744
 
743
- if (is_drill_down_pie && !highchartsRenderer.selfStartsWith(series_name, "Series ")) {
745
+ if (is_drill_down_pie && !highchartsRenderer.selfStartsWith(series_name, "Series ") && !isChartWithMultiValues) {
744
746
  let temp = cols;
745
747
  cols = rows;
746
748
  rows = temp;
@@ -863,18 +865,22 @@ let getHighchartsRenderer = function ($, document, Highcharts, default_colors, h
863
865
  var y = parseFloat(this.y);
864
866
 
865
867
  if (pivotData) {
868
+ const isChartWithMultiValues = highchartsRenderer.isChartWithMultiValues(pivotData);
866
869
  let series_name = highchartsRenderer.getSeriesNameInFormatterContext(this);
867
870
  var rows = series_name.split(highchartsRenderer.delimer);
868
- if (is_drill_down_pie && highchartsRenderer.selfStartsWith(series_name,"Series ")) {
871
+ var cols = highchartsRenderer.getColsInFormatterContext(this);
872
+
873
+ if (is_drill_down_pie && (highchartsRenderer.selfStartsWith(series_name,"Series ") || isChartWithMultiValues)) {
869
874
  rows = [];
875
+ cols = pivotData.getColKeys()[0];
870
876
  }
877
+
871
878
  if (pivotData.rowAttrs.length == 0
872
879
  || this.series.options.className === 'totalSeries'
873
880
  || isTrendSeries) {
874
881
  rows = [];
875
882
  }
876
883
 
877
- var cols = highchartsRenderer.getColsInFormatterContext(this);
878
884
  if (lodash.isNil(cols) && is_drill_down_pie) {
879
885
  cols = this.name;
880
886
  }
@@ -891,7 +897,7 @@ let getHighchartsRenderer = function ($, document, Highcharts, default_colors, h
891
897
  }
892
898
 
893
899
  try {
894
- if (is_drill_down_pie && !highchartsRenderer.selfStartsWith(series_name,"Series ")) {
900
+ if (is_drill_down_pie && !highchartsRenderer.selfStartsWith(series_name,"Series ") && !isChartWithMultiValues) {
895
901
  let temp = cols;
896
902
  cols = rows;
897
903
  rows = temp;
@@ -932,9 +938,9 @@ let getHighchartsRenderer = function ($, document, Highcharts, default_colors, h
932
938
  var aggr = pivotData.getAggregator(rows, cols);
933
939
 
934
940
  let formatted_value_to_return = $.pivotUtilities.getFormattedNumber(y, null, opts);
935
- if (aggr.value() || isWaterfallBreakdown) {
941
+ if (aggr.value() || isWaterfallBreakdown || isChartWithMultiValues) {
936
942
  formatted_value_to_return = $.pivotUtilities.getFormattedNumber(
937
- isWaterfallBreakdown || isWaterfallWalkthrough || isTrendSeries ? y : aggr.value(), aggr, opts
943
+ isChartWithMultiValues || isWaterfallBreakdown || isWaterfallWalkthrough || isTrendSeries ? y : aggr.value(), aggr, opts
938
944
  );
939
945
  }
940
946
 
@@ -1918,9 +1924,9 @@ let getHighchartsRenderer = function ($, document, Highcharts, default_colors, h
1918
1924
  lodash.forEach(series.data, function (it, index) {
1919
1925
  if (pivotData.rowKeys && pivotData.rowKeys.length && pivotData.colKeys && pivotData.colKeys.length) {
1920
1926
  const agg = pivotData.getAggregator([pivotData.rowKeys[0]], pivotData.colKeys[index]);
1921
- if (agg) {
1927
+ if (agg && valueFormatter.isAbsoluteValue(delta_column_options.formula)) {
1922
1928
  const baseValue = pivotData.getAggregator([pivotData.rowKeys[0]], pivotData.colKeys[index]).value();
1923
- it.y = valueFormatter.getPercentageValue(it.y, baseValue);
1929
+ it.y = valueFormatter.getRelatedValue(it.y, baseValue);
1924
1930
  }
1925
1931
  }
1926
1932
  });
@@ -2056,17 +2062,14 @@ let getHighchartsRenderer = function ($, document, Highcharts, default_colors, h
2056
2062
  col_ob.name = highchartsRenderer.getFormattedColKey(col_ob.initialName, pivotData);
2057
2063
  col_ob.data = [];
2058
2064
  lodash.forEach(row_n_keys, function (row_n_value) {
2059
- // always takes the first key during drilldown,
2060
- // we are only supporting one level drilldown but pivot table could have multiple nested row keys
2061
- const rowKey = [row_n_value[0]];
2062
- var agg = pivotData.getAggregator(rowKey, col_n_value);
2065
+ var agg = pivotData.getAggregator(row_n_value, col_n_value);
2063
2066
  var val = agg.value();
2064
- if (val != null && rowKey[0]) {
2067
+ if (val != null && row_n_value.length) {
2065
2068
  if ($.isNumeric(val)) {
2066
2069
  val = parseFloat(val);
2067
2070
  }
2068
2071
 
2069
- const row_initial_name = rowKey[0] === highchartsRenderer.DR_OTHERS_KEY ? othersName : rowKey;
2072
+ const row_initial_name = row_n_value[0] === highchartsRenderer.DR_OTHERS_KEY ? othersName : row_n_value;
2070
2073
  const row_name = highchartsRenderer.getFormattedRowKey(row_initial_name, pivotData);
2071
2074
  col_ob.data.push({
2072
2075
  name: row_name,
@@ -2075,11 +2078,20 @@ let getHighchartsRenderer = function ($, document, Highcharts, default_colors, h
2075
2078
  });
2076
2079
  }
2077
2080
  });
2081
+
2082
+ col_ob.data = Object.values(
2083
+ lodash.reduce(col_ob.data, (acc, item) => {
2084
+ const key = Array.isArray(item.name) ? item.name[0] : item.name;
2085
+ const name = key ? [key] : [col_n];
2086
+ if (!acc[key]) {
2087
+ acc[key] = { name: name, initialName: name, y: 0 };
2088
+ }
2089
+ acc[key].y += item.y;
2090
+ return acc;
2091
+ }, {})
2092
+ );
2093
+
2078
2094
  if (col_ob.data.length) {
2079
- // filtering out when there are multiple data series
2080
- col_ob.data = lodash.uniqBy(col_ob.data, item =>
2081
- Array.isArray(item.name) ? item.name.join(highchartsRenderer.delimer) : item.name
2082
- );
2083
2095
  pie_drill_down_series.push(col_ob);
2084
2096
  }
2085
2097
  }
@@ -2130,7 +2142,7 @@ let getHighchartsRenderer = function ($, document, Highcharts, default_colors, h
2130
2142
 
2131
2143
  highchartsRenderer.addSecondYAxis = function (pivotData, chartOptions, additionOptions, opts) {
2132
2144
  const varianceColor = (additionOptions && additionOptions.delta_column.color) || (highchartsRenderer && highchartsRenderer.variance_color) || Highcharts.getOptions().colors[7];
2133
- const forcePercentage = lodash.get(opts, 'comboOptions.secondaryAxisSettings.is_percentage', false);
2145
+ const forcePercentage = lodash.get(opts, 'comboOptions.secondaryAxisSettings.is_percentage', false) || lodash.get(additionOptions, 'delta_column.is_percentage', false);
2134
2146
 
2135
2147
  chartOptions.yAxis = [chartOptions.yAxis];
2136
2148
  chartOptions.yAxis[1] = {
@@ -3749,8 +3761,7 @@ let getHighchartsRenderer = function ($, document, Highcharts, default_colors, h
3749
3761
  }
3750
3762
 
3751
3763
  highchartsRenderer.isRowKeyShouldBePercentage = function(render_options, rowKey) {
3752
- if (render_options && render_options.comboOptions && render_options.comboOptions.secondaryAxisSettings &&
3753
- render_options.comboOptions.secondaryAxisSettings.is_percentage) {
3764
+ if (lodash.get(render_options, 'comboOptions.secondaryAxisSettings.is_percentage', false) || lodash.get(render_options, 'chartOptions.delta_column.is_percentage', false)) {
3754
3765
  const rowKeyString = rowKey.join(highchartsRenderer.delimer);
3755
3766
  const rowKeyOptions = lodash.find(render_options.comboOptions.seriesOptions, {series: rowKeyString});
3756
3767
  if (rowKeyOptions && rowKeyOptions.secondaryAxis) {
@@ -5308,7 +5319,6 @@ let getHighchartsRenderer = function ($, document, Highcharts, default_colors, h
5308
5319
  break;
5309
5320
  }
5310
5321
  options.comboOptions.seriesOptions.push(deltaColumnSeries);
5311
- options.comboOptions.secondaryAxisSettings.is_percentage = options.chartOptions.delta_column.is_percentage;
5312
5322
  }
5313
5323
  };
5314
5324
 
@@ -9982,7 +9992,7 @@ let getHighchartsRenderer = function ($, document, Highcharts, default_colors, h
9982
9992
 
9983
9993
  // Check if chart has multiple values
9984
9994
  highchartsRenderer.isChartWithMultiValues = function(pivotData) {
9985
- return lodash.get(pivotData, 'rowAttrs[0]') === 'DR_Values';
9995
+ return lodash.some(lodash.get(pivotData, 'rowAttrs'), attr => attr === 'DR_Values');
9986
9996
  }
9987
9997
 
9988
9998
  highchartsRenderer.checkFormats = function(render_options, widget_values_format) {
@@ -1,30 +1,41 @@
1
1
  const lodash = require('lodash');
2
2
 
3
3
  function getAggregatorPercentageValueIfRequired(value, render_options, data, rowKey, colKey) {
4
- const isPercentage = lodash.get(render_options, 'comboOptions.secondaryAxisSettings.is_percentage', false);
5
- const deltaColumnName = lodash.get(render_options, 'chartOptions.delta_column.name', '');
4
+ const deltaColumn = lodash.get(render_options, 'chartOptions.delta_column', null);
5
+ const isPercentage = lodash.get(render_options, 'comboOptions.secondaryAxisSettings.is_percentage', false) || deltaColumn && deltaColumn.is_percentage;
6
6
  const currentRowName = rowKey && rowKey.length ? String(rowKey[0]) : '';
7
- const isVariance = currentRowName.replace('_', '').toLowerCase() === deltaColumnName.replace('_', '').toLowerCase();
7
+ const isVariance = deltaColumn && currentRowName.replace('_', '').toLowerCase() === deltaColumn.name.replace('_', '').toLowerCase();
8
8
  const baseRowKey = data && data.rowKeys && data.rowKeys.length ? data.rowKeys[0] : null;
9
9
  const currentColKey = colKey ? colKey : [];
10
10
  const agg = data && baseRowKey ? data.getAggregator(baseRowKey, currentColKey) : null;
11
11
 
12
12
  if (isPercentage && isVariance && baseRowKey && agg) {
13
- const baseValue = agg.value();
14
- return getPercentageValue(value, baseValue) + '%';
13
+ if (deltaColumn && isAbsoluteValue(deltaColumn.formula)) {
14
+ value = getRelatedValue(value, agg.value());
15
+ }
16
+
17
+ return Math.round(value * 100) + '%';
15
18
  }
16
19
 
17
20
  return null;
18
21
  };
19
22
 
20
- function getPercentageValue(value, baseValue) {
23
+ function getRelatedValue(value, baseValue) {
21
24
  if (!baseValue)
22
- return value < 0 ? -100 : 100;
25
+ return value < 0 ? -1 : 1;
23
26
 
24
- return Math.round(value / baseValue * 100);
27
+ return value / baseValue;
28
+ };
29
+
30
+ function isAbsoluteValue(formula) {
31
+ if (!formula)
32
+ return false;
33
+
34
+ return lodash.includes(formula.replace(/\s+/g, ''), 'x2-x1');
25
35
  };
26
36
 
27
37
  module.exports = {
28
38
  getAggregatorPercentageValueIfRequired,
29
- getPercentageValue
39
+ getRelatedValue,
40
+ isAbsoluteValue
30
41
  };
@@ -453,7 +453,7 @@ describe('highcharts_renderer', () => {
453
453
  let funcContext = { value: '12345.678' };
454
454
  let persantageValueLabelsFormatterFn = highchartsRenderer.persantageValueLabelsFormatter(null, {})
455
455
  let result = persantageValueLabelsFormatterFn.call(funcContext)
456
- expect(result).toBe('12,345.678%');
456
+ expect(result).toBe('1,234,567.8%');
457
457
  });
458
458
  });
459
459
 
@@ -545,6 +545,7 @@ describe('highcharts_renderer', () => {
545
545
  mockPivotData = {
546
546
  rowAttrs: ['row1'],
547
547
  colAttrs: ['col1'],
548
+ getColKeys: jest.fn(() => [['col1'], ['col2']]),
548
549
  getRowKeys: jest.fn(() => [['row1'], ['row2']]),
549
550
  getAggregator: jest.fn(() => ({
550
551
  value: () => 1000
@@ -561,6 +562,7 @@ describe('highcharts_renderer', () => {
561
562
  });
562
563
  spyOn(highchartsRenderer, 'replaceDrOthersKeys');
563
564
  spyOn(highchartsRenderer, 'selfStartsWith').and.returnValue(false);
565
+ spyOn(highchartsRenderer, 'isChartWithMultiValues').and.returnValue(true);
564
566
 
565
567
  global.$ = {
566
568
  pivotUtilities: {
@@ -658,6 +660,17 @@ describe('highcharts_renderer', () => {
658
660
  expect(result).toBe('500');
659
661
  });
660
662
 
663
+ it('should handle drill-down pie when is a multi values chart', () => {
664
+ highchartsRenderer.isChartWithMultiValues.and.returnValue(true);
665
+
666
+ opts = { chartOptions: {} };
667
+ let fn = highchartsRenderer.defaultDataLabelFormatter(mockPivotData, opts, true);
668
+ let result = fn.call(funcContext);
669
+
670
+ expect(highchartsRenderer.isChartWithMultiValues).toHaveBeenCalledWith(mockPivotData);
671
+ expect(result).toBe('500');
672
+ });
673
+
661
674
  it('should use point name for columns when cols is null in drill-down pie', () => {
662
675
  highchartsRenderer.getColsInFormatterContext.and.returnValue(null);
663
676
  funcContext.point.name = 'DrillDownPoint';
@@ -2057,7 +2070,7 @@ describe('highcharts_renderer', () => {
2057
2070
  field: 'series',
2058
2071
  name: 'TEST_test',
2059
2072
  same_yaxis: true,
2060
- is_percentage: true,
2073
+ is_percentage: false,
2061
2074
  }
2062
2075
  }
2063
2076
  };
@@ -2074,7 +2087,7 @@ describe('highcharts_renderer', () => {
2074
2087
  name: 'Secondary Axis',
2075
2088
  max: null,
2076
2089
  min: null,
2077
- is_percentage: true
2090
+ is_percentage: false
2078
2091
  },
2079
2092
  seriesOptions: [{
2080
2093
  series: 'TEST_test',
@@ -405,6 +405,26 @@ describe('ptCreateDrillDownSeriesToDrilldownChart', () => {
405
405
  );
406
406
  expect(highchartsRenderer.getDataLabelsStylesForDrillDown).toHaveBeenCalledWith(mockAdditionOptions);
407
407
  });
408
+
409
+ it('should merge values when row name is a plain string (not array)', () => {
410
+ jest.spyOn(highchartsRenderer, 'getFormattedRowKey').mockImplementation(name => name);
411
+
412
+ mockPivotData.getRowKeys.mockReturnValue([ 'PLAIN_STRING_NAME' ]);
413
+ mockPivotData.getColKeys.mockReturnValue([ ['valid-key'] ]);
414
+ mockPivotData.getAggregator.mockImplementation(() => ({ value: () => 100 }));
415
+
416
+ const result = highchartsRenderer.ptCreateDrillDownSeriesToDrilldownChart(
417
+ mockPivotData,
418
+ mockChartOptions,
419
+ mockAdditionOptions,
420
+ mockOpts
421
+ );
422
+
423
+ expect(result.series).toHaveLength(1);
424
+ const dataPoint = result.series[0].data[0];
425
+ expect(dataPoint.name).toEqual(['PLAIN_STRING_NAME']);
426
+ expect(dataPoint.y).toBe(100);
427
+ });
408
428
  });
409
429
 
410
430
  describe('edge cases', () => {
@@ -1,4 +1,4 @@
1
- const { getAggregatorPercentageValueIfRequired, getPercentageValue } = require('../src/value.formatter');
1
+ const { getAggregatorPercentageValueIfRequired, getRelatedValue, isAbsoluteValue } = require('../src/value.formatter');
2
2
 
3
3
  describe('Function getAggregatorPercentageValueIfRequired', () => {
4
4
  let render_options, rowKey, colKey;
@@ -6,7 +6,12 @@ describe('Function getAggregatorPercentageValueIfRequired', () => {
6
6
  beforeEach(() => {
7
7
  render_options = {
8
8
  comboOptions: { secondaryAxisSettings: { is_percentage: true } },
9
- chartOptions: { delta_column: { name: 'Variance' } }
9
+ chartOptions: {
10
+ delta_column: {
11
+ name: 'Variance',
12
+ formula: 'x2-x1'
13
+ }
14
+ }
10
15
  };
11
16
  rowKey = ['Variance'];
12
17
  colKey = ['SomeCol'];
@@ -82,18 +87,44 @@ describe('Function getAggregatorPercentageValueIfRequired', () => {
82
87
  });
83
88
  });
84
89
 
85
- describe('Function getPercentageValue', () => {
86
- it('should return -100 when baseValue is falsy and value < 0', () => {
87
- expect(getPercentageValue(-50, 0)).toBe(-100);
90
+ describe('Function getRelatedValue', () => {
91
+ it('should return -1 when baseValue is falsy and value < 0', () => {
92
+ expect(getRelatedValue(-50, 0)).toBe(-1);
88
93
  });
89
94
 
90
- it('should return 100 when baseValue is falsy and value >= 0', () => {
91
- expect(getPercentageValue(50, null)).toBe(100);
95
+ it('should return 1 when baseValue is falsy and value >= 0', () => {
96
+ expect(getRelatedValue(50, null)).toBe(1);
92
97
  });
93
98
 
94
- it('should return rounded percentage when baseValue is truthy', () => {
95
- expect(getPercentageValue(25, 200)).toBe(13);
96
- expect(getPercentageValue(50, 200)).toBe(25);
97
- expect(getPercentageValue(200, 200)).toBe(100);
99
+ it('should return related value when baseValue is truthy', () => {
100
+ expect(getRelatedValue(50, 200)).toBe(0.25);
101
+ expect(getRelatedValue(200, 200)).toBe(1);
102
+ });
103
+ });
104
+
105
+ describe('Function isAbsoluteValue', () => {
106
+ it('should return false if formula is undefined', () => {
107
+ expect(isAbsoluteValue(undefined)).toBe(false);
108
+ });
109
+
110
+ it('should return false if formula is null', () => {
111
+ expect(isAbsoluteValue(null)).toBe(false);
112
+ });
113
+
114
+ it('should return false if formula does not contain "x2-x1"', () => {
115
+ expect(isAbsoluteValue('x2 + x1')).toBe(false);
116
+ expect(isAbsoluteValue('x1-x2')).toBe(false);
117
+ expect(isAbsoluteValue('')).toBe(false);
118
+ });
119
+
120
+ it('should return true if formula contains "x2-x1"', () => {
121
+ expect(isAbsoluteValue('x2-x1')).toBe(true);
122
+ expect(isAbsoluteValue(' x2 - x1 ')).toBe(true);
123
+ expect(isAbsoluteValue('some text x2-x1 more text')).toBe(true);
124
+ });
125
+
126
+ it('should ignore spaces in formula', () => {
127
+ expect(isAbsoluteValue('x2 - x1')).toBe(true);
128
+ expect(isAbsoluteValue(' x2 - x1 ')).toBe(true);
98
129
  });
99
130
  });