@datarailsshared/dr_renderer 1.3.40 → 1.3.42

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.3.40",
3
+ "version": "1.3.42",
4
4
  "description": "DataRails charts and tables renderer",
5
5
  "keywords": [
6
6
  "datarails",
@@ -1,3 +1,6 @@
1
+ const { DR_SCENARIO } = require('./smart_queries_helper');
2
+
3
+
1
4
  let initDRPivotTable = function($, window, document) {
2
5
  var hasProp = {}.hasOwnProperty;
3
6
  var slice = [].slice;
@@ -1269,6 +1272,9 @@ let initDRPivotTable = function($, window, document) {
1269
1272
  "rowspan": rowHeader.descendants + 1,
1270
1273
  "colspan": colspan
1271
1274
  });
1275
+ if (opts.chartOptions.isSmartQueriesEnabled) {
1276
+ th.textContent = th.textContent.replace(DR_SCENARIO.Forecast, 'Forecast Smart Query');
1277
+ }
1272
1278
  if (opts.chartOptions.table_options.hide_nulls_in_headers) {
1273
1279
  th.textContent = th.textContent.replace('[null]', '');
1274
1280
  }
@@ -1516,6 +1522,13 @@ let initDRPivotTable = function($, window, document) {
1516
1522
  }
1517
1523
  };
1518
1524
  val = aggregator.value();
1525
+
1526
+ if (opts.chartOptions.isSmartQueriesEnabled && flatRowKey.split(' , ').includes(DR_SCENARIO.Forecast)) {
1527
+ const actualsRow = tree[flatRowKey.replace(DR_SCENARIO.Forecast, DR_SCENARIO.SQ_Actuals)] || {};
1528
+ if (actualsRow && actualsRow[flatColKey]) {
1529
+ val += actualsRow[flatColKey]?.value() || 0;
1530
+ }
1531
+ }
1519
1532
  isColSubtotal = colHeader.children.length !== 0;
1520
1533
  style = "pvtVal";
