@devexperts/dxcharts-lite 1.0.2 → 2.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 (255) hide show
  1. package/README.md +2 -2
  2. package/dist/chart/animation/canvas-animation.js +134 -1
  3. package/dist/chart/animation/types/animation.js +43 -1
  4. package/dist/chart/animation/types/color-alpha-animation.d.ts +1 -1
  5. package/dist/chart/animation/types/color-alpha-animation.js +60 -1
  6. package/dist/chart/animation/types/color-transition-animation.d.ts +1 -1
  7. package/dist/chart/animation/types/color-transition-animation.js +86 -1
  8. package/dist/chart/animation/types/viewport-movement-animation.js +63 -1
  9. package/dist/chart/animation/viewport-model-animation.js +20 -1
  10. package/dist/chart/bootstrap.d.ts +51 -48
  11. package/dist/chart/bootstrap.js +449 -1
  12. package/dist/chart/canvas/canvas-bounds-container.d.ts +0 -5
  13. package/dist/chart/canvas/canvas-bounds-container.js +815 -1
  14. package/dist/chart/canvas/canvas-chart-html.js +16 -18
  15. package/dist/chart/canvas/chart-elements.d.ts +1 -4
  16. package/dist/chart/canvas/chart-elements.js +22 -1
  17. package/dist/chart/canvas/cursor.handler.js +122 -1
  18. package/dist/chart/canvas/layout-creator.js +34 -1
  19. package/dist/chart/canvas/y-axis-bounds.container.d.ts +2 -2
  20. package/dist/chart/canvas/y-axis-bounds.container.js +67 -1
  21. package/dist/chart/chart-container.js +1 -1
  22. package/dist/chart/chart.config.d.ts +4 -5
  23. package/dist/chart/chart.config.js +645 -1
  24. package/dist/chart/chart.d.ts +62 -0
  25. package/dist/chart/chart.js +106 -0
  26. package/dist/chart/components/chart/basic-scale.d.ts +2 -2
  27. package/dist/chart/components/chart/basic-scale.js +31 -1
  28. package/dist/chart/components/chart/candle-transformer.functions.js +15 -1
  29. package/dist/chart/components/chart/candle-width-calculator.functions.js +2 -1
  30. package/dist/chart/components/chart/candle.functions.js +44 -1
  31. package/dist/chart/components/chart/chart-area-pan.handler.d.ts +3 -3
  32. package/dist/chart/components/chart/chart-area-pan.handler.js +197 -1
  33. package/dist/chart/components/chart/chart-base.model.js +66 -1
  34. package/dist/chart/components/chart/chart.component.d.ts +7 -3
  35. package/dist/chart/components/chart/chart.component.js +408 -1
  36. package/dist/chart/components/chart/chart.model.d.ts +4 -11
  37. package/dist/chart/components/chart/chart.model.js +954 -1
  38. package/dist/chart/components/chart/data-series.high-low-provider.js +34 -1
  39. package/dist/chart/components/chart/fake-candles.js +97 -1
  40. package/dist/chart/components/chart/price.formatter.js +35 -1
  41. package/dist/chart/components/chart/secondary-chart-colors-pool.js +68 -1
  42. package/dist/chart/components/cross_tool/cross-tool.component.js +85 -1
  43. package/dist/chart/components/cross_tool/cross-tool.drawer.js +32 -1
  44. package/dist/chart/components/cross_tool/cross-tool.model.js +107 -1
  45. package/dist/chart/components/cross_tool/types/cross-and-labels.drawer.js +137 -1
  46. package/dist/chart/components/cross_tool/types/none.drawer.js +3 -1
  47. package/dist/chart/components/dran-n-drop_helper/drag-n-drop-x.component.js +28 -1
  48. package/dist/chart/components/dran-n-drop_helper/drag-n-drop-y.component.js +24 -1
  49. package/dist/chart/components/dran-n-drop_helper/drag-n-drop.component.js +53 -1
  50. package/dist/chart/components/dynamic-objects/dynamic-objects.component.d.ts +13 -0
  51. package/dist/chart/components/dynamic-objects/dynamic-objects.component.js +20 -0
  52. package/dist/chart/components/dynamic-objects/dynamic-objects.drawer.d.ts +18 -0
  53. package/dist/chart/components/dynamic-objects/dynamic-objects.drawer.js +23 -0
  54. package/dist/chart/components/dynamic-objects/dynamic-objects.model.d.ts +63 -0
  55. package/dist/chart/components/dynamic-objects/dynamic-objects.model.js +136 -0
  56. package/dist/chart/components/events/events-hit-test.drawer.js +63 -1
  57. package/dist/chart/components/events/events.component.js +61 -1
  58. package/dist/chart/components/events/events.drawer.js +195 -1
  59. package/dist/chart/components/events/events.model.js +69 -1
  60. package/dist/chart/components/grid/grid.component.d.ts +2 -2
  61. package/dist/chart/components/grid/grid.component.js +31 -1
  62. package/dist/chart/components/grid/grid.drawer.d.ts +4 -3
  63. package/dist/chart/components/grid/grid.drawer.js +106 -1
  64. package/dist/chart/components/high_low/high-low.component.js +12 -1
  65. package/dist/chart/components/high_low/high-low.drawer.js +126 -1
  66. package/dist/chart/components/highlights/highlights.component.js +46 -1
  67. package/dist/chart/components/highlights/highlights.drawer.js +154 -1
  68. package/dist/chart/components/highlights/highlights.model.js +75 -1
  69. package/dist/chart/components/labels_generator/numeric-axis-labels.generator.d.ts +1 -1
  70. package/dist/chart/components/labels_generator/numeric-axis-labels.generator.js +199 -1
  71. package/dist/chart/components/navigation_map/navigation-map-move.handler.d.ts +2 -2
  72. package/dist/chart/components/navigation_map/navigation-map-move.handler.js +108 -1
  73. package/dist/chart/components/navigation_map/navigation-map.component.js +112 -1
  74. package/dist/chart/components/navigation_map/navigation-map.drawer.js +244 -1
  75. package/dist/chart/components/navigation_map/navigation-map.model.js +33 -2
  76. package/dist/chart/components/pan/chart-pan.component.d.ts +2 -2
  77. package/dist/chart/components/pan/chart-pan.component.js +51 -1
  78. package/dist/chart/components/pane/extent/y-extent-component.d.ts +12 -18
  79. package/dist/chart/components/pane/extent/y-extent-component.js +120 -1
  80. package/dist/chart/components/pane/pane-hit-test.controller.js +55 -1
  81. package/dist/chart/components/pane/pane-manager.component.d.ts +10 -5
  82. package/dist/chart/components/pane/pane-manager.component.js +148 -1
  83. package/dist/chart/components/pane/pane.component.d.ts +16 -32
  84. package/dist/chart/components/pane/pane.component.js +278 -1
  85. package/dist/chart/components/resizer/bar-resizer.component.js +139 -1
  86. package/dist/chart/components/resizer/bar-resizer.drawer.js +46 -1
  87. package/dist/chart/components/snapshot/snapshot.component.js +48 -1
  88. package/dist/chart/components/volumes/separate-volumes.component.d.ts +6 -10
  89. package/dist/chart/components/volumes/separate-volumes.component.js +63 -1
  90. package/dist/chart/components/volumes/volume-color-resolvers.functions.js +18 -1
  91. package/dist/chart/components/volumes/volumes.component.d.ts +6 -7
  92. package/dist/chart/components/volumes/volumes.component.js +94 -1
  93. package/dist/chart/components/volumes/volumes.drawer.d.ts +5 -12
  94. package/dist/chart/components/volumes/volumes.drawer.js +140 -1
  95. package/dist/chart/components/volumes/volumes.formatter.js +31 -1
  96. package/dist/chart/components/volumes/volumes.model.d.ts +2 -2
  97. package/dist/chart/components/volumes/volumes.model.js +43 -1
  98. package/dist/chart/components/watermark/water-mark.component.js +103 -1
  99. package/dist/chart/components/watermark/water-mark.drawer.js +187 -1
  100. package/dist/chart/components/x_axis/numeric-x-axis-labels.generator.js +27 -1
  101. package/dist/chart/components/x_axis/time/parser/time-formats-matchers.functions.js +187 -1
  102. package/dist/chart/components/x_axis/time/parser/time-formats-parser.functions.js +76 -1
  103. package/dist/chart/components/x_axis/time/parser/time-formats-validators.functions.js +79 -1
  104. package/dist/chart/components/x_axis/time/parser/time-formats.model.js +14 -1
  105. package/dist/chart/components/x_axis/time/x-axis-weights.functions.d.ts +1 -1
  106. package/dist/chart/components/x_axis/time/x-axis-weights.functions.js +143 -1
  107. package/dist/chart/components/x_axis/time/x-axis-weights.generator.js +73 -1
  108. package/dist/chart/components/x_axis/x-axis-draw.functions.js +49 -1
  109. package/dist/chart/components/x_axis/x-axis-labels.drawer.js +70 -1
  110. package/dist/chart/components/x_axis/x-axis-labels.generator.d.ts +2 -2
  111. package/dist/chart/components/x_axis/x-axis-labels.generator.js +332 -1
  112. package/dist/chart/components/x_axis/x-axis-labels.model.js +35 -1
  113. package/dist/chart/components/x_axis/x-axis-scale.handler.d.ts +2 -2
  114. package/dist/chart/components/x_axis/x-axis-scale.handler.js +60 -1
  115. package/dist/chart/components/x_axis/x-axis-time-labels.drawer.js +82 -1
  116. package/dist/chart/components/x_axis/x-axis.component.d.ts +2 -2
  117. package/dist/chart/components/x_axis/x-axis.component.js +119 -1
  118. package/dist/chart/components/y_axis/label-color.functions.js +53 -1
  119. package/dist/chart/components/y_axis/numeric-y-axis-labels.generator.d.ts +4 -4
  120. package/dist/chart/components/y_axis/numeric-y-axis-labels.generator.js +33 -1
  121. package/dist/chart/components/y_axis/price_labels/data-series-y-axis-labels.provider.d.ts +3 -3
  122. package/dist/chart/components/y_axis/price_labels/data-series-y-axis-labels.provider.js +67 -1
  123. package/dist/chart/components/y_axis/price_labels/labels-positions-calculator.js +56 -1
  124. package/dist/chart/components/y_axis/price_labels/last-candle-labels.provider.js +95 -1
  125. package/dist/chart/components/y_axis/price_labels/price-label.drawer.d.ts +2 -2
  126. package/dist/chart/components/y_axis/price_labels/price-label.drawer.js +119 -1
  127. package/dist/chart/components/y_axis/price_labels/y-axis-labels.model.d.ts +9 -8
  128. package/dist/chart/components/y_axis/price_labels/y-axis-labels.model.js +201 -1
  129. package/dist/chart/components/y_axis/price_labels/y-axis-price-labels.drawer.d.ts +6 -7
  130. package/dist/chart/components/y_axis/price_labels/y-axis-price-labels.drawer.js +58 -1
  131. package/dist/chart/components/y_axis/y-axis-base-labels.model.d.ts +6 -4
  132. package/dist/chart/components/y_axis/y-axis-base-labels.model.js +47 -1
  133. package/dist/chart/components/y_axis/y-axis-labels.drawer.d.ts +4 -4
  134. package/dist/chart/components/y_axis/y-axis-labels.drawer.js +161 -1
  135. package/dist/chart/components/y_axis/y-axis-scale.handler.d.ts +2 -2
  136. package/dist/chart/components/y_axis/y-axis-scale.handler.js +72 -1
  137. package/dist/chart/components/y_axis/y-axis.component.d.ts +32 -41
  138. package/dist/chart/components/y_axis/y-axis.component.js +220 -1
  139. package/dist/chart/components/y_axis/y-axis.drawer.d.ts +9 -12
  140. package/dist/chart/components/y_axis/y-axis.drawer.js +96 -1
  141. package/dist/chart/components/y_axis/y-axis.model.d.ts +9 -17
  142. package/dist/chart/components/y_axis/y-axis.model.js +33 -1
  143. package/dist/chart/drawers/chart-background.drawer.js +69 -1
  144. package/dist/chart/drawers/chart-type-drawers/area.drawer.js +69 -1
  145. package/dist/chart/drawers/chart-type-drawers/bar.drawer.js +57 -1
  146. package/dist/chart/drawers/chart-type-drawers/baseline.drawer.js +84 -1
  147. package/dist/chart/drawers/chart-type-drawers/candle.drawer.js +162 -1
  148. package/dist/chart/drawers/chart-type-drawers/histogram.drawer.js +57 -1
  149. package/dist/chart/drawers/chart-type-drawers/line.drawer.js +38 -1
  150. package/dist/chart/drawers/chart-type-drawers/scatter-plot.drawer.js +19 -1
  151. package/dist/chart/drawers/clear-canvas.drawer.js +14 -1
  152. package/dist/chart/drawers/composite.drawer.js +67 -1
  153. package/dist/chart/drawers/data-series-drawers/candle-series-wrapper.js +50 -1
  154. package/dist/chart/drawers/data-series-drawers/color-candle.drawer.js +35 -1
  155. package/dist/chart/drawers/data-series-drawers/data-series-drawers.utils.js +16 -1
  156. package/dist/chart/drawers/data-series-drawers/difference-cloud.drawer.js +106 -1
  157. package/dist/chart/drawers/data-series-drawers/histogram.drawer.js +23 -1
  158. package/dist/chart/drawers/data-series-drawers/linear.drawer.js +19 -1
  159. package/dist/chart/drawers/data-series-drawers/points.drawer.js +19 -1
  160. package/dist/chart/drawers/data-series-drawers/rectangular-tool.drawer.js +7 -1
  161. package/dist/chart/drawers/data-series-drawers/text.drawer.js +25 -1
  162. package/dist/chart/drawers/data-series-drawers/trend-histogram.drawer.js +35 -1
  163. package/dist/chart/drawers/data-series-drawers/triangle.drawer.js +22 -1
  164. package/dist/chart/drawers/data-series.drawer.d.ts +5 -7
  165. package/dist/chart/drawers/data-series.drawer.js +56 -1
  166. package/dist/chart/drawers/drawing-manager.d.ts +1 -1
  167. package/dist/chart/drawers/drawing-manager.js +177 -1
  168. package/dist/chart/drawers/ht-data-series.drawer.js +37 -1
  169. package/dist/chart/events/event-bus.js +73 -1
  170. package/dist/chart/events/events.js +3 -1
  171. package/dist/chart/inputhandlers/candle-tap.handler.js +46 -1
  172. package/dist/chart/inputhandlers/chart-resize.handler.js +98 -1
  173. package/dist/chart/inputhandlers/cross-event-producer.component.js +67 -1
  174. package/dist/chart/inputhandlers/hover-producer.component.d.ts +2 -2
  175. package/dist/chart/inputhandlers/hover-producer.component.js +228 -1
  176. package/dist/chart/inputhandlers/main-canvas-touch.handler.d.ts +2 -2
  177. package/dist/chart/inputhandlers/main-canvas-touch.handler.js +80 -1
  178. package/dist/chart/inputlisteners/canvas-input-listener.component.js +629 -1
  179. package/dist/chart/model/baseline.model.js +88 -1
  180. package/dist/chart/model/bounds.model.js +1 -1
  181. package/dist/chart/model/candle-hover.js +70 -1
  182. package/dist/chart/model/candle-series-high-low.provider.js +45 -1
  183. package/dist/chart/model/candle-series.model.d.ts +3 -3
  184. package/dist/chart/model/candle-series.model.js +245 -1
  185. package/dist/chart/model/candle.model.js +69 -1
  186. package/dist/chart/model/canvas.model.js +228 -1
  187. package/dist/chart/model/chart-base-element.js +129 -1
  188. package/dist/chart/model/compare-series-hover.js +25 -1
  189. package/dist/chart/model/data-series-view.d.ts +5 -5
  190. package/dist/chart/model/data-series-view.js +111 -1
  191. package/dist/chart/model/data-series.config.js +14 -1
  192. package/dist/chart/model/data-series.model.d.ts +4 -4
  193. package/dist/chart/model/data-series.model.js +281 -1
  194. package/dist/chart/model/date-time.formatter.js +151 -1
  195. package/dist/chart/model/hit-test-canvas.model.d.ts +1 -1
  196. package/dist/chart/model/hit-test-canvas.model.js +256 -1
  197. package/dist/chart/model/main-candle-series.model.d.ts +1 -1
  198. package/dist/chart/model/main-candle-series.model.js +29 -1
  199. package/dist/chart/model/scale.model.d.ts +2 -1
  200. package/dist/chart/model/scale.model.js +316 -1
  201. package/dist/chart/model/scaling/auto-scale.model.js +97 -1
  202. package/dist/chart/model/scaling/constrait.functions.js +61 -1
  203. package/dist/chart/model/scaling/lock-ratio.model.js +17 -1
  204. package/dist/chart/model/scaling/move-chart.functions.js +22 -1
  205. package/dist/chart/model/scaling/viewport.model.d.ts +2 -2
  206. package/dist/chart/model/scaling/viewport.model.js +279 -1
  207. package/dist/chart/model/scaling/x-zooming.functions.js +35 -1
  208. package/dist/chart/model/time-zone.model.js +109 -1
  209. package/dist/chart/model/visual-candle.js +134 -1
  210. package/dist/chart/utils/__tests__/array.utils.test.js +135 -1
  211. package/dist/chart/utils/__tests__/math.utils.test.js +14 -1
  212. package/dist/chart/utils/__tests__/object.utils.test.js +11 -1
  213. package/dist/chart/utils/__tests__/priceIncrementsUtils.test.js +29 -1
  214. package/dist/chart/utils/array.utils.d.ts +1 -1
  215. package/dist/chart/utils/array.utils.js +269 -1
  216. package/dist/chart/utils/auto-period-detector.utils.js +39 -1
  217. package/dist/chart/utils/candles-generator-ts.utils.js +20 -1
  218. package/dist/chart/utils/candles-generator.utils.js +161 -1
  219. package/dist/chart/utils/candles.utils.js +42 -1
  220. package/dist/chart/utils/canvas/canvas-drawing-functions.utils.js +144 -1
  221. package/dist/chart/utils/canvas/canvas-font-measure-tool.utils.js +51 -1
  222. package/dist/chart/utils/canvas/canvas-text-functions.utils.js +156 -1
  223. package/dist/chart/utils/color.utils.js +31 -1
  224. package/dist/chart/utils/device/browser.utils.js +10 -1
  225. package/dist/chart/utils/device/device-detector.utils.js +24 -1
  226. package/dist/chart/utils/device/device-pixel-ratio.utils.js +21 -1
  227. package/dist/chart/utils/device/touchpad.utils.js +91 -1
  228. package/dist/chart/utils/dom.utils.js +15 -1
  229. package/dist/chart/utils/function.utils.js +16 -1
  230. package/dist/chart/utils/linkedList.utils.d.ts +28 -0
  231. package/dist/chart/utils/linkedList.utils.js +138 -0
  232. package/dist/chart/utils/math.utils.js +115 -1
  233. package/dist/chart/utils/merge.utils.js +56 -1
  234. package/dist/chart/utils/object.utils.js +43 -1
  235. package/dist/chart/utils/performance/animation-frame-cache.utils.js +77 -0
  236. package/dist/chart/utils/{perfomance → performance}/debounce.utils.js +7 -1
  237. package/dist/chart/utils/performance/memoize.utils.js +74 -0
  238. package/dist/chart/utils/performance/request-animation-frame-throttle.utils.js +43 -0
  239. package/dist/chart/utils/performance/throttle.utils.js +26 -0
  240. package/dist/chart/utils/price-increments.utils.d.ts +1 -0
  241. package/dist/chart/utils/price-increments.utils.js +127 -1
  242. package/dist/chart/utils/uuid.utils.js +4 -1
  243. package/dist/dxchart.min.js +9 -12
  244. package/dist/index.d.ts +3 -3
  245. package/dist/index.js +20 -1
  246. package/package.json +4 -4
  247. package/dist/chart/utils/perfomance/animation-frame-cache.utils.js +0 -6
  248. package/dist/chart/utils/perfomance/memoize.utils.js +0 -6
  249. package/dist/chart/utils/perfomance/request-animation-frame-throttle.utils.js +0 -6
  250. package/dist/chart/utils/perfomance/throttle.utils.js +0 -6
  251. /package/dist/chart/utils/{perfomance → performance}/animation-frame-cache.utils.d.ts +0 -0
  252. /package/dist/chart/utils/{perfomance → performance}/debounce.utils.d.ts +0 -0
  253. /package/dist/chart/utils/{perfomance → performance}/memoize.utils.d.ts +0 -0
  254. /package/dist/chart/utils/{perfomance → performance}/request-animation-frame-throttle.utils.d.ts +0 -0
  255. /package/dist/chart/utils/{perfomance → performance}/throttle.utils.d.ts +0 -0
