billboard.js 4.0.3 → 4.1.0-next.1

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 (270) hide show
  1. package/README.md +82 -7
  2. package/dist/billboard.css +1 -1
  3. package/dist/billboard.js +1841 -491
  4. package/dist/billboard.min.css +1 -1
  5. package/dist/billboard.min.js +19 -24
  6. package/dist/billboard.pkgd.js +1837 -676
  7. package/dist/billboard.pkgd.min.js +24 -29
  8. package/dist/billboard.react.js +205 -0
  9. package/dist/billboard.react.min.js +9 -0
  10. package/dist/billboard.worker.js +10 -0
  11. package/dist/plugin/billboardjs-plugin-bubblecompare.js +2 -2
  12. package/dist/plugin/billboardjs-plugin-bubblecompare.min.js +2 -2
  13. package/dist/plugin/billboardjs-plugin-sparkline.js +3 -2
  14. package/dist/plugin/billboardjs-plugin-sparkline.min.js +3 -3
  15. package/dist/plugin/billboardjs-plugin-stanford.js +3 -2
  16. package/dist/plugin/billboardjs-plugin-stanford.min.js +2 -2
  17. package/dist/plugin/billboardjs-plugin-tableview.js +57 -27
  18. package/dist/plugin/billboardjs-plugin-tableview.min.js +5 -5
  19. package/dist/plugin/billboardjs-plugin-textoverlap.js +72 -54
  20. package/dist/plugin/billboardjs-plugin-textoverlap.min.js +2 -2
  21. package/dist/plugin/pkgd/billboardjs-plugin-bubblecompare.pkgd.js +2 -2
  22. package/dist/plugin/pkgd/billboardjs-plugin-bubblecompare.pkgd.min.js +2 -2
  23. package/dist/plugin/pkgd/billboardjs-plugin-sparkline.pkgd.js +3 -2
  24. package/dist/plugin/pkgd/billboardjs-plugin-sparkline.pkgd.min.js +3 -3
  25. package/dist/plugin/pkgd/billboardjs-plugin-stanford.pkgd.js +3 -2
  26. package/dist/plugin/pkgd/billboardjs-plugin-stanford.pkgd.min.js +2 -2
  27. package/dist/plugin/pkgd/billboardjs-plugin-tableview.pkgd.js +57 -27
  28. package/dist/plugin/pkgd/billboardjs-plugin-tableview.pkgd.min.js +11 -11
  29. package/dist/plugin/pkgd/billboardjs-plugin-textoverlap.pkgd.js +65 -3556
  30. package/dist/plugin/pkgd/billboardjs-plugin-textoverlap.pkgd.min.js +8 -8
  31. package/dist/theme/dark.css +1 -1
  32. package/dist/theme/dark.min.css +1 -1
  33. package/dist/theme/datalab.css +1 -1
  34. package/dist/theme/datalab.min.css +1 -1
  35. package/dist/theme/graph.css +1 -1
  36. package/dist/theme/graph.min.css +1 -1
  37. package/dist/theme/insight.css +1 -1
  38. package/dist/theme/insight.min.css +1 -1
  39. package/dist/theme/modern.css +1 -1
  40. package/dist/theme/modern.min.css +1 -1
  41. package/dist-esm/Chart/Chart.js +134 -152
  42. package/dist-esm/Chart/api/axis.js +217 -237
  43. package/dist-esm/Chart/api/category.js +54 -54
  44. package/dist-esm/Chart/api/chart.js +172 -215
  45. package/dist-esm/Chart/api/color.js +21 -21
  46. package/dist-esm/Chart/api/data.js +172 -173
  47. package/dist-esm/Chart/api/export.js +242 -262
  48. package/dist-esm/Chart/api/flow.js +198 -254
  49. package/dist-esm/Chart/api/focus.js +136 -143
  50. package/dist-esm/Chart/api/grid.js +191 -190
  51. package/dist-esm/Chart/api/group.js +31 -34
  52. package/dist-esm/Chart/api/legend.js +54 -63
  53. package/dist-esm/Chart/api/load.js +237 -257
  54. package/dist-esm/Chart/api/regions.js +148 -168
  55. package/dist-esm/Chart/api/selection.js +181 -139
  56. package/dist-esm/Chart/api/show.js +133 -147
  57. package/dist-esm/Chart/api/stubs.js +43 -42
  58. package/dist-esm/Chart/api/subchart.js +155 -172
  59. package/dist-esm/Chart/api/tooltip.js +132 -177
  60. package/dist-esm/Chart/api/x.js +67 -71
  61. package/dist-esm/Chart/api/zoom.js +183 -220
  62. package/dist-esm/ChartInternal/Axis/Axis.js +1059 -1266
  63. package/dist-esm/ChartInternal/Axis/AxisRenderer.js +408 -540
  64. package/dist-esm/ChartInternal/Axis/AxisRendererHelper.js +113 -149
  65. package/dist-esm/ChartInternal/ChartInternal.js +573 -749
  66. package/dist-esm/ChartInternal/data/convert.helper.js +133 -159
  67. package/dist-esm/ChartInternal/data/convert.js +200 -229
  68. package/dist-esm/ChartInternal/data/data.js +967 -1153
  69. package/dist-esm/ChartInternal/data/load.js +145 -197
  70. package/dist-esm/ChartInternal/interactions/drag.js +86 -122
  71. package/dist-esm/ChartInternal/interactions/eventrect.js +455 -621
  72. package/dist-esm/ChartInternal/interactions/flow.js +151 -215
  73. package/dist-esm/ChartInternal/interactions/interaction.js +214 -252
  74. package/dist-esm/ChartInternal/interactions/subchart.js +425 -300
  75. package/dist-esm/ChartInternal/interactions/zoom.js +321 -440
  76. package/dist-esm/ChartInternal/internals/category.js +18 -18
  77. package/dist-esm/ChartInternal/internals/class.js +89 -99
  78. package/dist-esm/ChartInternal/internals/clip.js +88 -119
  79. package/dist-esm/ChartInternal/internals/color.js +233 -288
  80. package/dist-esm/ChartInternal/internals/domain.js +452 -588
  81. package/dist-esm/ChartInternal/internals/format.js +64 -77
  82. package/dist-esm/ChartInternal/internals/grid.js +333 -457
  83. package/dist-esm/ChartInternal/internals/legend.js +675 -927
  84. package/dist-esm/ChartInternal/internals/redraw.js +200 -289
  85. package/dist-esm/ChartInternal/internals/region.js +106 -159
  86. package/dist-esm/ChartInternal/internals/scale.js +186 -215
  87. package/dist-esm/ChartInternal/internals/selection.js +132 -159
  88. package/dist-esm/ChartInternal/internals/size.axis.js +92 -139
  89. package/dist-esm/ChartInternal/internals/size.js +286 -394
  90. package/dist-esm/ChartInternal/internals/style.js +39 -41
  91. package/dist-esm/ChartInternal/internals/subchart.util.js +49 -0
  92. package/dist-esm/ChartInternal/internals/text.arc.js +155 -219
  93. package/dist-esm/ChartInternal/internals/text.js +368 -528
  94. package/dist-esm/ChartInternal/internals/text.util.js +179 -239
  95. package/dist-esm/ChartInternal/internals/title.js +96 -121
  96. package/dist-esm/ChartInternal/internals/tooltip.js +450 -586
  97. package/dist-esm/ChartInternal/internals/transform.js +79 -97
  98. package/dist-esm/ChartInternal/internals/type.js +285 -236
  99. package/dist-esm/ChartInternal/shape/arc.js +825 -1112
  100. package/dist-esm/ChartInternal/shape/area.js +73 -94
  101. package/dist-esm/ChartInternal/shape/bar.js +227 -288
  102. package/dist-esm/ChartInternal/shape/bubble.js +9 -8
  103. package/dist-esm/ChartInternal/shape/candlestick.js +110 -138
  104. package/dist-esm/ChartInternal/shape/core/barRadius.js +197 -133
  105. package/dist-esm/ChartInternal/shape/core/bubble.js +64 -73
  106. package/dist-esm/ChartInternal/shape/core/candlestick.js +93 -86
  107. package/dist-esm/ChartInternal/shape/core/dataRegion.js +68 -75
  108. package/dist-esm/ChartInternal/shape/core/geometry.js +112 -112
  109. package/dist-esm/ChartInternal/shape/core/path.js +142 -115
  110. package/dist-esm/ChartInternal/shape/core/point.js +105 -125
  111. package/dist-esm/ChartInternal/shape/core/treemap.js +83 -88
  112. package/dist-esm/ChartInternal/shape/funnel.js +310 -338
  113. package/dist-esm/ChartInternal/shape/gauge.js +71 -103
  114. package/dist-esm/ChartInternal/shape/line.js +208 -278
  115. package/dist-esm/ChartInternal/shape/point.js +310 -425
  116. package/dist-esm/ChartInternal/shape/polar.js +84 -109
  117. package/dist-esm/ChartInternal/shape/radar.js +228 -318
  118. package/dist-esm/ChartInternal/shape/shape.js +782 -843
  119. package/dist-esm/ChartInternal/shape/treemap.js +152 -190
  120. package/dist-esm/canvas/CanvasAxisRenderer.js +1180 -1328
  121. package/dist-esm/canvas/CanvasEngine.js +148 -166
  122. package/dist-esm/canvas/CanvasPainter.js +395 -411
  123. package/dist-esm/canvas/CanvasRenderer.js +1584 -1834
  124. package/dist-esm/canvas/CanvasTheme.js +899 -802
  125. package/dist-esm/canvas/HitDetector.js +394 -463
  126. package/dist-esm/canvas/axisTicks.js +672 -786
  127. package/dist-esm/canvas/classes.js +19 -17
  128. package/dist-esm/canvas/color.js +40 -59
  129. package/dist-esm/canvas/geometry.js +38 -38
  130. package/dist-esm/canvas/labels.js +318 -371
  131. package/dist-esm/canvas/pointPattern.js +578 -580
  132. package/dist-esm/canvas/util.js +242 -262
  133. package/dist-esm/config/Options/Options.js +29 -32
  134. package/dist-esm/config/Options/axis/axis.js +101 -101
  135. package/dist-esm/config/Options/axis/x.js +759 -755
  136. package/dist-esm/config/Options/axis/y.js +525 -522
  137. package/dist-esm/config/Options/axis/y2.js +501 -485
  138. package/dist-esm/config/Options/common/boost.js +93 -32
  139. package/dist-esm/config/Options/common/color.js +88 -87
  140. package/dist-esm/config/Options/common/grid.js +74 -73
  141. package/dist-esm/config/Options/common/legend.js +180 -179
  142. package/dist-esm/config/Options/common/main.js +474 -473
  143. package/dist-esm/config/Options/common/point.js +160 -159
  144. package/dist-esm/config/Options/common/title.js +47 -46
  145. package/dist-esm/config/Options/common/tooltip.js +253 -252
  146. package/dist-esm/config/Options/data/axis.js +173 -172
  147. package/dist-esm/config/Options/data/data.js +958 -957
  148. package/dist-esm/config/Options/data/selection.js +131 -130
  149. package/dist-esm/config/Options/interaction/interaction.js +53 -52
  150. package/dist-esm/config/Options/interaction/subchart.js +164 -73
  151. package/dist-esm/config/Options/interaction/zoom.js +86 -85
  152. package/dist-esm/config/Options/shape/arc.js +150 -149
  153. package/dist-esm/config/Options/shape/area.js +69 -68
  154. package/dist-esm/config/Options/shape/bar.js +147 -146
  155. package/dist-esm/config/Options/shape/bubble.js +35 -34
  156. package/dist-esm/config/Options/shape/candlestick.js +61 -60
  157. package/dist-esm/config/Options/shape/donut.js +181 -180
  158. package/dist-esm/config/Options/shape/funnel.js +49 -48
  159. package/dist-esm/config/Options/shape/gauge.js +237 -236
  160. package/dist-esm/config/Options/shape/line.js +58 -57
  161. package/dist-esm/config/Options/shape/pie.js +185 -184
  162. package/dist-esm/config/Options/shape/polar.js +167 -166
  163. package/dist-esm/config/Options/shape/radar.js +76 -75
  164. package/dist-esm/config/Options/shape/scatter.js +36 -36
  165. package/dist-esm/config/Options/shape/spline.js +46 -46
  166. package/dist-esm/config/Options/shape/treemap.js +80 -79
  167. package/dist-esm/config/Store/Element.js +64 -62
  168. package/dist-esm/config/Store/State.js +189 -200
  169. package/dist-esm/config/Store/Store.js +25 -26
  170. package/dist-esm/config/classes.js +157 -155
  171. package/dist-esm/config/config.js +26 -35
  172. package/dist-esm/config/const.js +120 -128
  173. package/dist-esm/config/resolver/axis.core.js +25 -25
  174. package/dist-esm/config/resolver/axis.js +26 -27
  175. package/dist-esm/config/resolver/canvas.js +2071 -2472
  176. package/dist-esm/config/resolver/canvasShape.js +201 -177
  177. package/dist-esm/config/resolver/category.js +26 -27
  178. package/dist-esm/config/resolver/export.js +24 -26
  179. package/dist-esm/config/resolver/flow.js +29 -31
  180. package/dist-esm/config/resolver/grid.js +34 -35
  181. package/dist-esm/config/resolver/interaction/selection.js +17 -17
  182. package/dist-esm/config/resolver/interaction/subchart.js +17 -17
  183. package/dist-esm/config/resolver/interaction/zoom.js +17 -17
  184. package/dist-esm/config/resolver/regions.js +30 -31
  185. package/dist-esm/config/resolver/shape/arc.helpers.js +19 -19
  186. package/dist-esm/config/resolver/shape/area.js +17 -17
  187. package/dist-esm/config/resolver/shape/axis.helpers.js +28 -25
  188. package/dist-esm/config/resolver/shape/bar.js +13 -13
  189. package/dist-esm/config/resolver/shape/bubble.js +18 -14
  190. package/dist-esm/config/resolver/shape/candlestick.js +13 -13
  191. package/dist-esm/config/resolver/shape/donut.js +11 -11
  192. package/dist-esm/config/resolver/shape/funnel.js +11 -11
  193. package/dist-esm/config/resolver/shape/gauge.js +12 -12
  194. package/dist-esm/config/resolver/shape/line.js +10 -10
  195. package/dist-esm/config/resolver/shape/pie.js +11 -11
  196. package/dist-esm/config/resolver/shape/polar.js +12 -12
  197. package/dist-esm/config/resolver/shape/radar.js +22 -14
  198. package/dist-esm/config/resolver/shape/scatter.js +13 -13
  199. package/dist-esm/config/resolver/shape/treemap.js +11 -11
  200. package/dist-esm/core.js +208 -211
  201. package/dist-esm/index.canvas.js +14 -13
  202. package/dist-esm/index.esm.js +25 -24
  203. package/dist-esm/module/Cache.js +117 -129
  204. package/dist-esm/module/browser.js +30 -35
  205. package/dist-esm/module/dsv.js +107 -127
  206. package/dist-esm/module/error.js +45 -52
  207. package/dist-esm/module/generator.js +75 -83
  208. package/dist-esm/module/sanitize.js +302 -399
  209. package/dist-esm/module/util/brush.js +27 -38
  210. package/dist-esm/module/util/dom.js +288 -348
  211. package/dist-esm/module/util/object.js +277 -341
  212. package/dist-esm/module/util/type-checks.js +21 -25
  213. package/dist-esm/module/worker.js +312 -130
  214. package/dist-esm/plugin/billboardjs-plugin-bubblecompare.js +185 -198
  215. package/dist-esm/plugin/billboardjs-plugin-sparkline.js +442 -450
  216. package/dist-esm/plugin/billboardjs-plugin-stanford.js +813 -950
  217. package/dist-esm/plugin/billboardjs-plugin-tableview.js +708 -788
  218. package/dist-esm/plugin/billboardjs-plugin-textoverlap.js +344 -301
  219. package/dist-esm/react/index.js +157 -0
  220. package/package.json +28 -8
  221. package/src/Chart/api/selection.ts +130 -5
  222. package/src/ChartInternal/Axis/Axis.ts +164 -65
  223. package/src/ChartInternal/Axis/AxisRenderer.ts +41 -8
  224. package/src/ChartInternal/Axis/AxisRendererHelper.ts +1 -1
  225. package/src/ChartInternal/ChartInternal.ts +1 -1
  226. package/src/ChartInternal/data/convert.ts +54 -4
  227. package/src/ChartInternal/data/data.ts +4 -1
  228. package/src/ChartInternal/interactions/eventrect.ts +6 -0
  229. package/src/ChartInternal/interactions/subchart.ts +437 -66
  230. package/src/ChartInternal/internals/color.ts +2 -2
  231. package/src/ChartInternal/internals/domain.ts +15 -3
  232. package/src/ChartInternal/internals/grid.ts +21 -1
  233. package/src/ChartInternal/internals/scale.ts +9 -2
  234. package/src/ChartInternal/internals/selection.ts +1 -1
  235. package/src/ChartInternal/internals/subchart.util.ts +55 -0
  236. package/src/ChartInternal/internals/text.arc.ts +3 -1
  237. package/src/ChartInternal/internals/text.ts +18 -5
  238. package/src/ChartInternal/internals/transform.ts +8 -2
  239. package/src/ChartInternal/internals/type.ts +105 -1
  240. package/src/ChartInternal/shape/IShape.ts +6 -3
  241. package/src/ChartInternal/shape/bar.ts +62 -31
  242. package/src/ChartInternal/shape/core/barRadius.ts +129 -0
  243. package/src/ChartInternal/shape/core/candlestick.ts +1 -1
  244. package/src/ChartInternal/shape/core/path.ts +76 -9
  245. package/src/ChartInternal/shape/funnel.ts +2 -3
  246. package/src/ChartInternal/shape/point.ts +2 -2
  247. package/src/ChartInternal/shape/shape.ts +149 -17
  248. package/src/Plugin/stanford/index.ts +0 -4
  249. package/src/Plugin/tableview/index.ts +85 -3
  250. package/src/Plugin/textoverlap/index.ts +9 -15
  251. package/src/canvas/CanvasAxisRenderer.ts +184 -36
  252. package/src/canvas/CanvasRenderer.ts +247 -160
  253. package/src/canvas/CanvasTheme.ts +212 -0
  254. package/src/canvas/axisTicks.ts +122 -40
  255. package/src/config/Options/axis/y2.ts +13 -0
  256. package/src/config/Options/common/boost.ts +64 -4
  257. package/src/config/Options/common/color.ts +2 -2
  258. package/src/config/Options/interaction/subchart.ts +98 -1
  259. package/src/config/Options/shape/gauge.ts +1 -1
  260. package/src/config/Options/shape/treemap.ts +2 -2
  261. package/src/config/Store/Element.ts +3 -1
  262. package/src/config/Store/State.ts +1 -0
  263. package/src/config/classes.ts +1 -0
  264. package/src/config/resolver/canvas.ts +92 -10
  265. package/src/module/voronoi.ts +119 -0
  266. package/src/module/worker.entry.ts +61 -0
  267. package/src/module/worker.ts +346 -63
  268. package/src/react/index.ts +193 -0
  269. package/types/options.d.ts +206 -3
  270. package/types/react/index.d.ts +27 -0
