@thoughtspot/ts-chart-sdk 0.0.2-alpha.2 → 0.0.2-alpha.21

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 (107) hide show
  1. package/README.md +126 -50
  2. package/dist/ts-chart-sdk.d.ts +940 -0
  3. package/lib/index.d.ts +4 -0
  4. package/lib/index.d.ts.map +1 -1
  5. package/lib/index.js +4 -0
  6. package/lib/index.js.map +1 -1
  7. package/lib/main/custom-chart-context.d.ts +22 -7
  8. package/lib/main/custom-chart-context.d.ts.map +1 -1
  9. package/lib/main/custom-chart-context.js +59 -4
  10. package/lib/main/custom-chart-context.js.map +1 -1
  11. package/lib/main/custom-chart-context.spec.js +402 -5
  12. package/lib/main/custom-chart-context.spec.js.map +1 -1
  13. package/lib/main/post-message-event-bridge.js +2 -2
  14. package/lib/main/post-message-event-bridge.js.map +1 -1
  15. package/lib/main/util.spec.d.ts +2 -0
  16. package/lib/main/util.spec.d.ts.map +1 -0
  17. package/lib/main/util.spec.js +31 -0
  18. package/lib/main/util.spec.js.map +1 -0
  19. package/lib/react/use-custom-chart-context.spec.js +54 -3
  20. package/lib/react/use-custom-chart-context.spec.js.map +1 -1
  21. package/lib/test/test-conditional-formatting-utils.d.ts +5 -0
  22. package/lib/test/test-conditional-formatting-utils.d.ts.map +1 -0
  23. package/lib/test/test-conditional-formatting-utils.js +43 -0
  24. package/lib/test/test-conditional-formatting-utils.js.map +1 -0
  25. package/lib/types/answer-column.types.d.ts +57 -1
  26. package/lib/types/answer-column.types.d.ts.map +1 -1
  27. package/lib/types/answer-column.types.js +47 -0
  28. package/lib/types/answer-column.types.js.map +1 -1
  29. package/lib/types/chart-to-ts-event.types.d.ts +2 -0
  30. package/lib/types/chart-to-ts-event.types.d.ts.map +1 -1
  31. package/lib/types/chart-to-ts-event.types.js +1 -0
  32. package/lib/types/chart-to-ts-event.types.js.map +1 -1
  33. package/lib/types/common.types.d.ts +103 -4
  34. package/lib/types/common.types.d.ts.map +1 -1
  35. package/lib/types/common.types.js +15 -1
  36. package/lib/types/common.types.js.map +1 -1
  37. package/lib/types/conditional-formatting.types.d.ts +140 -0
  38. package/lib/types/conditional-formatting.types.d.ts.map +1 -0
  39. package/lib/types/conditional-formatting.types.js +60 -0
  40. package/lib/types/conditional-formatting.types.js.map +1 -0
  41. package/lib/types/configurator.types.d.ts +5 -0
  42. package/lib/types/configurator.types.d.ts.map +1 -1
  43. package/lib/types/number-formatting.types.d.ts +55 -0
  44. package/lib/types/number-formatting.types.d.ts.map +1 -0
  45. package/lib/types/number-formatting.types.js +23 -0
  46. package/lib/types/number-formatting.types.js.map +1 -0
  47. package/lib/types/ts-to-chart-event.types.d.ts +10 -5
  48. package/lib/types/ts-to-chart-event.types.d.ts.map +1 -1
  49. package/lib/types/ts-to-chart-event.types.js.map +1 -1
  50. package/lib/types/visual-prop.types.d.ts +21 -0
  51. package/lib/types/visual-prop.types.d.ts.map +1 -1
  52. package/lib/utils/conditional-formatting/conditional-formatting.d.ts +10 -0
  53. package/lib/utils/conditional-formatting/conditional-formatting.d.ts.map +1 -0
  54. package/lib/utils/conditional-formatting/conditional-formatting.js +103 -0
  55. package/lib/utils/conditional-formatting/conditional-formatting.js.map +1 -0
  56. package/lib/utils/conditional-formatting/conditional-formatting.spec.d.ts +2 -0
  57. package/lib/utils/conditional-formatting/conditional-formatting.spec.d.ts.map +1 -0
  58. package/lib/utils/conditional-formatting/conditional-formatting.spec.js +142 -0
  59. package/lib/utils/conditional-formatting/conditional-formatting.spec.js.map +1 -0
  60. package/lib/utils/date-formatting.d.ts +103 -0
  61. package/lib/utils/date-formatting.d.ts.map +1 -0
  62. package/lib/utils/date-formatting.js +389 -0
  63. package/lib/utils/date-formatting.js.map +1 -0
  64. package/lib/utils/date-formatting.spec.d.ts +2 -0
  65. package/lib/utils/date-formatting.spec.d.ts.map +1 -0
  66. package/lib/utils/date-formatting.spec.js +394 -0
  67. package/lib/utils/date-formatting.spec.js.map +1 -0
  68. package/lib/utils/date-utils.d.ts +6 -0
  69. package/lib/utils/date-utils.d.ts.map +1 -0
  70. package/lib/utils/date-utils.js +20 -0
  71. package/lib/utils/date-utils.js.map +1 -0
  72. package/lib/utils/date-utils.spec.d.ts +2 -0
  73. package/lib/utils/date-utils.spec.d.ts.map +1 -0
  74. package/lib/utils/date-utils.spec.js +63 -0
  75. package/lib/utils/date-utils.spec.js.map +1 -0
  76. package/lib/utils/formatting-util.d.ts +10 -0
  77. package/lib/utils/formatting-util.d.ts.map +1 -0
  78. package/lib/utils/formatting-util.js +78 -0
  79. package/lib/utils/formatting-util.js.map +1 -0
  80. package/lib/utils/formatting-util.spec.d.ts +2 -0
  81. package/lib/utils/formatting-util.spec.d.ts.map +1 -0
  82. package/lib/utils/formatting-util.spec.js +247 -0
  83. package/lib/utils/formatting-util.spec.js.map +1 -0
  84. package/package.json +6 -4
  85. package/src/index.ts +4 -0
  86. package/src/main/custom-chart-context.spec.ts +474 -9
  87. package/src/main/custom-chart-context.ts +246 -12
  88. package/src/main/post-message-event-bridge.ts +1 -1
  89. package/src/main/util.spec.ts +41 -0
  90. package/src/react/use-custom-chart-context.spec.tsx +65 -3
  91. package/src/test/test-conditional-formatting-utils.ts +95 -0
  92. package/src/types/answer-column.types.ts +102 -1
  93. package/src/types/chart-to-ts-event.types.ts +7 -0
  94. package/src/types/common.types.ts +165 -10
  95. package/src/types/conditional-formatting.types.ts +157 -0
  96. package/src/types/configurator.types.ts +21 -0
  97. package/src/types/number-formatting.types.ts +80 -0
  98. package/src/types/ts-to-chart-event.types.ts +38 -6
  99. package/src/types/visual-prop.types.ts +75 -1
  100. package/src/utils/conditional-formatting/conditional-formatting.spec.ts +469 -0
  101. package/src/utils/conditional-formatting/conditional-formatting.ts +229 -0
  102. package/src/utils/date-formatting.spec.ts +511 -0
  103. package/src/utils/date-formatting.ts +679 -0
  104. package/src/utils/date-utils.spec.ts +93 -0
  105. package/src/utils/date-utils.ts +69 -0
  106. package/src/utils/formatting-util.spec.ts +312 -0
  107. package/src/utils/formatting-util.ts +234 -0