@@ -3,4 +3,42 @@
3
3
  * This Source Code Form is subject to the terms of the Mozilla Public License, v. 2.0.
4
4
  * If a copy of the MPL was not distributed with this file, You can obtain one at https://mozilla.org/MPL/2.0/.
5
5
  */
6
- export function autoDetectPeriod(i){if(i.length>1){const f=i.reduce((t,e,o)=>{if(o!==i.length-1){const n=i[o+1].timestamp-e.timestamp;t[n]?t[n]++:t[n]=1}return t},{});let p=0,r;for(const t of Object.keys(f)){const e=parseInt(t,10),o=f[e];o>p&&e>0&&(p=o,r=e)}if(r)return r}}
6
+ /**
7
+ * Automatically detect period of candle source.
8
+ * Cannot just take first-second candles distance because of non-trading hours.
9
+ * Naive approach:
10
+ * 1. get all time distances between candles
11
+ * 2. the most frequent distance = period
12
+ * @param candles
13
+ * @doc-tags tricky,period
14
+ */
15
+ export function autoDetectPeriod(candles) {
16
+ if (candles.length > 1) {
17
+ // simply get distance between two candles
18
+ const periodToCount = candles.reduce((acc, candle, idx) => {
19
+ if (idx !== candles.length - 1) {
20
+ const period = candles[idx + 1].timestamp - candle.timestamp;
21
+ if (acc[period]) {
22
+ acc[period]++;
23
+ }
24
+ else {
25
+ acc[period] = 1;
26
+ }
27
+ }
28
+ return acc;
29
+ }, {});
30
+ let maxCount = 0;
31
+ let naivePeriod;
32
+ for (const _period of Object.keys(periodToCount)) {
33
+ const period = parseInt(_period, 10);
34
+ const count = periodToCount[period];
35
+ if (count > maxCount && period > 0) {
36
+ maxCount = count;
37
+ naivePeriod = period;
38
+ }
39
+ }
40
+ if (naivePeriod) {
41
+ return naivePeriod;
42
+ }
43
+ }
44
+ }
@@ -3,4 +3,23 @@
3
3
  * This Source Code Form is subject to the terms of the Mozilla Public License, v. 2.0.
