dtable-statistic 4.4.24-alpha.9 → 5.0.0

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.
Files changed (198) hide show
  1. package/es/assets/css/color-picker.css +53 -0
  2. package/es/assets/css/color-rules-popover.css +144 -0
  3. package/es/assets/css/dashboard.css +0 -1
  4. package/es/assets/css/dialog.css +0 -30
  5. package/es/assets/css/slider.css +77 -0
  6. package/es/assets/css/statistic-chart.module.css +11 -0
  7. package/es/assets/css/statistic-custom-title.module.css +3 -0
  8. package/es/assets/css/statistic-custom.module.css +4 -0
  9. package/es/assets/css/statistic-numeric-column-item.module.css +43 -0
  10. package/es/assets/css/statistic-time-picker.module.css +21 -0
  11. package/es/calculator/base-calculator.js +80 -0
  12. package/es/calculator/basic-chart-calculator.js +349 -0
  13. package/es/calculator/combination-calculator.js +246 -0
  14. package/es/calculator/compare-bar-calculator.js +197 -0
  15. package/es/calculator/completeness-calculator.js +215 -0
  16. package/es/calculator/copy-value.js +53 -0
  17. package/es/calculator/dashboard-calculator.js +70 -0
  18. package/es/calculator/heat-map-calculator.js +148 -0
  19. package/es/calculator/horizontal-bar-calculator.js +67 -0
  20. package/es/calculator/index.js +81 -0
  21. package/es/calculator/map-calculator.js +103 -0
  22. package/es/calculator/mirror-calculator.js +146 -0
  23. package/es/calculator/number-card-calculator.js +60 -0
  24. package/es/calculator/pivot-table-calculator.js +821 -0
  25. package/es/calculator/scatter-calculator.js +75 -0
  26. package/es/calculator/thread-manager.js +57 -0
  27. package/es/calculator/trend-calculator.js +113 -0
  28. package/es/calculator/workers/basic-chart-calculator-worker.js +495 -0
  29. package/es/calculator/workers/calculator.worker.js +44 -0
  30. package/es/calculator/workers/card-calculator-worker.js +40 -0
  31. package/es/calculator/workers/combination-calculator-worker.js +276 -0
  32. package/es/calculator/workers/compare-bar-chart-calculator-worker.js +150 -0
  33. package/es/calculator/workers/completeness-calculator-worker.js +208 -0
  34. package/es/calculator/workers/dashboard-calculator-worker.js +68 -0
  35. package/es/calculator/workers/mirror-calculator-worker.js +152 -0
  36. package/es/calculator/workers/pivot-table-calculator-worker.js +718 -0
  37. package/es/calculator/workers/scatter-calculator-worker.js +80 -0
  38. package/es/calculator/workers/trend-calculator-worker.js +108 -0
  39. package/es/calculator/world-map-calculator.js +124 -0
  40. package/es/components/dialog/chart-addition-edit-dialog.js +90 -0
  41. package/es/components/dialog/chart-addition-widgets/chart-selector.js +259 -0
  42. package/es/components/dialog/chart-addition-widgets/statistic-chart-selector.module.css +83 -0
  43. package/es/components/dialog/enlarged-chart-dialog.js +13 -8
  44. package/es/components/dialog/statistic-record-dialog/index.js +34 -25
  45. package/es/components/dialog/statistic-types-dialog/index.css +14 -0
  46. package/es/components/dialog/statistic-types-dialog/index.js +62 -0
  47. package/es/components/dropdown-menu/statistic-dropdown-menu.js +2 -2
  48. package/es/components/popover/color-rules/color-rule.js +183 -0
  49. package/es/components/popover/color-rules/index.js +87 -0
  50. package/es/components/popover/color-rules/rule-filters/filter.js +221 -0
  51. package/es/components/popover/color-rules/rule-filters/index.css +210 -0
  52. package/es/components/popover/color-rules/rule-filters/index.js +97 -0
  53. package/es/components/popover/color-rules/rule-filters/number-input.js +78 -0
  54. package/es/components/popover/color-rules-popover.js +218 -0
  55. package/es/components/popover/color-selector-popover.js +83 -0
  56. package/es/constants/index.js +2 -1
  57. package/es/custom-g2.js +644 -0
  58. package/es/dashboard.js +31 -9
  59. package/es/desktop-dashboard.js +32 -51
  60. package/es/mobile-dashboard.js +16 -8
  61. package/es/model/bar-group.js +58 -0
  62. package/es/model/bar.js +47 -0
  63. package/es/model/base-model.js +18 -0
  64. package/es/model/basic-number-card.js +25 -0
  65. package/es/model/combination.js +48 -0
  66. package/es/model/compare-bar.js +56 -0
  67. package/es/model/completeness-group.js +31 -0
  68. package/es/model/completeness.js +25 -0
  69. package/es/model/custom-bar.js +25 -0
  70. package/es/model/dashboard.js +20 -0
  71. package/es/model/generic-model.js +222 -0
  72. package/es/model/heat-map.js +33 -0
  73. package/es/model/horizontal-bar-group.js +55 -0
  74. package/es/model/horizontal-bar.js +47 -0
  75. package/es/model/index.js +187 -0
  76. package/es/model/map.js +38 -0
  77. package/es/model/mirror.js +33 -0
  78. package/es/model/pie.js +38 -0
  79. package/es/model/ring.js +40 -0
  80. package/es/model/scatter.js +22 -0
  81. package/es/model/table.js +32 -0
  82. package/es/model/trend.js +32 -0
  83. package/es/model/world-map.js +34 -0
  84. package/es/service/chart-service.js +15 -8
  85. package/es/service/dashboard-service.js +29 -25
  86. package/es/stat-editor/chart-name-editor.js +68 -0
  87. package/es/stat-editor/index.js +75 -0
  88. package/es/stat-editor/stat-settings/advance-chart-settings/basic-number-card-settings.js +153 -0
  89. package/es/stat-editor/stat-settings/advance-chart-settings/combination-settings.js +422 -0
  90. package/es/stat-editor/stat-settings/advance-chart-settings/dashboard-chart-settings.js +203 -0
  91. package/es/stat-editor/stat-settings/advance-chart-settings/geo-granularity-settings.js +23 -0
  92. package/es/stat-editor/stat-settings/advance-chart-settings/heat-map-settings.js +104 -0
  93. package/es/stat-editor/stat-settings/advance-chart-settings/index.js +334 -0
  94. package/es/stat-editor/stat-settings/advance-chart-settings/map-settings.js +119 -0
  95. package/es/stat-editor/stat-settings/advance-chart-settings/mirror-settings.js +132 -0
  96. package/es/stat-editor/stat-settings/advance-chart-settings/style-setting/combination-style-setting.js +240 -0
  97. package/es/stat-editor/stat-settings/advance-chart-settings/style-setting/heat-map-settings.js +89 -0
  98. package/es/stat-editor/stat-settings/advance-chart-settings/style-setting/map-setting.js +138 -0
  99. package/es/stat-editor/stat-settings/advance-chart-settings/summary-settings.js +331 -0
  100. package/es/stat-editor/stat-settings/advance-chart-settings/trend-chart-settings.js +143 -0
  101. package/es/stat-editor/stat-settings/advance-chart-settings/world-map-settings.js +109 -0
  102. package/es/stat-editor/stat-settings/basic-chart-settings/advance-bar-chart-settings.js +153 -0
  103. package/es/stat-editor/stat-settings/basic-chart-settings/bar-settings.js +144 -0
  104. package/es/stat-editor/stat-settings/basic-chart-settings/completeness-chart-settings.js +214 -0
  105. package/es/stat-editor/stat-settings/basic-chart-settings/custom-bar-settings.js +121 -0
  106. package/es/stat-editor/stat-settings/basic-chart-settings/groupby-settings.js +172 -0
  107. package/es/stat-editor/stat-settings/basic-chart-settings/horizontal-axis-group-settings.js +366 -0
  108. package/es/stat-editor/stat-settings/basic-chart-settings/horizontal-bar-settings.js +142 -0
  109. package/es/stat-editor/stat-settings/basic-chart-settings/horizontal-group-chart-settings.js +150 -0
  110. package/es/stat-editor/stat-settings/basic-chart-settings/index.js +473 -0
  111. package/es/stat-editor/stat-settings/basic-chart-settings/pie-settings.js +182 -0
  112. package/es/stat-editor/stat-settings/basic-chart-settings/pivot-table-settings.js +564 -0
  113. package/es/stat-editor/stat-settings/basic-chart-settings/scatter-settings.js +112 -0
  114. package/es/stat-editor/stat-settings/basic-chart-settings/stack-item-settings.js +87 -0
  115. package/es/stat-editor/stat-settings/basic-chart-settings/stacks-settings.js +187 -0
  116. package/es/stat-editor/stat-settings/basic-chart-settings/style-setting/bar-chart-style-setting.js +300 -0
  117. package/es/stat-editor/stat-settings/basic-chart-settings/style-setting/completeness-style.js +113 -0
  118. package/es/stat-editor/stat-settings/basic-chart-settings/style-setting/horizontal-bar-chart-style.js +289 -0
  119. package/es/stat-editor/stat-settings/basic-chart-settings/style-setting/label-font-size-editor.js +58 -0
  120. package/es/stat-editor/stat-settings/basic-chart-settings/style-setting/pie-chart-style-settings.js +359 -0
  121. package/es/stat-editor/stat-settings/basic-chart-settings/style-setting/time-compare-style.js +48 -0
  122. package/es/stat-editor/stat-settings/basic-chart-settings/summary-method-setting.js +121 -0
  123. package/es/stat-editor/stat-settings/basic-chart-settings/summary-settings.js +143 -0
  124. package/es/stat-editor/stat-settings/basic-chart-settings/time-comparison-settings.js +269 -0
  125. package/es/stat-editor/stat-settings/basic-chart-settings/timer-picker.js +104 -0
  126. package/es/stat-editor/stat-settings/basic-chart-settings/y-axis-group-settings.js +366 -0
  127. package/es/stat-editor/stat-settings/color-setting/color-group-selector.js +55 -0
  128. package/es/stat-editor/stat-settings/color-setting/color-picker.js +130 -0
  129. package/es/stat-editor/stat-settings/color-setting/color-use-type-selector.js +374 -0
  130. package/es/stat-editor/stat-settings/map/map-level.js +72 -0
  131. package/es/stat-editor/stat-settings/map/map-province-city.js +151 -0
  132. package/es/stat-editor/stat-settings/public-setting/axis-label-position-setting.js +108 -0
  133. package/es/stat-editor/stat-settings/public-setting/base-settings.js +154 -0
  134. package/es/stat-editor/stat-settings/public-setting/calender.js +125 -0
  135. package/es/stat-editor/stat-settings/public-setting/column-settings.js +24 -0
  136. package/es/stat-editor/stat-settings/public-setting/custom-title-setting.js +62 -0
  137. package/es/stat-editor/stat-settings/public-setting/data-sort-setting.js +61 -0
  138. package/es/stat-editor/stat-settings/public-setting/ind-toggle-setting.js +35 -0
  139. package/es/stat-editor/stat-settings/public-setting/min-max-setting.js +60 -0
  140. package/es/stat-editor/stat-settings/public-setting/numeric-summary-item.js +123 -0
  141. package/es/stat-editor/stat-settings/public-setting/toggle-setting.js +33 -0
  142. package/es/stat-editor/stat-settings/public-setting/type-settings/index.css +29 -0
  143. package/es/stat-editor/stat-settings/public-setting/type-settings/index.js +82 -0
  144. package/es/stat-list/chart-preview.js +20 -19
  145. package/es/stat-list/index.js +21 -15
  146. package/es/stat-view/area-chart.js +551 -0
  147. package/es/stat-view/bar-chart.js +582 -0
  148. package/es/stat-view/base-chart.js +132 -0
  149. package/es/stat-view/basic-number-card.js +201 -0
  150. package/es/stat-view/combination-chart.js +546 -0
  151. package/es/stat-view/compare-chart.js +483 -0
  152. package/es/stat-view/completeness-chart.js +390 -0
  153. package/es/stat-view/custom-bar.js +418 -0
  154. package/es/stat-view/dashboard-chart.js +266 -0
  155. package/es/stat-view/heat-map.js +463 -0
  156. package/es/stat-view/horizontal-bar-chart.js +562 -0
  157. package/es/stat-view/index.js +156 -47
  158. package/es/stat-view/line-chart.js +518 -0
  159. package/es/stat-view/map.js +448 -0
  160. package/es/stat-view/mirror.js +275 -0
  161. package/es/stat-view/pie-chart.js +290 -0
  162. package/es/stat-view/pivot-table/index.js +196 -0
  163. package/es/stat-view/pivot-table/one-dimension-table-no-numeric-columns.js +156 -0
  164. package/es/stat-view/pivot-table/one-dimension-table-with-numeric-columns.js +176 -0
  165. package/es/stat-view/pivot-table/pivot-table-display-name.js +215 -0
  166. package/es/stat-view/pivot-table/statistic-pivot-table.module.css +132 -0
  167. package/es/stat-view/pivot-table/two-dimension-table.js +384 -0
  168. package/es/stat-view/ring-chart.js +392 -0
  169. package/es/stat-view/scatter-chart.js +306 -0
  170. package/es/stat-view/treemap-chart.js +253 -0
  171. package/es/stat-view/trend-chart.js +249 -0
  172. package/es/stat-view/world-map.js +431 -0
  173. package/es/tabs/tab.js +2 -2
  174. package/es/utils/basic-chart-utils.js +29 -0
  175. package/es/utils/cell-format.js +115 -0
  176. package/es/utils/collaborator.js +50 -0
  177. package/es/utils/color-utils.js +134 -0
  178. package/es/utils/column-utils.js +110 -0
  179. package/es/utils/column.js +20 -0
  180. package/es/utils/common-utils.js +352 -2
  181. package/es/utils/date-format.js +71 -0
  182. package/es/utils/export-table-utils.js +69 -0
  183. package/es/utils/index.js +89 -12
  184. package/es/utils/map.js +114 -0
  185. package/es/utils/model.js +19 -0
  186. package/es/utils/row-utils.js +134 -0
  187. package/es/utils/search.js +73 -0
  188. package/es/utils/sql-utils.js +359 -0
  189. package/es/utils/stat-utils.js +351 -0
  190. package/es/utils/trend-utils.js +135 -0
  191. package/package.json +8 -6
  192. package/es/components/dialog/chart-edit-dialog.js +0 -93
  193. package/es/components/dtable-popover.js +0 -97
  194. package/es/components/dtable-search-input.js +0 -135
  195. package/es/components/index.js +0 -41
  196. package/es/components/loading.js +0 -15
  197. package/es/components/modal-portal.js +0 -26
  198. package/es/model/conver-statItem.js +0 -124