1521
1534
  if (isColSubtotal) {
@@ -1558,6 +1571,9 @@ let initDRPivotTable = function($, window, document) {
1558
1571
  }
1559
1572
 
1560
1573
  val = totalAggregator.value();
1574
+ if (opts.chartOptions.isSmartQueriesEnabled && flatRowKey.split(' , ').includes(DR_SCENARIO.Forecast)) {
1575
+ val += rowTotals[flatRowKey.replace(DR_SCENARIO.Forecast, DR_SCENARIO.SQ_Actuals)]?.value() || 0;
1576
+ }
1561
1577
  style = "pvtTotal rowTotal";
1562
1578
  if (isRowSubtotal) {
1563
1579
  style += " pvtRowSubtotal";
@@ -2338,6 +2354,9 @@ let initDRPivotTable = function($, window, document) {
2338
2354
  }
2339
2355
 
2340
2356
  main = function(rowAttrs, rowKeys, colAttrs, colKeys, pivotData) {
2357
+ if (opts.chartOptions.isSmartQueriesEnabled) {
2358
+ rowKeys = rowKeys.filter(rowKey => !rowKey.includes(DR_SCENARIO.SQ_Actuals));
2359
+ }
2341
2360
  var c,rowspan, colHeaderCols, colHeaderHeaders, colHeaders, h, k, l, len, len1, result, rowHeaderHeaders, rowHeaderRows, rowHeaders, tbody, thead, tr;
2342
2361
  rowHeaders = [];
2343
2362
  colHeaders = [];
@@ -1420,14 +1420,18 @@ let getHighchartsRenderer = function ($, document, Highcharts, default_colors, h
1420
1420
 
1421
1421
  const chartType = chartOptions && chartOptions.chart && chartOptions.chart.type ? chartOptions.chart.type : null;
1422
1422
  const smartQuerySeries = isChartTypeSupportedForSmartQuery(chartType)
1423
- ? smartQueriesHelper.createSingleDataSeriesForForecast(chart_series, opts.chartOptions)
1423
+ ? smartQueriesHelper.createSingleDataSeriesForForecast(chart_series, opts.chartOptions, pivotData)
1424
1424
  : null;
1425
1425
 
1426
1426
  if (smartQuerySeries) {
1427
- chart_series.push(smartQuerySeries);
1428
- lodash.remove(chart_series, s =>
1427
+ if (chart_series.length > 1) {
1428
+ lodash.remove(chart_series, s =>
1429
1429
  (s.name && lodash.includes(s.name, 'SQ_Actuals')) || s.name === 'Forecast'
1430
- );
1430
+ );
1431
+ } else {
1432
+ chart_series = []
1433
+ }
1434
+ chart_series.push(smartQuerySeries);
1431
1435
  }
1432
1436
 
1433
1437
  return chart_series;
@@ -9913,9 +9917,8 @@ let getHighchartsRenderer = function ($, document, Highcharts, default_colors, h
9913
9917
  const initialNameForSmartQueries = seriesData
9914
9918
  ? lodash.find(seriesData, obj => obj.name === e.point.name)
9915
9919
  : null;
9916
-
9917
9920
  if (initialNameForSmartQueries && initialNameForSmartQueries.type) {
9918
- e.point.series.name = initialNameForSmartQueries.type;
9921
+ e.point.series.name = initialNameForSmartQueries.type === "SQ_Actuals" ? 'Actuals' : 'Forecast';
9919
9922
  }
9920
9923
  lodash.set(e, 'point.category.userOptions', e.point.initialName.toString().split(highchartsRenderer.delimer));
9921
9924
  }
@@ -1,16 +1,55 @@
1
1
  const lodash = require('lodash');
2
2
 
3
- function createSingleDataSeriesForForecast(chart_series, chartOptions) {
3
+ const DR_SCENARIO = {
4
+ SQ_Actuals: 'SQ_Actuals',
5
+ Forecast: 'Forecast',
6
+ };
7
+
8
+ function createSingleDataSeriesForForecast(chart_series, chartOptions, pivotData) {
4
9
  const { actuals, forecast, smart_query } = chartOptions.chart;
10
+ const input = pivotData.input;
5
11
 
6
- const seriesA = lodash.find(chart_series, function(s) {
7
- return s.name && lodash.includes(s.name, 'SQ_Actuals');
8
- });
9
- const seriesB = lodash.find(chart_series, function(s) {
10
- return s.name && lodash.includes(s.name, 'Forecast');
11
- });
12
+ const hasSQActuals = input.some(item => item.Scenario && item.Scenario.indexOf(DR_SCENARIO.SQ_Actuals) !== -1);
13
+ chartOptions.isSmartQueriesEnabled = hasSQActuals;
14
+ if (!smart_query || !hasSQActuals) return null;
15
+
16
+ const midMonthOffset = 0.5
17
+ return chart_series.length === 1
18
+ ? buildChartSeriesFromPivotInputOnly(input, actuals, forecast, midMonthOffset)
19
+ : buildChartSeriesFromSeries(chart_series, actuals, forecast, midMonthOffset);
20
+ }
21
+
22
+ function buildChartSeriesFromPivotInputOnly(input, actuals, forecast, midMonthOffset) {
23
+ const filtered = input.filter(item =>
24
+ (item.Scenario === DR_SCENARIO.SQ_Actuals || item.Scenario === DR_SCENARIO.Forecast) &&
25
+ item.Amount !== 0
26
+ );
27
+
28
+ const data = filtered.map(item => ({
29
+ y: item.Amount,
30
+ name: item.Reporting_Month,
31
+ initialName: item.Reporting_Month,
32
+ type: item.Scenario,
33
+ })).sort((a, b) => new Date(a.name) - new Date(b.name));
12
34
 
13
- if (!seriesA || !seriesB || !smart_query) return null;
35
+ const sqCount = input.filter(item => item.Scenario === DR_SCENARIO.SQ_Actuals).length;
36
+
37
+ return {
38
+ name: "Forecast Smart Query",
39
+ data,
40
+ zoneAxis: "x",
41
+ zones: [
42
+ { value: sqCount - midMonthOffset, dashStyle: actuals },
43
+ { dashStyle: forecast },
44
+ ],
45
+ };
46
+ }
47
+
48
+ function buildChartSeriesFromSeries(chart_series, actuals, forecast, midMonthOffset) {
49
+ const seriesA = lodash.find(chart_series, s => s.name && lodash.includes(s.name, DR_SCENARIO.SQ_Actuals));
50
+ const seriesB = lodash.find(chart_series, s => s.name && lodash.includes(s.name, DR_SCENARIO.Forecast));
51
+
52
+ if (!seriesA || !seriesB) return null;
14
53
 
15
54
  const indexOfForecastFirstZero = lodash.findIndex(seriesB.data, function(value) {
16
55
  return value.y === 0;
@@ -20,17 +59,9 @@ function createSingleDataSeriesForForecast(chart_series, chartOptions) {
20
59
  });
21
60
  const cutoffIndex = Math.max(indexOfForecastFirstZero, indexOfActualsFirstZero);
22
61
 
23
- const derivedSeries = {
24
- name: "Forecast Smart Query",
25
- data: [],
26
- zoneAxis: "x",
27
- zones: [
28
- { value: cutoffIndex - 1, dashStyle: actuals },
29
- { dashStyle: forecast },
30
- ],
31
- };
32
62
 
33
63
  const minLength = Math.min(seriesA.data.length, seriesB.data.length);
64
+ const data = [];
34
65
 
35
66
  for (let i = 0; i < minLength; i++) {
36
67
  const pointA = seriesA.data[i];
@@ -39,19 +70,27 @@ function createSingleDataSeriesForForecast(chart_series, chartOptions) {
39
70
  const yA = (pointA && pointA.y !== null && typeof pointA.y !== undefined) ? pointA.y : pointA;
40
71
  const yB = (pointB && pointB.y !== null && typeof pointB.y !== undefined) ? pointB.y : pointB;
41
72
 
42
- derivedSeries.data.push({
73
+ data.push({
43
74
  x: i,
44
- y: yA || yB,
75
+ y: yA + yB,
45
76
  name: pointA && pointA.name ? pointA.name : null,
46
77
  initialName: (pointA && pointA.name) ? pointA.name : 'Point ' + (i + 1),
47
- type: yA ? "SQ_Actuals" : "Forecast",
78
+ type: yA ? DR_SCENARIO.SQ_Actuals : DR_SCENARIO.Forecast,
48
79
  });
49
80
  }
50
81
 
51
- return derivedSeries;
82
+ return {
83
+ name: "Forecast Smart Query",
84
+ data,
85
+ zoneAxis: "x",
86
+ zones: [
87
+ { value: cutoffIndex - midMonthOffset, dashStyle: actuals },
88
+ { dashStyle: forecast },
89
+ ],
90
+ };
52
91
  }
53
92
 
54
-
55
93
  module.exports = {
56
94
  createSingleDataSeriesForForecast,
95
+ DR_SCENARIO
57
96
  };