4
4
  * If a copy of the MPL was not distributed with this file, You can obtain one at https://mozilla.org/MPL/2.0/.
5
5
  */
6
- import a from"./candles-generator.utils.js";export function generateCandlesDataTS(e){return a(e)}
6
+ import generateCandlesData from './candles-generator.utils.js';
7
+ /**
8
+ * Generated mock candles data.
9
+ *
10
+ * @param config
11
+ * quantity - avg number of candles
12
+ * startY - avg candle Y to start generation with
13
+ * avgCandleSize - avg size of candle
14
+ * avgTrendLength:
15
+ * sw - avg length of SIDEWAYS trend
16
+ * down - avg length of DOWN trend
17
+ * up - avg length of UP trend
18
+ * withVolume - add random volumes or not
19
+ * period - specify distance between candles (60 for 1m, 3600 for 1h, 84600 for 1d)
20
+ * @return Array<Candle>
21
+ * @doc-tags tricky
22
+ */
23
+ export function generateCandlesDataTS(config) {
24
+ return generateCandlesData(config);
25
+ }
@@ -3,4 +3,164 @@
3
3
  * This Source Code Form is subject to the terms of the Mozilla Public License, v. 2.0.
4
4
  * If a copy of the MPL was not distributed with this file, You can obtain one at https://mozilla.org/MPL/2.0/.