@@ -5,489 +5,420 @@
5
5
  * billboard.js, JavaScript chart library
6
6
  * https://naver.github.io/billboard.js/
7
7
  *
8
- * @version 4.0.3
8
+ * @version 4.1.0-next.1
9
9
  */
10
- import { getTreemapNodeRect } from '../ChartInternal/shape/core/geometry.js';
11
- import { TYPE } from '../config/const.js';
12
- import { getCanvasBarGeometry, getCanvasCandlestickGeometry } from './geometry.js';
13
- import { isCanvasScatterType, isCanvasBubbleType, isCanvasTreemapType, isFiniteCanvasCoordinate, isCanvasBarType, getCanvasShapeIndices, isCanvasTargetSupported, getCanvasTargetVisibleRange, hasCanvasDrawableValue, isCanvasCandlestickType, isCanvasPointType, markCanvasPointOccupancy, createCanvasPointOccupancyGrid, DENSE_SCATTER_POINT_CULL_THRESHOLD } from './util.js';
14
-
10
+ import { TYPE } from "../config/const.js";
11
+ import { getTreemapNodeRect } from "../ChartInternal/shape/core/geometry.js";
12
+ import { createCanvasPointOccupancyGrid, getCanvasShapeIndices, getCanvasTargetVisibleRange, hasCanvasDrawableValue, isCanvasBarType, isCanvasBubbleType, isCanvasCandlestickType, isCanvasPointType, isCanvasScatterType, isCanvasTargetSupported, isCanvasTreemapType, isFiniteCanvasCoordinate, markCanvasPointOccupancy } from "./util.js";
13
+ import { getCanvasBarGeometry, getCanvasCandlestickGeometry } from "./geometry.js";
14
+ //#region src/canvas/HitDetector.ts
15
15
  /**
16
- * Copyright (c) 2017 ~ present NAVER Corp.
17
- * billboard.js project is licensed under the MIT license
18
- */
19
- // Canvas-only spatial hit fallback. SVG point hit testing resolves from point_sensitivity;
20
- // this keeps the point hit grid usable before a per-point sensitivity is known.
16
+ * Copyright (c) 2017 ~ present NAVER Corp.
17
+ * billboard.js project is licensed under the MIT license
18
+ */
21
19
  const HIT_DISTANCE = 24;
