@kepler.gl/utils 3.3.0-alpha.2 → 3.3.0-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 (264) hide show
  1. package/dist/application-config.d.ts +180 -1
  2. package/dist/arrow-data-container.d.ts +55 -2
  3. package/dist/camera-tour.d.ts +11 -0
  4. package/dist/camera-utils.d.ts +7 -0
  5. package/dist/cjs/aggregation.js +84 -0
  6. package/dist/cjs/aggregation.js.map +1 -0
  7. package/dist/cjs/application-config-types.js +6 -0
  8. package/dist/cjs/application-config-types.js.map +1 -0
  9. package/dist/cjs/application-config.js +178 -0
  10. package/dist/cjs/application-config.js.map +1 -0
  11. package/dist/cjs/arrow-data-container.js +1152 -0
  12. package/dist/cjs/arrow-data-container.js.map +1 -0
  13. package/dist/cjs/browser-utils.js +17 -0
  14. package/dist/cjs/browser-utils.js.map +1 -0
  15. package/dist/cjs/camera-tour.js +99 -0
  16. package/dist/cjs/camera-tour.js.map +1 -0
  17. package/dist/cjs/camera-utils.js +81 -0
  18. package/dist/cjs/camera-utils.js.map +1 -0
  19. package/dist/cjs/color-utils.js +462 -0
  20. package/dist/cjs/color-utils.js.map +1 -0
  21. package/dist/cjs/column-statistics.js +115 -0
  22. package/dist/cjs/column-statistics.js.map +1 -0
  23. package/dist/cjs/data-container-interface.js +6 -0
  24. package/dist/cjs/data-container-interface.js.map +1 -0
  25. package/dist/cjs/data-container-utils.js +76 -0
  26. package/dist/cjs/data-container-utils.js.map +1 -0
  27. package/dist/cjs/data-row.js +110 -0
  28. package/dist/cjs/data-row.js.map +1 -0
  29. package/dist/cjs/data-scale-utils.js +634 -0
  30. package/dist/cjs/data-scale-utils.js.map +1 -0
  31. package/dist/cjs/data-utils.js +538 -0
  32. package/dist/cjs/data-utils.js.map +1 -0
  33. package/dist/cjs/dataset-utils.js +361 -0
  34. package/dist/cjs/dataset-utils.js.map +1 -0
  35. package/dist/cjs/dom-to-image.js +433 -0
  36. package/dist/cjs/dom-to-image.js.map +1 -0
  37. package/dist/cjs/dom-utils.js +349 -0
  38. package/dist/cjs/dom-utils.js.map +1 -0
  39. package/dist/cjs/editor-feature-utils.js +177 -0
  40. package/dist/cjs/editor-feature-utils.js.map +1 -0
  41. package/dist/cjs/effect-utils.js +340 -0
  42. package/dist/cjs/effect-utils.js.map +1 -0
  43. package/dist/cjs/export-map-html.js +112 -0
  44. package/dist/cjs/export-map-html.js.map +1 -0
  45. package/dist/cjs/export-utils.js +318 -0
  46. package/dist/cjs/export-utils.js.map +1 -0
  47. package/dist/cjs/extract-dataset-utils.js +218 -0
  48. package/dist/cjs/extract-dataset-utils.js.map +1 -0
  49. package/dist/cjs/filter-utils.js +1762 -0
  50. package/dist/cjs/filter-utils.js.map +1 -0
  51. package/dist/cjs/format.js +38 -0
  52. package/dist/cjs/format.js.map +1 -0
  53. package/dist/cjs/gl-utils.js +118 -0
  54. package/dist/cjs/gl-utils.js.map +1 -0
  55. package/dist/{index.js → cjs/index.js} +391 -10
  56. package/dist/cjs/index.js.map +1 -0
  57. package/dist/cjs/indexed-data-container.js +214 -0
  58. package/dist/cjs/indexed-data-container.js.map +1 -0
  59. package/dist/cjs/jenks.js +156 -0
  60. package/dist/cjs/jenks.js.map +1 -0
  61. package/dist/cjs/locale-utils.js +39 -0
  62. package/dist/cjs/locale-utils.js.map +1 -0
  63. package/dist/cjs/map-info-utils.js +41 -0
  64. package/dist/cjs/map-info-utils.js.map +1 -0
  65. package/dist/cjs/map-style-utils/mapbox-gl-style-editor.js +198 -0
  66. package/dist/cjs/map-style-utils/mapbox-gl-style-editor.js.map +1 -0
  67. package/dist/cjs/map-style-utils/mapbox-utils.js +155 -0
  68. package/dist/cjs/map-style-utils/mapbox-utils.js.map +1 -0
  69. package/dist/cjs/map-utils.js +76 -0
  70. package/dist/cjs/map-utils.js.map +1 -0
  71. package/dist/cjs/mapbox-utils.js +19 -0
  72. package/dist/cjs/mapbox-utils.js.map +1 -0
  73. package/dist/cjs/notifications-utils.js +69 -0
  74. package/dist/cjs/notifications-utils.js.map +1 -0
  75. package/dist/cjs/observe-dimensions.js +136 -0
  76. package/dist/cjs/observe-dimensions.js.map +1 -0
  77. package/dist/cjs/plot.js +1502 -0
  78. package/dist/cjs/plot.js.map +1 -0
  79. package/dist/cjs/position-utils.js +29 -0
  80. package/dist/cjs/position-utils.js.map +1 -0
  81. package/dist/cjs/projection-utils.js +141 -0
  82. package/dist/cjs/projection-utils.js.map +1 -0
  83. package/dist/cjs/quick-insertion-sort.js +132 -0
  84. package/dist/cjs/quick-insertion-sort.js.map +1 -0
  85. package/dist/cjs/row-data-container.js +291 -0
  86. package/dist/cjs/row-data-container.js.map +1 -0
  87. package/dist/cjs/slider-scale-utils.js +138 -0
  88. package/dist/cjs/slider-scale-utils.js.map +1 -0
  89. package/dist/cjs/split-map-utils.js +117 -0
  90. package/dist/cjs/split-map-utils.js.map +1 -0
  91. package/dist/cjs/strings.js +16 -0
  92. package/dist/cjs/strings.js.map +1 -0
  93. package/dist/cjs/theme-utils.js +46 -0
  94. package/dist/cjs/theme-utils.js.map +1 -0
  95. package/dist/cjs/time.js +321 -0
  96. package/dist/cjs/time.js.map +1 -0
  97. package/dist/cjs/types.js +6 -0
  98. package/dist/cjs/types.js.map +1 -0
  99. package/dist/cjs/utils.js +224 -0
  100. package/dist/cjs/utils.js.map +1 -0
  101. package/dist/column-statistics.d.ts +54 -0
  102. package/dist/data-container-interface.d.ts +16 -1
  103. package/dist/data-container-utils.d.ts +2 -1
  104. package/dist/data-scale-utils.d.ts +5 -0
  105. package/dist/data-utils.d.ts +3 -3
  106. package/dist/dataset-utils.d.ts +11 -28
  107. package/dist/editor-feature-utils.d.ts +30 -0
  108. package/dist/esm/aggregation.js +75 -0
  109. package/dist/esm/aggregation.js.map +1 -0
  110. package/dist/esm/application-config-types.js +3 -0
  111. package/dist/esm/application-config-types.js.map +1 -0
  112. package/dist/esm/application-config.js +167 -0
  113. package/dist/esm/application-config.js.map +1 -0
  114. package/dist/esm/arrow-data-container.js +1136 -0
  115. package/dist/esm/arrow-data-container.js.map +1 -0
  116. package/dist/esm/browser-utils.js +11 -0
  117. package/dist/esm/browser-utils.js.map +1 -0
  118. package/dist/esm/camera-tour.js +92 -0
  119. package/dist/esm/camera-tour.js.map +1 -0
  120. package/dist/esm/camera-utils.js +74 -0
  121. package/dist/esm/camera-utils.js.map +1 -0
  122. package/dist/esm/color-utils.js +435 -0
  123. package/dist/esm/color-utils.js.map +1 -0
  124. package/dist/esm/column-statistics.js +105 -0
  125. package/dist/esm/column-statistics.js.map +1 -0
  126. package/dist/esm/data-container-interface.js +3 -0
  127. package/dist/esm/data-container-interface.js.map +1 -0
  128. package/dist/esm/data-container-utils.js +68 -0
  129. package/dist/esm/data-container-utils.js.map +1 -0
  130. package/dist/esm/data-row.js +106 -0
  131. package/dist/esm/data-row.js.map +1 -0
  132. package/dist/esm/data-scale-utils.js +602 -0
  133. package/dist/esm/data-scale-utils.js.map +1 -0
  134. package/dist/esm/data-utils.js +504 -0
  135. package/dist/esm/data-utils.js.map +1 -0
  136. package/dist/esm/dataset-utils.js +352 -0
  137. package/dist/esm/dataset-utils.js.map +1 -0
  138. package/dist/esm/dom-to-image.js +426 -0
  139. package/dist/esm/dom-to-image.js.map +1 -0
  140. package/dist/esm/dom-utils.js +323 -0
  141. package/dist/esm/dom-utils.js.map +1 -0
  142. package/dist/esm/editor-feature-utils.js +164 -0
  143. package/dist/esm/editor-feature-utils.js.map +1 -0
  144. package/dist/esm/effect-utils.js +333 -0
  145. package/dist/esm/effect-utils.js.map +1 -0
  146. package/dist/esm/export-map-html.js +107 -0
  147. package/dist/esm/export-map-html.js.map +1 -0
  148. package/dist/esm/export-utils.js +297 -0
  149. package/dist/esm/export-utils.js.map +1 -0
  150. package/dist/esm/extract-dataset-utils.js +206 -0
  151. package/dist/esm/extract-dataset-utils.js.map +1 -0
  152. package/dist/esm/filter-utils.js +1701 -0
  153. package/dist/esm/filter-utils.js.map +1 -0
  154. package/dist/esm/format.js +31 -0
  155. package/dist/esm/format.js.map +1 -0
  156. package/dist/esm/gl-utils.js +113 -0
  157. package/dist/esm/gl-utils.js.map +1 -0
  158. package/dist/esm/index.js +55 -0
  159. package/dist/esm/index.js.map +1 -0
  160. package/dist/esm/indexed-data-container.js +210 -0
  161. package/dist/esm/indexed-data-container.js.map +1 -0
  162. package/dist/esm/jenks.js +149 -0
  163. package/dist/esm/jenks.js.map +1 -0
  164. package/dist/esm/locale-utils.js +36 -0
  165. package/dist/esm/locale-utils.js.map +1 -0
  166. package/dist/esm/map-info-utils.js +35 -0
  167. package/dist/esm/map-info-utils.js.map +1 -0
  168. package/dist/esm/map-style-utils/mapbox-gl-style-editor.js +188 -0
  169. package/dist/esm/map-style-utils/mapbox-gl-style-editor.js.map +1 -0
  170. package/dist/esm/map-style-utils/mapbox-utils.js +144 -0
  171. package/dist/esm/map-style-utils/mapbox-utils.js.map +1 -0
  172. package/dist/esm/map-utils.js +72 -0
  173. package/dist/esm/map-utils.js.map +1 -0
  174. package/dist/esm/mapbox-utils.js +13 -0
  175. package/dist/esm/mapbox-utils.js.map +1 -0
  176. package/dist/esm/notifications-utils.js +63 -0
  177. package/dist/esm/notifications-utils.js.map +1 -0
  178. package/dist/esm/observe-dimensions.js +128 -0
  179. package/dist/esm/observe-dimensions.js.map +1 -0
  180. package/dist/esm/plot.js +1460 -0
  181. package/dist/esm/plot.js.map +1 -0
  182. package/dist/esm/position-utils.js +22 -0
  183. package/dist/esm/position-utils.js.map +1 -0
  184. package/dist/esm/projection-utils.js +131 -0
  185. package/dist/esm/projection-utils.js.map +1 -0
  186. package/dist/esm/quick-insertion-sort.js +125 -0
  187. package/dist/esm/quick-insertion-sort.js.map +1 -0
  188. package/dist/esm/row-data-container.js +286 -0
  189. package/dist/esm/row-data-container.js.map +1 -0
  190. package/dist/esm/slider-scale-utils.js +129 -0
  191. package/dist/esm/slider-scale-utils.js.map +1 -0
  192. package/dist/esm/split-map-utils.js +109 -0
  193. package/dist/esm/split-map-utils.js.map +1 -0
  194. package/dist/esm/strings.js +10 -0
  195. package/dist/esm/strings.js.map +1 -0
  196. package/dist/esm/theme-utils.js +38 -0
  197. package/dist/esm/theme-utils.js.map +1 -0
  198. package/dist/esm/time.js +308 -0
  199. package/dist/esm/time.js.map +1 -0
  200. package/dist/esm/types.js +3 -0
  201. package/dist/esm/types.js.map +1 -0
  202. package/dist/esm/utils.js +203 -0
  203. package/dist/esm/utils.js.map +1 -0
  204. package/dist/export-map-html.d.ts +5 -0
  205. package/dist/export-utils.d.ts +31 -3
  206. package/dist/extract-dataset-utils.d.ts +79 -0
  207. package/dist/filter-utils.d.ts +56 -7
  208. package/dist/index.d.ts +20 -9
  209. package/dist/jenks.d.ts +5 -0
  210. package/dist/map-info-utils.d.ts +11 -1
  211. package/dist/map-style-utils/mapbox-gl-style-editor.d.ts +11 -10
  212. package/dist/map-style-utils/mapbox-utils.d.ts +29 -1
  213. package/dist/plot.d.ts +187 -21
  214. package/dist/projection-utils.d.ts +6 -4
  215. package/dist/row-data-container.d.ts +19 -0
  216. package/dist/split-map-utils.d.ts +7 -0
  217. package/dist/theme-utils.d.ts +8 -0
  218. package/dist/time.d.ts +3 -3
  219. package/dist/types.d.ts +2 -0
  220. package/package.json +33 -26
  221. package/dist/aggregation.js +0 -84
  222. package/dist/application-config-types.js +0 -6
  223. package/dist/application-config.js +0 -113
  224. package/dist/arrow-data-container.js +0 -398
  225. package/dist/browser-utils.js +0 -17
  226. package/dist/color-utils.js +0 -462
  227. package/dist/data-container-interface.js +0 -6
  228. package/dist/data-container-utils.js +0 -75
  229. package/dist/data-row.js +0 -110
  230. package/dist/data-scale-utils.js +0 -620
  231. package/dist/data-utils.js +0 -532
  232. package/dist/dataset-utils.js +0 -330
  233. package/dist/dom-to-image.js +0 -433
  234. package/dist/dom-utils.js +0 -349
  235. package/dist/effect-utils.js +0 -318
  236. package/dist/export-map-html.js +0 -38
  237. package/dist/export-utils.js +0 -203
  238. package/dist/filter-utils.js +0 -1274
  239. package/dist/format.js +0 -38
  240. package/dist/gl-utils.js +0 -118
  241. package/dist/indexed-data-container.js +0 -214
  242. package/dist/locale-utils.js +0 -39
  243. package/dist/map-info-utils.js +0 -14
  244. package/dist/map-style-utils/mapbox-gl-style-editor.js +0 -194
  245. package/dist/map-style-utils/mapbox-utils.js +0 -107
  246. package/dist/map-utils.js +0 -72
  247. package/dist/mapbox-utils.js +0 -19
  248. package/dist/noop.d.ts +0 -1
  249. package/dist/noop.js +0 -13
  250. package/dist/notifications-utils.js +0 -69
  251. package/dist/observe-dimensions.js +0 -136
  252. package/dist/plot.js +0 -637
  253. package/dist/position-utils.js +0 -26
  254. package/dist/projection-utils.js +0 -91
  255. package/dist/quick-insertion-sort.js +0 -132
  256. package/dist/row-data-container.js +0 -206
  257. package/dist/searcher-utils.d.ts +0 -1
  258. package/dist/searcher-utils.js +0 -25
  259. package/dist/slider-scale-utils.js +0 -138
  260. package/dist/split-map-utils.js +0 -99
  261. package/dist/strings.js +0 -16
  262. package/dist/time.js +0 -325
  263. package/dist/types.js +0 -6
  264. package/dist/utils.js +0 -224