5
5
  */
6
- "use strict";export default function p(r){r=r||{},r.quantity=r.quantity||200,r.startY=r.startY||50,r.avgCandleSize=r.avgCandleSize||2,r.avgTrendLength=r.avgTrendLength||{},r.avgTrendLength.sw=r.avgTrendLength.sw||10,r.avgTrendLength.down=r.avgTrendLength.down||10,r.avgTrendLength.up=r.avgTrendLength.up||10,r.withVolume=r.withVolume===!0||!1,r.period=r.period||3600;var a=[],n=r.quantity||200,m=10,u=10,h=10;r.avgTrendLength&&(m=r.avgTrendLength.sw||10,u=r.avgTrendLength.down||10,h=r.avgTrendLength.up||10);var t=r.startY||50,e=r.avgCandleSize||2,v=t-e,o=t+e,w=T(v,o,e);for(a.push(w);a.length<n;){var d=Math.random()*10;d>6?a=a.concat(b(a[a.length-1],e,m)):d>3?a=a.concat(L(a[a.length-1],e,h)):a=a.concat(i(a[a.length-1],e,u))}a=a.slice(0,r.quantity);let s=Math.round(new Date().getTime()/3e5)*3e5;s-=n*1e3*r.period;for(let M=0;M<a.length;M++){const l=a[M];Object.assign(l,{timestamp:s,isVisible:!0}),s+=1e3*r.period,r.withVolume&&(l.volume=Math.ceil(Math.random()*1e3+200))}return a}function T(r,a,n,m){var u=Math.random()*(a-r)+r,h;if(m&&m==="bull"||!m&&Math.random()>.5){var t=u+n;h=t<a?t:a}else{var e=u-n;h=e>r?e:r}var v=Math.max(u,h)+Math.random()*n*.2,o=Math.min(u,h)-Math.random()*n*.2;return{hi:v,lo:o,open:u,close:h,timestamp:0,volume:0}}function L(r,a,n){for(var m=[],u=Math.random()*n+n/2,h=r.lo,t=r.hi+a*1.2,e=0;e<u;e++){var v=Math.random()>.9?"bear":"bull",o=T(h,t,a,v);h=o.lo-a*(Math.random()*.1+.1),t=o.hi+a*(Math.random()*1.3+.1),Math.random()>.9&&(t=o.hi+a),m.push(o)}return m}function i(r,a,n){for(var m=[],u=Math.random()*n+n/2,h=r.hi-a*1.2,t=r.hi,e=0;e<u;e++){var v=Math.random()<.9?"bear":"bull",o=T(h,t,a,v);h=o.lo-a*(Math.random()*.3+.1),t=o.hi-a*(Math.random()*.1+.1),Math.random()>.9&&(t=o.lo-a),m.push(o)}return m}function b(r,a,n){var m=[],u=Math.random()*n+n/2;function h(s){var M,l;return Math.random()>.5?(M=s.lo-a*.2,l=s.hi+a*1.2):(M=s.lo-a*1.2,l=s.hi+a*.2),{from:M,to:l}}for(var t=h(r),e=t.from,v=t.to,o=0;o<u;o++){var w=Math.random()<.5?"bear":"bull",d=T(e,v,a,w);t=h(d),e=t.from,v=t.to,Math.random()>.9&&(Math.random()>.5?e=d.lo-a:v=d.hi+a),m.push(d)}return m}window.DXChart=Object.assign(Object.assign({},window.DXChart),{generateCandlesData:p});
6
+ /* eslint-disable */
7
+ 'use strict';
8
+ export default function generateCandlesData(config) {
9
+ // default generation config
10
+ config = config || {};
11
+ config.quantity = config.quantity || 200;
12
+ config.startY = config.startY || 50;
13
+ config.avgCandleSize = config.avgCandleSize || 2;
14
+ config.avgTrendLength = config.avgTrendLength || {};
15
+ config.avgTrendLength.sw = config.avgTrendLength.sw || 10;
16
+ config.avgTrendLength.down = config.avgTrendLength.down || 10;
17
+ config.avgTrendLength.up = config.avgTrendLength.up || 10;
18
+ config.withVolume = config.withVolume === true || false;
19
+ config.period = config.period || 3600;
20
+ var result = [];
21
+ var quantity = config.quantity || 200;
22
+ var swTrendLength = 10;
23
+ var downTrendLength = 10;
24
+ var upTrendLength = 10;
25
+ if (config.avgTrendLength) {
26
+ swTrendLength = config.avgTrendLength.sw || 10;
27
+ downTrendLength = config.avgTrendLength.down || 10;
28
+ upTrendLength = config.avgTrendLength.up || 10;
29
+ }
30
+ var startY = config.startY || 50;
31
+ var candleAvgSize = config.avgCandleSize || 2;
32
+ // generate 1st candle
33
+ var from = startY - candleAvgSize;
34
+ var to = startY + candleAvgSize;
35
+ var entryCandle = generateRandomCandle(from, to, candleAvgSize);
36
+ result.push(entryCandle);
37
+ // generate UP, DOWN or SW trends until quantity is filled
38
+ while (result.length < quantity) {
39
+ var r = Math.random() * 10;
40
+ if (r > 6) {
41
+ result = result.concat(generateCandlesSideways(result[result.length - 1], candleAvgSize, swTrendLength));
42
+ }
43
+ else if (r > 3) {
44
+ result = result.concat(generateCandlesUp(result[result.length - 1], candleAvgSize, upTrendLength));
45
+ }
46
+ else {
47
+ result = result.concat(generateCandlesDown(result[result.length - 1], candleAvgSize, downTrendLength));
48
+ }
49
+ }
50
+ result = result.slice(0, config.quantity);
51
+ // set candle timestamps
52
+ let time = Math.round(new Date().getTime() / 300000) * 300000;
53
+ time -= quantity * 1000 * config.period;
54
+ for (let i = 0; i < result.length; i++) {
55
+ const candle = result[i];
56
+ Object.assign(candle, {
57
+ timestamp: time,
58
+ isVisible: true,
59
+ });
60
+ time += 1000 * config.period;
61
+ if (config.withVolume) {
62
+ candle.volume = Math.ceil(Math.random() * 1000 + 200);
63
+ }
64
+ }
65
+ return result;
66
+ }
67
+ function generateRandomCandle(from, to, candleAvgSize, type) {
68
+ var open = Math.random() * (to - from) + from;
69
+ var close;
70
+ if ((type && type === 'bull') || (!type && Math.random() > 0.5)) {
71
+ var close1 = open + candleAvgSize;
72
+ close = close1 < to ? close1 : to;
73
+ }
74
+ else {
75
+ var close2 = open - candleAvgSize;
76
+ close = close2 > from ? close2 : from;
77
+ }
78
+ var high = Math.max(open, close) + Math.random() * candleAvgSize * 0.2;
79
+ var low = Math.min(open, close) - Math.random() * candleAvgSize * 0.2;
80
+ return {
81
+ hi: high,
82
+ lo: low,
83
+ open: open,
84
+ close: close,
85
+ timestamp: 0,
86
+ volume: 0,
87
+ };
88
+ }
89
+ function generateCandlesUp(entryCandle, candleAvgSize, trendLength) {
90
+ var result = [];
91
+ var randomTrendLength = Math.random() * trendLength + trendLength / 2;
92
+ var from = entryCandle.lo;
93
+ var to = entryCandle.hi + candleAvgSize * 1.2;
94
+ for (var i = 0; i < randomTrendLength; i++) {
95
+ var type = Math.random() > 0.9 ? 'bear' : 'bull';
96
+ var candle = generateRandomCandle(from, to, candleAvgSize, type);
97
+ from = candle.lo - candleAvgSize * (Math.random() * 0.1 + 0.1);
98
+ to = candle.hi + candleAvgSize * (Math.random() * 1.3 + 0.1);
99
+ // spike
100
+ if (Math.random() > 0.9) {
101
+ to = candle.hi + candleAvgSize;
102
+ }
103
+ result.push(candle);
104
+ }
105
+ return result;
106
+ }
107
+ function generateCandlesDown(entryCandle, candleAvgSize, trendLength) {
108
+ var result = [];
109
+ var randomTrendLength = Math.random() * trendLength + trendLength / 2;
110
+ var from = entryCandle.hi - candleAvgSize * 1.2;
111
+ var to = entryCandle.hi;
112
+ for (var i = 0; i < randomTrendLength; i++) {
113
+ var type = Math.random() < 0.9 ? 'bear' : 'bull';
114
+ var candle = generateRandomCandle(from, to, candleAvgSize, type);
115
+ from = candle.lo - candleAvgSize * (Math.random() * 0.3 + 0.1);
116
+ to = candle.hi - candleAvgSize * (Math.random() * 0.1 + 0.1);
117
+ // spike
118
+ if (Math.random() > 0.9) {
119
+ to = candle.lo - candleAvgSize;
120
+ }
121
+ result.push(candle);
122
+ }
123
+ return result;
124
+ }
125
+ function generateCandlesSideways(entryCandle, candleAvgSize, trendLength) {
126
+ var result = [];
127
+ var randomTrendLength = Math.random() * trendLength + trendLength / 2;
128
+ function nextFromTo(candle) {
129
+ var from;
130
+ var to;
131
+ if (Math.random() > 0.5) {
132
+ from = candle.lo - candleAvgSize * 0.2;
133
+ to = candle.hi + candleAvgSize * 1.2;
134
+ }
135
+ else {
136
+ from = candle.lo - candleAvgSize * 1.2;
137
+ to = candle.hi + candleAvgSize * 0.2;
138
+ }
139
+ return {
140
+ from: from,
141
+ to: to,
142
+ };
143
+ }
144
+ var fromTo = nextFromTo(entryCandle);
145
+ var from = fromTo.from;
146
+ var to = fromTo.to;
147
+ for (var i = 0; i < randomTrendLength; i++) {
148
+ var type = Math.random() < 0.5 ? 'bear' : 'bull';
149
+ var candle = generateRandomCandle(from, to, candleAvgSize, type);
150
+ fromTo = nextFromTo(candle);
151
+ from = fromTo.from;
152
+ to = fromTo.to;
153
+ // spike
154
+ if (Math.random() > 0.9) {
155
+ if (Math.random() > 0.5) {
156
+ from = candle.lo - candleAvgSize;
157
+ }
158
+ else {
159
+ to = candle.hi + candleAvgSize;
160
+ }
161
+ }
162
+ result.push(candle);
163
+ }
164
+ return result;
165
+ }
166
+ window['DXChart'] = Object.assign(Object.assign({}, window['DXChart']), { generateCandlesData });
@@ -3,4 +3,45 @@
3
3
  * This Source Code Form is subject to the terms of the Mozilla Public License, v. 2.0.