22
- // Canvas-only spatial index bucket for rectangular shapes. SVG relies on DOM geometry,
23
- // while canvas scans grid candidates; 64px balances bucket count and candidate length.
24
20
  const BAR_CELL_SIZE = 64;
25
21
  const HIT_GROUPED_TYPE_FILTERS = [
26
- isCanvasPointType,
27
- isCanvasBarType,
28
- isCanvasCandlestickType
22
+ isCanvasPointType,
23
+ isCanvasBarType,
24
+ isCanvasCandlestickType
29
25
  ];
30
26
  /**
31
- * Get spatial grid cell coordinate.
32
- * @param {number} value Pixel coordinate
33
- * @param {number} size Cell size
34
- * @returns {number} Cell coordinate
35
- * @private
36
- */
27
+ * Get spatial grid cell coordinate.
28
+ * @param {number} value Pixel coordinate
29
+ * @param {number} size Cell size
30
+ * @returns {number} Cell coordinate
31
+ * @private
32
+ */
37
33
  function getCell(value, size) {
38
- return Math.floor(value / size);
34
+ return Math.floor(value / size);
39
35
  }
40
36
  /**
41
- * Add hit item to spatial grid cells it covers.
42
- * @param {Map} grid Spatial grid
43
- * @param {object} item Hit item
44
- * @param {number} size Cell size
45
- * @private
46
- */
37
+ * Add hit item to spatial grid cells it covers.
38
+ * @param {Map} grid Spatial grid
39
+ * @param {object} item Hit item
40
+ * @param {number} size Cell size
41
+ * @private
42
+ */
47
43
  function addGridItem(grid, item, size) {
48
- const minX = getCell(item.x, size);
49
- const maxX = getCell(item.x + (item.w ?? 0), size);
50
- const minY = getCell(item.y, size);
51
- const maxY = getCell(item.y + (item.h ?? 0), size);
52
- for (let x = minX; x <= maxX; x++) {
53
- for (let y = minY; y <= maxY; y++) {
54
- const key = `${x}:${y}`;
55
- const items = grid.get(key);
56
- items ? items.push(item) : grid.set(key, [item]);
57
- }
58
- }
44
+ const minX = getCell(item.x, size);
45
+ const maxX = getCell(item.x + (item.w ?? 0), size);
46
+ const minY = getCell(item.y, size);
47
+ const maxY = getCell(item.y + (item.h ?? 0), size);
48
+ for (let x = minX; x <= maxX; x++) for (let y = minY; y <= maxY; y++) {
49
+ const key = `${x}:${y}`;
50
+ const items = grid.get(key);
51
+ items ? items.push(item) : grid.set(key, [item]);
52
+ }
59
53
  }
