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,253 @@
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 = void 0;
8
+ var _react = _interopRequireDefault(require("react"));
9
+ var _propTypes = _interopRequireDefault(require("prop-types"));
10
+ var _dataSet = _interopRequireDefault(require("@antv/data-set"));
11
+ var _reactIntlUniversal = _interopRequireDefault(require("react-intl-universal"));
12
+ var _util = require("@antv/util");
13
+ var _text = require("@antv/g2/esm/util/text");
14
+ var _dtableUtils = require("dtable-utils");
15
+ var _baseChart = _interopRequireDefault(require("./base-chart"));
16
+ var _customG = require("../custom-g2");
17
+ var _commonUtils = require("../utils/common-utils");
18
+ var _constants = require("../constants");
19
+ var _statisticChartModule = _interopRequireDefault(require("../assets/css/statistic-chart.module.css"));
20
+ const propTypes = {
21
+ isPreview: _propTypes.default.bool,
22
+ isEnlarge: _propTypes.default.bool,
23
+ theme: _propTypes.default.string,
24
+ colorThemeName: _propTypes.default.string,
25
+ statItem: _propTypes.default.object,
26
+ chartCalculator: _propTypes.default.object,
27
+ getTableById: _propTypes.default.func,
28
+ toggleStatisticRecordsDialog: _propTypes.default.func
29
+ };
30
+ class TreemapChart extends _baseChart.default {
31
+ constructor(props) {
32
+ super(props);
33
+ this.drawChart = data => {
34
+ const {
35
+ getTableById,
36
+ isPreview,
37
+ statItem,
38
+ colorThemeName
39
+ } = this.props;
40
+ const {
41
+ table_id,
42
+ summary_type,
43
+ summary_column_key
44
+ } = statItem;
45
+ const table = getTableById(table_id);
46
+ const selectedNumericColumn = (0, _dtableUtils.getTableColumnByKey)(table, summary_column_key);
47
+ if (isPreview) {
48
+ this.chart = new _customG.Chart({
49
+ container: this.container,
50
+ autoFit: true
51
+ });
52
+ } else {
53
+ this.chart = new _customG.Chart({
54
+ container: this.container,
55
+ height: 400,
56
+ width: 700
57
+ });
58
+ }
59
+ this.chart.on('element:click', e => {
60
+ this.props.toggleStatisticRecordsDialog(e.data.data, statItem);
61
+ });
62
+ const currentTheme = (0, _commonUtils.getCurrentTheme)(colorThemeName);
63
+ const newData = (0, _commonUtils.formatPieChartData)(data, statItem, table, currentTheme);
64
+ const colorSet = newData.colorSet;
65
+ const {
66
+ DataView
67
+ } = _dataSet.default;
68
+ const dv = new DataView();
69
+ const fullData = {
70
+ name: 'root',
71
+ children: newData.map(item => {
72
+ let name = item.name;
73
+ item.name = name || name === 0 ? name : _reactIntlUniversal.default.get(_constants.EMPTY_NAME);
74
+ if (name === 'Others') {
75
+ item.name = _reactIntlUniversal.default.get('Others');
76
+ }
77
+ return item;
78
+ })
79
+ };
80
+ dv.source(fullData, {
81
+ type: 'hierarchy'
82
+ }).transform({
83
+ field: 'value',
84
+ type: 'hierarchy.treemap',
85
+ tile: 'treemapResquarify',
86
+ as: ['x', 'y']
87
+ });
88
+ const nodes = [];
89
+ // eslint-disable-next-line
90
+ for (const node of dv.getAllNodes()) {
91
+ if (node.data.name === 'root') {
92
+ continue;
93
+ }
94
+ const eachNode = {
95
+ name: node.data.name,
96
+ x: node.x,
97
+ y: node.y,
98
+ value: node.data.value,
99
+ formatted_value: node.data.formatted_value,
100
+ percent: node.data.percent,
101
+ original_name: node.data.original_name,
102
+ rows: node.data.rows || null
103
+ };
104
+ nodes.push(eachNode);
105
+ }
106
+ const chart = this.chart;
107
+ chart.data(nodes);
108
+ chart.scale({
109
+ x: {
110
+ nice: true
111
+ },
112
+ y: {
113
+ nice: true
114
+ }
115
+ });
116
+ chart.axis(false);
117
+ chart.legend(false);
118
+ chart.tooltip({
119
+ showTitle: false,
120
+ showMarkers: false,
121
+ containerTpl: '<div class="g2-tooltip"><div class="g2-tooltip-list"></div></div>',
122
+ itemTpl: '<div class="g2-tooltip-content"><div class="tooltip-name">{name}<div><div class="tooltip-item">{info}<div></div>',
123
+ domStyles: {
124
+ 'g2-tooltip': this.getDefaultChartStyleByKey('g2-tooltip'),
125
+ 'tooltip-item': this.getDefaultChartStyleByKey('tooltip-item')
126
+ }
127
+ });
128
+ chart.polygon().position('x*y').color('name', colorSet).tooltip('name*formatted_value*percent', (name, value, percent) => {
129
+ return {
130
+ name,
131
+ info: summary_type === _constants.STATISTICS_COUNT_TYPE.COUNT ? "".concat(value) : "".concat(selectedNumericColumn.name, ": ").concat(value, " (").concat(percent, ")")
132
+ };
133
+ }).style({
134
+ lineWidth: 1,
135
+ stroke: '#fff'
136
+ }).label('name', {
137
+ offset: 0,
138
+ style: {
139
+ textBaseline: 'middle'
140
+ },
141
+ content: obj => {
142
+ if (obj.name !== 'root') {
143
+ return obj.name;
144
+ }
145
+ },
146
+ layout: {
147
+ type: 'custom-limit-in-shape'
148
+ }
149
+ });
150
+ chart.interaction('element-active');
151
+ chart.render();
152
+ };
153
+ this.state = {
154
+ isCalculatingData: true
155
+ };
156
+ this.calculateData = null;
157
+
158
+ // customize label layout: show ellipsis for long labels
159
+ function limitInShape(items, labels, shapes, region) {
160
+ (0, _util.each)(labels, (label, index) => {
161
+ const labelBBox = label.getCanvasBBox();
162
+ const shapeBBox = shapes[index].getBBox();
163
+ if (labelBBox.minX < shapeBBox.minX || labelBBox.minY < shapeBBox.minY || labelBBox.maxX > shapeBBox.maxX || labelBBox.maxY > shapeBBox.maxY) {
164
+ const translateX = labelBBox.width - shapeBBox.width + 20;
165
+ const textShapes = label.findAll(shape => shape.get('type') === 'text');
166
+ textShapes.forEach(textShape => {
167
+ const style = (0, _util.pick)(textShape.attr(), ['fontSize', 'fontFamily', 'fontWeight', 'fontStyle', 'fontVariant']);
168
+ const textBox = textShape.getCanvasBBox();
169
+ const text = (0, _text.getEllipsisText)(textShape.attr('text'), textBox.width - Math.abs(translateX), style);
170
+ textShape.attr('text', text);
171
+ });
172
+ }
173
+ });
174
+ }
175
+ (0, _customG.registerGeometryLabelLayout)('custom-limit-in-shape', limitInShape);
176
+ }
177
+ async componentDidMount() {
178
+ const {
179
+ statItem,
180
+ chartCalculator
181
+ } = this.props;
182
+ if (this.container) {
183
+ const data = await chartCalculator.calculate(statItem);
184
+ this.setState({
185
+ isCalculatingData: false
186
+ });
187
+ this.drawChart(data);
188
+ this.calculateData = data;
189
+ }
190
+ }
191
+ shouldComponentUpdate(nextProps, nextState) {
192
+ const {
193
+ colorThemeName
194
+ } = this.props;
195
+ if (this.canUpdate(this.props, nextProps)) {
196
+ return true;
197
+ }
198
+ if (nextState.isCalculatingData !== this.state.isCalculatingData) {
199
+ return true;
200
+ }
201
+ if (nextProps.colorThemeName !== colorThemeName) {
202
+ return true;
203
+ }
204
+ return false;
205
+ }
206
+ async componentDidUpdate(preProps, preState) {
207
+ const {
208
+ statItem,
209
+ chartCalculator
210
+ } = this.props;
211
+ const {
212
+ isCalculatingData
213
+ } = this.state;
214
+ if (isCalculatingData !== preState.isCalculatingData) return;
215
+ if (this.shouldCalculateStatItem(preProps, this.props)) {
216
+ this.setState({
217
+ isCalculatingData: true
218
+ });
219
+ if (this.container) {
220
+ const data = await chartCalculator.calculate(statItem);
221
+ this.calculateData = data;
222
+ this.setState({
223
+ isCalculatingData: false
224
+ });
225
+ this.chart && this.chart.destroy();
226
+ this.drawChart(data);
227
+ if (this.props.isPreview) {
228
+ this.chart.forceFit();
229
+ }
230
+ }
231
+ } else {
232
+ this.chart && this.chart.destroy();
233
+ this.drawChart(this.calculateData);
234
+ if (this.props.isPreview) {
235
+ this.chart.forceFit();
236
+ }
237
+ }
238
+ }
239
+ componentWillUnmount() {
240
+ this.container = null;
241
+ }
242
+ render() {
243
+ const {
244
+ isCalculatingData
245
+ } = this.state;
246
+ return /*#__PURE__*/_react.default.createElement(_react.default.Fragment, null, isCalculatingData && this.renderLoading(), /*#__PURE__*/_react.default.createElement("div", {
247
+ ref: ref => this.container = ref,
248
+ className: "statistic-chart-container ".concat(_statisticChartModule.default['statistic-treemap-chart-container'])
249
+ }));
250
+ }
251
+ }
252
+ TreemapChart.propTypes = propTypes;
253
+ var _default = exports.default = TreemapChart;
@@ -0,0 +1,249 @@
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 = void 0;
8
+ var _react = _interopRequireDefault(require("react"));
9
+ var _propTypes = _interopRequireDefault(require("prop-types"));
10
+ var _reactIntlUniversal = _interopRequireDefault(require("react-intl-universal"));
11
+ var _dtableUtils = require("dtable-utils");
12
+ var _baseChart = _interopRequireDefault(require("./base-chart"));
13
+ var _customG = require("../custom-g2");
14
+ var _constants = require("../constants");
15
+ const propTypes = {
16
+ isPreview: _propTypes.default.bool,
17
+ statItem: _propTypes.default.object,
18
+ chartCalculator: _propTypes.default.object,
19
+ getTableById: _propTypes.default.func
20
+ };
21
+ class TrendChart extends _baseChart.default {
22
+ constructor(props) {
23
+ super(props);
24
+ this.drawChart = data => {
25
+ const {
26
+ isPreview,
27
+ statItem
28
+ } = this.props;
29
+ if (isPreview) {
30
+ this.chart = new _customG.Chart({
31
+ container: this.container,
32
+ autoFit: true
33
+ });
34
+ } else {
35
+ this.chart = new _customG.Chart({
36
+ container: this.container,
37
+ autoFit: true,
38
+ height: 400,
39
+ width: 700
40
+ });
41
+ }
42
+ const {
43
+ summary_method
44
+ } = statItem;
45
+ const latest = data && data.latest;
46
+ const content = summary_method === _constants.SUMMARY_METHOD_MAP.Distinct_values ? latest : this.formatData(latest);
47
+ const fontSize = this.getFontSize(content);
48
+ let labelFontSize = fontSize - 32;
49
+ const themeColors = this.getThemeColors();
50
+ const labelContent = this.createLabelContent(data, labelFontSize);
51
+ this.chart.annotation().html({
52
+ position: ['50%', '50%'],
53
+ html: "\n <div style=\"color:".concat(themeColors.cardColor, "; text-align: center;\">\n <p class=\"text-content\" style=\"font-size: ").concat(fontSize, "px; margin: -8px 0 2px 0;\">").concat(content, "</p>\n <p style=\"margin: 0;\">").concat(labelContent, "</p>\n </div>\n ")
54
+ });
55
+ this.chart.render();
56
+ };
57
+ this.formatData = data => {
58
+ const {
59
+ getTableById,
60
+ statItem
61
+ } = this.props;
62
+ if (typeof data !== 'number') return _reactIntlUniversal.default.get(_constants.EMPTY_NAME);
63
+ const {
64
+ table_id,
65
+ summary_type,
66
+ summary_column
67
+ } = statItem;
68
+ if (summary_type !== _constants.STATISTICS_COUNT_TYPE.COUNT) {
69
+ const table = getTableById(table_id);
70
+ const selectedColumn = (0, _dtableUtils.getTableColumnByKey)(table, summary_column);
71
+ return this.getNumberDisplayString(data, selectedColumn.data || {});
72
+ }
73
+ let string = data + '';
74
+ const isFloat = string.indexOf('.') > 0;
75
+ if (isFloat) {
76
+ string = string.slice(0, string.indexOf('.'));
77
+ }
78
+ let content = '';
79
+ let endPoint = -3;
80
+ let startPoint = 0;
81
+ let subString = string.slice(endPoint);
82
+ while (subString.length === 3) {
83
+ content = ',' + subString + content;
84
+ startPoint = endPoint;
85
+ endPoint -= 3;
86
+ subString = string.slice(endPoint, startPoint);
87
+ }
88
+ content = subString + content;
89
+ if (content[0] === ',') {
90
+ content = content.slice(1);
91
+ }
92
+ if (isFloat) {
93
+ const initString = data + '';
94
+ content = content + initString.slice(initString.indexOf('.'));
95
+ }
96
+ return content;
97
+ };
98
+ this.getFontSize = (content, maxFontSize) => {
99
+ let canvas = document.createElement('canvas');
100
+ const context = canvas.getContext('2d');
101
+ const {
102
+ width
103
+ } = this.chart;
104
+ let font = context.font;
105
+ let initFontSize = maxFontSize || 48;
106
+ const fontStyle = font.slice(font.indexOf('px'));
107
+ font = initFontSize + fontStyle;
108
+ context.font = font;
109
+ context.fontSize = initFontSize;
110
+ while (context.measureText(content).width >= width - 40) {
111
+ initFontSize -= 1;
112
+ context.font = initFontSize + fontStyle;
113
+ context.fontSize = initFontSize;
114
+ }
115
+ canvas = null;
116
+ if (maxFontSize) return initFontSize;
117
+ if (initFontSize < 16) {
118
+ initFontSize = 16;
119
+ }
120
+ return initFontSize;
121
+ };
122
+ this.createLabelContent = (data, maxFontSize) => {
123
+ const {
124
+ statItem
125
+ } = this.props;
126
+ const {
127
+ date_granularity
128
+ } = statItem;
129
+ let conjunctions = '';
130
+ if (date_granularity === _constants.DATE_GRANULARITY.YEAR) {
131
+ conjunctions = _reactIntlUniversal.default.get('The_year_before_last');
132
+ } else if (date_granularity === _constants.DATE_GRANULARITY.QUARTER) {
133
+ conjunctions = _reactIntlUniversal.default.get('The_quarter_before_last');
134
+ } else if (date_granularity === _constants.DATE_GRANULARITY.MONTH) {
135
+ conjunctions = _reactIntlUniversal.default.get('The_month_before_last');
136
+ } else if (date_granularity === _constants.DATE_GRANULARITY.WEEK) {
137
+ conjunctions = _reactIntlUniversal.default.get('The_week_before_last');
138
+ } else if (date_granularity === 'days_7') {
139
+ conjunctions = _reactIntlUniversal.default.get('Previous_7_days');
140
+ } else if (date_granularity === 'days_30') {
141
+ conjunctions = _reactIntlUniversal.default.get('Previous_30_days');
142
+ } else {
143
+ conjunctions = _reactIntlUniversal.default.get('The_day_before_yesterday');
144
+ }
145
+ const {
146
+ result,
147
+ type,
148
+ previous
149
+ } = data || {};
150
+ let color = '#ff0000',
151
+ icon = 'dtable-icon-down';
152
+ if (type === 'up') {
153
+ color = '#008000';
154
+ icon = 'dtable-icon-up';
155
+ }
156
+ let resultText = "".concat(Number.parseFloat(result * 100).toFixed(2), "%&#8226;").concat(conjunctions).concat(previous);
157
+ if (!previous) {
158
+ resultText = _reactIntlUniversal.default.get('Can_not_compare_with_var', {
159
+ var: conjunctions.toLowerCase()
160
+ });
161
+ }
162
+ let labelFontSize = this.getFontSize(resultText, maxFontSize);
163
+ labelFontSize = labelFontSize > 20 ? 20 : labelFontSize;
164
+ if (!previous) {
165
+ return "<span style='font-size: ".concat(labelFontSize, "px'>").concat(resultText, "</span>");
166
+ }
167
+ if (labelFontSize <= 12) {
168
+ if (!result) return '';
169
+ return "<span style='font-size: ".concat(labelFontSize, "px; width: 100%;'>\n <i style='color: ").concat(color, "' class='dtable-font ").concat(icon, "'></i>\n <span style='color: ").concat(color, "'>").concat(result ? Number.parseFloat(result * 100).toFixed(2) : '', "%</span>\n </span>");
170
+ }
171
+ const resultContent = "\n <span style='display: block; word-break: keep-all; font-size: ".concat(labelFontSize, "px; width: 100%;'>\n ").concat(!result ? '' : "<i style='color: ".concat(color, "' class='dtable-font ").concat(icon, "'></i>"), "\n <span style='color: ").concat(color, "'>").concat(result ? Number.parseFloat(result * 100).toFixed(2) + '%' : '', "</span>\n ").concat(result ? '&#8226;' : '', "\n ").concat(conjunctions, "\n ").concat(this.formatData(previous), "\n </span>\n ");
172
+ return resultContent;
173
+ };
174
+ this.state = {
175
+ isCalculatingData: true
176
+ };
177
+ this.calculateData = null;
178
+ }
179
+ async componentDidMount() {
180
+ const {
181
+ statItem,
182
+ chartCalculator
183
+ } = this.props;
184
+ if (this.container) {
185
+ const data = await chartCalculator.calculate(statItem);
186
+ this.setState({
187
+ isCalculatingData: false
188
+ });
189
+ this.drawChart(data);
190
+ this.calculateData = data;
191
+ }
192
+ }
193
+ shouldComponentUpdate(nextProps, nextState) {
194
+ if (this.canUpdate(this.props, nextProps)) {
195
+ return true;
196
+ }
197
+ if (nextState.isCalculatingData !== this.state.isCalculatingData) {
198
+ return true;
199
+ }
200
+ return false;
201
+ }
202
+ async componentDidUpdate(preProps, preState) {
203
+ const {
204
+ statItem,
205
+ chartCalculator
206
+ } = this.props;
207
+ const {
208
+ isCalculatingData
209
+ } = this.state;
210
+ if (isCalculatingData !== preState.isCalculatingData) return;
211
+ if (this.shouldCalculateStatItem(preProps, this.props)) {
212
+ this.setState({
213
+ isCalculatingData: true
214
+ });
215
+ if (this.container) {
216
+ const data = await chartCalculator.calculate(statItem);
217
+ this.calculateData = data;
218
+ this.setState({
219
+ isCalculatingData: false
220
+ });
221
+ this.chart && this.chart.destroy();
222
+ this.drawChart(data);
223
+ if (this.props.isPreview) {
224
+ this.chart.forceFit();
225
+ }
226
+ }
227
+ } else {
228
+ this.chart && this.chart.destroy();
229
+ this.drawChart(this.calculateData);
230
+ if (this.props.isPreview) {
231
+ this.chart.forceFit();
232
+ }
233
+ }
234
+ }
235
+ componentWillUnmount() {
236
+ this.container = null;
237
+ }
238
+ render() {
239
+ const {
240
+ isCalculatingData
241
+ } = this.state;
242
+ return /*#__PURE__*/_react.default.createElement(_react.default.Fragment, null, isCalculatingData && this.renderLoading(), /*#__PURE__*/_react.default.createElement("div", {
243
+ ref: ref => this.container = ref,
244
+ className: "statistic-chart-container statistic-number-card"
245
+ }));
246
+ }
247
+ }
248
+ TrendChart.propTypes = propTypes;
249
+ var _default = exports.default = TrendChart;