4
4
  * If a copy of the MPL was not distributed with this file, You can obtain one at https://mozilla.org/MPL/2.0/.
5
5
  */
6
- import{binarySearch as c,firstOf as d,lastOf as s}from"./array.utils";import{floor as v}from"./math.utils";export const searchCandleIndex=(e,o,r,u=1e3)=>{var t,i,n,l;const f=(i=(t=d(r))===null||t===void 0?void 0:t.timestamp)!==null&&i!==void 0?i:0,x=(l=(n=s(r))===null||n===void 0?void 0:n.timestamp)!==null&&l!==void 0?l:0;return e>x?o?{index:r.length-1+Math.ceil((e-x)/u),exact:!0}:{index:r.length-1,exact:!0}:e<f?o?{index:v((e-f)/u),exact:!0}:{index:-1,exact:!0}:c(r,e,a=>a.timestamp)};
6
+ import { binarySearch, firstOf, lastOf } from './array.utils';
7
+ import { floor } from './math.utils';
8
+ export const searchCandleIndex = (timestamp, shouldExtrapolate, candles, periodMs = 1000) => {
9
+ var _a, _b, _c, _d;
10
+ const firstTimestamp = (_b = (_a = firstOf(candles)) === null || _a === void 0 ? void 0 : _a.timestamp) !== null && _b !== void 0 ? _b : 0;
11
+ const lastTimestamp = (_d = (_c = lastOf(candles)) === null || _c === void 0 ? void 0 : _c.timestamp) !== null && _d !== void 0 ? _d : 0;
12
+ if (timestamp > lastTimestamp) {
13
+ // TODO rework the code below, it looks very very sus ( ≖.≖)
14
+ if (shouldExtrapolate) {
15
+ // div by 1000 because periodDuration is in seconds
16
+ return {
17
+ // -1 to skip fake candle and get the last existing/visible one if reached last index
18
+ index: candles.length - 1 + Math.ceil((timestamp - lastTimestamp) / periodMs),
19
+ exact: true,
20
+ };
21
+ }
22
+ else {
23
+ return {
24
+ // -1 to skip fake candle and get the last existing/visible one if reached last index
25
+ index: candles.length - 1,
26
+ exact: true,
27
+ };
28
+ }
29
+ }
30
+ else if (timestamp < firstTimestamp) {
31
+ if (shouldExtrapolate) {
32
+ return {
33
+ index: floor((timestamp - firstTimestamp) / periodMs),
34
+ exact: true,
35
+ };
36
+ }
37
+ else {
38
+ return {
39
+ index: -1,
40
+ exact: true,
41
+ };
42
+ }
43
+ }
44
+ else {
45
+ return binarySearch(candles, timestamp, candle => candle.timestamp);
46
+ }
47
+ };
@@ -3,4 +3,147 @@
3
3
  * This Source Code Form is subject to the terms of the Mozilla Public License, v. 2.0.