60
54
  /**
61
- * Get hit items around a coordinate from spatial grid.
62
- * @param {Map} grid Spatial grid
63
- * @param {number} x X coordinate
64
- * @param {number} y Y coordinate
65
- * @param {number} size Cell size
66
- * @param {number} radius Neighbor cell radius
67
- * @returns {Array} Candidate hit items
68
- * @private
69
- */
55
+ * Get hit items around a coordinate from spatial grid.
56
+ * @param {Map} grid Spatial grid
57
+ * @param {number} x X coordinate
58
+ * @param {number} y Y coordinate
59
+ * @param {number} size Cell size
60
+ * @param {number} radius Neighbor cell radius
61
+ * @returns {Array} Candidate hit items
62
+ * @private
63
+ */
70
64
  function getGridItems(grid, x, y, size, radius = 0) {
71
- const cellX = getCell(x, size);
72
- const cellY = getCell(y, size);
73
- const items = [];
74
- for (let dx = -radius; dx <= radius; dx++) {
75
- for (let dy = -radius; dy <= radius; dy++) {
76
- const values = grid.get(`${cellX + dx}:${cellY + dy}`);
77
- if (values?.length) {
78
- for (let i = 0; i < values.length; i++) {
79
- items.push(values[i]);
80
- }
81
- }
82
- }
83
- }
84
- return items;
65
+ const cellX = getCell(x, size);
66
+ const cellY = getCell(y, size);
67
+ const items = [];
68
+ for (let dx = -radius; dx <= radius; dx++) for (let dy = -radius; dy <= radius; dy++) {
69
+ const values = grid.get(`${cellX + dx}:${cellY + dy}`);
70
+ if (values?.length) for (let i = 0; i < values.length; i++) items.push(values[i]);
71
+ }
72
+ return items;
85
73
  }
86
74
  /**
87
- * Get point hit sensitivity, avoiding radius work for fixed numeric sensitivity.
88
- * @param {object} $$ ChartInternal instance
89
- * @param {object} d Data row
90
- * @returns {number} Hit sensitivity
91
- * @private
92
- */
75
+ * Get point hit sensitivity, avoiding radius work for fixed numeric sensitivity.
76
+ * @param {object} $$ ChartInternal instance
77
+ * @param {object} d Data row
78
+ * @returns {number} Hit sensitivity
79
+ * @private
80
+ */
93
81
  function getPointHitSensitivity($$, d) {
94
- const sensitivity = $$.config.point_sensitivity;
95
- if (Number.isFinite(sensitivity)) {
96
- return sensitivity;
97
- }
98
- $$.pointR?.(d);
99
- const resolved = $$.getPointSensitivity?.(d) ?? HIT_DISTANCE;
100
- return Number.isFinite(resolved) ? resolved : HIT_DISTANCE;
82
+ const sensitivity = $$.config.point_sensitivity;
83
+ if (Number.isFinite(sensitivity)) return sensitivity;
84
+ $$.pointR?.(d);
85
+ const resolved = $$.getPointSensitivity?.(d) ?? HIT_DISTANCE;
86
+ return Number.isFinite(resolved) ? resolved : HIT_DISTANCE;
101
87
  }
102
88
  /**
103
- * Check whether point hit grid can use the same visible-center culling as dense scatter draw.
104
- * @param {object} $$ ChartInternal instance
105
- * @param {object} target Data target
106
- * @returns {boolean} Whether hit points can be culled
107
- * @private
108
- */
89
+ * Check whether point hit grid can use the same visible-center culling as dense scatter draw.
90
+ * @param {object} $$ ChartInternal instance
91
+ * @param {object} target Data target
92
+ * @returns {boolean} Whether hit points can be culled
93
+ * @private
94
+ */
109
95
  function shouldCullDenseScatterHitPoints($$, target) {
110
- return isCanvasScatterType($$, target) &&
111
- target.values.length > DENSE_SCATTER_POINT_CULL_THRESHOLD &&
112
- !$$.config.data_selection_enabled &&
113
- Number.isFinite($$.config.point_sensitivity) &&
114
- Number.isFinite($$.config.point_r);
96
+ return isCanvasScatterType($$, target) && target.values.length > 1e5 && !$$.config.data_selection_enabled && Number.isFinite($$.config.point_sensitivity) && Number.isFinite($$.config.point_r);
115
97
  }