@@ -0,0 +1,718 @@
1
+ "use strict";
2
+
3
+ var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault").default;
4
+ Object.defineProperty(exports, "__esModule", {
5
+ value: true
6
+ });
7
+ exports.default = calculateStatItem;
8
+ var _dtableUtils = require("dtable-utils");
9
+ var _constants = require("../../constants");
10
+ var _commonUtils = require("../../utils/common-utils");
11
+ var _rowUtils = require("../../utils/row-utils");
12
+ var _statUtils = _interopRequireDefault(require("../../utils/stat-utils"));
13
+ function calculateStatItem(chart, value, _ref) {
14
+ let {
15
+ getViewRows,
16
+ getTableFormulaResults
17
+ } = _ref;
18
+ const {
19
+ groupby_column_key,
20
+ column_groupby_column_key
21
+ } = chart;
22
+ if (!groupby_column_key) {
23
+ return {};
24
+ }
25
+ if (!column_groupby_column_key) {
26
+ return calculateOneDimensionTable(chart, value, {
27
+ getViewRows,
28
+ getTableFormulaResults
29
+ });
30
+ }
31
+ return calculateTwoDimensionTable(chart, value, {
32
+ getViewRows,
33
+ getTableFormulaResults
34
+ });
35
+ }
36
+ async function calculateOneDimensionTable(chart, value, _ref2) {
37
+ let {
38
+ getViewRows,
39
+ getTableFormulaResults
40
+ } = _ref2;
41
+ const {
42
+ table_id,
43
+ view_id,
44
+ summary_type,
45
+ summary_method,
46
+ summary_column_key,
47
+ groupby_column_key,
48
+ groupby_include_empty_cells: include_empty,
49
+ groupby_date_granularity,
50
+ groupby_geolocation_granularity,
51
+ summary_columns_option
52
+ } = chart;
53
+ const table = (0, _dtableUtils.getTableById)(value.tables, table_id);
54
+ const view = table && (0, _dtableUtils.getViewById)(table.views, view_id);
55
+ const column = (0, _dtableUtils.getTableColumnByKey)(table, groupby_column_key);
56
+ if (!column) return {};
57
+ const isColumnDataAsAnArray = (0, _commonUtils.isArrayCellValue)(column);
58
+ const statRows = await getViewRows(view, table);
59
+ const formulaRows = await getTableFormulaResults(table, statRows);
60
+ let pivot_rows = [];
61
+ statRows.forEach(row => {
62
+ const {
63
+ _id: rowId
64
+ } = row;
65
+ const formulaRow = formulaRows[rowId];
66
+ if ((0, _rowUtils.isValidRow)(row, formulaRow, column, include_empty)) {
67
+ const name = _statUtils.default.getGroupLabel(row[column.key], formulaRow, column, groupby_date_granularity, groupby_geolocation_granularity, value);
68
+ let pivotRowIndex;
69
+ if (isColumnDataAsAnArray) {
70
+ if (name.length === 0 && include_empty) {
71
+ let pivotRowIndex = pivot_rows.findIndex(r => !r.name);
72
+ updateOneDimensionRows(pivot_rows, pivotRowIndex, null, row);
73
+ } else {
74
+ name.forEach(n => {
75
+ pivotRowIndex = pivot_rows.findIndex(r => n === r.name);
76
+ updateOneDimensionRows(pivot_rows, pivotRowIndex, n, row);
77
+ });
78
+ }
79
+ } else {
80
+ pivotRowIndex = pivot_rows.findIndex(pivotRow => {
81
+ return isSameName(pivotRow.name, name);
82
+ });
83
+ updateOneDimensionRows(pivot_rows, pivotRowIndex, name, row);
84
+ }
85
+ }
86
+ });
87
+ const summary_columns_option_keys = Array.isArray(summary_columns_option) ? summary_columns_option.map(option => option.key) : [];
88
+ const statisticColumnKeys = [summary_column_key, ...summary_columns_option_keys];
89
+ const validStatisticColumnKeys = statisticColumnKeys;
90
+ let statisticColumns = validStatisticColumnKeys.map(key => {
91
+ const column = (0, _dtableUtils.getTableColumnByKey)(table, key);
92
+ return column ? {
93
+ ...column
94
+ } : null;
95
+ }).filter(Boolean);
96
+ if (statisticColumns.length > 0) {
97
+ statisticColumns[0].method = summary_method;
98
+ }
99
+ Array.isArray(summary_columns_option) && summary_columns_option.forEach((option, index) => {
100
+ if (statisticColumns[index + 1]) {
101
+ statisticColumns[index + 1].method = option.method;
102
+ }
103
+ });
104
+ const {
105
+ pivot_columns_total
106
+ } = getOneDimensionTotal(statisticColumns, summary_type, formulaRows, pivot_rows);
107
+ _statUtils.default.sortStatistics(pivot_rows, column, 'name');
108
+ const pivot_columns = summary_type === _constants.SUMMARY_TYPE.COUNT ? [] : statisticColumns.map(column => {
109
+ return {
110
+ key: column.key,
111
+ method: column.method
112
+ };
113
+ }).filter(item => (0, _dtableUtils.getTableColumnByKey)(table, item.key));
114
+ pivot_rows.forEach(row => {
115
+ row.original_name = row.name;
116
+ row.name = (0, _rowUtils.getFormattedLabel)(column, row.name, value.collaborators);
117
+ });
118
+ return {
119
+ pivot_columns_total,
120
+ pivot_rows,
121
+ pivot_columns,
122
+ formulaRows
123
+ };
124
+ }
125
+ function updateOneDimensionRows(pivot_rows, index, name, row) {
126
+ if (index > -1) {
127
+ let updatedPivotRow = pivot_rows[index];
128
+ updatedPivotRow.rows.push(row);
129
+ pivot_rows[index] = updatedPivotRow;
130
+ } else {
131
+ const updatedPivotRow = {
132
+ name,
133
+ original_name: name,
134
+ rows: [row]
135
+ };
136
+ pivot_rows.push(updatedPivotRow);
137
+ }
138
+ }
139
+ function getOneDimensionTotal(columns, summary_type, formula_rows) {
140
+ let statResult = arguments.length > 3 && arguments[3] !== undefined ? arguments[3] : [];
141
+ let pivot_columns_total = {};
142
+ let dateColumnsTotalArr = {};
143
+ if (Array.isArray(columns) && columns.length > 0) {
144
+ columns.forEach(column => {
145
+ if (column && (0, _dtableUtils.isDateColumn)(column)) {
146
+ dateColumnsTotalArr[column.key] = [];
147
+ }
148
+ });
149
+ }
150
+ Array.isArray(statResult) && statResult.forEach((item, index) => {
151
+ const {
152
+ rows
153
+ } = item;
154
+ let updated = {
155
+ ...item
156
+ };
157
+ let totalMap = {};
158
+ if (summary_type === _constants.SUMMARY_TYPE.COUNT) {
159
+ totalMap['total'] = rows.length;
160
+ pivot_columns_total.total = (pivot_columns_total['total'] || 0) + rows.length;
161
+ } else {
162
+ if (Array.isArray(columns)) {
163
+ if (columns.length === 1) {
164
+ const summaryColumn = columns[0];
165
+ const {
166
+ key,
167
+ method
168
+ } = summaryColumn;
169
+ const total = getTotal(summaryColumn, summary_type, method, rows, formula_rows);
170
+ if (summaryColumn && (0, _dtableUtils.isDateColumn)(summaryColumn)) {
171
+ if (method === _constants.SUMMARY_METHOD_MAP.Distinct_values) {
172
+ totalMap['total'] = total;
173
+ } else {
174
+ totalMap['total'] = total === 0 ? 0 : total.date;
175
+ if (total !== 0) {
176
+ dateColumnsTotalArr[key].push(total);
177
+ }
178
+ }
179
+ if (method === _constants.SUMMARY_METHOD_MAP.Max) {
180
+ pivot_columns_total['total'] = dateColumnsTotalArr[key].length === 0 ? 0 : getDateMaxOrMinTotal(dateColumnsTotalArr[key], _constants.SUMMARY_METHOD_MAP.Max).date;
181
+ } else if (method === _constants.SUMMARY_METHOD_MAP.Min) {
182
+ pivot_columns_total['total'] = dateColumnsTotalArr[key].length === 0 ? 0 : getDateMaxOrMinTotal(dateColumnsTotalArr[key], _constants.SUMMARY_METHOD_MAP.Min).date;
183
+ } else if (method === _constants.SUMMARY_METHOD_MAP.Distinct_values) {
184
+ const pivotColumnTotal = (pivot_columns_total['total'] ? pivot_columns_total['total'] - 0 : 0) + total;
185
+ pivot_columns_total['total'] = Number.parseFloat(pivotColumnTotal.toFixed(8));
186
+ }
187
+ } else {
188
+ const validTotal = total ? total - 0 : 0;
189
+ totalMap['total'] = validTotal;
190
+ const pivotColumnTotal = (pivot_columns_total['total'] ? pivot_columns_total['total'] - 0 : 0) + validTotal;
191
+ pivot_columns_total['total'] = Number.parseFloat(pivotColumnTotal.toFixed(8));
192
+ }
193
+ } else {
194
+ columns.forEach(column => {
195
+ const {
196
+ key,
197
+ method
198
+ } = column;
199
+ const totalKey = key + method;
200
+ const total = getTotal(column, summary_type, method, rows, formula_rows);
201
+ if (column && (0, _dtableUtils.isDateColumn)(column)) {
202
+ if (method === _constants.SUMMARY_METHOD_MAP.Distinct_values) {
203
+ totalMap[totalKey] = total;
204
+ } else {
205
+ totalMap[totalKey] = total === 0 ? 0 : total.date;
206
+ if (total !== 0) {
207
+ dateColumnsTotalArr[key].push(total);
208
+ }
209
+ }
210
+ if (method === _constants.SUMMARY_METHOD_MAP.Max) {
211
+ pivot_columns_total[totalKey] = dateColumnsTotalArr[key].length === 0 ? 0 : getDateMaxOrMinTotal(dateColumnsTotalArr[key], _constants.SUMMARY_METHOD_MAP.Max).date;
212
+ } else if (method === _constants.SUMMARY_METHOD_MAP.Min) {
213
+ pivot_columns_total[totalKey] = dateColumnsTotalArr[key].length === 0 ? 0 : getDateMaxOrMinTotal(dateColumnsTotalArr[key], _constants.SUMMARY_METHOD_MAP.Min).date;
214
+ } else if (method === _constants.SUMMARY_METHOD_MAP.Distinct_values) {
215
+ const pivotColumnTotal = (pivot_columns_total[totalKey] ? pivot_columns_total[totalKey] - 0 : 0) + total;
216
+ pivot_columns_total[totalKey] = Number.parseFloat(pivotColumnTotal.toFixed(8));
217
+ }
218
+ } else {
219
+ const validTotal = total ? total - 0 : 0;
220
+ totalMap[totalKey] = validTotal;
221
+ const pivotColumnTotal = (pivot_columns_total[totalKey] ? pivot_columns_total[totalKey] - 0 : 0) + validTotal;
222
+ pivot_columns_total[totalKey] = Number.parseFloat(pivotColumnTotal.toFixed(8));
223
+ }
224
+ });
225
+ }
226
+ }
227
+ }
228
+ updated.total = totalMap;
229
+ statResult[index] = updated;
230
+ });
231
+ return {
232
+ pivot_columns_total
233
+ };
234
+ }
235
+ async function calculateTwoDimensionTable(chart, value, _ref3) {
236
+ let {
237
+ getViewRows,
238
+ getTableFormulaResults
239
+ } = _ref3;
240
+ const {
241
+ table_id,
242
+ view_id,
243
+ summary_type,
244
+ summary_method,
245
+ summary_column_key,
246
+ groupby_column_key,
247
+ groupby_include_empty_cells: include_empty,
248
+ groupby_date_granularity,
249
+ groupby_geolocation_granularity,
250
+ column_groupby_column_key,
251
+ column_groupby_date_granularity,
252
+ column_groupby_geolocation_granularity,
253
+ summary_columns_option
254
+ } = chart;
255
+ const table = (0, _dtableUtils.getTableById)(value.tables, table_id);
256
+ const view = table && (0, _dtableUtils.getViewById)(table.views, view_id);
257
+ if (!view) return;
258
+ const groupbyColumn = (0, _dtableUtils.getTableColumnByKey)(table, groupby_column_key);
259
+ if (!groupbyColumn) return;
260
+ const isGroupbyColumnDataAsAnArray = (0, _commonUtils.isArrayCellValue)(groupbyColumn);
261
+ const rowGroupbyColumn = (0, _dtableUtils.getTableColumnByKey)(table, column_groupby_column_key);
262
+ if (!rowGroupbyColumn) {
263
+ return calculateOneDimensionTable(chart, value);
264
+ }
265
+ const isRowGroupbyColumnDataAsAnArray = (0, _commonUtils.isArrayCellValue)(rowGroupbyColumn);
266
+ const summaryColumn = (0, _dtableUtils.getTableColumnByKey)(table, summary_column_key);
267
+ const statRows = await getViewRows(view, table);
268
+ const formulaRows = await getTableFormulaResults(table, statRows);
269
+ const pivot_summary_multiple_columns = [];
270
+ summary_columns_option.forEach(item => {
271
+ const column = (0, _dtableUtils.getTableColumnByKey)(table, item.key);
272
+ if (column && (0, _dtableUtils.isNumericColumn)(column)) {
273
+ pivot_summary_multiple_columns.push({
274
+ key: item.key,
275
+ type: column.type,
276
+ data: column.data,
277
+ method: item.method,
278
+ column_name: column.name
279
+ });
280
+ }
281
+ });
282
+
283
+ // [{key: null}, {key: cell_value}, {key: cell_value}, ...]
284
+ let pivot_columns = [];
285
+
286
+ // [
287
+ // {
288
+ // name: '',
289
+ // cells: {
290
+ // [key1]: {rows: [], total: ''},
291
+ // [key2]: {rows: [], total: ''},
292
+ // [key3]: {rows: [], total: ''},
293
+ // }
294
+ // },
295
+ // ...
296
+ // ]
297
+ let pivot_rows = [];
298
+ statRows.forEach(row => {
299
+ const {
300
+ _id: rowId
301
+ } = row;
302
+ const formulaRow = formulaRows[rowId];
303
+ const cellValueForColumn = _statUtils.default.getGroupLabel(row[rowGroupbyColumn.key], formulaRow, rowGroupbyColumn, column_groupby_date_granularity, column_groupby_geolocation_granularity, value);
304
+ if ((0, _rowUtils.isValidRow)(row, formulaRow, rowGroupbyColumn, include_empty)) {
305
+ updateTwoDimensionColumns(value, pivot_columns, rowGroupbyColumn, row, formulaRow, include_empty, column_groupby_date_granularity, column_groupby_geolocation_granularity, isRowGroupbyColumnDataAsAnArray);
306
+ }
307
+ if ((0, _rowUtils.isValidRow)(row, formulaRow, groupbyColumn, include_empty)) {
308
+ const name = _statUtils.default.getGroupLabel(row[groupbyColumn.key], formulaRow, groupbyColumn, groupby_date_granularity, groupby_geolocation_granularity, value);
309
+ let pivotRowIndex;
310
+ if (isGroupbyColumnDataAsAnArray) {
311
+ if (name.length === 0 && include_empty) {
312
+ let pivotRowIndex = pivot_rows.findIndex(r => !r.name);
313
+ updateTwoDimensionRows(pivot_rows, pivot_columns, pivotRowIndex, null, row, isRowGroupbyColumnDataAsAnArray, cellValueForColumn);
314
+ } else {
315
+ name.forEach(n => {
316
+ pivotRowIndex = pivot_rows.findIndex(r => n === r.name);
317
+ updateTwoDimensionRows(pivot_rows, pivot_columns, pivotRowIndex, n, row, isRowGroupbyColumnDataAsAnArray, cellValueForColumn);
318
+ });
319
+ }
320
+ } else {
321
+ pivotRowIndex = pivot_rows.findIndex(pivotRow => {
322
+ return isSameName(pivotRow.name, name);
323
+ });
324
+ updateTwoDimensionRows(pivot_rows, pivot_columns, pivotRowIndex, name, row, isRowGroupbyColumnDataAsAnArray, cellValueForColumn);
325
+ }
326
+ }
327
+ });
328
+ let {
329
+ pivot_columns_total,
330
+ pivot_table_total
331
+ } = getTwoDimensionTotal(summaryColumn, summary_type, summary_method, formulaRows, pivot_rows, pivot_columns);
332
+ _statUtils.default.sortStatistics(pivot_rows, groupbyColumn, 'name');
333
+ _statUtils.default.sortStatistics(pivot_columns, rowGroupbyColumn, 'key');
334
+
335
+ // format shown label(name)
336
+ const map = {};
337
+ pivot_columns.forEach(column => {
338
+ const name = (0, _rowUtils.getFormattedLabel)(rowGroupbyColumn, column.key, value.collaborators);
339
+ map[column.key] = name;
340
+ column.key = name;
341
+ });
342
+ pivot_rows.forEach(row => {
343
+ row.name = (0, _rowUtils.getFormattedLabel)(groupbyColumn, row.name, value.collaborators);
344
+ const cells = {};
345
+ Object.keys(row.cells).forEach(key => {
346
+ cells[map[key]] = row.cells[key];
347
+ });
348
+ row.cells = cells;
349
+ });
350
+ const new_pivot_columns_total = {};
351
+ Object.keys(pivot_columns_total).forEach(key => {
352
+ new_pivot_columns_total[map[key]] = pivot_columns_total[key];
353
+ });
354
+ return {
355
+ pivot_rows,
356
+ pivot_columns,
357
+ pivot_columns_total: new_pivot_columns_total,
358
+ pivot_table_total,
359
+ pivot_summary_multiple_columns,
360
+ formulaRows
361
+ };
362
+ }
363
+ function updateTwoDimensionColumns(value, pivot_columns, column, row, formulaRow, isIncludeEmpty, dateGranularity, geolocationGranularity, isRowGroupbyColumnDataAsAnArray) {
364
+ let key = _statUtils.default.getGroupLabel(row[column.key], formulaRow, column, dateGranularity, geolocationGranularity, value);
365
+ let pivotColumnIndex;
366
+ if (isRowGroupbyColumnDataAsAnArray) {
367
+ if (key.length === 0 && isIncludeEmpty) {
368
+ pivotColumnIndex = pivot_columns.findIndex(r => !r.key);
369
+ if (pivotColumnIndex < 0) {
370
+ pivot_columns.unshift({
371
+ key: null,
372
+ original_key: null
373
+ });
374
+ }
375
+ } else {
376
+ key.forEach(k => {
377
+ pivotColumnIndex = pivot_columns.findIndex(r => r.key === k);
378
+ if (pivotColumnIndex < 0) {
379
+ pivot_columns.push({
380
+ key: k,
381
+ original_key: k
382
+ });
383
+ }
384
+ });
385
+ }
386
+ } else {
387
+ pivotColumnIndex = pivot_columns.findIndex(r => {
388
+ let resKey = r.key;
389
+ return resKey === null && key === null || resKey === undefined && key === undefined || resKey === 0 && key === 0 || resKey === key;
390
+ });
391
+ if (pivotColumnIndex < 0) {
392
+ pivot_columns.push({
393
+ key,
394
+ original_key: key
395
+ });
396
+ }
397
+ }
398
+ }
399
+ function updateTwoDimensionRows(pivot_rows, pivot_columns, index, name, row, isColumnDataAsAnArray, cellValue) {
400
+ if (index > -1) {
401
+ let updatedPivotRow = pivot_rows[index];
402
+ let {
403
+ cells
404
+ } = updatedPivotRow;
405
+ pivot_columns.forEach(c => {
406
+ let {
407
+ key
408
+ } = c;
409
+ if (isSameGroup(isColumnDataAsAnArray, cellValue, key)) {
410
+ if (cells[key]) {
411
+ cells[key].rows.push(row);
412
+ } else {
413
+ cells[key] = {
414
+ rows: [row]
415
+ };
416
+ }
417
+ }
418
+ });
419
+ updatedPivotRow.cells = cells;
420
+ pivot_rows[index] = updatedPivotRow;
421
+ } else {
422
+ let cells = {};
423
+ pivot_columns.forEach(c => {
424
+ let {
425
+ key
426
+ } = c;
427
+ if (isSameGroup(isColumnDataAsAnArray, cellValue, key)) {
428
+ cells[key] = {
429
+ rows: [row]
430
+ };
431
+ }
432
+ });
433
+ pivot_rows.push({
434
+ name,
435
+ original_name: name,
436
+ cells
437
+ });
438
+ }
439
+ }
440
+ function isSameName(prevName, currName) {
441
+ if ((0, _dtableUtils.isNumber)(prevName) && (0, _dtableUtils.isNumber)(currName)) {
442
+ return prevName === currName;
443
+ }
444
+ if (!prevName && !currName) {
445
+ return prevName === null && currName === null || prevName === undefined && currName === undefined || isNaN(prevName) && isNaN(currName);
446
+ }
447
+ return prevName === currName;
448
+ }
449
+ function isSameGroup(isColumnDataAsAnArray, source, target) {
450
+ if (isColumnDataAsAnArray) {
451
+ return (!source || source.length === 0) && !target || source && source.includes(target);
452
+ }
453
+ return source === null && target === null || source === undefined && target === undefined || source === target;
454
+ }
455
+ function getTwoDimensionTotal(summaryColumn, summary_type, summary_method, formula_rows) {
456
+ let pivot_rows = arguments.length > 4 && arguments[4] !== undefined ? arguments[4] : [];
457
+ let pivot_columns = arguments.length > 5 && arguments[5] !== undefined ? arguments[5] : [];
458
+ let pivot_columns_total = {};
459
+ let pivot_table_total = 0;
460
+ let dateColumnsTotalArr = [];
461
+ let {
462
+ type: summary_column_type
463
+ } = summaryColumn || {};
464
+ const isSummaryDateColumn = summaryColumn && (0, _dtableUtils.isDateColumn)(summaryColumn);
465
+ let date_summary_column_format = isSummaryDateColumn ? summaryColumn.data.format : '';
466
+ Array.isArray(pivot_rows) && pivot_rows.forEach(pivotRow => {
467
+ let {
468
+ cells
469
+ } = pivotRow;
470
+ let total = 0;
471
+ Array.isArray(pivot_columns) && pivot_columns.forEach(pivotColumn => {
472
+ const {
473
+ key
474
+ } = pivotColumn;
475
+ if (cells[key]) {
476
+ let {
477
+ rows
478
+ } = cells[key];
479
+ let subTotal = getTotal(summaryColumn, summary_type, summary_method, rows, formula_rows);
480
+ if (isSummaryDateColumn) {
481
+ if (subTotal === 0) {
482
+ cells[key].total = 0;
483
+ } else {
484
+ subTotal.key = key;
485
+ subTotal.name = pivotRow.name;
486
+ dateColumnsTotalArr.push(subTotal);
487
+ cells[key].total = subTotal.date;
488
+ }
489
+ } else {
490
+ cells[key].total = subTotal;
491
+ total += subTotal ? subTotal - 0 : 0;
492
+ let columnTotal = pivot_columns_total[key] ? pivot_columns_total[key] : 0;
493
+ columnTotal += subTotal ? subTotal - 0 : 0;
494
+ pivot_columns_total[key] = columnTotal;
495
+ }
496
+ }
497
+ });
498
+ if (summary_column_type !== 'date') {
499
+ pivotRow.total = Number.parseFloat(total.toFixed(8));
500
+ pivot_table_total += total;
501
+ }
502
+ });
503
+ if (summaryColumn && (0, _dtableUtils.isDateColumn)(summaryColumn)) {
504
+ if (summary_method === _constants.SUMMARY_METHOD_MAP.Distinct_values) {
505
+ pivot_columns_total = Number.parseFloat(pivot_table_total.toFixed(8));
506
+ } else {
507
+ pivot_columns_total = showTwoDimensionDateColumnsAndRowsTotal(dateColumnsTotalArr, pivot_rows, date_summary_column_format, pivot_columns, summary_method);
508
+ }
509
+ if (summary_method === 'Max') {
510
+ pivot_table_total = dateColumnsTotalArr.length === 0 ? 0 : getDateMaxOrMinTotal(dateColumnsTotalArr, 'Max').date;
511
+ } else if (summary_method === 'Min') {
512
+ pivot_table_total = dateColumnsTotalArr.length === 0 ? 0 : getDateMaxOrMinTotal(dateColumnsTotalArr, 'Min').date;
513
+ }
514
+ } else {
515
+ pivot_table_total = Number.parseFloat(pivot_table_total.toFixed(8));
516
+ }
517
+ return {
518
+ pivot_columns_total,
519
+ pivot_table_total
520
+ };
521
+ }
522
+ function getTotal(summary_column, summary_type, summary_method) {
523
+ let rows = arguments.length > 3 && arguments[3] !== undefined ? arguments[3] : [];
524
+ let formula_rows = arguments.length > 4 && arguments[4] !== undefined ? arguments[4] : {};
525
+ const summary_column_key = summary_column ? summary_column.key : '';
526
+ const summary_column_type = summary_column ? summary_column.type : '';
527
+ const rowsLength = rows.length;
528
+ let total;
529
+ if (summary_type === _constants.SUMMARY_TYPE.COUNT) {
530
+ total = rowsLength;
531
+ } else if (summary_type === _constants.SUMMARY_TYPE.ADVANCED) {
532
+ if (summary_method === _constants.SUMMARY_METHOD_MAP.Distinct_values) {
533
+ total = 0;
534
+ let existMap = {};
535
+ rows.forEach(row => {
536
+ let num;
537
+ if (_dtableUtils.FORMULA_COLUMN_TYPES_MAP[summary_column_type]) {
538
+ const formulaRow = formula_rows[row._id] || {};
539
+ num = formulaRow[summary_column_key];
540
+ num = Array.isArray(num) ? num[0] : num;
541
+ } else {
542
+ num = row[summary_column_key];
543
+ }
544
+ if (!num) {
545
+ if (num === 0) {
546
+ num = '0';
547
+ } else {
548
+ num = !!num;
549
+ }
550
+ }
551
+ if (!existMap[num]) {
552
+ existMap[num] = true;
553
+ total++;
554
+ }
555
+ });
556
+ return total;
557
+ }
558
+ if (summary_column && (0, _dtableUtils.isDateColumn)(summary_column)) {
559
+ if (rowsLength === 0) return;
560
+ let dateArr = [];
561
+ rows.forEach(r => {
562
+ let formatDate = {};
563
+ let formatDateData;
564
+ let value = r[summary_column_key];
565
+ if (_dtableUtils.FORMULA_COLUMN_TYPES_MAP[summary_column_type]) {
566
+ let formulaRow = formula_rows[r._id] || {};
567
+ value = formulaRow[summary_column_key];
568
+ }
569
+ if (!value) return;
570
+ if (value.indexOf('T') != -1) {
571
+ const index = value.indexOf('T');
572
+ const targetStr = value.substring(0, index);
573
+ formatDateData = targetStr.replace(/-/g, '/');
574
+ formatDate.date = targetStr;
575
+ } else {
576
+ formatDateData = value.replace(/-/g, '/');
577
+ formatDate.date = value;
578
+ }
579
+ formatDate.value = Date.parse(formatDateData);
580
+ dateArr.push(formatDate);
581
+ });
582
+ if (summary_method === _constants.SUMMARY_METHOD_MAP.Max) {
583
+ total = getDateMaxOrMinTotal(dateArr, _constants.SUMMARY_METHOD_MAP.Max);
584
+ } else if (summary_method === _constants.SUMMARY_METHOD_MAP.Min) {
585
+ total = getDateMaxOrMinTotal(dateArr, _constants.SUMMARY_METHOD_MAP.Min);
586
+ }
587
+ } else {
588
+ switch (summary_method) {
589
+ case _constants.SUMMARY_METHOD_MAP.Sum:
590
+ case _constants.SUMMARY_METHOD_MAP.Mean:
591
+ {
592
+ let sum = 0;
593
+ let validNumbersCount = 0;
594
+ rows.forEach(r => {
595
+ let num;
596
+ if (_dtableUtils.FORMULA_COLUMN_TYPES_MAP[summary_column_type]) {
597
+ let formulaRow = formula_rows[r._id] || {};
598
+ if (formulaRow[summary_column_key] && typeof formulaRow[summary_column_key] === 'object') {
599
+ num = formulaRow[summary_column_key][0];
600
+ } else {
601
+ num = formulaRow[summary_column_key];
602
+ }
603
+ } else {
604
+ num = r[summary_column_key];
605
+ }
606
+ if ((0, _dtableUtils.isNumber)(num)) {
607
+ validNumbersCount++;
608
+ sum += num;
609
+ }
610
+ });
611
+ if (summary_method === 'Sum') {
612
+ total = Number.parseFloat(sum.toFixed(8));
613
+ } else if (summary_method === 'Mean') {
614
+ total = validNumbersCount === 0 ? 0 : Number.parseFloat((sum / validNumbersCount).toFixed(8));
615
+ }
616
+ break;
617
+ }
618
+ case _constants.SUMMARY_METHOD_MAP.Max:
619
+ case _constants.SUMMARY_METHOD_MAP.Min:
620
+ {
621
+ if (rowsLength > 0) {
622
+ let result = rows.reduce((current, next) => {
623
+ let currentValue, nextValue;
624
+ if (_dtableUtils.FORMULA_COLUMN_TYPES_MAP[summary_column_type]) {
625
+ let currentFormulaRow = formula_rows[current._id] || {};
626
+ let nextFormulaRow = formula_rows[next._id] || {};
627
+ currentValue = currentFormulaRow[summary_column_key];
628
+ nextValue = nextFormulaRow[summary_column_key];
629
+ } else {
630
+ currentValue = current[summary_column_key];
631
+ nextValue = next[summary_column_key];
632
+ }
633
+ if (!nextValue && nextValue !== 0) {
634
+ return current;
635
+ }
636
+ let isNextGreater = currentValue < nextValue;
637
+ if (summary_method === 'Min') {
638
+ return isNextGreater ? current : next;
639
+ } else {
640
+ return isNextGreater ? next : current;
641
+ }
642
+ });
643
+ if (_dtableUtils.FORMULA_COLUMN_TYPES_MAP[summary_column_type]) {
644
+ let formulaRow = formula_rows[result._id];
645
+ if (formulaRow) {
646
+ if (formulaRow[summary_column_key] && typeof formulaRow[summary_column_key] === 'object') {
647
+ total = formulaRow[summary_column_key][0];
648
+ } else {
649
+ total = formulaRow[summary_column_key];
650
+ }
651
+ } else {
652
+ total = null;
653
+ }
654
+ } else {
655
+ total = result[summary_column_key];
656
+ }
657
+ }
658
+ break;
659
+ }
660
+ default:
661
+ {
662
+ break;
663
+ }
664
+ }
665
+ }
666
+ }
667
+ // formula_rows result can be '#VALUE!'
668
+ if (total === '#VALUE!') {
669
+ total = 0;
670
+ }
671
+ return total || 0;
672
+ }
673
+ function showTwoDimensionDateColumnsAndRowsTotal(dateArr, pivot_rows, summary_column_format, pivot_columns, summary_method) {
674
+ let pivot_columns_total = {};
675
+ Array.isArray(pivot_columns) && pivot_columns.forEach(pivotColumn => {
676
+ const {
677
+ key
678
+ } = pivotColumn;
679
+ let dateColumnTotal = [];
680
+ dateArr.forEach(item => {
681
+ if (item.key === key) {
682
+ dateColumnTotal.push(item);
683
+ }
684
+ });
685
+ if (summary_method === 'Max') {
686
+ pivot_columns_total[key] = dateColumnTotal.length === 0 ? 0 : getDateMaxOrMinTotal(dateColumnTotal, 'Max').date;
687
+ } else if (summary_method === 'Min') {
688
+ pivot_columns_total[key] = dateColumnTotal.length === 0 ? 0 : getDateMaxOrMinTotal(dateColumnTotal, 'Min').date;
689
+ }
690
+ });
691
+ Array.isArray(pivot_rows) && pivot_rows.forEach(pivotRow => {
692
+ let dateRowTotal = [];
693
+ dateArr.forEach(item => {
694
+ if (item.name === pivotRow.name) {
695
+ dateRowTotal.push(item);
696
+ }
697
+ });
698
+ if (summary_method === 'Max') {
699
+ pivotRow.total = dateRowTotal.length === 0 ? 0 : getDateMaxOrMinTotal(dateRowTotal, 'Max').date;
700
+ } else if (summary_method === 'Min') {
701
+ pivotRow.total = dateRowTotal.length === 0 ? 0 : getDateMaxOrMinTotal(dateRowTotal, 'Min').date;
702
+ }
703
+ });
704
+ return pivot_columns_total;
705
+ }
706
+ function getDateMaxOrMinTotal(dateArr, type) {
707
+ if (dateArr.length === 0) return;
708
+ const list = dateArr.map(item => item.value);
709
+ let dateValue = 0;
710
+ let dateIndex = 0;
711
+ if (type === 'Max') {
712
+ dateValue = Math.max(...list);
713
+ } else if (type === 'Min') {
714
+ dateValue = Math.min(...list);
715
+ }
716
+ dateIndex = list.indexOf(dateValue);
717
+ return dateArr[dateIndex];
718
+ }