@@ -0,0 +1,602 @@
1
+ import _toConsumableArray from "@babel/runtime/helpers/esm/toConsumableArray";
2
+ import _defineProperty from "@babel/runtime/helpers/esm/defineProperty";
3
+ import _slicedToArray from "@babel/runtime/helpers/esm/slicedToArray";
4
+ function ownKeys(e, r) { var t = Object.keys(e); if (Object.getOwnPropertySymbols) { var o = Object.getOwnPropertySymbols(e); r && (o = o.filter(function (r) { return Object.getOwnPropertyDescriptor(e, r).enumerable; })), t.push.apply(t, o); } return t; }
5
+ function _objectSpread(e) { for (var r = 1; r < arguments.length; r++) { var t = null != arguments[r] ? arguments[r] : {}; r % 2 ? ownKeys(Object(t), !0).forEach(function (r) { _defineProperty(e, r, t[r]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(e, Object.getOwnPropertyDescriptors(t)) : ownKeys(Object(t)).forEach(function (r) { Object.defineProperty(e, r, Object.getOwnPropertyDescriptor(t, r)); }); } return e; }
6
+ // SPDX-License-Identifier: MIT
7
+ // Copyright contributors to the kepler.gl project
8
+
9
+ import { bisectLeft, quantileSorted as d3Quantile, extent } from 'd3-array';
10
+ import uniq from 'es-toolkit/compat/uniq';
11
+ import moment from 'moment';
12
+ import { notNullorUndefined, toArray } from '@kepler.gl/common-utils';
13
+ import { ALL_FIELD_TYPES, SCALE_FUNC, SCALE_TYPES, NO_VALUE_COLOR } from '@kepler.gl/constants';
14
+ // import {FilterProps, KeplerTable} from '@kepler.gl/layers';
15
+
16
+ import { isRgbColor, rgbToHex, hexToRgb } from './color-utils';
17
+ import { formatNumber, isNumber, reverseFormatNumber, unique } from './data-utils';
18
+ import { getTimeWidgetHintFormatter } from './filter-utils';
19
+ import naturalBreaks from './jenks';
20
+ import { isPlainObject } from './utils';
21
+
22
+ // TODO isolate types - depends on @kepler.gl/layers
23
+
24
+ /**
25
+ * return quantile domain for an array of data
26
+ */
27
+ export function getQuantileDomain(data, valueAccessor, sortFunc) {
28
+ var values = typeof valueAccessor === 'function' ? data.map(valueAccessor) : data;
29
+ return values.filter(notNullorUndefined).sort(sortFunc);
30
+ }
31
+
32
+ /**
33
+ * Return Jenks natural-break thresholds for an array of data.
34
+ * `k` is the number of color classes (range length).
35
+ */
36
+ export function getJenksDomain(data, valueAccessor, k) {
37
+ var values = typeof valueAccessor === 'function' ? data.map(valueAccessor).filter(isNumber) : data.filter(isNumber);
38
+ return naturalBreaks(values, k);
39
+ }
40
+
41
+ /**
42
+ * return ordinal domain for a data container
43
+ */
44
+ export function getOrdinalDomain(dataContainer, valueAccessor) {
45
+ var values = dataContainer.mapIndex(valueAccessor);
46
+ return unique(values).filter(notNullorUndefined).sort();
47
+ }
48
+
49
+ /**
50
+ * return linear domain for an array of data
51
+ */
52
+ export function getLinearDomain(data, valueAccessor) {
53
+ var range = typeof valueAccessor === 'function' ? extent(data, valueAccessor) : extent(data);
54
+ return range.map(function (d, i) {
55
+ return d === undefined ? i : d;
56
+ });
57
+ }
58
+
59
+ /**
60
+ * return linear domain for an array of data. A log scale domain cannot contain 0
61
+ */
62
+ export function getLogDomain(data, valueAccessor) {
63
+ var _getLinearDomain = getLinearDomain(data, valueAccessor),
64
+ _getLinearDomain2 = _slicedToArray(_getLinearDomain, 2),
65
+ d0 = _getLinearDomain2[0],
66
+ d1 = _getLinearDomain2[1];
67
+ return [d0 === 0 ? 1e-5 : d0, d1];
68
+ }
69
+ /**
70
+ * whether field domain is stops
71
+ */
72
+ export function isDomainStops(domain) {
73
+ return isPlainObject(domain) && Array.isArray(domain.stops) && Array.isArray(domain.z);
74
+ }
75
+ /**
76
+ * whether field domain is quantiles
77
+ */
78
+ export function isDomainQuantile(domain) {
79
+ return isPlainObject(domain) && Array.isArray(domain.quantiles) && Array.isArray(domain.z);
80
+ }
81
+
82
+ /**
83
+ * get the domain at zoom
84
+ */
85
+ export function getThresholdsFromQuantiles(quantiles, buckets) {
86
+ var thresholds = [];
87
+ if (!Number.isFinite(buckets) || buckets < 1) {
88
+ return [quantiles[0], quantiles[quantiles.length - 1]];
89
+ }
90
+ for (var i = 1; i < buckets; i++) {
91
+ // position in sorted array
92
+ var position = i / buckets;
93
+ // @ts-ignore
94
+ thresholds.push(d3Quantile(quantiles, position));
95
+ }
96
+ return thresholds;
97
+ }
98
+
99
+ /**
100
+ * get the domain at zoom
101
+ */
102
+ export function getDomainStepsbyZoom(domain, steps, z) {
103
+ var i = bisectLeft(steps, z);
104
+ if (steps[i] === z) {
105
+ // If z is an integer value exactly matching a step, return the corresponding domain
106
+ return domain[i];
107
+ }
108
+ // Otherwise, return the next coarsest domain
109
+ return domain[Math.max(i - 1, 0)];
110
+ }
111
+
112
+ /**
113
+ * Get d3 scale function
114
+ */
115
+ export function getScaleFunction(scale, range, domain, fixed) {
116
+ var scaleFunction = SCALE_FUNC[fixed ? 'linear' : scale]().domain(domain).range(fixed ? domain : range);
117
+ scaleFunction.scaleType = fixed ? 'linear' : scale;
118
+ return scaleFunction;
119
+ }
120
+
121
+ /**
122
+ * Get threshold scale color labels
123
+ */
124
+ function getThresholdLabels(scale, labelFormat) {
125
+ var genLength = scale.range().length;
126
+ return scale.range().map(function (d, i) {
127
+ var invert = scale.invertExtent(d);
128
+ var inputs = [i === 0 ? null : reverseFormatNumber(labelFormat(invert[0])), i === genLength - 1 ? null : reverseFormatNumber(labelFormat(invert[1]))];
129
+ return {
130
+ // raw value
131
+ range: invert,
132
+ // formatted value
133
+ inputs: inputs,
134
+ label: i === 0 ? "Less than ".concat(labelFormat(invert[1])) : i === genLength - 1 ? "".concat(labelFormat(invert[0]), " or more") : "".concat(labelFormat(invert[0]), " to ").concat(labelFormat(invert[1]))
135
+ };
136
+ });
137
+ }
138
+
139
+ /**
140
+ * Get linear / quant scale color labels
141
+ */
142
+ function getScaleLabels(scale, labelFormat) {
143
+ return scale.range().map(function (d) {
144
+ // @ts-ignore
145
+ var invert = scale.invertExtent(d);
146
+ var inputs = [reverseFormatNumber(labelFormat(invert[0])), reverseFormatNumber(labelFormat(invert[1]))];
147
+ return {
148
+ label: "".concat(labelFormat(invert[0]), " to ").concat(labelFormat(invert[1])),
149
+ // raw value
150
+ range: invert,
151
+ // formatted value
152
+ inputs: inputs
153
+ };
154
+ });
155
+ }
156
+ var customScaleLabelFormat = function customScaleLabelFormat(n) {
157
+ return n || n === 0 ? formatNumber(n, 'real') : 'no value';
158
+ };
159
+ /**
160
+ * Get linear / quant scale color breaks
161
+ */
162
+ export function getQuantLegends(scale, labelFormat) {
163
+ if (typeof scale.invertExtent !== 'function') {
164
+ return [];
165
+ }
166
+ var thresholdLabelFormat = function thresholdLabelFormat(n, type) {
167
+ return n || n === 0 ? labelFormat ? labelFormat(n) : formatNumber(n, type) : 'no value';
168
+ };
169
+ var labels = scale.scaleType === 'threshold' || scale.scaleType === SCALE_TYPES.jenks ? getThresholdLabels(scale, thresholdLabelFormat) : scale.scaleType === 'custom' ? getThresholdLabels(scale, customScaleLabelFormat) : getScaleLabels(scale, labelFormat);
170
+ var data = scale.range();
171
+ return labels.map(function (label, index) {
172
+ return _objectSpread({
173
+ data: Array.isArray(data[index]) ? rgbToHex(data[index]) : data[index]
174
+ }, label);
175
+ });
176
+ }
177
+
178
+ /**
179
+ * Get ordinal color scale legends
180
+ */
181
+ export function getOrdinalLegends(scale) {
182
+ var domain = scale.domain();
183
+ var labels = scale.domain();
184
+ var data = domain.map(scale);
185
+ return data.map(function (datum, index) {
186
+ return {
187
+ data: isRgbColor(datum) ? rgbToHex(datum) : datum,
188
+ label: labels[index]
189
+ };
190
+ });
191
+ }
192
+ var defaultFormat = function defaultFormat(d) {
193
+ return d;
194
+ };
195
+ var getTimeLabelFormat = function getTimeLabelFormat(domain) {
196
+ var formatter = getTimeWidgetHintFormatter(domain);
197
+ return function (val) {
198
+ return moment.utc(val).format(formatter);
199
+ };
200
+ };
201
+ export function getQuantLabelFormat(domain, fieldType) {
202
+ // quant scale can only be assigned to linear Fields: real, timestamp, integer
203
+ return fieldType === ALL_FIELD_TYPES.timestamp ? getTimeLabelFormat(domain) : !fieldType ? defaultFormat : function (n) {
204
+ return isNumber(n) ? formatNumber(n, fieldType) : 'no value';
205
+ };
206
+ }
207
+
208
+ /**
209
+ * Get legends for scale
210
+ */
211
+ export function getLegendOfScale(_ref) {
212
+ var scale = _ref.scale,
213
+ scaleType = _ref.scaleType,
214
+ labelFormat = _ref.labelFormat,
215
+ fieldType = _ref.fieldType;
216
+ if (!scale || scale.byZoom) {
217
+ return [];
218
+ }
219
+ if (scaleType === SCALE_TYPES.ordinal || scaleType === SCALE_TYPES.customOrdinal || fieldType === ALL_FIELD_TYPES.string) {
220
+ return getOrdinalLegends(scale);
221
+ }
222
+ var formatLabel = labelFormat || getQuantLabelFormat(scale.domain(), fieldType);
223
+ if (scaleType === SCALE_TYPES.jenks) {
224
+ scale.scaleType = SCALE_TYPES.jenks;
225
+ }
226
+ return getQuantLegends(scale, formatLabel);
227
+ }
228
+
229
+ /**
230
+ * Get color scale function
231
+ */
232
+ export function getLayerColorScale(_ref2) {
233
+ var range = _ref2.range,
234
+ domain = _ref2.domain,
235
+ scaleType = _ref2.scaleType,
236
+ layer = _ref2.layer;
237
+ if (range && domain && scaleType) {
238
+ return layer.getColorScale(scaleType, domain, range);
239
+ }
240
+ return null;
241
+ }
242
+
243
+ /**
244
+ * Convert colorRange.colorMap into color breaks UI input
245
+ */
246
+ export function initializeLayerColorMap(layer, visualChannel) {
247
+ var domain = layer.config[visualChannel.domain];
248
+ var range = layer.config.visConfig[visualChannel.range];
249
+ var scaleType = layer.config[visualChannel.scale];
250
+ var field = layer.config[visualChannel.field];
251
+ var scale = getLayerColorScale({
252
+ range: range,
253
+ domain: domain,
254
+ scaleType: scaleType,
255
+ layer: layer
256
+ });
257
+ var colorBreaks = getLegendOfScale({
258
+ scale: scale !== null && scale !== void 0 && scale.byZoom ? scale(0) : scale,
259
+ scaleType: scaleType,
260
+ fieldType: field.type
261
+ });
262
+ return colorBreaksToColorMap(colorBreaks);
263
+ }
264
+
265
+ /**
266
+ * Get visual chanel scale function if it's based on zoom
267
+ */
268
+ export function getVisualChannelScaleByZoom(_ref3) {
269
+ var _scale;
270
+ var scale = _ref3.scale,
271
+ layer = _ref3.layer,
272
+ mapState = _ref3.mapState;
273
+ if ((_scale = scale) !== null && _scale !== void 0 && _scale.byZoom) {
274
+ var _layer$meta;
275
+ var z = (_layer$meta = layer.meta) !== null && _layer$meta !== void 0 && _layer$meta.getZoom ? layer.meta.getZoom(mapState) : mapState === null || mapState === void 0 ? void 0 : mapState.zoom;
276
+ scale = Number.isFinite(z) ? scale(z) : null;
277
+ }
278
+ return scale;
279
+ }
280
+
281
+ /**
282
+ * Get categorical colorMap from colors and domain (unique values)
283
+ */
284
+ export function getCategoricalColorMap(colors, domain) {
285
+ var uniqueValues = unique(domain).sort();
286
+ var colorMap = colors.map(function (color) {
287
+ return [null, color];
288
+ });
289
+ if (colors.length === 0 || uniqueValues.length === 0) {
290
+ return colorMap;
291
+ }
292
+ var lastIndex = colors.length - 1;
293
+ var assignCount = Math.min(lastIndex, uniqueValues.length);
294
+
295
+ // Assign first values one-to-one up to the penultimate color (if any)
296
+ for (var i = 0; i < assignCount; i++) {
297
+ // @ts-ignore tuple
298
+ colorMap[i][0] = uniqueValues[i];
299
+ }
300
+ if (uniqueValues.length > colors.length) {
301
+ // Aggregate the rest (including the value that would have gone to last color)
302
+ // Build aggregated array incrementally to match legacy behavior
303
+ var aggregatedValues = [];
304
+ for (var _i = lastIndex; _i < uniqueValues.length; _i++) {
305
+ var value = uniqueValues[_i];
306
+ if (Array.isArray(value)) {
307
+ // Spread array elements to match legacy flattening behavior
308
+ aggregatedValues.push.apply(aggregatedValues, _toConsumableArray(value));
309
+ } else {
310
+ aggregatedValues.push(value);
311
+ }
312
+ }
313
+ // @ts-ignore tuple
314
+ colorMap[lastIndex][0] = aggregatedValues;
315
+ } else if (uniqueValues.length === colors.length) {
316
+ // Exactly one per color
317
+ // @ts-ignore tuple
318
+ colorMap[lastIndex][0] = uniqueValues[lastIndex];
319
+ }
320
+
321
+ // @ts-ignore tuple
322
+ return colorMap;
323
+ }
324
+
325
+ /**
326
+ * Get categorical colorBreaks from colorMap
327
+ */
328
+ export function colorMapToCategoricalColorBreaks(colorMap) {
329
+ if (!colorMap) {
330
+ return null;
331
+ }
332
+ var colorBreaks = colorMap.map(function (_ref4) {
333
+ var _ref5 = _slicedToArray(_ref4, 2),
334
+ value = _ref5[0],
335
+ color = _ref5[1];
336
+ return {
337
+ data: color,
338
+ label: value
339
+ };
340
+ });
341
+ return colorBreaks;
342
+ }
343
+
344
+ /**
345
+ * create categorical colorMap from colorBreaks
346
+ */
347
+ export function colorBreaksToCategoricalColorMap(colorBreaks) {
348
+ // colorMap: [string | string[], hexstring]
349
+ var colors = uniq(colorBreaks.map(function (cb) {
350
+ return cb.data;
351
+ }));
352
+ var values = uniq(colorBreaks.map(function (cb) {
353
+ return cb.label;
354
+ }));
355
+ return getCategoricalColorMap(colors, values);
356
+ }
357
+
358
+ /**
359
+ * Convert color breaks UI input into colorRange.colorMap
360
+ */
361
+ export function colorBreaksToColorMap(colorBreaks) {
362
+ var colorMap = colorBreaks.map(function (colorBreak, i) {
363
+ // [value, hex]
364
+ return [colorBreak.inputs ? i === colorBreaks.length - 1 ? null // last
365
+ : colorBreak.inputs[1] : colorBreak.label, colorBreak.data];
366
+ });
367
+
368
+ // @ts-ignore tuple
369
+ return colorMap;
370
+ }
371
+
372
+ /**
373
+ * Convert colorRange.colorMap into color breaks UI input
374
+ */
375
+ export function colorMapToColorBreaks(colorMap) {
376
+ if (!colorMap) {
377
+ return null;
378
+ }
379
+ var colorBreaks = colorMap.map(function (_ref6, i) {
380
+ var _ref7 = _slicedToArray(_ref6, 2),
381
+ value = _ref7[0],
382
+ color = _ref7[1];
383
+ var range = i === 0 ?
384
+ // first
385
+ [-Infinity, value] :
386
+ // last
387
+ i === colorMap.length - 1 ? [colorMap[i - 1][0], Infinity] :
388
+ // else
389
+ [colorMap[i - 1][0], value];
390
+ return {
391
+ data: color,
392
+ range: range,
393
+ inputs: range,
394
+ label:
395
+ // first
396
+ i === 0 ? "Less than ".concat(value) :
397
+ // last
398
+ i === colorMap.length - 1 ? "".concat(colorMap[i - 1][0], " or more") : "".concat(colorMap[i - 1][0], " to ").concat(value)
399
+ };
400
+ });
401
+
402
+ // @ts-ignore implement conversion for ordinal
403
+ return colorBreaks;
404
+ }
405
+
406
+ /**
407
+ * Whether color breaks is for numeric field
408
+ */
409
+ export function isNumericColorBreaks(colorBreaks) {
410
+ return Boolean(Array.isArray(colorBreaks) && colorBreaks.length && colorBreaks[0].inputs);
411
+ }
412
+
413
+ // return domainMin, domainMax, histogramMean
414
+ export function getHistogramDomain(_ref8) {
415
+ var aggregatedBins = _ref8.aggregatedBins,
416
+ columnStats = _ref8.columnStats,
417
+ dataset = _ref8.dataset,
418
+ fieldValueAccessor = _ref8.fieldValueAccessor;
419
+ var domainMin = Number.POSITIVE_INFINITY;
420
+ var domainMax = Number.NEGATIVE_INFINITY;
421
+ var nValid = 0;
422
+ var domainSum = 0;
423
+ if (aggregatedBins) {
424
+ Object.values(aggregatedBins).forEach(function (bin) {
425
+ var val = bin.value;
426
+ if (isNumber(val)) {
427
+ if (val < domainMin) domainMin = val;
428
+ if (val > domainMax) domainMax = val;
429
+ domainSum += val;
430
+ nValid += 1;
431
+ }
432
+ });
433
+ } else {
434
+ if (columnStats && columnStats.quantiles && columnStats.mean) {
435
+ // no need to recalcuate min/max/mean if its already in columnStats
436
+ return [columnStats.quantiles[0].value, columnStats.quantiles[columnStats.quantiles.length - 1].value, columnStats.mean];
437
+ }
438
+ if (dataset && fieldValueAccessor) {
439
+ dataset.allIndexes.forEach(function (x) {
440
+ var val = fieldValueAccessor(x);
441
+ if (isNumber(val)) {
442
+ if (val < domainMin) domainMin = val;
443
+ if (val > domainMax) domainMax = val;
444
+ domainSum += val;
445
+ nValid += 1;
446
+ }
447
+ });
448
+ }
449
+ }
450
+ var histogramMean = nValid > 0 ? domainSum / nValid : 0;
451
+ return [nValid > 0 ? domainMin : 0, nValid > 0 ? domainMax : 0, histogramMean];
452
+ }
453
+ export function resetCategoricalColorMapByIndex(colorMap, index) {
454
+ if (!colorMap) {
455
+ return colorMap;
456
+ }
457
+ var newColorMap = colorMap.map(function (cm, i) {
458
+ if (i === index) {
459
+ return [null, cm[1]];
460
+ }
461
+ return cm;
462
+ });
463
+ return newColorMap;
464
+ }
465
+
466
+ /**
467
+ * select rest categorical values for a colorMap by its index
468
+ */
469
+ export function selectRestCategoricalColorMapByIndex(colorMap, index, uniqueValues) {
470
+ if (!colorMap || !uniqueValues) {
471
+ return colorMap;
472
+ }
473
+
474
+ // find unique values that has not been used in current colorMap
475
+ var uniqValueDict = Object.fromEntries(uniqueValues.map(function (val) {
476
+ return [val, false];
477
+ }));
478
+ colorMap.forEach(function (cm) {
479
+ toArray(cm[0]).forEach(function (v) {
480
+ if (notNullorUndefined(v)) uniqValueDict[v] = true;
481
+ });
482
+ });
483
+ var rest = Object.keys(uniqValueDict).filter(function (v) {
484
+ return !uniqValueDict[v];
485
+ });
486
+
487
+ // use the not used unique values in the selected color map
488
+ var newColorMap = colorMap.map(function (cm, i) {
489
+ if (i === index) {
490
+ return [[].concat(_toConsumableArray(rest), _toConsumableArray(toArray(cm[0]))), cm[1]];
491
+ }
492
+ return cm;
493
+ });
494
+ return newColorMap;
495
+ }
496
+
497
+ /**
498
+ * remove a categorical value from a colorMap by its index
499
+ */
500
+ export function removeCategoricalValueFromColorMap(colorMap, item, index) {
501
+ if (!colorMap) {
502
+ return colorMap;
503
+ }
504
+ var newColorMap = colorMap.map(function (cm, i) {
505
+ if (i === index) {
506
+ if (!notNullorUndefined(cm[0])) {
507
+ return [null, cm[1]];
508
+ }
509
+ var currentUniqueValues = toArray(cm[0]);
510
+ var updatedUniqueValues = currentUniqueValues.filter(function (v) {
511
+ return v !== item;
512
+ });
513
+ return [updatedUniqueValues, cm[1]];
514
+ }
515
+ return cm;
516
+ });
517
+ return newColorMap;
518
+ }
519
+
520
+ /**
521
+ * add categorical values (from multisel dropdown) to a colorMap by its index
522
+ */
523
+ export function addCategoricalValuesToColorMap(colorMap, items, index) {
524
+ if (!colorMap) {
525
+ return colorMap;
526
+ }
527
+ var newColorMap = colorMap.map(function (cm, i) {
528
+ if (i === index) {
529
+ if (!notNullorUndefined(cm[0])) {
530
+ return [items, cm[1]];
531
+ }
532
+ var _currentUniqueValues = toArray(cm[0]);
533
+ var updatedUniqueValues = uniq(_currentUniqueValues.concat(items));
534
+ return [updatedUniqueValues, cm[1]];
535
+ }
536
+ // remove value from other colorMap
537
+ var currentUniqueValues = cm[0];
538
+ if (Array.isArray(currentUniqueValues)) {
539
+ var _updatedUniqueValues = currentUniqueValues.filter(function (v) {
540
+ return !items.includes(v);
541
+ });
542
+ return [_updatedUniqueValues, cm[1]];
543
+ } else if (notNullorUndefined(currentUniqueValues) && items.includes(currentUniqueValues)) {
544
+ return [null, cm[1]];
545
+ }
546
+ return cm;
547
+ });
548
+ return newColorMap;
549
+ }
550
+
551
+ /**
552
+ * get a color scale func for categorical (custom ordinal) scale
553
+ */
554
+ export function getCategoricalColorScale(colorDomain, colorRange) {
555
+ var useRgb = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : true;
556
+ var cMap = colorRange.colorMap ? colorRange.colorMap : getCategoricalColorMap(colorRange.colors, colorDomain);
557
+ var range = [];
558
+ var domain = [];
559
+ cMap.forEach(function (cm) {
560
+ if (Array.isArray(cm[0])) {
561
+ cm[0].forEach(function (val) {
562
+ domain.push(val);
563
+ range.push(useRgb ? hexToRgb(cm[1]) : cm[1]);
564
+ });
565
+ } else {
566
+ domain.push(cm[0]);
567
+ range.push(useRgb ? hexToRgb(cm[1]) : cm[1]);
568
+ }
569
+ });
570
+ var scale = getScaleFunction(SCALE_TYPES.customOrdinal, range, domain, false);
571
+ scale.unknown(NO_VALUE_COLOR);
572
+ return scale;
573
+ }
574
+
575
+ /**
576
+ * initialize customPalette by custom scale or customOrdinal scale
577
+ */
578
+ export function initCustomPaletteByCustomScale(_ref9) {
579
+ var scale = _ref9.scale,
580
+ field = _ref9.field,
581
+ ordinalDomain = _ref9.ordinalDomain,
582
+ range = _ref9.range,
583
+ colorBreaks = _ref9.colorBreaks;
584
+ var customPaletteName = "color.customPalette.".concat(scale, ".").concat((field === null || field === void 0 ? void 0 : field.name) || 'point-count');
585
+ // reuse range.colorMap if the field and scale not changed
586
+ var reuseColorMap = range.colorMap && range.name === customPaletteName && range.type === scale;
587
+ var colorMap = reuseColorMap ? range.colorMap : scale === SCALE_TYPES.customOrdinal && ordinalDomain ? getCategoricalColorMap(range.colors, ordinalDomain) : colorBreaks && isNumericColorBreaks(colorBreaks) ? colorBreaksToColorMap(colorBreaks) : null;
588
+ var colors = reuseColorMap ? range.colors : colorMap ? colorMap.map(function (cm) {
589
+ return cm[1];
590
+ }) : range.colors;
591
+
592
+ // update custom breaks
593
+ var customPalette = {
594
+ category: 'Custom',
595
+ name: customPaletteName,
596
+ type: scale,
597
+ colorMap: colorMap,
598
+ colors: colors || []
599
+ };
600
+ return customPalette;
601
+ }
602
+ //# sourceMappingURL=data-scale-utils.js.map