4
4
  * If a copy of the MPL was not distributed with this file, You can obtain one at https://mozilla.org/MPL/2.0/.
5
5
  */
6
- export function drawRoundedRect(e,i,o,l,a,n=4,r=!0,v=!1){e.save(),e.beginPath(),e.moveTo(i+n,o),e.lineTo(i+l-n,o),e.quadraticCurveTo(i+l,o,i+l,o+n),e.lineTo(i+l,o+a-n),e.quadraticCurveTo(i+l,o+a,i+l-n,o+a),e.lineTo(i+n,o+a),e.quadraticCurveTo(i,o+a,i,o+a-n),e.lineTo(i,o+n),e.quadraticCurveTo(i,o,i+n,o),e.closePath(),r&&e.fill(),v&&e.stroke(),e.restore()}export function drawPriceLabel(e,i,o,l,a,n,r,v,u,C,q=4,P=!0,m=!1){const s=C==="right"?v:-v,T=C==="right"?q:-q,f=q;e.save(),e.beginPath(),u?e.moveTo(n+T,r):e.moveTo(n,r),u?(e.lineTo(n+s-T,r),e.quadraticCurveTo(n+s,r,n+s,r-f),e.lineTo(n+s,o+f),e.quadraticCurveTo(n+s,o,n+s-T,o),e.lineTo(i+T,o)):(e.lineTo(n+s,r),e.lineTo(n+s,o),e.lineTo(i,o)),u?(e.quadraticCurveTo(i,o,i-T/3,o+f/3),e.lineTo(l+T/3,a-f/3),e.quadraticCurveTo(l,a,l+T/3,a+f/3),e.lineTo(n-T/3,r-f/3),e.quadraticCurveTo(n,r,n+T/3,r)):(e.lineTo(l,a),e.lineTo(n,r)),e.closePath(),P&&e.fill(),m&&e.stroke(),e.restore()}export function drawLine(e,i,o,l,a,n=1){e.save(),e.lineWidth=n,e.beginPath(),e.moveTo(i,o),e.lineTo(l,a),e.stroke(),e.closePath(),e.restore()}export function avoidAntialiasing(e,i){e.save(),e.lineWidth%2&&e.translate(.5,.5),i(),e.restore()}export function fillRect(e,i,o){const l=Math.min(i.x,o.x)+.5,a=Math.min(i.y,o.y),n=Math.abs(i.x-o.x),r=Math.abs(i.y-o.y);e.fillRect(l,a,n,r)}
6
+ /**
7
+ * Draws a rounded rectangle on a canvas context
8
+ * @param {CanvasRenderingContext2D} ctx - The canvas context to draw on
9
+ * @param {number} x - The x-coordinate of the top-left corner of the rectangle
10
+ * @param {number} y - The y-coordinate of the top-left corner of the rectangle
11
+ * @param {number} width - The width of the rectangle
12
+ * @param {number} height - The height of the rectangle
13
+ * @param {number} [radius=4] - The radius of the corners of the rectangle
14
+ * @param {boolean} [fill=true] - Whether or not to fill the rectangle
15
+ * @param {boolean} [stroke=false] - Whether or not to stroke the rectangle
16
+ */
17
+ export function drawRoundedRect(ctx, x, y, width, height, radius = 4, fill = true, stroke = false) {
18
+ ctx.save();
19
+ ctx.beginPath();
20
+ ctx.moveTo(x + radius, y);
21
+ ctx.lineTo(x + width - radius, y);
22
+ ctx.quadraticCurveTo(x + width, y, x + width, y + radius);
23
+ ctx.lineTo(x + width, y + height - radius);
24
+ ctx.quadraticCurveTo(x + width, y + height, x + width - radius, y + height);
25
+ ctx.lineTo(x + radius, y + height);
26
+ ctx.quadraticCurveTo(x, y + height, x, y + height - radius);
27
+ ctx.lineTo(x, y + radius);
28
+ ctx.quadraticCurveTo(x, y, x + radius, y);
29
+ ctx.closePath();
30
+ if (fill) {
31
+ ctx.fill();
32
+ }
33
+ if (stroke) {
34
+ ctx.stroke();
35
+ }
36
+ ctx.restore();
37
+ }
38
+ /**
39
+ * Draws a price label on a canvas context.
40
+ * @param {CanvasRenderingContext2D} ctx - The canvas context to draw on.
41
+ * @param {number} x0 - The x-coordinate of the starting point of the label.
42
+ * @param {number} y0 - The y-coordinate of the starting point of the label.
43
+ * @param {number} x1 - The x-coordinate of the middle point of the label.
44
+ * @param {number} y1 - The y-coordinate of the middle point of the label.
45
+ * @param {number} x2 - The x-coordinate of the ending point of the label.
46
+ * @param {number} y2 - The y-coordinate of the ending point of the label.
47
+ * @param {number} _width - The width of the label.
48
+ * @param {boolean} rounded - Whether the label should have rounded corners.
49
+ * @param {string} align - The alignment of the label on the y-axis. Can be 'left' or 'right'.
50
+ * @param {number} radius - The radius of the rounded corners of the label.
51
+ * @param {boolean} fill - Whether the label should be filled with color.
52
+ * @param {boolean} stroke - Whether the label should have a stroke.
53
+ */
54
+ export function drawPriceLabel(ctx, x0, y0, x1, y1, x2, y2, _width, rounded, align, radius = 4, fill = true, stroke = false) {
55
+ const width = align === 'right' ? _width : -_width;
56
+ const xRadius = align === 'right' ? radius : -radius;
57
+ const yRadius = radius;
58
+ ctx.save();
59
+ ctx.beginPath();
60
+ if (rounded) {
61
+ ctx.moveTo(x2 + xRadius, y2);
62
+ }
63
+ else {
64
+ ctx.moveTo(x2, y2);
65
+ }
66
+ // rect
67
+ if (rounded) {
68
+ ctx.lineTo(x2 + width - xRadius, y2);
69
+ ctx.quadraticCurveTo(x2 + width, y2, x2 + width, y2 - yRadius);
70
+ ctx.lineTo(x2 + width, y0 + yRadius);
71
+ ctx.quadraticCurveTo(x2 + width, y0, x2 + width - xRadius, y0);
72
+ ctx.lineTo(x0 + xRadius, y0);
73
+ }
74
+ else {
75
+ ctx.lineTo(x2 + width, y2);
76
+ ctx.lineTo(x2 + width, y0);
77
+ ctx.lineTo(x0, y0);
78
+ }
79
+ // triangle
80
+ if (rounded) {
81
+ ctx.quadraticCurveTo(x0, y0, x0 - xRadius / 3, y0 + yRadius / 3);
82
+ ctx.lineTo(x1 + xRadius / 3, y1 - yRadius / 3);
83
+ ctx.quadraticCurveTo(x1, y1, x1 + xRadius / 3, y1 + yRadius / 3);
84
+ ctx.lineTo(x2 - xRadius / 3, y2 - yRadius / 3);
85
+ ctx.quadraticCurveTo(x2, y2, x2 + xRadius / 3, y2);
86
+ }
87
+ else {
88
+ ctx.lineTo(x1, y1);
89
+ ctx.lineTo(x2, y2);
90
+ }
91
+ ctx.closePath();
92
+ if (fill) {
93
+ ctx.fill();
94
+ }
95
+ if (stroke) {
96
+ ctx.stroke();
97
+ }
98
+ ctx.restore();
99
+ }
100
+ /**
101
+ * Draws a line on a canvas context with the specified thickness.
102
+ *
103
+ * @param {CanvasRenderingContext2D} ctx - The canvas context to draw on.
104
+ * @param {number} x0 - The x-coordinate of the starting point of the line.
105
+ * @param {number} y0 - The y-coordinate of the starting point of the line.
106
+ * @param {number} x1 - The x-coordinate of the ending point of the line.
107
+ * @param {number} y1 - The y-coordinate of the ending point of the line.
108
+ * @param {number} [thickness=1] - The thickness of the line. Default is 1.
109
+ *
110
+ * @returns {void}
111
+ */
112
+ export function drawLine(ctx, x0, y0, x1, y1, thickness = 1) {
113
+ ctx.save();
114
+ ctx.lineWidth = thickness;
115
+ ctx.beginPath();
116
+ ctx.moveTo(x0, y0);
117
+ ctx.lineTo(x1, y1);
118
+ ctx.stroke();
119
+ ctx.closePath();
120
+ ctx.restore();
121
+ }
122
+ /**
123
+ * Try to avoid anti-aliasing
124
+ */
125
+ export function avoidAntialiasing(ctx, draw) {
126
+ // TODO rework, potentially make it one time per DRAW event
127
+ ctx.save();
128
+ if (ctx.lineWidth % 2) {
129
+ ctx.translate(0.5, 0.5);
130
+ }
131
+ draw();
132
+ ctx.restore();
133
+ }
134
+ /**
135
+ * Draws a filled rectangle on a canvas context.
136
+ *
137
+ * @param {CanvasRenderingContext2D} ctx - The canvas context to draw on.
138
+ * @param {{x: number, y: number}} a - The first point of the rectangle.
139
+ * @param {{x: number, y: number}} b - The second point of the rectangle.
140
+ * @returns {void}
141
+
142
+ */
143
+ export function fillRect(ctx, a, b) {
144
+ const x = Math.min(a.x, b.x) + 0.5;
145
+ const y = Math.min(a.y, b.y);
146
+ const w = Math.abs(a.x - b.x);
147
+ const h = Math.abs(a.y - b.y);
148
+ ctx.fillRect(x, y, w, h);
149
+ }
@@ -3,4 +3,54 @@
3
3
  * This Source Code Form is subject to the terms of the Mozilla Public License, v. 2.0.