116
98
  /**
117
- * Build and query canvas hit-test indexes.
118
- * @private
119
- */
120
- class HitDetector {
121
- bars = [];
122
- points = [];
123
- indices = [];
124
- barGrid = new Map();
125
- pointGrid = new Map();
126
- plot = { x: 0, y: 0, w: 0, h: 0 };
127
- pointCellSize = HIT_DISTANCE;
128
- maxPointSensitivity = HIT_DISTANCE;
129
- grouped = false;
130
- pointBased = false;
131
- indexAxis = "x";
132
- /**
133
- * Rebuild hit-test indexes from current chart geometry.
134
- * @param {object} $$ ChartInternal instance
135
- * @param {object} shape Cached draw shape object
136
- * @private
137
- */
138
- rebuild($$, shape) {
139
- const { current, margin, width, height } = $$.state;
140
- const targets = $$.filterTargetsToShow();
141
- this.bars = [];
142
- this.points = [];
143
- this.indices = [];
144
- this.barGrid = new Map();
145
- this.pointGrid = new Map();
146
- this.maxPointSensitivity = HIT_DISTANCE;
147
- this.indexAxis = $$.config.axis_rotated ? "y" : "x";
148
- this.plot = $$.state.hasTreemap ?
149
- { x: 0, y: 0, w: current.width, h: current.height } :
150
- { x: margin.left, y: margin.top, w: width, h: height };
151
- this.grouped = !!$$.config.tooltip_grouped && !$$.state.hasTreemap;
152
- this.pointBased = !($$.config.axis_x_forceAsSingle && this.grouped) && (!!$$.isMultipleX?.() ||
153
- targets.some(target => isCanvasScatterType($$, target) || isCanvasBubbleType($$, target)));
154
- const needsIndex = this.grouped &&
155
- (!this.pointBased ||
156
- ($$.config.data_selection_enabled && $$.config.data_selection_grouped));
157
- const indexMap = needsIndex ? new Map() : null;
158
- const addIndex = (x, y, data) => {
159
- if (!indexMap) {
160
- return;
161
- }
162
- const key = $$.getXCacheKey?.(data.x) ?? data.index;
163
- if (Number.isFinite(data.index) && !indexMap.has(key) &&
164
- isFiniteCanvasCoordinate(x, y)) {
165
- indexMap.set(key, { x, y, data });
166
- }
167
- };
168
- if ($$.state.hasTreemap) {
169
- const root = $$.getTreemapRoot?.($$.data.targets);
170
- const nodes = root?.children || [];
171
- for (const node of nodes) {
172
- const { data } = node;
173
- if (!isCanvasTreemapType($$, data)) {
174
- continue;
175
- }
176
- const { x, y, w, h } = getTreemapNodeRect($$, node, root, true);
177
- if (!isFiniteCanvasCoordinate(x, y) ||
178
- !isFiniteCanvasCoordinate(x + w, y + h)) {
179
- continue;
180
- }
181
- this.addBar({ x, y, w, h, data });
182
- }
183
- this.indices = [];
184
- return;
185
- }
186
- if (shape.indices[TYPE.BAR] || targets.some(isCanvasBarType.bind(null, $$))) {
187
- const isBar = isCanvasBarType.bind(null, $$);
188
- const indices = getCanvasShapeIndices($$, shape, TYPE.BAR, isBar);
189
- const getPoints = $$.generateGetBarPoints(indices, false);
190
- targets
191
- .filter(isBar)
192
- .filter(target => isCanvasTargetSupported($$, target, HIT_GROUPED_TYPE_FILTERS))
193
- .forEach(target => {
194
- const range = getCanvasTargetVisibleRange($$, target);
195
- for (let i = range.start; i < range.end; i++) {
196
- const d = target.values[i];
197
- if (!hasCanvasDrawableValue($$, d)) {
198
- continue;
199
- }
200
- const geometry = getCanvasBarGeometry($$, getPoints, d, i);
201
- if (!geometry) {
202
- continue;
203
- }
204
- const { rect } = geometry;
205
- const x = margin.left + rect.x;
206
- const y = margin.top + rect.y;
207
- const { w, h } = rect;
208
- this.addBar({ x, y, w, h, data: d });
209
- addIndex(x + w / 2, y + h / 2, d);
210
- }
211
- });
212
- }
213
- if (shape.indices[TYPE.CANDLESTICK] ||
214
- targets.some(isCanvasCandlestickType.bind(null, $$))) {
215
- const isCandlestick = isCanvasCandlestickType.bind(null, $$);
216
- const indices = getCanvasShapeIndices($$, shape, TYPE.CANDLESTICK, isCandlestick);
217
- const getPoints = $$.generateGetCandlestickPoints?.(indices, false);
218
- if (getPoints) {
219
- targets
220
- .filter(isCandlestick)
221
- .filter(target => isCanvasTargetSupported($$, target, HIT_GROUPED_TYPE_FILTERS))
222
- .forEach(target => {
223
- const range = getCanvasTargetVisibleRange($$, target);
224
- for (let i = range.start; i < range.end; i++) {
225
- const d = target.values[i];
226
- const value = $$.getCandlestickData?.(d);
227
- if (!value) {
228
- continue;
229
- }
230
- const geometry = getCanvasCandlestickGeometry($$, getPoints, d, i);
231
- if (!geometry) {
232
- continue;
233
- }
234
- const { body: rect } = geometry;
235
- const x = margin.left + rect.x;
236
- const y = margin.top + rect.y;
237
- const { w, h } = rect;
238
- this.addBar({
239
- x,
240
- y,
241
- w: Math.max(1, w),
242
- h: Math.max(1, h),
243
- data: d
244
- });
245
- addIndex(x + w / 2, y + h / 2, d);
246
- }
247
- });
248
- }
249
- }
250
- const { cx, cy } = shape.pos;
251
- if (cx && cy) {
252
- targets
253
- .filter(isCanvasPointType.bind(null, $$))
254
- .filter(target => isCanvasTargetSupported($$, target, HIT_GROUPED_TYPE_FILTERS))
255
- .forEach(target => {
256
- const range = getCanvasTargetVisibleRange($$, target);
257
- const occupancy = shouldCullDenseScatterHitPoints($$, target) ?
258
- createCanvasPointOccupancyGrid(width, height, $$.config.point_r) :
259
- null;
260
- for (let i = range.start; i < range.end; i++) {
261
- const d = target.values[i];
262
- if (!hasCanvasDrawableValue($$, d)) {
263
- continue;
264
- }
265
- const x = margin.left + cx(d, i);
266
- const y = margin.top + cy(d, i);
267
- const sensitivity = getPointHitSensitivity($$, d);
268
- if (!isFiniteCanvasCoordinate(x, y)) {
269
- continue;
270
- }
271
- if (occupancy &&
272
- !markCanvasPointOccupancy(occupancy, x - margin.left, y - margin.top)) {
273
- continue;
274
- }
275
- this.addPoint({
276
- x,
277
- y,
278
- sensitivity,
279
- data: d
280
- });
281
- addIndex(x, y, d);
282
- }
283
- });
284
- }
285
- this.buildPointGrid();
286
- this.indices = indexMap ?
287
- Array.from(indexMap.values())
288
- .sort((a, b) => a[this.indexAxis] - b[this.indexAxis]) :
289
- [];
290
- }
291
- /**
292
- * Hit-test bars at the given canvas coordinates.
293
- * @param {number} mx Mouse x coordinate
294
- * @param {number} my Mouse y coordinate
295
- * @returns {object|null} Matching data row
296
- * @private
297
- */
298
- hitBar(mx, my) {
299
- for (const item of getGridItems(this.barGrid, mx, my, BAR_CELL_SIZE)) {
300
- const { w = 0, h = 0 } = item;
301
- if (mx >= item.x &&
302
- mx <= item.x + w &&
303
- my >= item.y &&
304
- my <= item.y + h) {
305
- return item.data;
306
- }
307
- }
308
- return null;
309
- }
310
- /**
311
- * Find the nearest point within sensitivity at the given canvas coordinates.
312
- * @param {number} mx Mouse x coordinate
313
- * @param {number} my Mouse y coordinate
314
- * @returns {object|null} Matching data row
315
- * @private
316
- */
317
- hitPoint(mx, my) {
318
- let nearest = null;
319
- let min = Number.POSITIVE_INFINITY;
320
- for (const item of getGridItems(this.pointGrid, mx, my, this.pointCellSize, 1)) {
321
- const dx = item.x - mx;
322
- const dy = item.y - my;
323
- const dist = Math.sqrt(dx * dx + dy * dy);
324
- const sensitivity = item.sensitivity ?? HIT_DISTANCE;
325
- if (dist <= sensitivity && dist < min) {
326
- min = dist;
327
- nearest = item;
328
- }
329
- }
330
- return nearest?.data ?? null;
331
- }
332
- /**
333
- * Find the nearest data row for the given canvas coordinates.
334
- * @param {number} mx Mouse x coordinate
335
- * @param {number} my Mouse y coordinate
336
- * @returns {object|null} Matching data row
337
- * @private
338
- */
339
- findNearest(mx, my) {
340
- const bar = this.hitBar(mx, my);
341
- if (bar) {
342
- return bar;
343
- }
344
- if (!this.pointBased && this.grouped && this.isWithinPlot(mx, my)) {
345
- const item = this.findNearestIndexItem(mx, my);
346
- if (item) {
347
- return item.data;
348
- }
349
- }
350
- return this.hitPoint(mx, my);
351
- }
352
- /**
353
- * Find the nearest directly hit shape row, excluding grouped index fallback.
354
- * @param {number} mx Mouse x coordinate
355
- * @param {number} my Mouse y coordinate
356
- * @returns {object|null} Matching data row
357
- * @private
358
- */
359
- findNearestShape(mx, my) {
360
- return this.hitBar(mx, my) ?? this.hitPoint(mx, my);
361
- }
362
- /**
363
- * Find the nearest grouped x-index row for an axis-adjacent pointer coordinate.
364
- * @param {number} mx Mouse x coordinate
365
- * @param {number} my Mouse y coordinate
366
- * @returns {object|null} Matching data row
367
- * @private
368
- */
369
- findNearestIndexByCoord(mx, my) {
370
- return this.findNearestIndexItem(mx, my)?.data ?? null;
371
- }
372
- /**
373
- * Find data rows included by a rectangular selection area.
374
- * @param {object} rect Selection rectangle in canvas coordinates
375
- * @param {boolean} grouped Whether to match by the index axis only
376
- * @returns {Array} Matching data rows
377
- * @private
378
- */
379
- findInRect(rect, grouped = false) {
380
- const x1 = Math.min(rect.x, rect.x + rect.w);
381
- const x2 = Math.max(rect.x, rect.x + rect.w);
382
- const y1 = Math.min(rect.y, rect.y + rect.h);
383
- const y2 = Math.max(rect.y, rect.y + rect.h);
384
- const seen = new Set();
385
- const data = [];
386
- const add = (item) => {
387
- const d = item.data;
388
- const key = `${d.id}:${d.index}`;
389
- if (!seen.has(key)) {
390
- seen.add(key);
391
- data.push(d);
392
- }
393
- };
394
- if (grouped) {
395
- const axis = this.indexAxis;
396
- const min = axis === "y" ? y1 : x1;
397
- const max = axis === "y" ? y2 : x2;
398
- this.indices
399
- .filter(item => item[axis] >= min && item[axis] <= max)
400
- .forEach(add);
401
- return data;
402
- }
403
- this.bars
404
- .filter(item => {
405
- const w = item.w ?? 0;
406
- const h = item.h ?? 0;
407
- return !(x2 < item.x || item.x + w < x1) &&
408
- !(y2 < item.y || item.y + h < y1);
409
- })
410
- .forEach(add);
411
- this.points
412
- .filter(item => item.x >= x1 && item.x <= x2 && item.y >= y1 && item.y <= y2)
413
- .forEach(add);
414
- return data;
415
- }
416
- /**
417
- * Add bar-like hit item and register its grid coverage.
418
- * @param {object} item Hit item
419
- * @private
420
- */
421
- addBar(item) {
422
- this.bars.push(item);
423
- addGridItem(this.barGrid, item, BAR_CELL_SIZE);
424
- }
425
- /**
426
- * Add point-like hit item.
427
- * @param {object} item Hit item
428
- * @private
429
- */
430
- addPoint(item) {
431
- const sensitivity = item.sensitivity ?? HIT_DISTANCE;
432
- this.maxPointSensitivity = Math.max(this.maxPointSensitivity, sensitivity);
433
- this.points.push(item);
434
- }
435
- /**
436
- * Build point spatial grid after the maximum sensitivity is known.
437
- * @private
438
- */
439
- buildPointGrid() {
440
- this.pointCellSize = Math.max(1, this.maxPointSensitivity);
441
- this.pointGrid = new Map();
442
- for (const point of this.points) {
443
- addGridItem(this.pointGrid, point, this.pointCellSize);
444
- }
445
- }
446
- /**
447
- * Check if coordinates are inside the plot area.
448
- * @param {number} x X coordinate
449
- * @param {number} y Y coordinate
450
- * @returns {boolean} Whether the coordinates are inside the plot
451
- * @private
452
- */
453
- isWithinPlot(x, y) {
454
- const { plot } = this;
455
- return x >= plot.x &&
456
- x <= plot.x + plot.w &&
457
- y >= plot.y &&
458
- y <= plot.y + plot.h;
459
- }
460
- /**
461
- * Find the nearest indexed data row by current index-axis coordinate.
462
- * @param {number} x X coordinate
463
- * @param {number} y Y coordinate
464
- * @returns {object|null} Matching hit item
465
- * @private
466
- */
467
- findNearestIndexItem(x, y) {
468
- const { indexAxis, indices } = this;
469
- const value = indexAxis === "y" ? y : x;
470
- if (!indices.length) {
471
- return null;
472
- }
473
- let start = 0;
474
- let end = indices.length - 1;
475
- while (start < end) {
476
- const mid = (start + end) >> 1;
477
- if (indices[mid][indexAxis] < value) {
478
- start = mid + 1;
479
- }
480
- else {
481
- end = mid;
482
- }
483
- }
484
- const current = indices[start];
485
- const previous = indices[start - 1];
486
- return previous &&
487
- Math.abs(previous[indexAxis] - value) < Math.abs(current[indexAxis] - value) ?
488
- previous :
489
- current;
490
- }
491
- }
492
-
99
+ * Build and query canvas hit-test indexes.
100
+ * @private
101
+ */
102
+ var HitDetector = class {
103
+ bars = [];
104
+ points = [];
105
+ indices = [];
106
+ barGrid = /* @__PURE__ */ new Map();
107
+ pointGrid = /* @__PURE__ */ new Map();
108
+ plot = {
109
+ x: 0,
110
+ y: 0,
111
+ w: 0,
112
+ h: 0
113
+ };
114
+ pointCellSize = HIT_DISTANCE;
115
+ maxPointSensitivity = HIT_DISTANCE;
116
+ grouped = false;
117
+ pointBased = false;
118
+ indexAxis = "x";
119
+ /**
120
+ * Rebuild hit-test indexes from current chart geometry.
121
+ * @param {object} $$ ChartInternal instance
122
+ * @param {object} shape Cached draw shape object
123
+ * @private
124
+ */
125
+ rebuild($$, shape) {
126
+ const { current, margin, width, height } = $$.state;
127
+ const targets = $$.filterTargetsToShow();
128
+ this.bars = [];
129
+ this.points = [];
130
+ this.indices = [];
131
+ this.barGrid = /* @__PURE__ */ new Map();
132
+ this.pointGrid = /* @__PURE__ */ new Map();
133
+ this.maxPointSensitivity = HIT_DISTANCE;
134
+ this.indexAxis = $$.config.axis_rotated ? "y" : "x";
135
+ this.plot = $$.state.hasTreemap ? {
136
+ x: 0,
137
+ y: 0,
138
+ w: current.width,
139
+ h: current.height
140
+ } : {
141
+ x: margin.left,
142
+ y: margin.top,
143
+ w: width,
144
+ h: height
145
+ };
146
+ this.grouped = !!$$.config.tooltip_grouped && !$$.state.hasTreemap;
147
+ this.pointBased = !($$.config.axis_x_forceAsSingle && this.grouped) && (!!$$.isMultipleX?.() || targets.some((target) => isCanvasScatterType($$, target) || isCanvasBubbleType($$, target)));
148
+ const indexMap = this.grouped && (!this.pointBased || $$.config.data_selection_enabled && $$.config.data_selection_grouped) ? /* @__PURE__ */ new Map() : null;
149
+ const addIndex = (x, y, data) => {
150
+ if (!indexMap) return;
151
+ const key = $$.getXCacheKey?.(data.x) ?? data.index;
152
+ if (Number.isFinite(data.index) && !indexMap.has(key) && isFiniteCanvasCoordinate(x, y)) indexMap.set(key, {
153
+ x,
154
+ y,
155
+ data
156
+ });
157
+ };
158
+ if ($$.state.hasTreemap) {
159
+ const root = $$.getTreemapRoot?.($$.data.targets);
160
+ const nodes = root?.children || [];
161
+ for (const node of nodes) {
162
+ const { data } = node;
163
+ if (!isCanvasTreemapType($$, data)) continue;
164
+ const { x, y, w, h } = getTreemapNodeRect($$, node, root, true);
165
+ if (!isFiniteCanvasCoordinate(x, y) || !isFiniteCanvasCoordinate(x + w, y + h)) continue;
166
+ this.addBar({
167
+ x,
168
+ y,
169
+ w,
170
+ h,
171
+ data
172
+ });
173
+ }
174
+ this.indices = [];
175
+ return;
176
+ }
177
+ if (shape.indices[TYPE.BAR] || targets.some(isCanvasBarType.bind(null, $$))) {
178
+ const isBar = isCanvasBarType.bind(null, $$);
179
+ const indices = getCanvasShapeIndices($$, shape, TYPE.BAR, isBar);
180
+ const getPoints = $$.generateGetBarPoints(indices, false);
181
+ targets.filter(isBar).filter((target) => isCanvasTargetSupported($$, target, HIT_GROUPED_TYPE_FILTERS)).forEach((target) => {
182
+ const range = getCanvasTargetVisibleRange($$, target);
183
+ for (let i = range.start; i < range.end; i++) {
184
+ const d = target.values[i];
185
+ if (!hasCanvasDrawableValue($$, d)) continue;
186
+ const geometry = getCanvasBarGeometry($$, getPoints, d, i);
187
+ if (!geometry) continue;
188
+ const { rect } = geometry;
189
+ const x = margin.left + rect.x;
190
+ const y = margin.top + rect.y;
191
+ const { w, h } = rect;
192
+ this.addBar({
193
+ x,
194
+ y,
195
+ w,
196
+ h,
197
+ data: d
198
+ });
199
+ addIndex(x + w / 2, y + h / 2, d);
200
+ }
201
+ });
202
+ }
203
+ if (shape.indices[TYPE.CANDLESTICK] || targets.some(isCanvasCandlestickType.bind(null, $$))) {
204
+ const isCandlestick = isCanvasCandlestickType.bind(null, $$);
205
+ const indices = getCanvasShapeIndices($$, shape, TYPE.CANDLESTICK, isCandlestick);
206
+ const getPoints = $$.generateGetCandlestickPoints?.(indices, false);
207
+ if (getPoints) targets.filter(isCandlestick).filter((target) => isCanvasTargetSupported($$, target, HIT_GROUPED_TYPE_FILTERS)).forEach((target) => {
208
+ const range = getCanvasTargetVisibleRange($$, target);
209
+ for (let i = range.start; i < range.end; i++) {
210
+ const d = target.values[i];
211
+ if (!$$.getCandlestickData?.(d)) continue;
212
+ const geometry = getCanvasCandlestickGeometry($$, getPoints, d, i);
213
+ if (!geometry) continue;
214
+ const { body: rect } = geometry;
215
+ const x = margin.left + rect.x;
216
+ const y = margin.top + rect.y;
217
+ const { w, h } = rect;
218
+ this.addBar({
219
+ x,
220
+ y,
221
+ w: Math.max(1, w),
222
+ h: Math.max(1, h),
223
+ data: d
224
+ });
225
+ addIndex(x + w / 2, y + h / 2, d);
226
+ }
227
+ });
228
+ }
229
+ const { cx, cy } = shape.pos;
230
+ if (cx && cy) targets.filter(isCanvasPointType.bind(null, $$)).filter((target) => isCanvasTargetSupported($$, target, HIT_GROUPED_TYPE_FILTERS)).forEach((target) => {
231
+ const range = getCanvasTargetVisibleRange($$, target);
232
+ const occupancy = shouldCullDenseScatterHitPoints($$, target) ? createCanvasPointOccupancyGrid(width, height, $$.config.point_r) : null;
233
+ for (let i = range.start; i < range.end; i++) {
234
+ const d = target.values[i];
235
+ if (!hasCanvasDrawableValue($$, d)) continue;
236
+ const x = margin.left + cx(d, i);
237
+ const y = margin.top + cy(d, i);
238
+ const sensitivity = getPointHitSensitivity($$, d);
239
+ if (!isFiniteCanvasCoordinate(x, y)) continue;
240
+ if (occupancy && !markCanvasPointOccupancy(occupancy, x - margin.left, y - margin.top)) continue;
241
+ this.addPoint({
242
+ x,
243
+ y,
244
+ sensitivity,
245
+ data: d
246
+ });
247
+ addIndex(x, y, d);
248
+ }
249
+ });
250
+ this.buildPointGrid();
251
+ this.indices = indexMap ? Array.from(indexMap.values()).sort((a, b) => a[this.indexAxis] - b[this.indexAxis]) : [];
252
+ }
253
+ /**
254
+ * Hit-test bars at the given canvas coordinates.
255
+ * @param {number} mx Mouse x coordinate
256
+ * @param {number} my Mouse y coordinate
257
+ * @returns {object|null} Matching data row
258
+ * @private
259
+ */
260
+ hitBar(mx, my) {
261
+ for (const item of getGridItems(this.barGrid, mx, my, BAR_CELL_SIZE)) {
262
+ const { w = 0, h = 0 } = item;
263
+ if (mx >= item.x && mx <= item.x + w && my >= item.y && my <= item.y + h) return item.data;
264
+ }
265
+ return null;
266
+ }
267
+ /**
268
+ * Find the nearest point within sensitivity at the given canvas coordinates.
269
+ * @param {number} mx Mouse x coordinate
270
+ * @param {number} my Mouse y coordinate
271
+ * @returns {object|null} Matching data row
272
+ * @private
273
+ */
274
+ hitPoint(mx, my) {
275
+ let nearest = null;
276
+ let min = Number.POSITIVE_INFINITY;
277
+ for (const item of getGridItems(this.pointGrid, mx, my, this.pointCellSize, 1)) {
278
+ const dx = item.x - mx;
279
+ const dy = item.y - my;
280
+ const dist = Math.sqrt(dx * dx + dy * dy);
281
+ if (dist <= (item.sensitivity ?? HIT_DISTANCE) && dist < min) {
282
+ min = dist;
283
+ nearest = item;
284
+ }
285
+ }
286
+ return nearest?.data ?? null;
287
+ }
288
+ /**
289
+ * Find the nearest data row for the given canvas coordinates.
290
+ * @param {number} mx Mouse x coordinate
291
+ * @param {number} my Mouse y coordinate
292
+ * @returns {object|null} Matching data row
293
+ * @private
294
+ */
295
+ findNearest(mx, my) {
296
+ const bar = this.hitBar(mx, my);
297
+ if (bar) return bar;
298
+ if (!this.pointBased && this.grouped && this.isWithinPlot(mx, my)) {
299
+ const item = this.findNearestIndexItem(mx, my);
300
+ if (item) return item.data;
301
+ }
302
+ return this.hitPoint(mx, my);
303
+ }
304
+ /**
305
+ * Find the nearest directly hit shape row, excluding grouped index fallback.
306
+ * @param {number} mx Mouse x coordinate
307
+ * @param {number} my Mouse y coordinate
308
+ * @returns {object|null} Matching data row
309
+ * @private
310
+ */
311
+ findNearestShape(mx, my) {
312
+ return this.hitBar(mx, my) ?? this.hitPoint(mx, my);
313
+ }
314
+ /**
315
+ * Find the nearest grouped x-index row for an axis-adjacent pointer coordinate.
316
+ * @param {number} mx Mouse x coordinate
317
+ * @param {number} my Mouse y coordinate
318
+ * @returns {object|null} Matching data row
319
+ * @private
320
+ */
321
+ findNearestIndexByCoord(mx, my) {
322
+ return this.findNearestIndexItem(mx, my)?.data ?? null;
323
+ }
324
+ /**
325
+ * Find data rows included by a rectangular selection area.
326
+ * @param {object} rect Selection rectangle in canvas coordinates
327
+ * @param {boolean} grouped Whether to match by the index axis only
328
+ * @returns {Array} Matching data rows
329
+ * @private
330
+ */
331
+ findInRect(rect, grouped = false) {
332
+ const x1 = Math.min(rect.x, rect.x + rect.w);
333
+ const x2 = Math.max(rect.x, rect.x + rect.w);
334
+ const y1 = Math.min(rect.y, rect.y + rect.h);
335
+ const y2 = Math.max(rect.y, rect.y + rect.h);
336
+ const seen = /* @__PURE__ */ new Set();
337
+ const data = [];
338
+ const add = (item) => {
339
+ const d = item.data;
340
+ const key = `${d.id}:${d.index}`;
341
+ if (!seen.has(key)) {
342
+ seen.add(key);
343
+ data.push(d);
344
+ }
345
+ };
346
+ if (grouped) {
347
+ const axis = this.indexAxis;
348
+ const min = axis === "y" ? y1 : x1;
349
+ const max = axis === "y" ? y2 : x2;
350
+ this.indices.filter((item) => item[axis] >= min && item[axis] <= max).forEach(add);
351
+ return data;
352
+ }
353
+ this.bars.filter((item) => {
354
+ const w = item.w ?? 0;
355
+ const h = item.h ?? 0;
356
+ return !(x2 < item.x || item.x + w < x1) && !(y2 < item.y || item.y + h < y1);
357
+ }).forEach(add);
358
+ this.points.filter((item) => item.x >= x1 && item.x <= x2 && item.y >= y1 && item.y <= y2).forEach(add);
359
+ return data;
360
+ }
361
+ /**
362
+ * Add bar-like hit item and register its grid coverage.
363
+ * @param {object} item Hit item
364
+ * @private
365
+ */
366
+ addBar(item) {
367
+ this.bars.push(item);
368
+ addGridItem(this.barGrid, item, BAR_CELL_SIZE);
369
+ }
370
+ /**
371
+ * Add point-like hit item.
372
+ * @param {object} item Hit item
373
+ * @private
374
+ */
375
+ addPoint(item) {
376
+ const sensitivity = item.sensitivity ?? HIT_DISTANCE;
377
+ this.maxPointSensitivity = Math.max(this.maxPointSensitivity, sensitivity);
378
+ this.points.push(item);
379
+ }
380
+ /**
381
+ * Build point spatial grid after the maximum sensitivity is known.
382
+ * @private
383
+ */
384
+ buildPointGrid() {
385
+ this.pointCellSize = Math.max(1, this.maxPointSensitivity);
386
+ this.pointGrid = /* @__PURE__ */ new Map();
387
+ for (const point of this.points) addGridItem(this.pointGrid, point, this.pointCellSize);
388
+ }
389
+ /**
390
+ * Check if coordinates are inside the plot area.
391
+ * @param {number} x X coordinate
392
+ * @param {number} y Y coordinate
393
+ * @returns {boolean} Whether the coordinates are inside the plot
394
+ * @private
395
+ */
396
+ isWithinPlot(x, y) {
397
+ const { plot } = this;
398
+ return x >= plot.x && x <= plot.x + plot.w && y >= plot.y && y <= plot.y + plot.h;
399
+ }
400
+ /**
401
+ * Find the nearest indexed data row by current index-axis coordinate.
402
+ * @param {number} x X coordinate
403
+ * @param {number} y Y coordinate
404
+ * @returns {object|null} Matching hit item
405
+ * @private
406
+ */
407
+ findNearestIndexItem(x, y) {
408
+ const { indexAxis, indices } = this;
409
+ const value = indexAxis === "y" ? y : x;
410
+ if (!indices.length) return null;
411
+ let start = 0;
412
+ let end = indices.length - 1;
413
+ while (start < end) {
414
+ const mid = start + end >> 1;
415
+ if (indices[mid][indexAxis] < value) start = mid + 1;
416
+ else end = mid;
417
+ }
418
+ const current = indices[start];
419
+ const previous = indices[start - 1];
420
+ return previous && Math.abs(previous[indexAxis] - value) < Math.abs(current[indexAxis] - value) ? previous : current;
421
+ }
422
+ };
423
+ //#endregion
493
424
  export { HitDetector as default };