@@ -0,0 +1,234 @@
1
+ import _ from 'lodash';
2
+ import { CustomChartContext } from '../main/custom-chart-context';
3
+ import {
4
+ ChartColumn,
5
+ ColumnAggregationType,
6
+ ColumnTimeBucket,
7
+ ColumnType,
8
+ DataType,
9
+ } from '../types/answer-column.types';
10
+ import {
11
+ CustomCalendarDate,
12
+ dateFormatPresets,
13
+ formatDate,
14
+ formatDateNum,
15
+ getCustomCalendarGuidFromColumn,
16
+ getCustomCalendarValueFromEpoch,
17
+ getDisplayString,
18
+ getEffectiveDateNumDataType,
19
+ hasCustomCalendar,
20
+ isDateColumn,
21
+ isDateNumColumn,
22
+ isDateTimeColumn,
23
+ } from './date-formatting';
24
+ import {
25
+ getCustomCalendarGuid,
26
+ getFormatPatternForBucket,
27
+ showDateFinancialYearFormat,
28
+ } from './date-utils';
29
+
30
+ interface FormatOptionsType {
31
+ isMillisIncluded: boolean;
32
+ }
33
+
34
+ const getBucketization = (col: ChartColumn) => col.timeBucket;
35
+ /**
36
+ * Retrieves the format pattern for a given column based on its bucketization or custom format.
37
+ *
38
+ * This function first checks the column's bucketization and attempts to retrieve a format pattern
39
+ * using `getFormatPatternForBucket`. If no format pattern is found for the bucketization, it falls
40
+ * back to the column's custom format pattern (if available).
41
+ *
42
+ * @param col - The column to retrieve the format pattern for, represented as a `ChartColumn`.
43
+ * @returns The format pattern as a string, either derived from the bucketization or the column's custom format.
44
+ */
45
+ export const getFormatPattern = (col: ChartColumn): string =>
46
+ getFormatPatternForBucket(getBucketization(col)) || col.format?.pattern;
47
+
48
+ /**
49
+ * Retrieves a formatter function for a column based on its type (e.g., date, date_number).
50
+ *
51
+ * This function returns a specialized formatter based on the column's type and format pattern.
52
+ * It first checks if the column is a date column and applies the appropriate date format,
53
+ * considering options like whether the fiscal year or milliseconds are included. If the column is
54
+ * a date number column, it applies date number formatting. For other types of columns, it returns
55
+ * the raw data value without formatting.
56
+ *
57
+ * @param col - The column for which the formatter is to be created, represented as a `ChartColumn`.
58
+ * @param options - The formatting options, including locale, fiscal settings, and custom calendar information.
59
+ *
60
+ * @returns A function that formats a data value based on the column type and format pattern.
61
+ * This returned function takes in the data value and optional additional options.
62
+ *
63
+ * @example
64
+ *
65
+ * const dateFormatter = getBaseTypeFormatterInstance(column, options);
66
+ * const formattedValue = dateFormatter(dataValue, options);
67
+ * For more info in column
68
+ * @link ChartColumn
69
+ * To see how options are feched see src/example/custom-bar-chart
70
+ */
71
+ export function getBaseTypeFormatterInstance(
72
+ col: ChartColumn,
73
+ options: FormatOptionsType,
74
+ ): any {
75
+ let formatPattern = getFormatPattern(col);
76
+ // TODO: add numberic formatter if the col is numeric.
77
+ if (isDateColumn(col)) {
78
+ const showFinancialFormat = showDateFinancialYearFormat(col);
79
+ const isDateTime = isDateTimeColumn(col);
80
+
81
+ if (!formatPattern) {
82
+ if (isDateTime) {
83
+ if (options.isMillisIncluded) {
84
+ formatPattern =
85
+ dateFormatPresets.DATETIME_SHORT_WITH_MILLIS;
86
+ } else {
87
+ formatPattern =
88
+ dateFormatPresets.DATETIME_SHORT_WITH_SECONDS;
89
+ }
90
+ }
91
+ }
92
+ return (dataValue: any, options?: any) => {
93
+ const customCalendarValueFromEpoch: CustomCalendarDate =
94
+ getCustomCalendarValueFromEpoch(
95
+ col,
96
+ dataValue,
97
+ options?.displayToCustomCalendarValueMap,
98
+ );
99
+ const customCalendarDisplayStr = getDisplayString(
100
+ customCalendarValueFromEpoch,
101
+ );
102
+ if (customCalendarDisplayStr) {
103
+ return customCalendarDisplayStr;
104
+ }
105
+ const customCalendarOverridesFiscalOffset =
106
+ hasCustomCalendar(col) &&
107
+ getCustomCalendarGuidFromColumn(col) !==
108
+ getCustomCalendarGuid(
109
+ 'fiscal',
110
+ options.defaultDataSourceId,
111
+ options.tsDefinedCustomCalenders,
112
+ );
113
+ const optionsWithFiscalOffset = {
114
+ ...options,
115
+ customCalendarOverridesFiscalOffset:
116
+ !!customCalendarOverridesFiscalOffset,
117
+ };
118
+ return formatDate(
119
+ dataValue,
120
+ formatPattern,
121
+ !showFinancialFormat,
122
+ optionsWithFiscalOffset,
123
+ );
124
+ };
125
+ }
126
+ if (isDateNumColumn(col)) {
127
+ return (dataValue: any, options?: any) => {
128
+ const customCalendarValueFromEpoch: CustomCalendarDate =
129
+ getCustomCalendarValueFromEpoch(
130
+ col,
131
+ dataValue,
132
+ options?.displayToCustomCalendarValueMap,
133
+ );
134
+ const customCalendarDisplayStr = getDisplayString(
135
+ customCalendarValueFromEpoch,
136
+ );
137
+ if (customCalendarValueFromEpoch && customCalendarDisplayStr) {
138
+ return customCalendarDisplayStr;
139
+ }
140
+ return formatDateNum(
141
+ getEffectiveDateNumDataType(col),
142
+ dataValue,
143
+ formatPattern,
144
+ options,
145
+ );
146
+ };
147
+ }
148
+ return (dataValue: any) => {
149
+ return dataValue;
150
+ };
151
+ }
152
+ /**
153
+ * Retrieves the appropriate data formatter function for a specified column,
154
+ * based on its type, format options, and an optional aggregation type override.
155
+ *
156
+ *
157
+ * @param col - The column for which the formatter is to be created, represented as a `ChartColumn`.
158
+ * @param options - The formatting options, including settings such as locale, custom calendars, and date constants.
159
+ * @param aggrTypeOverride - Optional override for the column's aggregation type (e.g., `SUM`, `AVERAGE`),
160
+ * which could affect formatting behavior in future implementations.
161
+ *
162
+ * @returns A function to format data values based on the column type, format pattern, and options.
163
+ * This returned function can be used to format individual data values within the column.
164
+ *
165
+ * @example
166
+ * const formatter = getDataFormatter(column, formatOptions, 'SUM');
167
+ * const formattedValue = formatter(dataValue);
168
+ */
169
+ export const getDataFormatter = (
170
+ col: ChartColumn,
171
+ options: FormatOptionsType,
172
+ aggrTypeOverride?: ColumnAggregationType,
173
+ ) => {
174
+ // TODO: number formatter for column based on column type based on
175
+ // aggregation type
176
+
177
+ return getBaseTypeFormatterInstance(col, options);
178
+ };
179
+
180
+ /**
181
+ * Generates a configuration object with formatting and locale-based options for date handling,
182
+ * including custom calendar mapping for a specific column.
183
+ *
184
+ * This function builds an options object that includes locale settings, quarter start month,
185
+ * date formats, and a custom calendar map, which is populated when the column contains a
186
+ * custom calendar GUID. The custom calendar map links display values to custom calendar data
187
+ * for date formatting needs.
188
+ *
189
+ * @param appConfig - The application configuration containing locale, date format settings,
190
+ * and custom calendar definitions.
191
+ * @param col - The column to check for a custom calendar GUID, represented as a `ChartColumn`.
192
+ * @param data - The dataset, used to populate the custom calendar map if a custom calendar
193
+ * GUID is associated with the column.
194
+ *
195
+ * @returns An options object with locale and date formatting details, including a custom
196
+ * calendar map if applicable.
197
+ *
198
+ * @example
199
+ * const mapOptions = generateMapOptions(appConfig, column, data);
200
+ * const locale = mapOptions.locale;
201
+ */
202
+
203
+ export const generateMapOptions = (
204
+ appConfig: any,
205
+ col: ChartColumn,
206
+ data: any,
207
+ ): any => {
208
+ let customCalenderMap = {};
209
+
210
+ if (!_.isEmpty(getCustomCalendarGuidFromColumn(col))) {
211
+ customCalenderMap = data.reduce(
212
+ (customCalenderMapAcc: any, dataValue: any) => {
213
+ return {
214
+ ...customCalenderMapAcc,
215
+ [dataValue.v.s]: dataValue,
216
+ };
217
+ },
218
+ {},
219
+ );
220
+ }
221
+ return {
222
+ locale: appConfig?.localeOptions?.locale,
223
+ quarterStartMonth: appConfig?.localeOptions?.quarterStartMonth,
224
+ tsLocaleBasedDateFormats:
225
+ appConfig?.dateFormatsConfig?.tsLocaleBasedDateFormats,
226
+ tsLocaleBasedStringsFormats:
227
+ appConfig?.dateFormatsConfig?.tsLocaleBasedStringsFormats,
228
+ tsDateConstants: appConfig?.dateFormatsConfig?.tsDateConstants,
229
+ tsDefinedCustomCalenders:
230
+ appConfig?.dateFormatsConfig?.tsDefinedCustomCalenders,
231
+ defaultDataSourceId: appConfig?.dateFormatsConfig?.defaultDataSourceId,
232
+ displayToCustomCalendarValueMap: customCalenderMap,
233
+ };
234
+ };