4
4
  * If a copy of the MPL was not distributed with this file, You can obtain one at https://mozilla.org/MPL/2.0/.
5
5
  */
6
- const u={};export function calculateSymbolHeight(r,e){let t=u[r];return t!==void 0||(e.save(),e.font=r,t=e.measureText("M").width,u[r]=t,e.restore()),t}const a=new Map;export function calculateTextWidth(r,e,t){const n=t+r;let s=a.get(n);return s||(e.save(),e.font=t,s=e.measureText(r).width,a.set(n,s),e.restore()),s}
6
+ /**
7
+ * Calculates max symbol height for provided font on canvas.
8
+ * @doc-tags tricky,chart-core,canvas
9
+ */
10
+ const symbolHeightCache = {};
11
+ /**
12
+ * Calculates the height of a symbol for a given font using a CanvasRenderingContext2D.
13
+ * @param {string} font - The font to calculate the symbol height for.
14
+ * @param {CanvasRenderingContext2D} ctx - The CanvasRenderingContext2D to use for the calculation.
15
+ * @returns {number} - The height of the symbol.
16
+ */
17
+ export function calculateSymbolHeight(font, ctx) {
18
+ let result = symbolHeightCache[font];
19
+ if (result !== undefined) {
20
+ return result;
21
+ }
22
+ // Capital "M" width is very close to height, 90% approximation
23
+ ctx.save();
24
+ ctx.font = font;
25
+ result = ctx.measureText('M').width;
26
+ symbolHeightCache[font] = result;
27
+ ctx.restore();
28
+ return result;
29
+ }
30
+ /**
31
+ * Calculates text width in pixels for provided font on canvas.
32
+ * @param text
33
+ * @param ctx
34
+ * @param font
35
+ * @doc-tags tricky,chart-core,canvas
36
+ */
37
+ const textWidthCache = new Map();
38
+ /**
39
+ * Calculates the width of a given text using the provided font and canvas rendering context.
40
+ * @param {string} text - The text to calculate the width of.
41
+ * @param {CanvasRenderingContext2D} ctx - The canvas rendering context to use for measuring the text width.
42
+ * @param {string} font - The font to use for measuring the text width.
43
+ * @returns {number} - The width of the text in pixels.
44
+ */
45
+ export function calculateTextWidth(text, ctx, font) {
46
+ const key = font + text;
47
+ let result = textWidthCache.get(key);
48
+ if (!result) {
49
+ ctx.save();
50
+ ctx.font = font;
51
+ result = ctx.measureText(text).width;
52
+ textWidthCache.set(key, result);
53
+ ctx.restore();
54
+ }
55
+ return result;
56
+ }
@@ -3,4 +3,159 @@
3
3
  * This Source Code Form is subject to the terms of the Mozilla Public License, v. 2.0.
4
4
  * If a copy of the MPL was not distributed with this file, You can obtain one at https://mozilla.org/MPL/2.0/.
5
5
  */
6
- import*as g from"color";export function prepareTextForFill(t,e){let n="";e.textStyle&&(e.textStyle.italic&&(n+="italic "),e.textStyle.bold&&(n+="bold ")),t.font=n+(e.textSize||12)+" "+(e.textFontFamily||"monospace"),t.fillStyle=e.textFill||"#FFFFFF",e.rtl?t.textAlign="right":t.textAlign="start"}export function getTextLineHeight(t){const e=t.font.match(/(\d+.)?\d+(px|pt)/gi);let n="10px";return e&&e.length&&(e[0].includes("pt")?n=+e[0].slice(0,e[0].indexOf("pt"))*96/72+"px":n=e[0]),parseInt(n,10)*1.33}export function getTextBounds(t,e,n){t.save(),prepareTextForFill(t,n);const i=Math.ceil(Math.max.apply(Math,e.map(o=>t.measureText(o).width))),l=getTextLineHeight(t),r=l*e.length;return t.restore(),[i,r,l]}export function getTextLines(t){return(t||"").split(/\r\n|\r|\n/)}export function drawText(t,e,n,i,l){t.save();const[r,,o]=getTextBounds(t,e,l);e.forEach((s,d)=>{var u,x,a;const h=t.measureText(s).width,f=c(n,r,h,(u=l.textAlign)!==null&&u!==void 0?u:"left");l.textStyle&&l.textStyle.underline&&underlineText(t,s,f,i+o*d,(x=l.textFill)!==null&&x!==void 0?x:"",(a=l.textSize)!==null&&a!==void 0?a:""),t.fillText(s,f,i+o*d)}),t.restore()}function c(t,e,n,i){switch(i){case"left":return t;case"right":return t+e-n;default:return t}}export function underlineText(t,e,n,i,l,r){t.save();const o=t.measureText(e).width,s=n,d=n+o,u=i+2,x=u;let a=parseInt(r,10)/15;a<1&&(a=1),t.beginPath(),t.strokeStyle=l,t.lineWidth=a,t.moveTo(s,u),t.lineTo(d,x),t.stroke(),t.restore()}export function getFontSizeInPx(t){return`${t}px`}export function getLabelTextColorByBackgroundColor(t,e,n){const i=g.rgb(t).array(),l=i[0],r=i[1],o=i[2];return(l*299+r*587+o*114)/1e3<128?e:n}
6
+ import Color from 'color';
7
+ /**
8
+ * Sets the font, fill style and text alignment of a canvas context based on the provided properties.
9
+ * @param {CanvasRenderingContext2D} ctx - The canvas context to modify.
10
+ * @param {CanvasTextProperties} properties - An object containing the properties to apply to the canvas context.
11
+ * @param {string} properties.textStyle - The style of the text. Can be 'italic', 'bold' or both.
12
+ * @param {number} properties.textSize - The size of the text in pixels.
13
+ * @param {string} properties.textFontFamily - The font family of the text.
14
+ * @param {string} properties.textFill - The fill style of the text.
15
+ * @param {boolean} properties.rtl - Whether the text should be aligned to the right or to the left.
16
+ */
17
+ export function prepareTextForFill(ctx, properties) {
18
+ let textStyles = '';
19
+ if (properties.textStyle) {
20
+ properties.textStyle.italic && (textStyles += `italic `);
21
+ properties.textStyle.bold && (textStyles += `bold `);
22
+ }
23
+ ctx.font = textStyles + (properties.textSize || 12) + ' ' + (properties.textFontFamily || 'monospace');
24
+ ctx.fillStyle = properties.textFill || '#FFFFFF';
25
+ if (properties.rtl) {
26
+ ctx.textAlign = 'right';
27
+ }
28
+ else {
29
+ ctx.textAlign = 'start';
30
+ }
31
+ }
32
+ /**
33
+ * Calculates the line height of a text based on the font size of the provided CanvasRenderingContext2D.
34
+ * @param {CanvasRenderingContext2D} ctx - The CanvasRenderingContext2D object used to draw the text.
35
+ * @returns {number} The calculated line height of the text.
36
+ */
37
+ export function getTextLineHeight(ctx) {
38
+ const textSizeMatch = ctx.font.match(/(\d+.)?\d+(px|pt)/gi);
39
+ let textSize = '10px';
40
+ if (textSizeMatch && textSizeMatch.length) {
41
+ if (textSizeMatch[0].includes('pt')) {
42
+ const ptSize = +textSizeMatch[0].slice(0, textSizeMatch[0].indexOf('pt'));
43
+ textSize = (ptSize * 96) / 72 + 'px';
44
+ }
45
+ else {
46
+ textSize = textSizeMatch[0];
47
+ }
48
+ }
49
+ return parseInt(textSize, 10) * 1.33; // Base Line Height in Project
50
+ }
51
+ /**
52
+ * Calculates text bounds [textWidth, textHeight, lineHeight]
53
+ * @param ctx
54
+ * @param lines
55
+ * @param properties
56
+ */
57
+ export function getTextBounds(ctx, lines, properties) {
58
+ ctx.save();
59
+ prepareTextForFill(ctx, properties);
60
+ const textWidth = Math.ceil(Math.max.apply(Math, lines.map((text) => {
61
+ return ctx.measureText(text).width;
62
+ })));
63
+ const lineHeight = getTextLineHeight(ctx);
64
+ const textHeight = lineHeight * lines.length;
65
+ ctx.restore();
66
+ return [textWidth, textHeight, lineHeight];
67
+ }
68
+ /**
69
+ * Splits multiline text to array of lines
70
+ * @param text
71
+ */
72
+ export function getTextLines(text) {
73
+ return (text || '').split(/\r\n|\r|\n/);
74
+ }
75
+ /**
76
+ * Draws multiple lines of text on a canvas context at a given position.
77
+ * @param {CanvasRenderingContext2D} ctx - The canvas context to draw on.
78
+ * @param {string[]} lines - An array of strings representing the lines of text to draw.
79
+ * @param {number} x - The x-coordinate of the starting position of the text.
80
+ * @param {number} y - The y-coordinate of the starting position of the text.
81
+ * @param {CanvasTextProperties} properties - An object containing properties for the text, such as font size, style, and alignment.
82
+ * @returns {void}
83
+
84
+ */
85
+ export function drawText(ctx, lines, x, y, properties) {
86
+ ctx.save();
87
+ const [textWidth, , lineHeight] = getTextBounds(ctx, lines, properties);
88
+ lines.forEach((l, i) => {
89
+ var _a, _b, _c;
90
+ const lineWidth = ctx.measureText(l).width;
91
+ const alignedX = alignCanvasText(x, textWidth, lineWidth, (_a = properties.textAlign) !== null && _a !== void 0 ? _a : 'left');
92
+ if (properties.textStyle && properties.textStyle.underline) {
93
+ underlineText(ctx, l, alignedX, y + lineHeight * i, (_b = properties.textFill) !== null && _b !== void 0 ? _b : '', (_c = properties.textSize) !== null && _c !== void 0 ? _c : '');
94
+ }
95
+ ctx.fillText(l, alignedX, y + lineHeight * i);
96
+ });
97
+ ctx.restore();
98
+ }
99
+ /**
100
+ * Aligns canvas text based on the given parameters.
101
+ * @param {number} originalCoordinate - The original coordinate of the text.
102
+ * @param {number} maxWidth - The maximum width of the text.
103
+ * @param {number} textWidth - The width of the text.
104
+ * @param {CanvasTextAlignment} alignment - The alignment of the text.
105
+ * @returns {number} - The aligned coordinate of the text.
106
+ */
107
+ function alignCanvasText(originalCoordinate, maxWidth, textWidth, alignment) {
108
+ switch (alignment) {
109
+ case 'left':
110
+ return originalCoordinate;
111
+ case 'right':
112
+ return originalCoordinate + maxWidth - textWidth;
113
+ default:
114
+ return originalCoordinate;
115
+ }
116
+ }
117
+ /**
118
+ * Draws an underline below the given text on a canvas context.
119
+ * @param {CanvasRenderingContext2D} ctx - The canvas context to draw on.
120
+ * @param {string} text - The text to underline.
121
+ * @param {number} x - The x-coordinate of the starting point of the text.
122
+ * @param {number} y - The y-coordinate of the starting point of the text.
123
+ * @param {string} color - The color of the underline.
124
+ * @param {string} textSize - The size of the text in pixels.
125
+ */
126
+ export function underlineText(ctx, text, x, y, color, textSize) {
127
+ ctx.save();
128
+ const textWidth = ctx.measureText(text).width;
129
+ const startX = x;
130
+ const endX = x + textWidth;
131
+ const startY = y + 2; // shifts underline 2px lower than the text
132
+ const endY = startY;
133
+ let underlineHeight = parseInt(textSize, 10) / 15;
134
+ if (underlineHeight < 1) {
135
+ underlineHeight = 1;
136
+ }
137
+ ctx.beginPath();
138
+ ctx.strokeStyle = color;
139
+ ctx.lineWidth = underlineHeight;
140
+ ctx.moveTo(startX, startY);
141
+ ctx.lineTo(endX, endY);
142
+ ctx.stroke();
143
+ ctx.restore();
144
+ }
145
+ /**
146
+ * Returns a string with the font size in pixels.
147
+ *
148
+ * @param {number} fontSize - The font size in points.
149
+ * @returns {string} The font size in pixels as a string.
150
+ */
151
+ export function getFontSizeInPx(fontSize) {
152
+ return `${fontSize}px`;
153
+ }
154
+ export function getLabelTextColorByBackgroundColor(bgColor, textColor, invertedTextColor) {
155
+ const rgb = Color.rgb(bgColor).array();
156
+ const r = rgb[0];
157
+ const g = rgb[1];
158
+ const b = rgb[2];
159
+ const yiq = (r * 299 + g * 587 + b * 114) / 1000;
160
+ return yiq < 128 ? textColor : invertedTextColor;
161
+ }