billboard.js 4.0.2 → 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 (272) hide show
  1. package/README.md +82 -7
  2. package/dist/billboard.css +1 -1
  3. package/dist/billboard.js +1938 -557
  4. package/dist/billboard.min.css +1 -1
  5. package/dist/billboard.min.js +19 -25
  6. package/dist/billboard.pkgd.js +1934 -742
  7. package/dist/billboard.pkgd.min.js +24 -30
  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 -147
  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 +970 -1147
  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 +286 -245
  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 -272
  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 -413
  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 -828
  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/focus.ts +8 -12
  222. package/src/Chart/api/selection.ts +130 -5
  223. package/src/ChartInternal/Axis/Axis.ts +164 -65
  224. package/src/ChartInternal/Axis/AxisRenderer.ts +41 -8
  225. package/src/ChartInternal/Axis/AxisRendererHelper.ts +1 -1
  226. package/src/ChartInternal/ChartInternal.ts +1 -1
  227. package/src/ChartInternal/data/convert.ts +54 -4
  228. package/src/ChartInternal/data/data.ts +19 -6
  229. package/src/ChartInternal/interactions/eventrect.ts +6 -0
  230. package/src/ChartInternal/interactions/subchart.ts +437 -66
  231. package/src/ChartInternal/internals/color.ts +7 -6
  232. package/src/ChartInternal/internals/domain.ts +15 -3
  233. package/src/ChartInternal/internals/grid.ts +21 -1
  234. package/src/ChartInternal/internals/scale.ts +9 -2
  235. package/src/ChartInternal/internals/selection.ts +1 -1
  236. package/src/ChartInternal/internals/subchart.util.ts +55 -0
  237. package/src/ChartInternal/internals/text.arc.ts +3 -1
  238. package/src/ChartInternal/internals/text.ts +19 -6
  239. package/src/ChartInternal/internals/transform.ts +8 -2
  240. package/src/ChartInternal/internals/type.ts +131 -33
  241. package/src/ChartInternal/shape/IShape.ts +6 -3
  242. package/src/ChartInternal/shape/bar.ts +87 -37
  243. package/src/ChartInternal/shape/core/barRadius.ts +129 -0
  244. package/src/ChartInternal/shape/core/candlestick.ts +1 -1
  245. package/src/ChartInternal/shape/core/path.ts +76 -9
  246. package/src/ChartInternal/shape/funnel.ts +2 -3
  247. package/src/ChartInternal/shape/line.ts +1 -1
  248. package/src/ChartInternal/shape/point.ts +27 -13
  249. package/src/ChartInternal/shape/shape.ts +172 -18
  250. package/src/Plugin/stanford/index.ts +0 -4
  251. package/src/Plugin/tableview/index.ts +85 -3
  252. package/src/Plugin/textoverlap/index.ts +9 -15
  253. package/src/canvas/CanvasAxisRenderer.ts +184 -36
  254. package/src/canvas/CanvasRenderer.ts +247 -160
  255. package/src/canvas/CanvasTheme.ts +212 -0
  256. package/src/canvas/axisTicks.ts +122 -40
  257. package/src/config/Options/axis/y2.ts +13 -0
  258. package/src/config/Options/common/boost.ts +64 -4
  259. package/src/config/Options/common/color.ts +2 -2
  260. package/src/config/Options/interaction/subchart.ts +98 -1
  261. package/src/config/Options/shape/gauge.ts +1 -1
  262. package/src/config/Options/shape/treemap.ts +2 -2
  263. package/src/config/Store/Element.ts +3 -1
  264. package/src/config/Store/State.ts +1 -0
  265. package/src/config/classes.ts +1 -0
  266. package/src/config/resolver/canvas.ts +92 -10
  267. package/src/module/voronoi.ts +119 -0
  268. package/src/module/worker.entry.ts +61 -0
  269. package/src/module/worker.ts +346 -63
  270. package/src/react/index.ts +193 -0
  271. package/types/options.d.ts +206 -3
  272. package/types/react/index.d.ts +27 -0
@@ -5,1930 +5,1680 @@
5
5
  * billboard.js, JavaScript chart library
6
6
  * https://naver.github.io/billboard.js/
7
7
  *
8
- * @version 4.0.2
8
+ * @version 4.1.0-next.1
9
9
  */
10
- import { getStackingBarRadiusSet, getBarRadiusInfo, getBarRadiusResolver } from '../ChartInternal/shape/core/barRadius.js';
11
- import { getLineRegionSegments } from '../ChartInternal/shape/core/dataRegion.js';
12
- import { getRenderDataPoint, getShapePoint, getTreemapNodeRect, getTreemapLabelText, getRenderPoint } from '../ChartInternal/shape/core/geometry.js';
13
- import { generateDrawAreaPath, generateDrawLinePath } from '../ChartInternal/shape/core/path.js';
14
- import { TYPE, SUBCHART_BRUSH_HANDLE_PATH } from '../config/const.js';
15
- import { window as win } from '../module/browser.js';
16
- import CanvasPainter from './CanvasPainter.js';
17
- import { withOpacity } from './color.js';
18
- import { getCanvasBarGeometry, getCanvasCandlestickGeometry } from './geometry.js';
19
- import { getLabelImageOption, getLabelImageUrl, getLabelImagePosition, getRotatedLabelPosition, getLabelColor, drawLabelDecorations, getLabelText, getPointLabelAnchor, getLabelPosition, getExpandedFocusMatcher, getLabelDecorationBox } from './labels.js';
20
- import { drawPointPattern } from './pointPattern.js';
21
- import { hasCanvasDrawableValue, isCanvasPointType, isFiniteCanvasCoordinate, isCanvasAreaType, getCanvasShapeIndices, isCanvasBarType, isCanvasCandlestickType, isCanvasLineType, isCanvasScatterType, isCanvasBubbleType, getCanvasTargetVisibleRange, createCanvasPointOccupancyGrid, markCanvasPointOccupancy, isCanvasTreemapType, getFontSize, isCanvasTargetSupported, DENSE_SCATTER_POINT_CULL_THRESHOLD } from './util.js';
22
- import { isNumber, isFunction, isObject, isString, asHalfPixel } from '../module/util/type-checks.js';
23
-
10
+ import { SUBCHART_BRUSH_HANDLE_PATH, TYPE } from "../config/const.js";
11
+ import { window as win } from "../module/browser.js";
12
+ import { asHalfPixel, isFunction, isNumber, isObject, isString } from "../module/util/type-checks.js";
13
+ import { generateDrawAreaPath, generateDrawLinePath } from "../ChartInternal/shape/core/path.js";
14
+ import { getLineRegionSegments } from "../ChartInternal/shape/core/dataRegion.js";
15
+ import { getBarRadiusInfo, getBarRadiusResolver, getStackingBarRadiusSet } from "../ChartInternal/shape/core/barRadius.js";
16
+ import { getRenderDataPoint, getRenderPoint, getShapePoint, getTreemapLabelText, getTreemapNodeRect } from "../ChartInternal/shape/core/geometry.js";
17
+ import { isContinuousGridFocusEnabled } from "../ChartInternal/internals/subchart.util.js";
18
+ import { createCanvasPointOccupancyGrid, getCanvasShapeIndices, getCanvasTargetVisibleRange, getFontSize, hasCanvasDrawableValue, isCanvasAreaType, isCanvasBarType, isCanvasBubbleType, isCanvasCandlestickType, isCanvasLineType, isCanvasPointType, isCanvasScatterType, isCanvasTargetSupported, isCanvasTreemapType, isFiniteCanvasCoordinate, markCanvasPointOccupancy } from "./util.js";
19
+ import CanvasPainter from "./CanvasPainter.js";
20
+ import { withOpacity } from "./color.js";
21
+ import { getCanvasBarGeometry, getCanvasCandlestickGeometry } from "./geometry.js";
22
+ import { drawLabelDecorations, getExpandedFocusMatcher, getLabelColor, getLabelDecorationBox, getLabelImageOption, getLabelImagePosition, getLabelImageUrl, getLabelPosition, getLabelText, getPointLabelAnchor, getRotatedLabelPosition } from "./labels.js";
23
+ import { drawPointPattern } from "./pointPattern.js";
24
+ //#region src/canvas/CanvasRenderer.ts
24
25
  /**
25
- * Copyright (c) 2017 ~ present NAVER Corp.
26
- * billboard.js project is licensed under the MIT license
27
- */
26
+ * Copyright (c) 2017 ~ present NAVER Corp.
27
+ * billboard.js project is licensed under the MIT license
28
+ */
28
29
  const RENDERER_GROUPED_TYPE_FILTERS = [
29
- isCanvasAreaType,
30
- isCanvasBarType,
31
- isCanvasPointType,
32
- isCanvasCandlestickType
30
+ isCanvasAreaType,
31
+ isCanvasBarType,
32
+ isCanvasPointType,
33
+ isCanvasCandlestickType
33
34
  ];
34
- // Small point series are faster as one batched path; beyond this, huge paths make
35
- // fill() expensive, so canvas draws circles individually. SVG has no equivalent knob.
36
- const MAX_BATCHED_CIRCLE_POINTS = 1000;
37
- const canvasFocusLookupCache = new WeakMap();
38
- const subchartBrushHandlePathCache = new Map();
35
+ const MAX_BATCHED_CIRCLE_POINTS = 1e3;
36
+ const canvasFocusLookupCache = /* @__PURE__ */ new WeakMap();
37
+ const subchartBrushHandlePathCache = /* @__PURE__ */ new Map();
39
38
  /**
40
- * Apply a CSS matrix transform to a canvas context.
41
- * @param {CanvasRenderingContext2D} ctx Canvas context
42
- * @param {string} transform CSS transform value
43
- * @private
44
- */
39
+ * Apply a CSS matrix transform to a canvas context.
40
+ * @param {CanvasRenderingContext2D} ctx Canvas context
41
+ * @param {string} transform CSS transform value
42
+ * @private
43
+ */
45
44
  function applyCssMatrixTransform(ctx, transform) {
46
- if (!transform || transform === "none") {
47
- return;
48
- }
49
- const matrix = transform.match(/^matrix\(([^)]+)\)$/);
50
- const matrix3d = transform.match(/^matrix3d\(([^)]+)\)$/);
51
- if (matrix) {
52
- const values = matrix[1].split(",").map(value => Number(value.trim()));
53
- values.length === 6 &&
54
- values.every(Number.isFinite) &&
55
- ctx.transform(values[0], values[1], values[2], values[3], values[4], values[5]);
56
- }
57
- else if (matrix3d) {
58
- const values = matrix3d[1].split(",").map(value => Number(value.trim()));
59
- values.length === 16 &&
60
- values.every(Number.isFinite) &&
61
- ctx.transform(values[0], values[1], values[4], values[5], values[12], values[13]);
62
- }
45
+ if (!transform || transform === "none") return;
46
+ const matrix = transform.match(/^matrix\(([^)]+)\)$/);
47
+ const matrix3d = transform.match(/^matrix3d\(([^)]+)\)$/);
48
+ if (matrix) {
49
+ const values = matrix[1].split(",").map((value) => Number(value.trim()));
50
+ values.length === 6 && values.every(Number.isFinite) && ctx.transform(values[0], values[1], values[2], values[3], values[4], values[5]);
51
+ } else if (matrix3d) {
52
+ const values = matrix3d[1].split(",").map((value) => Number(value.trim()));
53
+ values.length === 16 && values.every(Number.isFinite) && ctx.transform(values[0], values[1], values[4], values[5], values[12], values[13]);
54
+ }
63
55
  }
64
56
  /**
65
- * Get SVG image equivalent destination rect for canvas background.
66
- * @param {HTMLImageElement} image Image element
67
- * @param {number} width Background viewport width
68
- * @param {number} height Background viewport height
69
- * @returns {object} Destination rect
70
- * @private
71
- */
57
+ * Get SVG image equivalent destination rect for canvas background.
58
+ * @param {HTMLImageElement} image Image element
59
+ * @param {number} width Background viewport width
60
+ * @param {number} height Background viewport height
61
+ * @returns {object} Destination rect
62
+ * @private
63
+ */
72
64
  function getPreservedAspectRatioRect(image, width, height) {
73
- const imageWidth = image.naturalWidth || image.width;
74
- const imageHeight = image.naturalHeight || image.height;
75
- if (!imageWidth || !imageHeight || !width || !height) {
76
- return { x: 0, y: 0, w: width, h: height };
77
- }
78
- const scale = Math.min(width / imageWidth, height / imageHeight);
79
- const w = imageWidth * scale;
80
- const h = imageHeight * scale;
81
- return {
82
- x: (width - w) / 2,
83
- y: (height - h) / 2,
84
- w,
85
- h
86
- };
65
+ const imageWidth = image.naturalWidth || image.width;
66
+ const imageHeight = image.naturalHeight || image.height;
67
+ if (!imageWidth || !imageHeight || !width || !height) return {
68
+ x: 0,
69
+ y: 0,
70
+ w: width,
71
+ h: height
72
+ };
73
+ const scale = Math.min(width / imageWidth, height / imageHeight);
74
+ const w = imageWidth * scale;
75
+ const h = imageHeight * scale;
76
+ return {
77
+ x: (width - w) / 2,
78
+ y: (height - h) / 2,
79
+ w,
80
+ h
81
+ };
87
82
  }
88
83
  /**
89
- * Get SVG main group translate offset for canvas background image parity.
90
- * @param {object} $$ ChartInternal instance
91
- * @returns {object} Main group offset
92
- * @private
93
- */
84
+ * Get SVG main group translate offset for canvas background image parity.
85
+ * @param {object} $$ ChartInternal instance
86
+ * @returns {object} Main group offset
87
+ * @private
88
+ */
94
89
  function getBackgroundImageOffset($$) {
95
- const { state } = $$;
96
- return state.hasFunnel || state.hasTreemap ? { x: 0, y: 0 } : {
97
- x: asHalfPixel(state.margin.left),
98
- y: asHalfPixel(state.margin.top)
99
- };
90
+ const { state } = $$;
91
+ return state.hasFunnel || state.hasTreemap ? {
92
+ x: 0,
93
+ y: 0
94
+ } : {
95
+ x: asHalfPixel(state.margin.left),
96
+ y: asHalfPixel(state.margin.top)
97
+ };
100
98
  }
101
99
  /**
102
- * Draw a canvas line target directly from common geometry.
103
- * @param {object} $$ ChartInternal instance
104
- * @param {object} target Data target
105
- * @param {object} indices Shape indices
106
- * @param {CanvasPainter} painter Canvas painter
107
- * @param {boolean} isSub Whether to use subchart scales
108
- * @private
109
- */
100
+ * Draw a canvas line target directly from common geometry.
101
+ * @param {object} $$ ChartInternal instance
102
+ * @param {object} target Data target
103
+ * @param {object} indices Shape indices
104
+ * @param {CanvasPainter} painter Canvas painter
105
+ * @param {boolean} isSub Whether to use subchart scales
106
+ * @private
107
+ */
110
108
  function drawCanvasLine($$, target, indices, painter, isSub = false) {
111
- painter.strokePath(ctx => {
112
- generateDrawLinePath($$, indices, isSub, ctx)(target);
113
- }, { lineDash: [] });
109
+ painter.strokePath((ctx) => {
110
+ generateDrawLinePath($$, indices, isSub, ctx)(target);
111
+ }, { lineDash: [] });
114
112
  }
115
113
  /**
116
- * Draw a canvas area target directly from common geometry.
117
- * @param {object} $$ ChartInternal instance
118
- * @param {object} target Data target
119
- * @param {object} indices Shape indices
120
- * @param {CanvasPainter} painter Canvas painter
121
- * @param {boolean} isSub Whether to use subchart scales
122
- * @private
123
- */
114
+ * Draw a canvas area target directly from common geometry.
115
+ * @param {object} $$ ChartInternal instance
116
+ * @param {object} target Data target
117
+ * @param {object} indices Shape indices
118
+ * @param {CanvasPainter} painter Canvas painter
119
+ * @param {boolean} isSub Whether to use subchart scales
120
+ * @private
121
+ */
124
122
  function drawCanvasArea($$, target, indices, painter, isSub = false) {
125
- painter.fillPath(ctx => {
126
- generateDrawAreaPath($$, indices, isSub, ctx)(target);
127
- });
123
+ painter.fillPath((ctx) => {
124
+ generateDrawAreaPath($$, indices, isSub, ctx)(target);
125
+ });
128
126
  }
129
127
  /**
130
- * Get a target copy clipped to the current visible x range.
131
- * @param {object} $$ ChartInternal instance
132
- * @param {object} target Data target
133
- * @returns {object} Original target or a visible values copy
134
- * @private
135
- */
128
+ * Get a target copy clipped to the current visible x range.
129
+ * @param {object} $$ ChartInternal instance
130
+ * @param {object} target Data target
131
+ * @returns {object} Original target or a visible values copy
132
+ * @private
133
+ */
136
134
  function getVisibleCanvasTarget($$, target) {
137
- const range = getCanvasTargetVisibleRange($$, target);
138
- return range.start === 0 && range.end === target.values.length ? target : {
139
- ...target,
140
- values: target.values.slice(range.start, range.end)
141
- };
135
+ const range = getCanvasTargetVisibleRange($$, target);
136
+ return range.start === 0 && range.end === target.values.length ? target : {
137
+ ...target,
138
+ values: target.values.slice(range.start, range.end)
139
+ };
142
140
  }
143
141
  /**
144
- * Get focused row for a datum or target.
145
- * @param {Array} focusData Focused data rows
146
- * @param {object} d Rendered data row or target
147
- * @returns {object|null}
148
- * @private
149
- */
142
+ * Get focused row for a datum or target.
143
+ * @param {Array} focusData Focused data rows
144
+ * @param {object} d Rendered data row or target
145
+ * @returns {object|null}
146
+ * @private
147
+ */
150
148
  function getFocusedCanvasDatum(focusData, d) {
151
- if (!focusData?.length || !d?.id) {
152
- return null;
153
- }
154
- let lookup = canvasFocusLookupCache.get(focusData);
155
- if (!lookup) {
156
- lookup = new Map();
157
- for (const focus of focusData) {
158
- if (!focus?.id) {
159
- continue;
160
- }
161
- const targetKey = `${focus.id}:*`;
162
- if (!lookup.has(targetKey)) {
163
- lookup.set(targetKey, focus);
164
- }
165
- if ("index" in focus) {
166
- lookup.set(`${focus.id}:${focus.index}`, focus);
167
- }
168
- }
169
- canvasFocusLookupCache.set(focusData, lookup);
170
- }
171
- return "index" in d ?
172
- lookup.get(`${d.id}:${d.index}`) || null :
173
- lookup.get(`${d.id}:*`) || null;
149
+ if (!focusData?.length || !d?.id) return null;
150
+ let lookup = canvasFocusLookupCache.get(focusData);
151
+ if (!lookup) {
152
+ lookup = /* @__PURE__ */ new Map();
153
+ for (const focus of focusData) {
154
+ if (!focus?.id) continue;
155
+ const targetKey = `${focus.id}:*`;
156
+ if (!lookup.has(targetKey)) lookup.set(targetKey, focus);
157
+ if ("index" in focus) lookup.set(`${focus.id}:${focus.index}`, focus);
158
+ }
159
+ canvasFocusLookupCache.set(focusData, lookup);
160
+ }
161
+ return "index" in d ? lookup.get(`${d.id}:${d.index}`) || null : lookup.get(`${d.id}:*`) || null;
174
162
  }
175
163
  /**
176
- * Get cached Path2D for a subchart brush handle.
177
- * @param {string} axis Brush axis
178
- * @param {string} type Brush handle side
179
- * @returns {Path2D|null}
180
- * @private
181
- */
164
+ * Get cached Path2D for a subchart brush handle.
165
+ * @param {string} axis Brush axis
166
+ * @param {string} type Brush handle side
167
+ * @returns {Path2D|null}
168
+ * @private
169
+ */
182
170
  function getSubchartBrushHandlePath(axis, type) {
183
- const Path2DCtor = win.Path2D;
184
- if (!Path2DCtor) {
185
- return null;
186
- }
187
- const key = `${axis}:${type}`;
188
- const cached = subchartBrushHandlePathCache.get(key);
189
- if (cached) {
190
- return cached;
191
- }
192
- const path = new Path2DCtor(SUBCHART_BRUSH_HANDLE_PATH[axis][type]);
193
- subchartBrushHandlePathCache.set(key, path);
194
- return path;
171
+ const Path2DCtor = win.Path2D;
172
+ if (!Path2DCtor) return null;
173
+ const key = `${axis}:${type}`;
174
+ const cached = subchartBrushHandlePathCache.get(key);
175
+ if (cached) return cached;
176
+ const path = new Path2DCtor(SUBCHART_BRUSH_HANDLE_PATH[axis][type]);
177
+ subchartBrushHandlePathCache.set(key, path);
178
+ return path;
195
179
  }
196
180
  /**
197
- * Resolve color.onover for a focused data row.
198
- * @param {object} $$ ChartInternal instance
199
- * @param {object} d Focused data row
200
- * @returns {string|null}
201
- * @private
202
- */
181
+ * Resolve color.onover for a focused data row.
182
+ * @param {object} $$ ChartInternal instance
183
+ * @param {object} d Focused data row
184
+ * @returns {string|null}
185
+ * @private
186
+ */
203
187
  function getCanvasOverColor($$, d) {
204
- const onover = $$.config.color_onover;
205
- if (!onover || !d) {
206
- return null;
207
- }
208
- if (isObject(onover)) {
209
- return d.id in onover ? onover[d.id] : null;
210
- }
211
- else if (isString(onover)) {
212
- return onover;
213
- }
214
- else if (isFunction(onover)) {
215
- return onover.call($$.api, d);
216
- }
217
- return null;
188
+ const onover = $$.config.color_onover;
189
+ if (!onover || !d) return null;
190
+ if (isObject(onover)) return d.id in onover ? onover[d.id] : null;
191
+ else if (isString(onover)) return onover;
192
+ else if (isFunction(onover)) return onover.call($$.api, d);
193
+ return null;
218
194
  }
219
195
  /**
220
- * Resolve a target/datum color, including color.onover focus color.
221
- * @param {object} $$ ChartInternal instance
222
- * @param {object} d Data target or row
223
- * @param {Array} focusData Focused data rows
224
- * @returns {string}
225
- * @private
226
- */
196
+ * Resolve a target/datum color, including color.onover focus color.
197
+ * @param {object} $$ ChartInternal instance
198
+ * @param {object} d Data target or row
199
+ * @param {Array} focusData Focused data rows
200
+ * @returns {string}
201
+ * @private
202
+ */
227
203
  function getCanvasRenderColor($$, d, focusData) {
228
- const focus = getFocusedCanvasDatum(focusData, d);
229
- const overColor = getCanvasOverColor($$, focus);
230
- return overColor || $$.color(d.id);
204
+ return getCanvasOverColor($$, getFocusedCanvasDatum(focusData, d)) || $$.color(d.id);
231
205
  }
232
206
  /**
233
- * Get target id from a data target or row.
234
- * @param {object} d Data target or row
235
- * @returns {string|undefined} Target id
236
- * @private
237
- */
207
+ * Get target id from a data target or row.
208
+ * @param {object} d Data target or row
209
+ * @returns {string|undefined} Target id
210
+ * @private
211
+ */
238
212
  function getCanvasTargetId(d) {
239
- return d?.id || d?.data?.id;
213
+ return d?.id || d?.data?.id;
240
214
  }
241
215
  /**
242
- * Check whether the canvas target is focused.
243
- * @param {object} $$ ChartInternal instance
244
- * @param {object} d Data target or row
245
- * @returns {boolean} Whether target is focused
246
- * @private
247
- */
216
+ * Check whether the canvas target is focused.
217
+ * @param {object} $$ ChartInternal instance
218
+ * @param {object} d Data target or row
219
+ * @returns {boolean} Whether target is focused
220
+ * @private
221
+ */
248
222
  function isCanvasTargetFocused($$, d) {
249
- const id = getCanvasTargetId(d);
250
- return !!id && $$.state.focusedTargetIds?.has(id);
223
+ const id = getCanvasTargetId(d);
224
+ return !!id && $$.state.focusedTargetIds?.has(id);
251
225
  }
252
226
  /**
253
- * Get SVG-compatible target focus opacity.
254
- * @param {object} $$ ChartInternal instance
255
- * @param {object} d Data target or row
256
- * @returns {number} Target opacity
257
- * @private
258
- */
227
+ * Get SVG-compatible target focus opacity.
228
+ * @param {object} $$ ChartInternal instance
229
+ * @param {object} d Data target or row
230
+ * @returns {number} Target opacity
231
+ * @private
232
+ */
259
233
  function getCanvasTargetFocusOpacity($$, d) {
260
- const id = getCanvasTargetId(d);
261
- return id && $$.state.defocusedTargetIds?.has(id) ?
262
- $$.canvasTheme.style.shape.targetDefocusedOpacity :
263
- 1;
234
+ const id = getCanvasTargetId(d);
235
+ return id && $$.state.defocusedTargetIds?.has(id) ? $$.canvasTheme.style.shape.targetDefocusedOpacity : 1;
264
236
  }
265
237
  /**
266
- * Get normalized bounding box for area gradient coordinates.
267
- * @param {object} $$ ChartInternal instance
268
- * @param {object} target Data target
269
- * @param {object} indices Shape indices
270
- * @param {boolean} isSub Whether to use subchart scales
271
- * @returns {object|null}
272
- * @private
273
- */
238
+ * Get normalized bounding box for area gradient coordinates.
239
+ * @param {object} $$ ChartInternal instance
240
+ * @param {object} target Data target
241
+ * @param {object} indices Shape indices
242
+ * @param {boolean} isSub Whether to use subchart scales
243
+ * @returns {object|null}
244
+ * @private
245
+ */
274
246
  function getCanvasAreaBounds($$, target, indices, isSub = false) {
275
- const getPoints = $$.generateGetAreaPoints?.(indices, isSub);
276
- if (!getPoints) {
277
- return null;
278
- }
279
- const range = getCanvasTargetVisibleRange($$, target);
280
- let minX = Infinity;
281
- let minY = Infinity;
282
- let maxX = -Infinity;
283
- let maxY = -Infinity;
284
- for (let i = range.start; i < range.end; i++) {
285
- const d = target.values[i];
286
- if (!hasCanvasDrawableValue($$, d)) {
287
- continue;
288
- }
289
- getPoints(d, i).forEach(([x, y]) => {
290
- if (!isFiniteCanvasCoordinate(x, y)) {
291
- return;
292
- }
293
- minX = Math.min(minX, x);
294
- minY = Math.min(minY, y);
295
- maxX = Math.max(maxX, x);
296
- maxY = Math.max(maxY, y);
297
- });
298
- }
299
- return Number.isFinite(minX) && Number.isFinite(minY) ?
300
- { x: minX, y: minY, w: maxX - minX, h: maxY - minY } :
301
- null;
247
+ const getPoints = $$.generateGetAreaPoints?.(indices, isSub);
248
+ if (!getPoints) return null;
249
+ const range = getCanvasTargetVisibleRange($$, target);
250
+ let minX = Infinity;
251
+ let minY = Infinity;
252
+ let maxX = -Infinity;
253
+ let maxY = -Infinity;
254
+ for (let i = range.start; i < range.end; i++) {
255
+ const d = target.values[i];
256
+ if (!hasCanvasDrawableValue($$, d)) continue;
257
+ getPoints(d, i).forEach(([x, y]) => {
258
+ if (!isFiniteCanvasCoordinate(x, y)) return;
259
+ minX = Math.min(minX, x);
260
+ minY = Math.min(minY, y);
261
+ maxX = Math.max(maxX, x);
262
+ maxY = Math.max(maxY, y);
263
+ });
264
+ }
265
+ return Number.isFinite(minX) && Number.isFinite(minY) ? {
266
+ x: minX,
267
+ y: minY,
268
+ w: maxX - minX,
269
+ h: maxY - minY
270
+ } : null;
302
271
  }
303
272
  /**
304
- * Resolve objectBoundingBox gradient coordinate.
305
- * @param {number} value Normalized coordinate
306
- * @param {number} origin Bounding box origin
307
- * @param {number} size Bounding box size
308
- * @returns {number}
309
- * @private
310
- */
273
+ * Resolve objectBoundingBox gradient coordinate.
274
+ * @param {number} value Normalized coordinate
275
+ * @param {number} origin Bounding box origin
276
+ * @param {number} size Bounding box size
277
+ * @returns {number}
278
+ * @private
279
+ */
311
280
  function getLinearGradientCoord(value, origin, size) {
312
- return origin + size * (isNumber(value) ? value : 0);
281
+ return origin + size * (isNumber(value) ? value : 0);
313
282
  }
314
283
  /**
315
- * Create a canvas linear gradient from area/bar linearGradient options.
316
- * @param {object} $$ ChartInternal instance
317
- * @param {CanvasRenderingContext2D} ctx Canvas context
318
- * @param {object} target Data target
319
- * @param {string} shape Shape option prefix
320
- * @param {object} rect Shape bounding box
321
- * @param {string} baseColor Fallback target color
322
- * @returns {CanvasGradient|string}
323
- * @private
324
- */
284
+ * Create a canvas linear gradient from area/bar linearGradient options.
285
+ * @param {object} $$ ChartInternal instance
286
+ * @param {CanvasRenderingContext2D} ctx Canvas context
287
+ * @param {object} target Data target
288
+ * @param {string} shape Shape option prefix
289
+ * @param {object} rect Shape bounding box
290
+ * @param {string} baseColor Fallback target color
291
+ * @returns {CanvasGradient|string}
292
+ * @private
293
+ */
325
294
  function getCanvasLinearGradientFill($$, ctx, target, shape, rect, baseColor) {
326
- const option = $$.config[`${shape}_linearGradient`];
327
- if (!option || !rect || !rect.w || !rect.h) {
328
- return baseColor;
329
- }
330
- const isRotated = $$.config.axis_rotated;
331
- const gradientOption = typeof option === "object" ? option : {};
332
- const x = gradientOption.x || (isRotated ? [1, 0] : [0, 0]);
333
- const y = gradientOption.y || (isRotated ? [0, 0] : [0, 1]);
334
- const stops = Array.isArray(gradientOption.stops) ?
335
- gradientOption.stops :
336
- [[0, baseColor, 1], [1, baseColor, 0]];
337
- const gradient = ctx.createLinearGradient(getLinearGradientCoord(x[0], rect.x, rect.w), getLinearGradientCoord(y[0], rect.y, rect.h), getLinearGradientCoord(x[1], rect.x, rect.w), getLinearGradientCoord(y[1], rect.y, rect.h));
338
- stops.forEach(([offset, stopColor, stopOpacity]) => {
339
- const colorValue = isFunction(stopColor) ? stopColor.call($$.api, target.id) : stopColor;
340
- const color = String(colorValue || baseColor);
341
- const numericOffset = Number(offset);
342
- const parsedOffset = Number.isFinite(numericOffset) ?
343
- Math.max(0, Math.min(1, numericOffset)) :
344
- 0;
345
- gradient.addColorStop(parsedOffset, withOpacity(color, stopOpacity));
346
- });
347
- return gradient;
295
+ const option = $$.config[`${shape}_linearGradient`];
296
+ if (!option || !rect || !rect.w || !rect.h) return baseColor;
297
+ const isRotated = $$.config.axis_rotated;
298
+ const gradientOption = typeof option === "object" ? option : {};
299
+ const x = gradientOption.x || (isRotated ? [1, 0] : [0, 0]);
300
+ const y = gradientOption.y || (isRotated ? [0, 0] : [0, 1]);
301
+ const stops = Array.isArray(gradientOption.stops) ? gradientOption.stops : [[
302
+ 0,
303
+ baseColor,
304
+ 1
305
+ ], [
306
+ 1,
307
+ baseColor,
308
+ 0
309
+ ]];
310
+ const gradient = ctx.createLinearGradient(getLinearGradientCoord(x[0], rect.x, rect.w), getLinearGradientCoord(y[0], rect.y, rect.h), getLinearGradientCoord(x[1], rect.x, rect.w), getLinearGradientCoord(y[1], rect.y, rect.h));
311
+ stops.forEach(([offset, stopColor, stopOpacity]) => {
312
+ const colorValue = isFunction(stopColor) ? stopColor.call($$.api, target.id) : stopColor;
313
+ const color = String(colorValue || baseColor);
314
+ const numericOffset = Number(offset);
315
+ gradient.addColorStop(Number.isFinite(numericOffset) ? Math.max(0, Math.min(1, numericOffset)) : 0, withOpacity(color, stopOpacity));
316
+ });
317
+ return gradient;
348
318
  }
349
319
  /**
350
- * Check if a target is supported by canvas v1.
351
- * @param {object} $$ ChartInternal instance
352
- * @param {object} target Data target
353
- * @returns {boolean} Whether the target is supported
354
- * @private
355
- */
320
+ * Check if a target is supported by canvas v1.
321
+ * @param {object} $$ ChartInternal instance
322
+ * @param {object} target Data target
323
+ * @returns {boolean} Whether the target is supported
324
+ * @private
325
+ */
356
326
  function isCanvasRenderableTarget($$, target) {
357
- return isCanvasTargetSupported($$, target, RENDERER_GROUPED_TYPE_FILTERS);
327
+ return isCanvasTargetSupported($$, target, RENDERER_GROUPED_TYPE_FILTERS);
358
328
  }
359
329
  /**
360
- * Get canvas point opacity following SVG circle defaults.
361
- * @param {object} $$ ChartInternal instance
362
- * @param {object} target Data target
363
- * @returns {number} Point opacity
364
- * @private
365
- */
330
+ * Get canvas point opacity following SVG circle defaults.
331
+ * @param {object} $$ ChartInternal instance
332
+ * @param {object} target Data target
333
+ * @returns {number} Point opacity
334
+ * @private
335
+ */
366
336
  function getPointOpacity($$, target) {
367
- const opacity = $$.config.point_opacity;
368
- return Number.isFinite(opacity) ? opacity : (isCanvasScatterType($$, target) || isCanvasBubbleType($$, target) ? 0.5 : 1);
337
+ const opacity = $$.config.point_opacity;
338
+ return Number.isFinite(opacity) ? opacity : isCanvasScatterType($$, target) || isCanvasBubbleType($$, target) ? .5 : 1;
369
339
  }
370
340
  /**
371
- * Get a target point radius without calling pointR for static non-bubble points.
372
- * @param {object} $$ ChartInternal instance
373
- * @param {object} target Data target
374
- * @param {object} d Data row
375
- * @returns {number} Point radius
376
- * @private
377
- */
341
+ * Get a target point radius without calling pointR for static non-bubble points.
342
+ * @param {object} $$ ChartInternal instance
343
+ * @param {object} target Data target
344
+ * @param {object} d Data row
345
+ * @returns {number} Point radius
346
+ * @private
347
+ */
378
348
  function getTargetPointRadius($$, target, d) {
379
- const pointR = $$.config.point_r;
380
- return isCanvasBubbleType($$, target) || isFunction(pointR) ? ($$.pointR?.(d) ?? 2.5) : pointR;
349
+ const pointR = $$.config.point_r;
350
+ return isCanvasBubbleType($$, target) || isFunction(pointR) ? $$.pointR?.(d) ?? 2.5 : pointR;
381
351
  }
382
352
  /**
383
- * Check whether dense scatter point drawing can skip duplicate pixel centers.
384
- * @param {object} $$ ChartInternal instance
385
- * @param {object} target Data target
386
- * @param {string} pointType Canvas point type
387
- * @param {boolean} hasGradient Whether radial gradient point fill is enabled
388
- * @returns {boolean} Whether pixel-center culling can be used
389
- * @private
390
- */
353
+ * Check whether dense scatter point drawing can skip duplicate pixel centers.
354
+ * @param {object} $$ ChartInternal instance
355
+ * @param {object} target Data target
356
+ * @param {string} pointType Canvas point type
357
+ * @param {boolean} hasGradient Whether radial gradient point fill is enabled
358
+ * @returns {boolean} Whether pixel-center culling can be used
359
+ * @private
360
+ */
391
361
  function shouldCullDenseScatterPoints($$, target, pointType, hasGradient) {
392
- return !hasGradient &&
393
- pointType === "circle" &&
394
- isCanvasScatterType($$, target) &&
395
- !isFunction($$.config.point_r) &&
396
- target.values.length > DENSE_SCATTER_POINT_CULL_THRESHOLD;
362
+ return !hasGradient && pointType === "circle" && isCanvasScatterType($$, target) && !isFunction($$.config.point_r) && target.values.length > 1e5;
397
363
  }
398
364
  /**
399
- * Get configured bar connect line type for a target.
400
- * @param {object} $$ ChartInternal instance
401
- * @param {string} id Data id
402
- * @returns {string|null} Connect line type
403
- * @private
404
- */
365
+ * Get configured bar connect line type for a target.
366
+ * @param {object} $$ ChartInternal instance
367
+ * @param {string} id Data id
368
+ * @returns {string|null} Connect line type
369
+ * @private
370
+ */
405
371
  function getBarConnectLineType($$, id) {
406
- const { bar_connectLine: connectLine } = $$.config;
407
- const type = isObject(connectLine) ? connectLine[id] : connectLine;
408
- return /^(start|end)-(start|end)$/.test(type) ? type : null;
372
+ const { bar_connectLine: connectLine } = $$.config;
373
+ const type = isObject(connectLine) ? connectLine[id] : connectLine;
374
+ return /^(start|end)-(start|end)$/.test(type) ? type : null;
409
375
  }
410
376
  /**
411
- * Get SVG-compatible bar connect line geometry.
412
- * @param {object} $$ ChartInternal instance
413
- * @param {Array} points Shared bar points
414
- * @param {object} radiusInfo Bar radius geometry info
415
- * @returns {object} Connect line geometry
416
- * @private
417
- */
377
+ * Get SVG-compatible bar connect line geometry.
378
+ * @param {object} $$ ChartInternal instance
379
+ * @param {Array} points Shared bar points
380
+ * @param {object} radiusInfo Bar radius geometry info
381
+ * @returns {object} Connect line geometry
382
+ * @private
383
+ */
418
384
  function getBarConnectLineBox($$, points, radiusInfo) {
419
- const { indexX, indexY, pos } = radiusInfo;
420
- return $$.config.axis_rotated ?
421
- {
422
- x: points[0][indexX],
423
- y: points[0][indexY],
424
- width: points[0][indexX] - pos,
425
- height: points[2][indexY] - points[0][indexY]
426
- } :
427
- {
428
- x: points[0][indexX],
429
- y: pos,
430
- width: points[2][indexX] - points[0][indexX],
431
- height: points[3][indexY] - pos
432
- };
385
+ const { indexX, indexY, pos } = radiusInfo;
386
+ return $$.config.axis_rotated ? {
387
+ x: points[0][indexX],
388
+ y: points[0][indexY],
389
+ width: points[0][indexX] - pos,
390
+ height: points[2][indexY] - points[0][indexY]
391
+ } : {
392
+ x: points[0][indexX],
393
+ y: pos,
394
+ width: points[2][indexX] - points[0][indexX],
395
+ height: points[3][indexY] - pos
396
+ };
433
397
  }
434
398
  /**
435
- * Draw SVG-compatible bar connect lines.
436
- * @param {object} $$ ChartInternal instance
437
- * @param {object} painter Canvas painter
438
- * @param {string} type Connect line type
439
- * @param {Array} boxes Bar connect line geometry list
440
- * @param {number} alpha Line opacity
441
- * @private
442
- */
399
+ * Draw SVG-compatible bar connect lines.
400
+ * @param {object} $$ ChartInternal instance
401
+ * @param {object} painter Canvas painter
402
+ * @param {string} type Connect line type
403
+ * @param {Array} boxes Bar connect line geometry list
404
+ * @param {number} alpha Line opacity
405
+ * @private
406
+ */
443
407
  function drawBarConnectLine($$, painter, type, boxes, alpha = 1) {
444
- if (boxes.length < 2) {
445
- return;
446
- }
447
- const isRotated = $$.config.axis_rotated;
448
- const isStart = /^start-(start|end)$/.test(type);
449
- const isEnd = /^end-(start|end)$/.test(type);
450
- const isToEnd = /\w+-end$/.test(type);
451
- const isToStart = /\w+-start$/.test(type);
452
- const getMovePoint = (box) => ({
453
- x: isRotated ? (isEnd ? box.x - box.width : box.x) : (box.x + box.width),
454
- y: isRotated ? box.y + box.height : (isStart ? box.y + box.height : box.y)
455
- });
456
- const getLinePoint = (box) => ({
457
- x: isRotated ? box.x - (isToEnd ? box.width : 0) : box.x,
458
- y: isRotated ? box.y : box.y + (isToStart ? box.height : 0)
459
- });
460
- let movePoint = getMovePoint(boxes[0]);
461
- painter.strokePath(() => {
462
- for (let i = 1; i < boxes.length; i++) {
463
- const linePoint = getLinePoint(boxes[i]);
464
- painter.traceLine(movePoint.x, movePoint.y, linePoint.x, linePoint.y);
465
- if (i < boxes.length - 1) {
466
- movePoint = getMovePoint(boxes[i]);
467
- }
468
- }
469
- }, {
470
- alpha,
471
- stroke: $$.canvasTheme.style.shape.barConnectLineColor,
472
- lineWidth: $$.canvasTheme.style.shape.barConnectLineWidth,
473
- lineDash: []
474
- });
408
+ if (boxes.length < 2) return;
409
+ const isRotated = $$.config.axis_rotated;
410
+ const isStart = /^start-(start|end)$/.test(type);
411
+ const isEnd = /^end-(start|end)$/.test(type);
412
+ const isToEnd = /\w+-end$/.test(type);
413
+ const isToStart = /\w+-start$/.test(type);
414
+ const getMovePoint = (box) => ({
415
+ x: isRotated ? isEnd ? box.x - box.width : box.x : box.x + box.width,
416
+ y: isRotated ? box.y + box.height : isStart ? box.y + box.height : box.y
417
+ });
418
+ const getLinePoint = (box) => ({
419
+ x: isRotated ? box.x - (isToEnd ? box.width : 0) : box.x,
420
+ y: isRotated ? box.y : box.y + (isToStart ? box.height : 0)
421
+ });
422
+ let movePoint = getMovePoint(boxes[0]);
423
+ painter.strokePath(() => {
424
+ for (let i = 1; i < boxes.length; i++) {
425
+ const linePoint = getLinePoint(boxes[i]);
426
+ painter.traceLine(movePoint.x, movePoint.y, linePoint.x, linePoint.y);
427
+ if (i < boxes.length - 1) movePoint = getMovePoint(boxes[i]);
428
+ }
429
+ }, {
430
+ alpha,
431
+ stroke: $$.canvasTheme.style.shape.barConnectLineColor,
432
+ lineWidth: $$.canvasTheme.style.shape.barConnectLineWidth,
433
+ lineDash: []
434
+ });
475
435
  }
476
436
  /**
477
- * Get normalized canvas point shape for the target.
478
- * @param {object} $$ ChartInternal instance
479
- * @param {object} target Data target
480
- * @returns {string} Canvas point shape
481
- * @private
482
- */
437
+ * Get normalized canvas point shape for the target.
438
+ * @param {object} $$ ChartInternal instance
439
+ * @param {object} target Data target
440
+ * @returns {string} Canvas point shape
441
+ * @private
442
+ */
483
443
  function getPointType($$, target) {
484
- const { config } = $$;
485
- if (isCanvasBubbleType($$, target)) {
486
- return "circle";
487
- }
488
- const targetIds = $$.mapToIds?.($$.data.targets) || [];
489
- const pattern = Array.isArray(config.point_pattern) && config.point_pattern.length ?
490
- config.point_pattern :
491
- [config.point_type];
492
- const index = Math.max(0, targetIds.indexOf(target.id));
493
- const type = pattern[index % pattern.length];
494
- return /^rect(angle)?$/i.test(type) || type === "rectangle" ? "rectangle" : (type || "circle");
444
+ const { config } = $$;
445
+ if (isCanvasBubbleType($$, target)) return "circle";
446
+ const targetIds = $$.mapToIds?.($$.data.targets) || [];
447
+ const pattern = Array.isArray(config.point_pattern) && config.point_pattern.length ? config.point_pattern : [config.point_type];
448
+ const type = pattern[Math.max(0, targetIds.indexOf(target.id)) % pattern.length];
449
+ return /^rect(angle)?$/i.test(type) || type === "rectangle" ? "rectangle" : type || "circle";
495
450
  }
496
451
  /**
497
- * Get point fill style, including canvas radial gradient when configured.
498
- * @param {object} $$ ChartInternal instance
499
- * @param {CanvasRenderingContext2D} ctx Canvas context
500
- * @param {object} d Data row
501
- * @param {number} x X coordinate
502
- * @param {number} y Y coordinate
503
- * @param {number} r Point radius
504
- * @param {string} fallback Fallback color
505
- * @returns {string|CanvasGradient} Fill style
506
- * @private
507
- */
452
+ * Get point fill style, including canvas radial gradient when configured.
453
+ * @param {object} $$ ChartInternal instance
454
+ * @param {CanvasRenderingContext2D} ctx Canvas context
455
+ * @param {object} d Data row
456
+ * @param {number} x X coordinate
457
+ * @param {number} y Y coordinate
458
+ * @param {number} r Point radius
459
+ * @param {string} fallback Fallback color
460
+ * @returns {string|CanvasGradient} Fill style
461
+ * @private
462
+ */
508
463
  function getPointFillStyle($$, ctx, d, x, y, r, fallback) {
509
- const option = $$.config.point_radialGradient;
510
- if (!option || !r) {
511
- return fallback;
512
- }
513
- const gradientOption = isObject(option) ? option : {};
514
- const cx = isNumber(gradientOption.cx) ? gradientOption.cx : 0.3;
515
- const cy = isNumber(gradientOption.cy) ? gradientOption.cy : 0.3;
516
- const radius = isNumber(gradientOption.r) ? gradientOption.r : 0.7;
517
- const stops = Array.isArray(gradientOption.stops) ?
518
- gradientOption.stops :
519
- [[0.1, null, 1], [0.9, null, 0]];
520
- const gradientX = x + (cx - 0.5) * r * 2;
521
- const gradientY = y + (cy - 0.5) * r * 2;
522
- const gradient = ctx.createRadialGradient(gradientX, gradientY, 0, gradientX, gradientY, Math.max(1, r * radius * 2));
523
- stops.forEach(([offset, stopColor, stopOpacity]) => {
524
- let color = isFunction(stopColor) ? stopColor.bind($$.api)(d.id) : stopColor;
525
- if (!color) {
526
- color = fallback;
527
- }
528
- // addColorStop throws IndexSizeError for out-of-range offsets
529
- const numericOffset = Number(offset);
530
- const parsedOffset = Number.isFinite(numericOffset) ?
531
- Math.max(0, Math.min(1, numericOffset)) :
532
- 0;
533
- gradient.addColorStop(parsedOffset, withOpacity(color, stopOpacity));
534
- });
535
- return gradient;
464
+ const option = $$.config.point_radialGradient;
465
+ if (!option || !r) return fallback;
466
+ const gradientOption = isObject(option) ? option : {};
467
+ const cx = isNumber(gradientOption.cx) ? gradientOption.cx : .3;
468
+ const cy = isNumber(gradientOption.cy) ? gradientOption.cy : .3;
469
+ const radius = isNumber(gradientOption.r) ? gradientOption.r : .7;
470
+ const stops = Array.isArray(gradientOption.stops) ? gradientOption.stops : [[
471
+ .1,
472
+ null,
473
+ 1
474
+ ], [
475
+ .9,
476
+ null,
477
+ 0
478
+ ]];
479
+ const gradientX = x + (cx - .5) * r * 2;
480
+ const gradientY = y + (cy - .5) * r * 2;
481
+ const gradient = ctx.createRadialGradient(gradientX, gradientY, 0, gradientX, gradientY, Math.max(1, r * radius * 2));
482
+ stops.forEach(([offset, stopColor, stopOpacity]) => {
483
+ let color = isFunction(stopColor) ? stopColor.bind($$.api)(d.id) : stopColor;
484
+ if (!color) color = fallback;
485
+ const numericOffset = Number(offset);
486
+ gradient.addColorStop(Number.isFinite(numericOffset) ? Math.max(0, Math.min(1, numericOffset)) : 0, withOpacity(color, stopOpacity));
487
+ });
488
+ return gradient;
536
489
  }
537
490
  /**
538
- * Get candlestick fill color for the data row.
539
- * @param {object} $$ ChartInternal instance
540
- * @param {object} d Data row
541
- * @param {object} value Parsed candlestick value
542
- * @returns {string} Fill color
543
- * @private
544
- */
491
+ * Get candlestick fill color for the data row.
492
+ * @param {object} $$ ChartInternal instance
493
+ * @param {object} d Data row
494
+ * @param {object} value Parsed candlestick value
495
+ * @returns {string} Fill color
496
+ * @private
497
+ */
545
498
  function getCandlestickColor($$, d, value) {
546
- const downColor = $$.config.candlestick_color_down;
547
- const color = value?._isUp ? $$.color(d) : (downColor && typeof downColor === "object" ? downColor[d.id] : downColor);
548
- return color || $$.color(d);
499
+ const downColor = $$.config.candlestick_color_down;
500
+ return (value?._isUp ? $$.color(d) : downColor && typeof downColor === "object" ? downColor[d.id] : downColor) || $$.color(d);
549
501
  }
550
502
  /**
551
- * Check if points should be drawn for a line target.
552
- * @param {object} $$ ChartInternal instance
553
- * @param {object} target Data target
554
- * @returns {boolean} Whether points should be drawn
555
- * @private
556
- */
503
+ * Check if points should be drawn for a line target.
504
+ * @param {object} $$ ChartInternal instance
505
+ * @param {object} target Data target
506
+ * @returns {boolean} Whether points should be drawn
507
+ * @private
508
+ */
557
509
  function shouldDrawPoints($$, target) {
558
- return $$.shouldDrawPointsForLine ? $$.shouldDrawPointsForLine(target) : true;
510
+ return $$.shouldDrawPointsForLine ? $$.shouldDrawPointsForLine(target) : true;
559
511
  }
560
512
  /**
561
- * Check whether two canvas dash arrays are equal.
562
- * @param {Array} a First dash array
563
- * @param {Array} b Second dash array
564
- * @returns {boolean} Whether arrays have the same values
565
- * @private
566
- */
513
+ * Check whether two canvas dash arrays are equal.
514
+ * @param {Array} a First dash array
515
+ * @param {Array} b Second dash array
516
+ * @returns {boolean} Whether arrays have the same values
517
+ * @private
518
+ */
567
519
  function isSameDash(a, b) {
568
- return a.length === b.length && a.every((value, index) => value === b[index]);
520
+ return a.length === b.length && a.every((value, index) => value === b[index]);
569
521
  }
570
522
  /**
571
- * Draw a line target with per-data dashed regions.
572
- * @param {object} $$ ChartInternal instance
573
- * @param {object} target Data target
574
- * @param {object} painter Canvas painter
575
- * @private
576
- */
523
+ * Draw a line target with per-data dashed regions.
524
+ * @param {object} $$ ChartInternal instance
525
+ * @param {object} target Data target
526
+ * @param {object} painter Canvas painter
527
+ * @private
528
+ */
577
529
  function drawCanvasLineWithDataRegions($$, target, painter) {
578
- const { config, scale } = $$;
579
- const x = scale.zoom || scale.x;
580
- const y = $$.getYScaleById(target.id);
581
- const rawValues = config.line_connectNull ? $$.filterRemoveNull(target.values) : target.values;
582
- let values = $$.isAreaRangeType(target) ?
583
- rawValues.map(d => ({ ...d, value: $$.getRangedData(d, "mid") })) :
584
- rawValues;
585
- if ($$.isStepType(target)) {
586
- values = $$.convertValuesToStep(values);
587
- }
588
- const segments = getLineRegionSegments($$, values, x, y, config.data_regions[target.id]);
589
- let currentDash = null;
590
- let currentPoints = [];
591
- const flush = () => {
592
- if (currentPoints.length < 2 || !currentDash) {
593
- currentPoints = [];
594
- return;
595
- }
596
- painter.strokePath(ctx => {
597
- const [start, ...rest] = currentPoints;
598
- ctx.moveTo(start[0], start[1]);
599
- rest.forEach(point => {
600
- ctx.lineTo(point[0], point[1]);
601
- });
602
- }, { lineDash: currentDash });
603
- currentPoints = [];
604
- };
605
- const appendSegment = (start, end, dash) => {
606
- if (!currentDash || !isSameDash(currentDash, dash)) {
607
- flush();
608
- currentDash = dash;
609
- currentPoints = [start, end];
610
- }
611
- else {
612
- currentPoints.push(end);
613
- }
614
- };
615
- for (const { start, end, dash, isBreak } of segments) {
616
- if (isBreak || !start || !end || !dash) {
617
- flush();
618
- currentDash = null;
619
- continue;
620
- }
621
- if (!isFiniteCanvasCoordinate(start[0], start[1]) ||
622
- !isFiniteCanvasCoordinate(end[0], end[1])) {
623
- flush();
624
- currentDash = null;
625
- continue;
626
- }
627
- appendSegment(start, end, dash);
628
- }
629
- flush();
530
+ const { config, scale } = $$;
531
+ const x = scale.zoom || scale.x;
532
+ const y = $$.getYScaleById(target.id);
533
+ const rawValues = config.line_connectNull ? $$.filterRemoveNull(target.values) : target.values;
534
+ let values = $$.isAreaRangeType(target) ? rawValues.map((d) => ({
535
+ ...d,
536
+ value: $$.getRangedData(d, "mid")
537
+ })) : rawValues;
538
+ if ($$.isStepType(target)) values = $$.convertValuesToStep(values);
539
+ const segments = getLineRegionSegments($$, values, x, y, config.data_regions[target.id]);
540
+ let currentDash = null;
541
+ let currentPoints = [];
542
+ const flush = () => {
543
+ if (currentPoints.length < 2 || !currentDash) {
544
+ currentPoints = [];
545
+ return;
546
+ }
547
+ painter.strokePath((ctx) => {
548
+ const [start, ...rest] = currentPoints;
549
+ ctx.moveTo(start[0], start[1]);
550
+ rest.forEach((point) => {
551
+ ctx.lineTo(point[0], point[1]);
552
+ });
553
+ }, { lineDash: currentDash });
554
+ currentPoints = [];
555
+ };
556
+ const appendSegment = (start, end, dash) => {
557
+ if (!currentDash || !isSameDash(currentDash, dash)) {
558
+ flush();
559
+ currentDash = dash;
560
+ currentPoints = [start, end];
561
+ } else currentPoints.push(end);
562
+ };
563
+ for (const { start, end, dash, isBreak } of segments) {
564
+ if (isBreak || !start || !end || !dash) {
565
+ flush();
566
+ currentDash = null;
567
+ continue;
568
+ }
569
+ if (!isFiniteCanvasCoordinate(start[0], start[1]) || !isFiniteCanvasCoordinate(end[0], end[1])) {
570
+ flush();
571
+ currentDash = null;
572
+ continue;
573
+ }
574
+ appendSegment(start, end, dash);
575
+ }
576
+ flush();
630
577
  }
631
578
  /**
632
- * Draw axis-based chart shapes on canvas.
633
- * @private
634
- */
635
- class CanvasRenderer {
636
- engine;
637
- theme;
638
- painter;
639
- labelImageCache = new Map();
640
- backgroundImageCache = new Map();
641
- backgroundClassStyleCache = new Map();
642
- /**
643
- * Constructor.
644
- * @param {CanvasEngine} engine Canvas drawing engine
645
- * @param {CanvasTheme} theme Canvas theme resolver
646
- * @private
647
- */
648
- constructor(engine, theme) {
649
- this.engine = engine;
650
- this.theme = theme;
651
- this.painter = new CanvasPainter(engine.ctx);
652
- }
653
- /**
654
- * Get the drawing context for the main canvas.
655
- * @returns {CanvasRenderingContext2D} Canvas drawing context
656
- * @private
657
- */
658
- get ctx() {
659
- return this.painter.context;
660
- }
661
- /**
662
- * Run renderer draw calls on another canvas context.
663
- * @param {CanvasRenderingContext2D} ctx Canvas drawing context
664
- * @param {function} draw Draw callback
665
- * @private
666
- */
667
- withContext(ctx, draw) {
668
- this.painter.withContext(ctx, draw);
669
- }
670
- /**
671
- * Release renderer caches that can hold chart or DOM references.
672
- * @private
673
- */
674
- destroy() {
675
- const clearImageHandler = (entry) => {
676
- entry.image.onload = null;
677
- entry.image.onerror = null;
678
- };
679
- this.labelImageCache.forEach(clearImageHandler);
680
- this.backgroundImageCache.forEach(clearImageHandler);
681
- this.labelImageCache.clear();
682
- this.backgroundImageCache.clear();
683
- this.backgroundClassStyleCache.clear();
684
- }
685
- /**
686
- * Get cached image for data label.
687
- * @param {string} url Image URL
688
- * @param {object} $$ ChartInternal instance
689
- * @returns {object|null} Cached image entry
690
- * @private
691
- */
692
- getLabelImage(url, $$) {
693
- if (!url || !win.Image) {
694
- return null;
695
- }
696
- let entry = this.labelImageCache.get(url);
697
- if (!entry) {
698
- const nextEntry = {
699
- image: new win.Image(),
700
- loaded: false,
701
- loading: true
702
- };
703
- entry = nextEntry;
704
- this.labelImageCache.set(url, entry);
705
- nextEntry.image.onload = () => {
706
- nextEntry.loaded = true;
707
- nextEntry.loading = false;
708
- $$.redraw?.();
709
- };
710
- nextEntry.image.onerror = () => {
711
- nextEntry.loaded = false;
712
- nextEntry.loading = false;
713
- };
714
- nextEntry.image.src = url;
715
- }
716
- return entry;
717
- }
718
- /**
719
- * Get cached chart background image.
720
- * @param {string} url Image URL
721
- * @param {object} $$ ChartInternal instance
722
- * @returns {object|null} Cached image entry
723
- * @private
724
- */
725
- getBackgroundImage(url, $$) {
726
- if (!url || !win.Image) {
727
- return null;
728
- }
729
- let entry = this.backgroundImageCache.get(url);
730
- if (!entry) {
731
- const nextEntry = {
732
- image: new win.Image(),
733
- loaded: false,
734
- loading: true
735
- };
736
- entry = nextEntry;
737
- this.backgroundImageCache.set(url, entry);
738
- nextEntry.image.onload = () => {
739
- nextEntry.loaded = true;
740
- nextEntry.loading = false;
741
- $$.renderCanvasFrame?.(undefined, null, false);
742
- };
743
- nextEntry.image.onerror = () => {
744
- nextEntry.loaded = false;
745
- nextEntry.loading = false;
746
- };
747
- nextEntry.image.src = url;
748
- }
749
- return entry;
750
- }
751
- /**
752
- * Resolve background.class CSS values that canvas can reasonably mirror.
753
- * @param {object} $$ ChartInternal instance
754
- * @param {string} className Background class name
755
- * @returns {object} Class style
756
- * @private
757
- */
758
- getBackgroundClassStyle($$, className) {
759
- if (!className || !win.document) {
760
- return {};
761
- }
762
- const cached = this.backgroundClassStyleCache.get(className);
763
- if (cached) {
764
- return cached;
765
- }
766
- const probe = win.document.createElement("div");
767
- probe.className = className;
768
- probe.style.cssText =
769
- "position:absolute;visibility:hidden;pointer-events:none;width:1px;height:1px;";
770
- $$.$el.chart.node().appendChild(probe);
771
- const style = win.getComputedStyle(probe);
772
- const opacity = parseFloat(style.opacity);
773
- const result = {
774
- opacity: Number.isFinite(opacity) ? opacity : undefined,
775
- transform: style.transform || undefined
776
- };
777
- probe.remove();
778
- this.backgroundClassStyleCache.set(className, result);
779
- return result;
780
- }
781
- /**
782
- * Draw configured chart background behind all canvas layers.
783
- * @param {object} $$ ChartInternal instance
784
- * @private
785
- */
786
- drawBackground($$) {
787
- const bg = $$.config.background;
788
- if (!bg?.imgUrl && !bg?.color) {
789
- return;
790
- }
791
- const { ctx, painter } = this;
792
- const { current, margin, width, height } = $$.state;
793
- const classStyle = this.getBackgroundClassStyle($$, bg.class);
794
- painter.withState(() => {
795
- if (classStyle.opacity !== undefined) {
796
- ctx.globalAlpha *= classStyle.opacity;
797
- }
798
- if (bg.imgUrl) {
799
- const entry = this.getBackgroundImage(bg.imgUrl, $$);
800
- if (entry?.loaded) {
801
- const offset = getBackgroundImageOffset($$);
802
- const rect = getPreservedAspectRatioRect(entry.image, current.width, current.height);
803
- ctx.translate(offset.x, offset.y);
804
- applyCssMatrixTransform(ctx, classStyle.transform);
805
- ctx.drawImage(entry.image, rect.x, rect.y, rect.w, rect.h);
806
- }
807
- }
808
- else if (bg.color) {
809
- applyCssMatrixTransform(ctx, classStyle.transform);
810
- painter.fillRect({
811
- x: margin.left,
812
- y: margin.top,
813
- w: width,
814
- h: height
815
- }, { fill: bg.color });
816
- }
817
- });
818
- }
819
- /**
820
- * Draw data label image when loaded, or queue it for loading.
821
- * @param {object} $$ ChartInternal instance
822
- * @param {object} d Data row
823
- * @param {string} text Label text
824
- * @param {number} x Label x coordinate
825
- * @param {number} y Label y coordinate
826
- * @returns {object} Adjusted text position
827
- * @private
828
- */
829
- drawLabelImage($$, d, text, x, y) {
830
- const option = getLabelImageOption($$, d);
831
- if (!option?.url) {
832
- return { x, y };
833
- }
834
- const url = getLabelImageUrl(option, d);
835
- const position = getLabelImagePosition($$, option, text, x, y, d);
836
- const entry = this.getLabelImage(url, $$);
837
- if (entry?.loaded) {
838
- this.ctx.drawImage(entry.image, position.x, position.y, option.width, option.height);
839
- }
840
- return {
841
- x: position.textX,
842
- y: position.textY
843
- };
844
- }
845
- /**
846
- * Draw a data label at a resolved canvas position.
847
- * @param {object} $$ ChartInternal instance
848
- * @param {object} d Data row
849
- * @param {string} text Label text
850
- * @param {number} x Label x coordinate
851
- * @param {number} y Label y coordinate
852
- * @private
853
- */
854
- drawDataLabel($$, d, text, x, y) {
855
- const { painter, theme: { style } } = this;
856
- ({ x, y } = this.drawLabelImage($$, d, text, x, y));
857
- if ($$.config.data_labels.rotate) {
858
- const position = getRotatedLabelPosition($$, d, x, y);
859
- x = position.x;
860
- y = position.y;
861
- this.ctx.textAlign = position.textAlign;
862
- }
863
- this.ctx.fillStyle = getLabelColor($$, d, style.label.color);
864
- drawLabelDecorations($$, painter, d, text, x, y);
865
- painter.textLines(text, x, y, {
866
- angle: $$.config.data_labels.rotate
867
- });
868
- }
869
- /**
870
- * Redraw focused point data labels over the hover overlay.
871
- * @param {object} $$ ChartInternal instance
872
- * @param {Array} selectedData Focused data rows
873
- * @private
874
- */
875
- drawFocusLabels($$, selectedData) {
876
- if (!$$.hasDataLabel?.()) {
877
- return;
878
- }
879
- const { ctx, theme: { style } } = this;
880
- const rows = selectedData.filter(d => d &&
881
- hasCanvasDrawableValue($$, d) &&
882
- isCanvasPointType($$, d) &&
883
- isCanvasRenderableTarget($$, { id: d.id }));
884
- const texts = {
885
- size: () => rows.length
886
- };
887
- if (!rows.length) {
888
- return;
889
- }
890
- ctx.font = style.label.font;
891
- rows.forEach(d => {
892
- const text = getLabelText($$, d);
893
- let { x, y } = getRenderDataPoint($$, d);
894
- if (!text) {
895
- return;
896
- }
897
- ({ x, y } = getPointLabelAnchor($$, ctx, d, x, y));
898
- x += getLabelPosition($$, d, "x", texts);
899
- y += getLabelPosition($$, d, "y", texts);
900
- if (!isFiniteCanvasCoordinate(x, y)) {
901
- return;
902
- }
903
- this.drawDataLabel($$, d, text, x, y);
904
- });
905
- }
906
- /**
907
- * Draw all supported canvas shape layers.
908
- * @param {object} $$ ChartInternal instance
909
- * @param {object} shape Cached draw shape object
910
- * @param {Array} focusData Focused data rows
911
- * @private
912
- */
913
- draw($$, shape, focusData) {
914
- if ($$.state.hasTreemap) {
915
- this.drawTreemaps($$);
916
- return;
917
- }
918
- const { margin, width, height } = $$.state;
919
- const drawLineLayers = () => {
920
- if (!$$.config.area_front) {
921
- this.drawAreas($$, shape, focusData);
922
- }
923
- this.drawLines($$, shape);
924
- if ($$.config.area_front) {
925
- this.drawAreas($$, shape, focusData);
926
- }
927
- this.drawCircles($$, shape, focusData);
928
- };
929
- const drawContent = () => {
930
- if (!$$.config.bar_front) {
931
- this.drawBars($$, shape, focusData);
932
- }
933
- this.drawCandlesticks($$, shape, focusData);
934
- drawLineLayers();
935
- if ($$.config.bar_front) {
936
- this.drawBars($$, shape, focusData);
937
- }
938
- this.drawSelections($$, shape);
939
- this.drawLabels($$, shape);
940
- };
941
- $$.config.clipPath === false ?
942
- drawContent() :
943
- this.painter.clipRect({ x: margin.left, y: margin.top, w: width, h: height }, drawContent);
944
- }
945
- /**
946
- * Draw the canvas subchart overview and brush selection.
947
- * @param {object} $$ ChartInternal instance
948
- * @param {object} shape Cached draw shape object
949
- * @private
950
- */
951
- drawSubchart($$, shape) {
952
- const { config, state } = $$;
953
- if (!config.subchart_show || !state.hasAxis || state.width2 <= 0 || state.height2 <= 0) {
954
- return;
955
- }
956
- const { ctx, painter, theme: { style } } = this;
957
- const { margin2, width2, height2 } = state;
958
- const rect = { x: margin2.left, y: margin2.top, w: width2, h: height2 };
959
- const targets = $$.filterTargetsToShow()
960
- .filter(isCanvasRenderableTarget.bind(null, $$));
961
- painter.withState(() => {
962
- ctx.strokeStyle = style.axis.lineColor;
963
- ctx.lineWidth = style.axis.lineWidth;
964
- painter.strokePath(() => {
965
- if (config.axis_rotated) {
966
- painter.traceLine(rect.x, rect.y, rect.x, rect.y + rect.h);
967
- }
968
- else {
969
- painter.traceLine(rect.x, rect.y + rect.h, rect.x + rect.w, rect.y + rect.h);
970
- }
971
- });
972
- painter.clipRect(rect, () => {
973
- painter.withTranslation(rect.x, rect.y, () => {
974
- const areaTargets = targets.filter(isCanvasAreaType.bind(null, $$));
975
- const areaIndices = getCanvasShapeIndices($$, shape, TYPE.AREA, isCanvasAreaType.bind(null, $$));
976
- ctx.globalAlpha = style.shape.areaOpacity;
977
- for (const target of areaTargets) {
978
- if (!target.values.some(hasCanvasDrawableValue.bind(null, $$))) {
979
- continue;
980
- }
981
- ctx.fillStyle = $$.color(target.id);
982
- drawCanvasArea($$, target, areaIndices, painter, true);
983
- }
984
- ctx.globalAlpha = 1;
985
- const barTargets = targets.filter(isCanvasBarType.bind(null, $$));
986
- const barIndices = getCanvasShapeIndices($$, shape, TYPE.BAR, isCanvasBarType.bind(null, $$));
987
- const getBarPoints = $$.generateGetBarPoints?.(barIndices, true);
988
- if (getBarPoints) {
989
- ctx.globalAlpha = style.shape.barOpacity;
990
- for (const target of barTargets) {
991
- ctx.fillStyle = $$.color(target.id);
992
- target.values.forEach((d, i) => {
993
- if (!hasCanvasDrawableValue($$, d)) {
994
- return;
995
- }
996
- const geometry = getCanvasBarGeometry($$, getBarPoints, d, i);
997
- geometry && painter.fillRect(geometry.rect, { fill: ctx.fillStyle });
998
- });
999
- }
1000
- ctx.globalAlpha = 1;
1001
- }
1002
- const candlestickTargets = targets.filter(isCanvasCandlestickType.bind(null, $$));
1003
- const candlestickIndices = getCanvasShapeIndices($$, shape, TYPE.CANDLESTICK, isCanvasCandlestickType.bind(null, $$));
1004
- const getCandlestickPoints = $$.generateGetCandlestickPoints?.(candlestickIndices, true);
1005
- if (getCandlestickPoints) {
1006
- ctx.lineWidth = style.shape.candlestickLineWidth;
1007
- for (const target of candlestickTargets) {
1008
- target.values.forEach((d, i) => {
1009
- const value = $$.getCandlestickData?.(d);
1010
- const geometry = value && getCanvasCandlestickGeometry($$, getCandlestickPoints, d, i);
1011
- if (!geometry) {
1012
- return;
1013
- }
1014
- const color = getCandlestickColor($$, { id: target.id }, value);
1015
- ctx.strokeStyle = color;
1016
- ctx.fillStyle = color;
1017
- painter.strokePath(() => {
1018
- painter.traceLine(geometry.wickStart[0], geometry.wickStart[1], geometry.wickEnd[0], geometry.wickEnd[1]);
1019
- });
1020
- painter.fillRect(geometry.body, { fill: ctx.fillStyle });
1021
- });
1022
- }
1023
- }
1024
- const lineTargets = targets.filter(isCanvasLineType.bind(null, $$));
1025
- const lineIndices = getCanvasShapeIndices($$, shape, TYPE.LINE, isCanvasLineType.bind(null, $$));
1026
- ctx.globalAlpha = 1;
1027
- ctx.lineWidth = style.shape.lineWidth;
1028
- for (const target of lineTargets) {
1029
- if (!target.values.some(hasCanvasDrawableValue.bind(null, $$))) {
1030
- continue;
1031
- }
1032
- ctx.strokeStyle = $$.color(target.id);
1033
- drawCanvasLine($$, target, lineIndices, painter, true);
1034
- }
1035
- if (config.point_show && !$$.isPointFocusOnly?.()) {
1036
- const cy = $$.updateCircleY?.(true);
1037
- const cx = $$.subxx?.bind($$);
1038
- if (cx && cy) {
1039
- for (const target of targets) {
1040
- if (!isCanvasScatterType($$, target) &&
1041
- !isCanvasBubbleType($$, target)) {
1042
- continue;
1043
- }
1044
- const color = $$.color(target.id);
1045
- const pointFill = style.shape.pointFillColor || color;
1046
- const pointStroke = style.shape.pointStrokeColor || color;
1047
- const pointLineWidth = pointStroke ?
1048
- (style.shape.pointLineWidth ?? 1) :
1049
- 0;
1050
- const pointStyle = pointStroke && pointLineWidth > 0 ?
1051
- {
1052
- fill: pointFill,
1053
- stroke: pointStroke,
1054
- lineWidth: pointLineWidth
1055
- } :
1056
- { fill: pointFill };
1057
- ctx.globalAlpha = getPointOpacity($$, target);
1058
- target.values.forEach((d, i) => {
1059
- if (!hasCanvasDrawableValue($$, d)) {
1060
- return;
1061
- }
1062
- const x = config.axis_rotated ? cy(d, i) : cx(d);
1063
- const y = config.axis_rotated ? cx(d) : cy(d, i);
1064
- const r = Math.min(getTargetPointRadius($$, target, d), 3);
1065
- if (isFiniteCanvasCoordinate(x, y)) {
1066
- drawPointPattern(painter, "circle", x, y, r, pointStyle);
1067
- }
1068
- });
1069
- }
1070
- ctx.globalAlpha = 1;
1071
- }
1072
- }
1073
- });
1074
- });
1075
- this.drawSubchartBrush($$);
1076
- });
1077
- }
1078
- /**
1079
- * Draw current subchart brush selection.
1080
- * @param {object} $$ ChartInternal instance
1081
- * @private
1082
- */
1083
- drawSubchartBrush($$) {
1084
- const { config, scale, state } = $$;
1085
- const domain = state.domain;
1086
- if (!config.subchart_show || !domain?.length || !scale.subX) {
1087
- return;
1088
- }
1089
- const { margin2, width2, height2 } = state;
1090
- const p0 = scale.subX(domain[0]);
1091
- const p1 = scale.subX(domain[1]);
1092
- const axisLength = config.axis_rotated ? height2 : width2;
1093
- const extent = $$.axis?.getExtent?.();
1094
- const extentValues = Array.isArray(extent) && extent.length >= 2 && extent.every(Number.isFinite) ?
1095
- extent.slice(0, 2) :
1096
- [0, axisLength];
1097
- const extentStart = Math.max(0, Math.min(axisLength, Math.min(...extentValues)));
1098
- const extentEnd = Math.max(0, Math.min(axisLength, Math.max(...extentValues)));
1099
- const start = Math.max(extentStart, Math.min(extentEnd, Math.min(p0, p1)));
1100
- const end = Math.max(extentStart, Math.min(extentEnd, Math.max(p0, p1)));
1101
- const size = end - start;
1102
- if (size <= 0) {
1103
- return;
1104
- }
1105
- const rect = config.axis_rotated ?
1106
- { x: margin2.left, y: margin2.top + start, w: width2, h: size } :
1107
- { x: margin2.left + start, y: margin2.top, w: size, h: height2 };
1108
- this.painter.fillRect(rect, {
1109
- fill: this.theme.style.subchartBrush.fill,
1110
- alpha: this.theme.style.subchartBrush.opacity
1111
- });
1112
- if (config.subchart_showHandle) {
1113
- this.drawSubchartBrushHandle($$, start, "start");
1114
- this.drawSubchartBrushHandle($$, end, "end");
1115
- }
1116
- }
1117
- /**
1118
- * Draw a visible subchart brush resize handle.
1119
- * @param {object} $$ ChartInternal instance
1120
- * @param {number} coord Brush handle coordinate
1121
- * @param {string} type Brush handle side
1122
- * @private
1123
- */
1124
- drawSubchartBrushHandle($$, coord, type) {
1125
- const { config, state: { margin2, width2, height2 } } = $$;
1126
- const { ctx, painter, theme: { style } } = this;
1127
- const isRotated = config.axis_rotated;
1128
- const fill = style.subchartBrush.handleFill;
1129
- const stroke = style.subchartBrush.handleStroke;
1130
- const x = isRotated ? margin2.left + (width2 / 2) : margin2.left + coord;
1131
- const y = isRotated ? margin2.top + coord : margin2.top + (height2 / 2);
1132
- const path = getSubchartBrushHandlePath(isRotated ? "y" : "x", type);
1133
- if (!path) {
1134
- return;
1135
- }
1136
- painter.withState(() => {
1137
- ctx.translate(x, y);
1138
- ctx.fillStyle = fill;
1139
- ctx.strokeStyle = stroke;
1140
- ctx.lineWidth = style.subchartBrush.handleLineWidth;
1141
- ctx.globalAlpha = style.subchartBrush.handleOpacity;
1142
- ctx.fill(path);
1143
- ctx.globalAlpha = 1;
1144
- ctx.stroke(path);
1145
- });
1146
- }
1147
- /**
1148
- * Draw bar shapes on canvas.
1149
- * @param {object} $$ ChartInternal instance
1150
- * @param {object} shape Cached draw shape object
1151
- * @param {Array} focusData Focused data rows
1152
- * @private
1153
- */
1154
- drawBars($$, shape, focusData) {
1155
- const { ctx, painter, theme: { style } } = this;
1156
- const isBar = isCanvasBarType.bind(null, $$);
1157
- const isExpanded = getExpandedFocusMatcher($$, focusData, isCanvasBarType);
1158
- const targets = $$.filterTargetsToShow()
1159
- .filter(isBar)
1160
- .filter(isCanvasRenderableTarget.bind(null, $$));
1161
- const getPoints = $$.generateGetBarPoints(getCanvasShapeIndices($$, shape, TYPE.BAR, isBar), false);
1162
- const { margin } = $$.state;
1163
- const getRadius = getBarRadiusResolver($$);
1164
- const stackingRadiusSet = getRadius ? getStackingBarRadiusSet($$) : new Set();
1165
- if (!getPoints) {
1166
- return;
1167
- }
1168
- painter.withTranslation(margin.left, margin.top, () => {
1169
- for (const target of targets) {
1170
- const range = getCanvasTargetVisibleRange($$, target);
1171
- const connectLineType = getBarConnectLineType($$, target.id);
1172
- const targetOpacity = getCanvasTargetFocusOpacity($$, target);
1173
- const connectLineBoxes = [];
1174
- const bars = [];
1175
- for (let i = range.start; i < range.end; i++) {
1176
- const d = target.values[i];
1177
- if (!hasCanvasDrawableValue($$, d)) {
1178
- continue;
1179
- }
1180
- const geometry = getCanvasBarGeometry($$, getPoints, d, i);
1181
- if (!geometry) {
1182
- continue;
1183
- }
1184
- const { points, rect } = geometry;
1185
- const radiusInfo = getBarRadiusInfo($$, d, points, getRadius, stackingRadiusSet, $$.isStackingRadiusData?.bind($$));
1186
- bars.push({ d, points, rect, radiusInfo });
1187
- if (connectLineType) {
1188
- connectLineBoxes.push(getBarConnectLineBox($$, points, radiusInfo));
1189
- }
1190
- }
1191
- if (connectLineType && style.shape.barConnectLineWidth > 0) {
1192
- drawBarConnectLine($$, painter, connectLineType, connectLineBoxes, targetOpacity);
1193
- }
1194
- for (const { d, rect, radiusInfo } of bars) {
1195
- const overColor = getCanvasOverColor($$, getFocusedCanvasDatum(focusData, d));
1196
- const color = overColor || $$.color(target.id);
1197
- const fillAlpha = isExpanded(d) ?
1198
- style.shape.barExpandedOpacity :
1199
- style.shape.barOpacity;
1200
- const alpha = fillAlpha * targetOpacity;
1201
- ctx.fillStyle = overColor ?
1202
- color :
1203
- getCanvasLinearGradientFill($$, ctx, target, "bar", rect, color);
1204
- getRadius ?
1205
- painter.fillRoundRect(rect, radiusInfo.corners, { alpha }) :
1206
- painter.fillRect(rect, { alpha });
1207
- if (style.shape.barLineWidth > 0) {
1208
- ctx.strokeStyle = style.shape.barStrokeColor;
1209
- ctx.lineWidth = style.shape.barLineWidth;
1210
- getRadius ?
1211
- painter.strokeRoundRect(rect, radiusInfo.corners, { alpha: style.shape.barOpacity * targetOpacity }) :
1212
- painter.strokeRect(rect, {
1213
- alpha: style.shape.barOpacity * targetOpacity
1214
- });
1215
- }
1216
- }
1217
- }
1218
- });
1219
- }
1220
- /**
1221
- * Draw candlestick shapes on canvas.
1222
- * @param {object} $$ ChartInternal instance
1223
- * @param {object} shape Cached draw shape object
1224
- * @param {Array} focusData Focused data rows
1225
- * @private
1226
- */
1227
- drawCandlesticks($$, shape, focusData) {
1228
- const { ctx, painter, theme: { style } } = this;
1229
- const isCandlestick = isCanvasCandlestickType.bind(null, $$);
1230
- const isExpanded = getExpandedFocusMatcher($$, focusData, isCanvasCandlestickType);
1231
- const targets = $$.filterTargetsToShow()
1232
- .filter(isCandlestick)
1233
- .filter(isCanvasRenderableTarget.bind(null, $$));
1234
- const getPoints = $$.generateGetCandlestickPoints?.(getCanvasShapeIndices($$, shape, TYPE.CANDLESTICK, isCandlestick), false);
1235
- const { margin } = $$.state;
1236
- if (!getPoints) {
1237
- return;
1238
- }
1239
- painter.withTranslation(margin.left, margin.top, () => {
1240
- ctx.lineWidth = style.shape.candlestickLineWidth;
1241
- for (const target of targets) {
1242
- const range = getCanvasTargetVisibleRange($$, target);
1243
- const targetOpacity = getCanvasTargetFocusOpacity($$, target);
1244
- const lineWidth = style.shape.candlestickLineWidth;
1245
- const strokeColor = style.shape.candlestickStrokeColor || "#000";
1246
- for (let i = range.start; i < range.end; i++) {
1247
- const d = target.values[i];
1248
- const value = $$.getCandlestickData?.(d);
1249
- if (!value) {
1250
- continue;
1251
- }
1252
- const geometry = getCanvasCandlestickGeometry($$, getPoints, d, i);
1253
- if (!geometry) {
1254
- continue;
1255
- }
1256
- const { body: rect, wickStart, wickEnd } = geometry;
1257
- ctx.fillStyle = getCanvasOverColor($$, getFocusedCanvasDatum(focusData, d)) ||
1258
- getCandlestickColor($$, d, value);
1259
- ctx.strokeStyle = strokeColor;
1260
- ctx.globalAlpha = targetOpacity;
1261
- lineWidth > 0 && painter.strokePath(() => {
1262
- painter.traceLine(wickStart[0], wickStart[1], wickEnd[0], wickEnd[1]);
1263
- });
1264
- painter.fillRect(rect, {
1265
- alpha: (isExpanded(d) ? style.shape.candlestickExpandedOpacity : 1) * targetOpacity
1266
- });
1267
- lineWidth > 0 && painter.strokeRect(rect, {
1268
- alpha: targetOpacity,
1269
- lineWidth,
1270
- stroke: strokeColor
1271
- });
1272
- }
1273
- }
1274
- ctx.globalAlpha = 1;
1275
- });
1276
- }
1277
- /**
1278
- * Check whether focus data requires a full redraw to reflect _expanded_ shape styles.
1279
- * @param {object} $$ ChartInternal instance
1280
- * @param {Array} selectedData Focused data rows
1281
- * @returns {boolean} Whether focused shapes need a main redraw
1282
- * @private
1283
- */
1284
- hasExpandedShapeFocus($$, selectedData) {
1285
- return !!selectedData?.some(d => d &&
1286
- (isCanvasBarType($$, d) ||
1287
- isCanvasCandlestickType($$, d)));
1288
- }
1289
- /**
1290
- * Draw line strokes on canvas.
1291
- * @param {object} $$ ChartInternal instance
1292
- * @param {object} shape Cached draw shape object
1293
- * @private
1294
- */
1295
- drawLines($$, shape) {
1296
- const { ctx, painter, theme: { style } } = this;
1297
- const isLine = isCanvasLineType.bind(null, $$);
1298
- const targets = $$.filterTargetsToShow()
1299
- .filter(isLine)
1300
- .filter(isCanvasRenderableTarget.bind(null, $$));
1301
- const indices = getCanvasShapeIndices($$, shape, TYPE.LINE, isLine);
1302
- const { margin } = $$.state;
1303
- if (!$$.generateGetLinePoints) {
1304
- return;
1305
- }
1306
- painter.withTranslation(margin.left, margin.top, () => {
1307
- for (const target of targets) {
1308
- const visibleTarget = getVisibleCanvasTarget($$, target);
1309
- if (!visibleTarget.values.some(hasCanvasDrawableValue.bind(null, $$))) {
1310
- continue;
1311
- }
1312
- ctx.globalAlpha = getCanvasTargetFocusOpacity($$, target);
1313
- ctx.lineWidth = isCanvasTargetFocused($$, target) ?
1314
- style.shape.lineFocusedWidth :
1315
- style.shape.lineWidth;
1316
- ctx.strokeStyle = $$.color(target.id);
1317
- $$.config.data_regions?.[visibleTarget.id] ?
1318
- drawCanvasLineWithDataRegions($$, visibleTarget, painter) :
1319
- drawCanvasLine($$, visibleTarget, indices, painter);
1320
- }
1321
- ctx.globalAlpha = 1;
1322
- });
1323
- }
1324
- /**
1325
- * Draw area fills on canvas.
1326
- * @param {object} $$ ChartInternal instance
1327
- * @param {object} shape Cached draw shape object
1328
- * @param {Array} focusData Focused data rows
1329
- * @private
1330
- */
1331
- drawAreas($$, shape, focusData) {
1332
- const { ctx, painter, theme: { style } } = this;
1333
- const isArea = isCanvasAreaType.bind(null, $$);
1334
- const targets = $$.filterTargetsToShow()
1335
- .filter(isArea)
1336
- .filter(isCanvasRenderableTarget.bind(null, $$));
1337
- const indices = getCanvasShapeIndices($$, shape, TYPE.AREA, isArea);
1338
- const { margin } = $$.state;
1339
- if (!$$.generateGetAreaPoints) {
1340
- return;
1341
- }
1342
- painter.withTranslation(margin.left, margin.top, () => {
1343
- for (const target of targets) {
1344
- const visibleTarget = getVisibleCanvasTarget($$, target);
1345
- if (!visibleTarget.values.some(hasCanvasDrawableValue.bind(null, $$))) {
1346
- continue;
1347
- }
1348
- const color = getCanvasRenderColor($$, visibleTarget, focusData);
1349
- ctx.globalAlpha = style.shape.areaOpacity * getCanvasTargetFocusOpacity($$, target);
1350
- ctx.fillStyle = getCanvasLinearGradientFill($$, ctx, visibleTarget, "area",
1351
- // Bounds computation walks visible rows, so skip it when no gradient is set.
1352
- // Use the original target so the range cache stays aligned with unsliced values.
1353
- $$.config.area_linearGradient ? getCanvasAreaBounds($$, target, indices) : null, color);
1354
- drawCanvasArea($$, visibleTarget, indices, painter);
1355
- }
1356
- ctx.globalAlpha = 1;
1357
- });
1358
- }
1359
- /**
1360
- * Draw point circles on canvas.
1361
- * @param {object} $$ ChartInternal instance
1362
- * @param {object} shape Cached draw shape object
1363
- * @param {Array} focusData Focused data rows
1364
- * @private
1365
- */
1366
- drawCircles($$, shape, focusData) {
1367
- if (!$$.config.point_show || $$.isPointFocusOnly?.()) {
1368
- return;
1369
- }
1370
- const { ctx, painter, theme: { style } } = this;
1371
- const { cx, cy } = shape.pos;
1372
- const { margin } = $$.state;
1373
- const isExpanded = getExpandedFocusMatcher($$, focusData, isCanvasPointType);
1374
- const hasExpandedFocus = !!focusData?.some(d => d && isCanvasPointType($$, d));
1375
- if (!cx || !cy) {
1376
- return;
1377
- }
1378
- painter.withTranslation(margin.left, margin.top, () => {
1379
- for (const target of $$.filterTargetsToShow()) {
1380
- if (!isCanvasPointType($$, target) ||
1381
- !isCanvasRenderableTarget($$, target) ||
1382
- (isCanvasLineType($$, target) && !shouldDrawPoints($$, target))) {
1383
- continue;
1384
- }
1385
- const color = $$.color(target.id);
1386
- const pointType = getPointType($$, target);
1387
- const targetOpacity = getCanvasTargetFocusOpacity($$, target);
1388
- const hasGradient = !!$$.config.point_radialGradient;
1389
- const cullDenseScatter = shouldCullDenseScatterPoints($$, target, pointType, hasGradient);
1390
- const range = getCanvasTargetVisibleRange($$, target);
1391
- const visibleCount = range.end - range.start;
1392
- const occupancy = cullDenseScatter ?
1393
- createCanvasPointOccupancyGrid($$.state.width, $$.state.height, getTargetPointRadius($$, target, target.values[range.start] || target.values[0])) :
1394
- null;
1395
- const pointAlpha = getPointOpacity($$, target) * targetOpacity;
1396
- ctx.fillStyle = color;
1397
- ctx.globalAlpha = pointAlpha;
1398
- const pointFill = style.shape.pointFillColor || color;
1399
- const pointStroke = style.shape.pointStrokeColor || color;
1400
- const pointLineWidth = pointStroke ? (style.shape.pointLineWidth ?? 1) : 0;
1401
- const hasPointStroke = !!pointStroke && pointLineWidth > 0;
1402
- const mergeSameColorStroke = hasPointStroke &&
1403
- pointType === "circle" &&
1404
- isNumber(pointAlpha) &&
1405
- pointAlpha < 1 &&
1406
- pointFill === pointStroke;
1407
- if (!hasExpandedFocus && !hasGradient && pointType === "circle") {
1408
- ctx.fillStyle = pointFill;
1409
- if (hasPointStroke && !mergeSameColorStroke) {
1410
- ctx.strokeStyle = pointStroke;
1411
- ctx.lineWidth = pointLineWidth;
1412
- }
1413
- if (visibleCount > MAX_BATCHED_CIRCLE_POINTS) {
1414
- for (let i = range.start; i < range.end; i++) {
1415
- const d = target.values[i];
1416
- if (!hasCanvasDrawableValue($$, d)) {
1417
- continue;
1418
- }
1419
- const baseR = getTargetPointRadius($$, target, d);
1420
- const r = mergeSameColorStroke ? baseR + pointLineWidth / 2 : baseR;
1421
- const x = cx(d, i);
1422
- const y = cy(d, i);
1423
- if (!isFiniteCanvasCoordinate(x, y)) {
1424
- continue;
1425
- }
1426
- if (occupancy && !markCanvasPointOccupancy(occupancy, x, y)) {
1427
- continue;
1428
- }
1429
- ctx.beginPath();
1430
- ctx.arc(x, y, r, 0, Math.PI * 2);
1431
- ctx.fill();
1432
- hasPointStroke && !mergeSameColorStroke && ctx.stroke();
1433
- }
1434
- }
1435
- else {
1436
- ctx.beginPath();
1437
- for (let i = range.start; i < range.end; i++) {
1438
- const d = target.values[i];
1439
- if (!hasCanvasDrawableValue($$, d)) {
1440
- continue;
1441
- }
1442
- const baseR = getTargetPointRadius($$, target, d);
1443
- const r = mergeSameColorStroke ? baseR + pointLineWidth / 2 : baseR;
1444
- const x = cx(d, i);
1445
- const y = cy(d, i);
1446
- if (!isFiniteCanvasCoordinate(x, y)) {
1447
- continue;
1448
- }
1449
- painter.traceCircle(x, y, r);
1450
- }
1451
- ctx.fill();
1452
- hasPointStroke && !mergeSameColorStroke && ctx.stroke();
1453
- }
1454
- }
1455
- else {
1456
- for (let i = range.start; i < range.end; i++) {
1457
- const d = target.values[i];
1458
- if (!hasCanvasDrawableValue($$, d)) {
1459
- continue;
1460
- }
1461
- const expanded = isExpanded(d);
1462
- const baseR = getTargetPointRadius($$, target, d);
1463
- const r = expanded ?
1464
- ($$.pointExpandedR?.(d) ??
1465
- (baseR * 1.75)) :
1466
- baseR;
1467
- const x = cx(d, i);
1468
- const y = cy(d, i);
1469
- const overColor = getCanvasOverColor($$, d);
1470
- const renderColor = overColor || color;
1471
- const renderPointFill = style.shape.pointFillColor || renderColor;
1472
- const renderPointStroke = style.shape.pointStrokeColor || renderColor;
1473
- const fill = expanded ?
1474
- (overColor || style.focusPoint.fill || renderPointFill) :
1475
- renderPointFill;
1476
- const stroke = expanded ?
1477
- (overColor || style.focusPoint.stroke || renderColor) :
1478
- renderPointStroke;
1479
- const lineWidth = expanded ? style.focusPoint.lineWidth : pointLineWidth;
1480
- const alpha = getPointOpacity($$, d) * targetOpacity;
1481
- const hasStroke = !!stroke && (lineWidth ?? 0) > 0;
1482
- const mergeSameColorStroke = pointType === "circle" &&
1483
- hasStroke &&
1484
- isNumber(alpha) &&
1485
- alpha < 1 &&
1486
- fill === stroke;
1487
- const drawR = mergeSameColorStroke ? r + (lineWidth || 0) / 2 : r;
1488
- if (!isFiniteCanvasCoordinate(x, y)) {
1489
- continue;
1490
- }
1491
- ctx.fillStyle = hasGradient ?
1492
- getPointFillStyle($$, ctx, d, x, y, r, renderColor) :
1493
- fill;
1494
- ctx.globalAlpha = alpha;
1495
- drawPointPattern(painter, pointType, x, y, drawR, hasStroke && !mergeSameColorStroke ?
1496
- {
1497
- fill: ctx.fillStyle,
1498
- stroke,
1499
- lineWidth
1500
- } :
1501
- undefined, baseR);
1502
- }
1503
- }
1504
- }
1505
- });
1506
- }
1507
- /**
1508
- * Draw selected canvas shapes.
1509
- * @param {object} $$ ChartInternal instance
1510
- * @param {object} shape Cached draw shape object
1511
- * @private
1512
- */
1513
- drawSelections($$, shape) {
1514
- const selectedData = $$.getCanvasSelectedData?.();
1515
- if (!$$.config.data_selection_enabled || !selectedData?.length || $$.state.hasTreemap) {
1516
- return;
1517
- }
1518
- const { ctx, painter, theme: { style } } = this;
1519
- const { margin } = $$.state;
1520
- const isBar = isCanvasBarType.bind(null, $$);
1521
- const isCandlestick = isCanvasCandlestickType.bind(null, $$);
1522
- const barPoints = selectedData.some(isBar) ?
1523
- $$.generateGetBarPoints(getCanvasShapeIndices($$, shape, TYPE.BAR, isBar), false) :
1524
- null;
1525
- const candlestickPoints = selectedData.some(isCandlestick) ?
1526
- $$.generateGetCandlestickPoints?.(getCanvasShapeIndices($$, shape, TYPE.CANDLESTICK, isCandlestick), false) :
1527
- null;
1528
- const getRadius = barPoints ? getBarRadiusResolver($$) : null;
1529
- const stackingRadiusSet = getRadius ? getStackingBarRadiusSet($$) : new Set();
1530
- painter.withTranslation(margin.left, margin.top, () => {
1531
- selectedData.forEach(d => {
1532
- const color = $$.color(d.id);
1533
- if (isBar(d) && barPoints) {
1534
- const geometry = getCanvasBarGeometry($$, barPoints, d, d.index);
1535
- if (!geometry) {
1536
- return;
1537
- }
1538
- const { points, rect } = geometry;
1539
- const corners = getRadius ?
1540
- getBarRadiusInfo($$, d, points, getRadius, stackingRadiusSet).corners :
1541
- 0;
1542
- getRadius ?
1543
- painter.fillRoundRect(rect, corners, { fill: "#fff", alpha: 0.3 }) :
1544
- painter.fillRect(rect, { fill: "#fff", alpha: 0.3 });
1545
- getRadius ?
1546
- painter.strokeRoundRect(rect, corners, { stroke: color, lineWidth: 2 }) :
1547
- painter.strokeRect(rect, { stroke: color, lineWidth: 2 });
1548
- }
1549
- else if (isCandlestick(d) && candlestickPoints) {
1550
- const value = $$.getCandlestickData?.(d);
1551
- if (!value) {
1552
- return;
1553
- }
1554
- const geometry = getCanvasCandlestickGeometry($$, candlestickPoints, d, d.index);
1555
- if (!geometry) {
1556
- return;
1557
- }
1558
- const { body, wickStart, wickEnd } = geometry;
1559
- painter.strokePath(() => {
1560
- painter.traceLine(wickStart[0], wickStart[1], wickEnd[0], wickEnd[1]);
1561
- }, { stroke: color, lineWidth: 3 });
1562
- painter.fillRect(body, { fill: "#fff", alpha: 0.3 });
1563
- painter.strokeRect(body, { stroke: color, lineWidth: 2 });
1564
- }
1565
- else if (isCanvasPointType($$, d)) {
1566
- const pointType = getPointType($$, { id: d.id });
1567
- const { x, y } = getRenderDataPoint($$, d);
1568
- const r = $$.pointR?.(d) ?? 2.5;
1569
- const selectR = $$.pointSelectR?.(d) ?? (r * 2);
1570
- if (!isFiniteCanvasCoordinate(x, y)) {
1571
- return;
1572
- }
1573
- drawPointPattern(painter, "circle", x, y, selectR, {
1574
- fill: style.selectedPoint.fill,
1575
- lineWidth: style.selectedPoint.lineWidth,
1576
- stroke: style.selectedPoint.stroke || color
1577
- });
1578
- drawPointPattern(painter, pointType, x, y, r, {
1579
- fill: style.shape.pointFillColor || color,
1580
- stroke: style.shape.pointStrokeColor || color,
1581
- lineWidth: style.shape.pointLineWidth ?? 1
1582
- });
1583
- }
1584
- });
1585
- ctx.globalAlpha = 1;
1586
- });
1587
- }
1588
- /**
1589
- * Draw data labels on canvas.
1590
- * @param {object} $$ ChartInternal instance
1591
- * @param {object} shape Cached draw shape object
1592
- * @private
1593
- */
1594
- drawLabels($$, shape) {
1595
- if (!$$.hasDataLabel?.()) {
1596
- return;
1597
- }
1598
- const { ctx, painter, theme: { style } } = this;
1599
- const { margin } = $$.state;
1600
- const { cx, cy } = shape.pos;
1601
- const barPoints = shape.indices[TYPE.BAR] ?
1602
- $$.generateGetBarPoints(shape.indices[TYPE.BAR], false) :
1603
- null;
1604
- const candlestickPoints = $$.generateGetCandlestickPoints ?
1605
- $$.generateGetCandlestickPoints(getCanvasShapeIndices($$, shape, TYPE.CANDLESTICK, isCanvasCandlestickType.bind(null, $$)), false) :
1606
- null;
1607
- const targets = $$.filterTargetsToShow()
1608
- .filter(target => (isCanvasBarType($$, target) ||
1609
- isCanvasPointType($$, target) ||
1610
- isCanvasAreaType($$, target) ||
1611
- isCanvasCandlestickType($$, target)) &&
1612
- isCanvasRenderableTarget($$, target));
1613
- const targetRows = [];
1614
- let labelCount = 0;
1615
- targets.forEach(target => {
1616
- const range = getCanvasTargetVisibleRange($$, target);
1617
- const data = $$.labelishData(target);
1618
- const rows = [];
1619
- for (let i = 0; i < data.length; i++) {
1620
- const d = data[i];
1621
- const text = getLabelText($$, d);
1622
- if (text &&
1623
- d.index >= range.start &&
1624
- d.index < range.end &&
1625
- hasCanvasDrawableValue($$, d)) {
1626
- rows.push({ d, text });
1627
- }
1628
- }
1629
- labelCount += rows.length;
1630
- rows.length && targetRows.push(rows);
1631
- });
1632
- const texts = {
1633
- size: () => labelCount
1634
- };
1635
- painter.withTranslation(margin.left, margin.top, () => {
1636
- ctx.font = style.label.font;
1637
- ctx.textAlign = "center";
1638
- targetRows
1639
- .forEach(rows => {
1640
- for (const { d, text } of rows) {
1641
- let x;
1642
- let y;
1643
- if (isCanvasBarType($$, d) && barPoints) {
1644
- const geometry = getCanvasBarGeometry($$, barPoints, d, d.index);
1645
- if (!geometry) {
1646
- continue;
1647
- }
1648
- const { points, rect } = geometry;
1649
- const value = $$.getBaseValue(d);
1650
- const end = getRenderPoint($$, points[2]);
1651
- const isNegative = value < 0;
1652
- const labelHeight = getLabelDecorationBox(ctx, text, 0, 0).h;
1653
- x = $$.config.axis_rotated && !$$.config.data_labels.centered ?
1654
- end[0] + (isNegative ? -4 : 4) :
1655
- rect.x + rect.w / 2;
1656
- y = $$.config.data_labels.centered ?
1657
- rect.y + rect.h / 2 :
1658
- ($$.config.axis_rotated ? rect.y + rect.h / 2 : end[1] + (isNegative ? labelHeight - 3 : -3));
1659
- ctx.textAlign = $$.config.axis_rotated &&
1660
- !$$.config.data_labels.centered ?
1661
- (isNegative ? "right" : "left") :
1662
- "center";
1663
- ctx.textBaseline = $$.config.data_labels.centered ?
1664
- "middle" :
1665
- ($$.config.axis_rotated ? "middle" : "alphabetic");
1666
- }
1667
- else if (isCanvasCandlestickType($$, d) && candlestickPoints) {
1668
- const value = $$.getCandlestickData?.(d);
1669
- const geometry = getCanvasCandlestickGeometry($$, candlestickPoints, d, d.index);
1670
- const isUp = value?._isUp;
1671
- if (!geometry) {
1672
- continue;
1673
- }
1674
- const { wickEnd, wickStart } = geometry;
1675
- const isInverted = $$.config[`axis_${$$.axis?.getId(d.id)}_inverted`];
1676
- x = $$.config.axis_rotated ?
1677
- (isUp ? wickEnd[0] + 4 : wickStart[0] - 4) :
1678
- wickStart[0];
1679
- y = $$.config.axis_rotated ?
1680
- wickStart[1] + 3 :
1681
- (isUp ? wickEnd[1] - 3 : wickStart[1] + 12);
1682
- !$$.config.axis_rotated && isInverted &&
1683
- (y += 15 * (isUp ? 1 : -1));
1684
- ctx.textAlign = $$.config.axis_rotated ?
1685
- (isUp ? "left" : "right") :
1686
- "center";
1687
- ctx.textBaseline = "alphabetic";
1688
- }
1689
- else if (cx && cy) {
1690
- ({ x, y } = getShapePoint(shape.pos, d, d.index));
1691
- ({ x, y } = getPointLabelAnchor($$, ctx, d, x, y));
1692
- }
1693
- x += getLabelPosition($$, d, "x", texts);
1694
- y += getLabelPosition($$, d, "y", texts);
1695
- if (!isFiniteCanvasCoordinate(x, y)) {
1696
- continue;
1697
- }
1698
- this.drawDataLabel($$, d, text, x, y);
1699
- }
1700
- });
1701
- });
1702
- }
1703
- /**
1704
- * Draw empty-data label on canvas.
1705
- * @param {object} $$ ChartInternal instance
1706
- * @private
1707
- */
1708
- drawEmptyLabel($$) {
1709
- const text = $$.config.data_empty_label_text;
1710
- const targetsToShow = $$.filterTargetsToShow();
1711
- if (!text || targetsToShow.length) {
1712
- return;
1713
- }
1714
- const { margin, width, height } = $$.state;
1715
- const { style } = this.theme;
1716
- this.painter.text(String(text), margin.left + width / 2, margin.top + height / 2, {
1717
- fill: style.emptyLabel.color,
1718
- font: style.emptyLabel.font,
1719
- textAlign: "center",
1720
- textBaseline: "middle"
1721
- });
1722
- }
1723
- /**
1724
- * Draw drag-zoom brush overlay.
1725
- * @param {object} $$ ChartInternal instance
1726
- * @param {number} start Start coordinate
1727
- * @param {number} end End coordinate
1728
- * @private
1729
- */
1730
- drawZoomBrush($$, start, end) {
1731
- const { axis_rotated } = $$.config;
1732
- const { height, margin, width } = $$.state;
1733
- const brushStart = Math.min(start, end);
1734
- const brushSize = Math.abs(end - start);
1735
- const rect = axis_rotated ?
1736
- { x: margin.left, y: margin.top + brushStart, w: width, h: brushSize } :
1737
- { x: margin.left + brushStart, y: margin.top, w: brushSize, h: height };
1738
- if (brushSize <= 0) {
1739
- return;
1740
- }
1741
- this.painter.fillRect(rect, {
1742
- fill: this.theme.style.zoomBrush.fill,
1743
- alpha: this.theme.style.zoomBrush.opacity
1744
- });
1745
- }
1746
- /**
1747
- * Draw data selection drag area.
1748
- * @param {object} $$ ChartInternal instance
1749
- * @param {object} rect Selection rectangle
1750
- * @private
1751
- */
1752
- drawSelectionDragArea($$, rect) {
1753
- const { margin, width, height } = $$.state;
1754
- const plot = {
1755
- x: margin.left,
1756
- y: margin.top,
1757
- w: width,
1758
- h: height
1759
- };
1760
- const x = Math.max(plot.x, rect.x);
1761
- const y = Math.max(plot.y, rect.y);
1762
- const right = Math.min(plot.x + plot.w, rect.x + rect.w);
1763
- const bottom = Math.min(plot.y + plot.h, rect.y + rect.h);
1764
- if (right <= x || bottom <= y) {
1765
- return;
1766
- }
1767
- this.painter.fillRect({
1768
- x,
1769
- y,
1770
- w: right - x,
1771
- h: bottom - y
1772
- }, {
1773
- fill: this.theme.style.zoomBrush.fill,
1774
- alpha: this.theme.style.zoomBrush.opacity
1775
- });
1776
- }
1777
- /**
1778
- * Draw treemap tiles and labels on canvas.
1779
- * @param {object} $$ ChartInternal instance
1780
- * @private
1781
- */
1782
- drawTreemaps($$) {
1783
- const { ctx, painter, theme: { style } } = this;
1784
- const { config } = $$;
1785
- const root = $$.getTreemapRoot?.($$.data.targets);
1786
- const nodes = root?.children || [];
1787
- painter.withState(() => {
1788
- ctx.lineWidth = style.treemap.lineWidth;
1789
- ctx.strokeStyle = style.treemap.stroke;
1790
- for (const node of nodes) {
1791
- const { data } = node;
1792
- if (!isCanvasTreemapType($$, data)) {
1793
- continue;
1794
- }
1795
- const { x, y, w, h } = getTreemapNodeRect($$, node, root, true);
1796
- if (!isFiniteCanvasCoordinate(x, y) ||
1797
- !isFiniteCanvasCoordinate(x + w, y + h) ||
1798
- w <= 0 ||
1799
- h <= 0) {
1800
- continue;
1801
- }
1802
- const rect = { x, y, w, h };
1803
- ctx.fillStyle = $$.color(data.name);
1804
- painter.fillRect(rect);
1805
- painter.strokeRect(rect);
1806
- if (!config.treemap_label_show ||
1807
- (data.ratio || 0) < (config.treemap_label_threshold || 0)) {
1808
- continue;
1809
- }
1810
- const label = getTreemapLabelText($$, data, w, h);
1811
- if (!label) {
1812
- continue;
1813
- }
1814
- const lines = label.split("\n");
1815
- const isCentered = !!config.data_labels.centered;
1816
- ctx.font = style.label.font;
1817
- const lineHeight = getFontSize(ctx.font);
1818
- const metrics = ctx.measureText(lines[0] ?? "");
1819
- const fontBoundingHeight = ((metrics.fontBoundingBoxAscent || 0) +
1820
- (metrics.fontBoundingBoxDescent || 0)) || ((metrics.actualBoundingBoxAscent || 0) +
1821
- (metrics.actualBoundingBoxDescent || 0));
1822
- const textHeight = Math.max(lineHeight, fontBoundingHeight) +
1823
- ((lines.length - 1) * lineHeight);
1824
- const blockY = isCentered ? y + h / 2 : y + textHeight + 5;
1825
- let textX = isCentered ? x + w / 2 : x + 5;
1826
- let textY = blockY - (lines.length - 1) * lineHeight / (isCentered ? 2 : 1);
1827
- ctx.textAlign = isCentered ? "center" : "left";
1828
- ctx.textBaseline = isCentered ? "middle" : "alphabetic";
1829
- ({ x: textX, y: textY } = this.drawLabelImage($$, data, label, textX, textY));
1830
- ctx.fillStyle = getLabelColor($$, data, style.label.color);
1831
- drawLabelDecorations($$, painter, data, label, textX, isCentered ? textY + ((lines.length - 1) * lineHeight / 2) : blockY);
1832
- lines.forEach((line, i) => {
1833
- painter.text(line, textX, textY + i * lineHeight, {
1834
- maxWidth: Math.max(0, w - 8)
1835
- });
1836
- });
1837
- }
1838
- });
1839
- }
1840
- /**
1841
- * Draw focus grid and focused points on canvas.
1842
- * @param {object} $$ ChartInternal instance
1843
- * @param {Array} selectedData Focused data rows
1844
- * @private
1845
- */
1846
- drawFocus($$, selectedData) {
1847
- if (!selectedData?.length) {
1848
- return;
1849
- }
1850
- const { painter } = this;
1851
- const { style } = this.theme;
1852
- const { margin } = $$.state;
1853
- const focus = selectedData.find(d => d &&
1854
- hasCanvasDrawableValue($$, d));
1855
- painter.withTranslation(margin.left, margin.top, () => {
1856
- if ($$.config.tooltip_show &&
1857
- $$.config.grid_focus_show !== false &&
1858
- !$$.config.axis_tooltip &&
1859
- focus) {
1860
- const { x, y } = getRenderDataPoint($$, focus);
1861
- const axisLineWidth = style.axis.lineWidth;
1862
- const isRotated = $$.config.axis_rotated;
1863
- const hasIndexCoordinate = Number.isFinite(isRotated ? y : x);
1864
- const hasValueCoordinate = Number.isFinite(isRotated ? x : y);
1865
- const crispEdgeX = value => painter.crisp(margin.left + value, axisLineWidth) - margin.left;
1866
- const crispEdgeY = value => painter.crisp(margin.top + value, axisLineWidth) - margin.top;
1867
- const isEdge = $$.config.grid_focus_edge && !$$.config.tooltip_grouped;
1868
- if (hasIndexCoordinate) {
1869
- painter.strokePath(() => {
1870
- if (isRotated) {
1871
- painter.traceLine(crispEdgeX(0), y, isEdge && hasValueCoordinate ? x : crispEdgeX($$.state.width), y);
1872
- if (hasValueCoordinate &&
1873
- $$.config.grid_focus_y &&
1874
- !$$.config.tooltip_grouped) {
1875
- const isY2 = $$.axis?.getId(focus.id) === "y2";
1876
- painter.traceLine(x, isEdge && !isY2 ? y : crispEdgeY(0), x, isEdge && isY2 ? y : crispEdgeY($$.state.height));
1877
- }
1878
- }
1879
- else {
1880
- painter.traceLine(x, isEdge && hasValueCoordinate ? y : crispEdgeY(0), x, crispEdgeY($$.state.height));
1881
- if (hasValueCoordinate &&
1882
- $$.config.grid_focus_y &&
1883
- !$$.config.tooltip_grouped) {
1884
- const isY2 = $$.axis?.getId(focus.id) === "y2";
1885
- painter.traceLine(isEdge && isY2 ? x : crispEdgeX(0), y, isEdge && !isY2 ? x : crispEdgeX($$.state.width), y);
1886
- }
1887
- }
1888
- }, {
1889
- lineDash: style.focusGrid.dashArray,
1890
- lineWidth: style.focusGrid.lineWidth,
1891
- stroke: style.focusGrid.lineColor
1892
- });
1893
- }
1894
- }
1895
- if ($$.config.point_show && !$$.state.canvasFocusMainRedraw) {
1896
- selectedData
1897
- .filter(d => d &&
1898
- hasCanvasDrawableValue($$, d) &&
1899
- isCanvasPointType($$, d) &&
1900
- isCanvasRenderableTarget($$, { id: d.id }))
1901
- .forEach(d => {
1902
- const pointType = getPointType($$, { id: d.id });
1903
- const { x, y } = getRenderDataPoint($$, d);
1904
- const baseR = $$.pointR?.(d) ?? 2.5;
1905
- const r = $$.pointExpandedR?.(d) ?? (baseR * 1.75);
1906
- const overColor = getCanvasOverColor($$, d);
1907
- const color = overColor || $$.color(d.id);
1908
- const fill = overColor ||
1909
- style.focusPoint.fill ||
1910
- style.shape.pointFillColor ||
1911
- color;
1912
- const stroke = overColor || style.focusPoint.stroke || color;
1913
- const lineWidth = style.focusPoint.lineWidth;
1914
- const alpha = getPointOpacity($$, d);
1915
- const hasStroke = !!stroke && (lineWidth ?? 0) > 0;
1916
- const mergeSameColorStroke = pointType === "circle" &&
1917
- hasStroke &&
1918
- isNumber(alpha) &&
1919
- alpha < 1 &&
1920
- fill === stroke;
1921
- if (!isFiniteCanvasCoordinate(x, y)) {
1922
- return;
1923
- }
1924
- drawPointPattern(painter, pointType, x, y, mergeSameColorStroke ? r + (lineWidth || 0) / 2 : r, hasStroke && !mergeSameColorStroke ?
1925
- { fill, lineWidth, stroke, alpha } :
1926
- { fill, alpha }, baseR);
1927
- });
1928
- }
1929
- this.drawFocusLabels($$, selectedData);
1930
- });
1931
- }
1932
- }
1933
-
579
+ * Draw axis-based chart shapes on canvas.
580
+ * @private
581
+ */
582
+ var CanvasRenderer = class {
583
+ engine;
584
+ theme;
585
+ painter;
586
+ labelImageCache = /* @__PURE__ */ new Map();
587
+ backgroundImageCache = /* @__PURE__ */ new Map();
588
+ backgroundClassStyleCache = /* @__PURE__ */ new Map();
589
+ /**
590
+ * Constructor.
591
+ * @param {CanvasEngine} engine Canvas drawing engine
592
+ * @param {CanvasTheme} theme Canvas theme resolver
593
+ * @private
594
+ */
595
+ constructor(engine, theme) {
596
+ this.engine = engine;
597
+ this.theme = theme;
598
+ this.painter = new CanvasPainter(engine.ctx);
599
+ }
600
+ /**
601
+ * Get the drawing context for the main canvas.
602
+ * @returns {CanvasRenderingContext2D} Canvas drawing context
603
+ * @private
604
+ */
605
+ get ctx() {
606
+ return this.painter.context;
607
+ }
608
+ /**
609
+ * Run renderer draw calls on another canvas context.
610
+ * @param {CanvasRenderingContext2D} ctx Canvas drawing context
611
+ * @param {function} draw Draw callback
612
+ * @private
613
+ */
614
+ withContext(ctx, draw) {
615
+ this.painter.withContext(ctx, draw);
616
+ }
617
+ /**
618
+ * Release renderer caches that can hold chart or DOM references.
619
+ * @private
620
+ */
621
+ destroy() {
622
+ const clearImageHandler = (entry) => {
623
+ entry.image.onload = null;
624
+ entry.image.onerror = null;
625
+ };
626
+ this.labelImageCache.forEach(clearImageHandler);
627
+ this.backgroundImageCache.forEach(clearImageHandler);
628
+ this.labelImageCache.clear();
629
+ this.backgroundImageCache.clear();
630
+ this.backgroundClassStyleCache.clear();
631
+ }
632
+ /**
633
+ * Get cached image for data label.
634
+ * @param {string} url Image URL
635
+ * @param {object} $$ ChartInternal instance
636
+ * @returns {object|null} Cached image entry
637
+ * @private
638
+ */
639
+ getLabelImage(url, $$) {
640
+ if (!url || !win.Image) return null;
641
+ let entry = this.labelImageCache.get(url);
642
+ if (!entry) {
643
+ const nextEntry = {
644
+ image: new win.Image(),
645
+ loaded: false,
646
+ loading: true
647
+ };
648
+ entry = nextEntry;
649
+ this.labelImageCache.set(url, entry);
650
+ nextEntry.image.onload = () => {
651
+ nextEntry.loaded = true;
652
+ nextEntry.loading = false;
653
+ $$.redraw?.();
654
+ };
655
+ nextEntry.image.onerror = () => {
656
+ nextEntry.loaded = false;
657
+ nextEntry.loading = false;
658
+ };
659
+ nextEntry.image.src = url;
660
+ }
661
+ return entry;
662
+ }
663
+ /**
664
+ * Get cached chart background image.
665
+ * @param {string} url Image URL
666
+ * @param {object} $$ ChartInternal instance
667
+ * @returns {object|null} Cached image entry
668
+ * @private
669
+ */
670
+ getBackgroundImage(url, $$) {
671
+ if (!url || !win.Image) return null;
672
+ let entry = this.backgroundImageCache.get(url);
673
+ if (!entry) {
674
+ const nextEntry = {
675
+ image: new win.Image(),
676
+ loaded: false,
677
+ loading: true
678
+ };
679
+ entry = nextEntry;
680
+ this.backgroundImageCache.set(url, entry);
681
+ nextEntry.image.onload = () => {
682
+ nextEntry.loaded = true;
683
+ nextEntry.loading = false;
684
+ $$.renderCanvasFrame?.(void 0, null, false);
685
+ };
686
+ nextEntry.image.onerror = () => {
687
+ nextEntry.loaded = false;
688
+ nextEntry.loading = false;
689
+ };
690
+ nextEntry.image.src = url;
691
+ }
692
+ return entry;
693
+ }
694
+ /**
695
+ * Resolve background.class CSS values that canvas can reasonably mirror.
696
+ * @param {object} $$ ChartInternal instance
697
+ * @param {string} className Background class name
698
+ * @returns {object} Class style
699
+ * @private
700
+ */
701
+ getBackgroundClassStyle($$, className) {
702
+ if (!className || !win.document) return {};
703
+ const cached = this.backgroundClassStyleCache.get(className);
704
+ if (cached) return cached;
705
+ const probe = win.document.createElement("div");
706
+ probe.className = className;
707
+ probe.style.cssText = "position:absolute;visibility:hidden;pointer-events:none;width:1px;height:1px;";
708
+ $$.$el.chart.node().appendChild(probe);
709
+ const style = win.getComputedStyle(probe);
710
+ const opacity = parseFloat(style.opacity);
711
+ const result = {
712
+ opacity: Number.isFinite(opacity) ? opacity : void 0,
713
+ transform: style.transform || void 0
714
+ };
715
+ probe.remove();
716
+ this.backgroundClassStyleCache.set(className, result);
717
+ return result;
718
+ }
719
+ /**
720
+ * Draw configured chart background behind all canvas layers.
721
+ * @param {object} $$ ChartInternal instance
722
+ * @private
723
+ */
724
+ drawBackground($$) {
725
+ const bg = $$.config.background;
726
+ if (!bg?.imgUrl && !bg?.color) return;
727
+ const { ctx, painter } = this;
728
+ const { current, margin, width, height } = $$.state;
729
+ const classStyle = this.getBackgroundClassStyle($$, bg.class);
730
+ painter.withState(() => {
731
+ if (classStyle.opacity !== void 0) ctx.globalAlpha *= classStyle.opacity;
732
+ if (bg.imgUrl) {
733
+ const entry = this.getBackgroundImage(bg.imgUrl, $$);
734
+ if (entry?.loaded) {
735
+ const offset = getBackgroundImageOffset($$);
736
+ const rect = getPreservedAspectRatioRect(entry.image, current.width, current.height);
737
+ ctx.translate(offset.x, offset.y);
738
+ applyCssMatrixTransform(ctx, classStyle.transform);
739
+ ctx.drawImage(entry.image, rect.x, rect.y, rect.w, rect.h);
740
+ }
741
+ } else if (bg.color) {
742
+ applyCssMatrixTransform(ctx, classStyle.transform);
743
+ painter.fillRect({
744
+ x: margin.left,
745
+ y: margin.top,
746
+ w: width,
747
+ h: height
748
+ }, { fill: bg.color });
749
+ }
750
+ });
751
+ }
752
+ /**
753
+ * Draw data label image when loaded, or queue it for loading.
754
+ * @param {object} $$ ChartInternal instance
755
+ * @param {object} d Data row
756
+ * @param {string} text Label text
757
+ * @param {number} x Label x coordinate
758
+ * @param {number} y Label y coordinate
759
+ * @returns {object} Adjusted text position
760
+ * @private
761
+ */
762
+ drawLabelImage($$, d, text, x, y) {
763
+ const option = getLabelImageOption($$, d);
764
+ if (!option?.url) return {
765
+ x,
766
+ y
767
+ };
768
+ const url = getLabelImageUrl(option, d);
769
+ const position = getLabelImagePosition($$, option, text, x, y, d);
770
+ const entry = this.getLabelImage(url, $$);
771
+ if (entry?.loaded) this.ctx.drawImage(entry.image, position.x, position.y, option.width, option.height);
772
+ return {
773
+ x: position.textX,
774
+ y: position.textY
775
+ };
776
+ }
777
+ /**
778
+ * Draw a data label at a resolved canvas position.
779
+ * @param {object} $$ ChartInternal instance
780
+ * @param {object} d Data row
781
+ * @param {string} text Label text
782
+ * @param {number} x Label x coordinate
783
+ * @param {number} y Label y coordinate
784
+ * @private
785
+ */
786
+ drawDataLabel($$, d, text, x, y) {
787
+ const { painter, theme: { style } } = this;
788
+ ({x, y} = this.drawLabelImage($$, d, text, x, y));
789
+ if ($$.config.data_labels.rotate) {
790
+ const position = getRotatedLabelPosition($$, d, x, y);
791
+ x = position.x;
792
+ y = position.y;
793
+ this.ctx.textAlign = position.textAlign;
794
+ }
795
+ this.ctx.fillStyle = getLabelColor($$, d, style.label.color);
796
+ drawLabelDecorations($$, painter, d, text, x, y);
797
+ painter.textLines(text, x, y, { angle: $$.config.data_labels.rotate });
798
+ }
799
+ /**
800
+ * Redraw focused point data labels over the hover overlay.
801
+ * @param {object} $$ ChartInternal instance
802
+ * @param {Array} selectedData Focused data rows
803
+ * @private
804
+ */
805
+ drawFocusLabels($$, selectedData) {
806
+ if (!$$.hasDataLabel?.()) return;
807
+ const { ctx, theme: { style } } = this;
808
+ const rows = selectedData.filter((d) => d && hasCanvasDrawableValue($$, d) && isCanvasPointType($$, d) && isCanvasRenderableTarget($$, { id: d.id }));
809
+ const texts = { size: () => rows.length };
810
+ if (!rows.length) return;
811
+ ctx.font = style.label.font;
812
+ rows.forEach((d) => {
813
+ const text = getLabelText($$, d);
814
+ let { x, y } = getRenderDataPoint($$, d);
815
+ if (!text) return;
816
+ ({x, y} = getPointLabelAnchor($$, ctx, d, x, y));
817
+ x += getLabelPosition($$, d, "x", texts);
818
+ y += getLabelPosition($$, d, "y", texts);
819
+ if (!isFiniteCanvasCoordinate(x, y)) return;
820
+ this.drawDataLabel($$, d, text, x, y);
821
+ });
822
+ }
823
+ /**
824
+ * Draw all supported canvas shape layers.
825
+ * @param {object} $$ ChartInternal instance
826
+ * @param {object} shape Cached draw shape object
827
+ * @param {Array} focusData Focused data rows
828
+ * @private
829
+ */
830
+ draw($$, shape, focusData) {
831
+ if ($$.state.hasTreemap) {
832
+ this.drawTreemaps($$);
833
+ return;
834
+ }
835
+ const { margin, width, height } = $$.state;
836
+ const drawLineLayers = () => {
837
+ if (!$$.config.area_front) this.drawAreas($$, shape, focusData);
838
+ this.drawLines($$, shape);
839
+ if ($$.config.area_front) this.drawAreas($$, shape, focusData);
840
+ this.drawCircles($$, shape, focusData);
841
+ };
842
+ const drawContent = () => {
843
+ if (!$$.config.bar_front) this.drawBars($$, shape, focusData);
844
+ this.drawCandlesticks($$, shape, focusData);
845
+ drawLineLayers();
846
+ if ($$.config.bar_front) this.drawBars($$, shape, focusData);
847
+ this.drawSelections($$, shape);
848
+ this.drawLabels($$, shape);
849
+ };
850
+ $$.config.clipPath === false ? drawContent() : this.painter.clipRect({
851
+ x: margin.left,
852
+ y: margin.top,
853
+ w: width,
854
+ h: height
855
+ }, drawContent);
856
+ }
857
+ /**
858
+ * Draw the canvas subchart overview and brush selection.
859
+ * @param {object} $$ ChartInternal instance
860
+ * @param {object} _shape Cached main shape object
861
+ * @private
862
+ */
863
+ drawSubchart($$, _shape) {
864
+ const { config, state } = $$;
865
+ if (!config.subchart_show || !state.hasAxis || state.width2 <= 0 || state.height2 <= 0) return;
866
+ const { ctx, painter, theme: { style } } = this;
867
+ const { margin2, width2, height2 } = state;
868
+ const rect = {
869
+ x: margin2.left,
870
+ y: margin2.top,
871
+ w: width2,
872
+ h: height2
873
+ };
874
+ $$.withSubchartTypeContext(() => {
875
+ const targets = $$.filterTargetsToShow().filter(isCanvasRenderableTarget.bind(null, $$));
876
+ const shape = $$.getDrawShape();
877
+ $$.updateSubchartYDomain?.(targets);
878
+ painter.withState(() => {
879
+ painter.clipRect(rect, () => {
880
+ painter.withTranslation(rect.x, rect.y, () => {
881
+ const areaTargets = targets.filter(isCanvasAreaType.bind(null, $$));
882
+ const areaIndices = getCanvasShapeIndices($$, shape, TYPE.AREA, isCanvasAreaType.bind(null, $$));
883
+ for (const target of areaTargets) {
884
+ if (!target.values.some(hasCanvasDrawableValue.bind(null, $$))) continue;
885
+ ctx.globalAlpha = style.shape.areaOpacity * getCanvasTargetFocusOpacity($$, target);
886
+ ctx.fillStyle = $$.color(target.id);
887
+ drawCanvasArea($$, target, areaIndices, painter, true);
888
+ }
889
+ ctx.globalAlpha = 1;
890
+ const barTargets = targets.filter(isCanvasBarType.bind(null, $$));
891
+ const barIndices = getCanvasShapeIndices($$, shape, TYPE.BAR, isCanvasBarType.bind(null, $$));
892
+ const getBarPoints = $$.generateGetBarPoints?.(barIndices, true);
893
+ if (getBarPoints) {
894
+ for (const target of barTargets) {
895
+ ctx.globalAlpha = style.shape.barOpacity * getCanvasTargetFocusOpacity($$, target);
896
+ ctx.fillStyle = $$.color(target.id);
897
+ target.values.forEach((d, i) => {
898
+ if (!hasCanvasDrawableValue($$, d)) return;
899
+ const geometry = getCanvasBarGeometry($$, getBarPoints, d, i);
900
+ const fill = $$.getSubchartCandlestickBarColor?.(d, true) || ctx.fillStyle;
901
+ geometry && painter.fillRect(geometry.rect, { fill });
902
+ });
903
+ }
904
+ ctx.globalAlpha = 1;
905
+ }
906
+ const candlestickTargets = targets.filter(isCanvasCandlestickType.bind(null, $$));
907
+ const candlestickIndices = getCanvasShapeIndices($$, shape, TYPE.CANDLESTICK, isCanvasCandlestickType.bind(null, $$));
908
+ const getCandlestickPoints = $$.generateGetCandlestickPoints?.(candlestickIndices, true);
909
+ if (getCandlestickPoints) {
910
+ ctx.lineWidth = style.shape.candlestickLineWidth;
911
+ for (const target of candlestickTargets) {
912
+ const targetOpacity = getCanvasTargetFocusOpacity($$, target);
913
+ ctx.globalAlpha = targetOpacity;
914
+ target.values.forEach((d, i) => {
915
+ const value = $$.getCandlestickData?.(d);
916
+ const geometry = value && getCanvasCandlestickGeometry($$, getCandlestickPoints, d, i);
917
+ if (!geometry) return;
918
+ const color = getCandlestickColor($$, { id: target.id }, value);
919
+ ctx.strokeStyle = color;
920
+ ctx.fillStyle = color;
921
+ painter.strokePath(() => {
922
+ painter.traceLine(geometry.wickStart[0], geometry.wickStart[1], geometry.wickEnd[0], geometry.wickEnd[1]);
923
+ });
924
+ painter.fillRect(geometry.body, { fill: ctx.fillStyle });
925
+ });
926
+ }
927
+ }
928
+ const lineTargets = targets.filter(isCanvasLineType.bind(null, $$));
929
+ const lineIndices = getCanvasShapeIndices($$, shape, TYPE.LINE, isCanvasLineType.bind(null, $$));
930
+ ctx.globalAlpha = 1;
931
+ for (const target of lineTargets) {
932
+ if (!target.values.some(hasCanvasDrawableValue.bind(null, $$))) continue;
933
+ ctx.globalAlpha = getCanvasTargetFocusOpacity($$, target);
934
+ ctx.lineWidth = isCanvasTargetFocused($$, target) ? style.shape.lineFocusedWidth : style.shape.lineWidth;
935
+ ctx.strokeStyle = $$.color(target.id);
936
+ drawCanvasLine($$, target, lineIndices, painter, true);
937
+ }
938
+ ctx.globalAlpha = 1;
939
+ if (config.point_show && !$$.isPointFocusOnly?.()) {
940
+ const cy = $$.updateCircleY?.(true);
941
+ const cx = $$.subxx?.bind($$);
942
+ if (cx && cy) {
943
+ for (const target of targets) {
944
+ if (!isCanvasScatterType($$, target) && !isCanvasBubbleType($$, target)) continue;
945
+ const color = $$.color(target.id);
946
+ const pointFill = style.shape.pointFillColor || color;
947
+ const pointStroke = style.shape.pointStrokeColor || color;
948
+ const pointLineWidth = pointStroke ? style.shape.pointLineWidth ?? 1 : 0;
949
+ const pointStyle = pointStroke && pointLineWidth > 0 ? {
950
+ fill: pointFill,
951
+ stroke: pointStroke,
952
+ lineWidth: pointLineWidth
953
+ } : { fill: pointFill };
954
+ ctx.globalAlpha = getPointOpacity($$, target) * getCanvasTargetFocusOpacity($$, target);
955
+ target.values.forEach((d, i) => {
956
+ if (!hasCanvasDrawableValue($$, d)) return;
957
+ const x = config.axis_rotated ? cy(d, i) : cx(d);
958
+ const y = config.axis_rotated ? cx(d) : cy(d, i);
959
+ const r = Math.min(getTargetPointRadius($$, target, d), 3);
960
+ if (isFiniteCanvasCoordinate(x, y)) drawPointPattern(painter, "circle", x, y, r, pointStyle);
961
+ });
962
+ }
963
+ ctx.globalAlpha = 1;
964
+ }
965
+ }
966
+ });
967
+ });
968
+ this.drawSubchartBrush($$);
969
+ });
970
+ });
971
+ }
972
+ /**
973
+ * Draw current subchart brush selection.
974
+ * @param {object} $$ ChartInternal instance
975
+ * @private
976
+ */
977
+ drawSubchartBrush($$) {
978
+ const { config, scale, state } = $$;
979
+ const domain = state.domain;
980
+ if (!config.subchart_show || config.subchart_brush_enabled === false || !domain?.length || !scale.subX) return;
981
+ const { margin2, width2, height2 } = state;
982
+ const p0 = scale.subX(domain[0]);
983
+ const p1 = scale.subX(domain[1]);
984
+ const axisLength = config.axis_rotated ? height2 : width2;
985
+ const extent = $$.axis?.getExtent?.();
986
+ const extentValues = Array.isArray(extent) && extent.length >= 2 && extent.every(Number.isFinite) ? extent.slice(0, 2) : [0, axisLength];
987
+ const extentStart = Math.max(0, Math.min(axisLength, Math.min(...extentValues)));
988
+ const extentEnd = Math.max(0, Math.min(axisLength, Math.max(...extentValues)));
989
+ const start = Math.max(extentStart, Math.min(extentEnd, Math.min(p0, p1)));
990
+ const end = Math.max(extentStart, Math.min(extentEnd, Math.max(p0, p1)));
991
+ const size = end - start;
992
+ if (size <= 0) return;
993
+ const rect = config.axis_rotated ? {
994
+ x: margin2.left,
995
+ y: margin2.top + start,
996
+ w: width2,
997
+ h: size
998
+ } : {
999
+ x: margin2.left + start,
1000
+ y: margin2.top,
1001
+ w: size,
1002
+ h: height2
1003
+ };
1004
+ this.painter.fillRect(rect, {
1005
+ fill: this.theme.style.subchartBrush.fill,
1006
+ alpha: this.theme.style.subchartBrush.opacity
1007
+ });
1008
+ if (config.subchart_showHandle) {
1009
+ this.drawSubchartBrushHandle($$, start, "start");
1010
+ this.drawSubchartBrushHandle($$, end, "end");
1011
+ }
1012
+ }
1013
+ /**
1014
+ * Draw a visible subchart brush resize handle.
1015
+ * @param {object} $$ ChartInternal instance
1016
+ * @param {number} coord Brush handle coordinate
1017
+ * @param {string} type Brush handle side
1018
+ * @private
1019
+ */
1020
+ drawSubchartBrushHandle($$, coord, type) {
1021
+ const { config, state: { margin2, width2, height2 } } = $$;
1022
+ const { ctx, painter, theme: { style } } = this;
1023
+ const isRotated = config.axis_rotated;
1024
+ const fill = style.subchartBrush.handleFill;
1025
+ const stroke = style.subchartBrush.handleStroke;
1026
+ const x = isRotated ? margin2.left + width2 / 2 : margin2.left + coord;
1027
+ const y = isRotated ? margin2.top + coord : margin2.top + height2 / 2;
1028
+ const path = getSubchartBrushHandlePath(isRotated ? "y" : "x", type);
1029
+ if (!path) return;
1030
+ painter.withState(() => {
1031
+ ctx.translate(x, y);
1032
+ ctx.fillStyle = fill;
1033
+ ctx.strokeStyle = stroke;
1034
+ ctx.lineWidth = style.subchartBrush.handleLineWidth;
1035
+ ctx.globalAlpha = style.subchartBrush.handleOpacity;
1036
+ ctx.fill(path);
1037
+ ctx.globalAlpha = 1;
1038
+ ctx.stroke(path);
1039
+ });
1040
+ }
1041
+ /**
1042
+ * Draw bar shapes on canvas.
1043
+ * @param {object} $$ ChartInternal instance
1044
+ * @param {object} shape Cached draw shape object
1045
+ * @param {Array} focusData Focused data rows
1046
+ * @private
1047
+ */
1048
+ drawBars($$, shape, focusData) {
1049
+ const { ctx, painter, theme: { style } } = this;
1050
+ const isBar = isCanvasBarType.bind(null, $$);
1051
+ const isExpanded = getExpandedFocusMatcher($$, focusData, isCanvasBarType);
1052
+ const targets = $$.filterTargetsToShow().filter(isBar).filter(isCanvasRenderableTarget.bind(null, $$));
1053
+ const getPoints = $$.generateGetBarPoints(getCanvasShapeIndices($$, shape, TYPE.BAR, isBar), false);
1054
+ const { margin } = $$.state;
1055
+ const getRadius = getBarRadiusResolver($$);
1056
+ const stackingRadiusSet = getRadius ? getStackingBarRadiusSet($$) : /* @__PURE__ */ new Set();
1057
+ if (!getPoints) return;
1058
+ painter.withTranslation(margin.left, margin.top, () => {
1059
+ for (const target of targets) {
1060
+ const range = getCanvasTargetVisibleRange($$, target);
1061
+ const connectLineType = getBarConnectLineType($$, target.id);
1062
+ const targetOpacity = getCanvasTargetFocusOpacity($$, target);
1063
+ const connectLineBoxes = [];
1064
+ const bars = [];
1065
+ for (let i = range.start; i < range.end; i++) {
1066
+ const d = target.values[i];
1067
+ if (!hasCanvasDrawableValue($$, d)) continue;
1068
+ const geometry = getCanvasBarGeometry($$, getPoints, d, i);
1069
+ if (!geometry) continue;
1070
+ const { points, rect } = geometry;
1071
+ const radiusInfo = getBarRadiusInfo($$, d, points, getRadius, stackingRadiusSet, $$.isStackingRadiusData?.bind($$));
1072
+ bars.push({
1073
+ d,
1074
+ points,
1075
+ rect,
1076
+ radiusInfo
1077
+ });
1078
+ if (connectLineType) connectLineBoxes.push(getBarConnectLineBox($$, points, radiusInfo));
1079
+ }
1080
+ if (connectLineType && style.shape.barConnectLineWidth > 0) drawBarConnectLine($$, painter, connectLineType, connectLineBoxes, targetOpacity);
1081
+ for (const { d, rect, radiusInfo } of bars) {
1082
+ const overColor = getCanvasOverColor($$, getFocusedCanvasDatum(focusData, d));
1083
+ const color = overColor || $$.color(target.id);
1084
+ const alpha = (isExpanded(d) ? style.shape.barExpandedOpacity : style.shape.barOpacity) * targetOpacity;
1085
+ ctx.fillStyle = overColor ? color : getCanvasLinearGradientFill($$, ctx, target, "bar", rect, color);
1086
+ getRadius ? painter.fillRoundRect(rect, radiusInfo.corners, { alpha }) : painter.fillRect(rect, { alpha });
1087
+ if (style.shape.barLineWidth > 0) {
1088
+ ctx.strokeStyle = style.shape.barStrokeColor;
1089
+ ctx.lineWidth = style.shape.barLineWidth;
1090
+ getRadius ? painter.strokeRoundRect(rect, radiusInfo.corners, { alpha: style.shape.barOpacity * targetOpacity }) : painter.strokeRect(rect, { alpha: style.shape.barOpacity * targetOpacity });
1091
+ }
1092
+ }
1093
+ }
1094
+ });
1095
+ }
1096
+ /**
1097
+ * Draw candlestick shapes on canvas.
1098
+ * @param {object} $$ ChartInternal instance
1099
+ * @param {object} shape Cached draw shape object
1100
+ * @param {Array} focusData Focused data rows
1101
+ * @private
1102
+ */
1103
+ drawCandlesticks($$, shape, focusData) {
1104
+ const { ctx, painter, theme: { style } } = this;
1105
+ const isCandlestick = isCanvasCandlestickType.bind(null, $$);
1106
+ const isExpanded = getExpandedFocusMatcher($$, focusData, isCanvasCandlestickType);
1107
+ const targets = $$.filterTargetsToShow().filter(isCandlestick).filter(isCanvasRenderableTarget.bind(null, $$));
1108
+ const getPoints = $$.generateGetCandlestickPoints?.(getCanvasShapeIndices($$, shape, TYPE.CANDLESTICK, isCandlestick), false);
1109
+ const { margin } = $$.state;
1110
+ if (!getPoints) return;
1111
+ painter.withTranslation(margin.left, margin.top, () => {
1112
+ ctx.lineWidth = style.shape.candlestickLineWidth;
1113
+ for (const target of targets) {
1114
+ const range = getCanvasTargetVisibleRange($$, target);
1115
+ const targetOpacity = getCanvasTargetFocusOpacity($$, target);
1116
+ const lineWidth = style.shape.candlestickLineWidth;
1117
+ const strokeColor = style.shape.candlestickStrokeColor || "#000";
1118
+ for (let i = range.start; i < range.end; i++) {
1119
+ const d = target.values[i];
1120
+ const value = $$.getCandlestickData?.(d);
1121
+ if (!value) continue;
1122
+ const geometry = getCanvasCandlestickGeometry($$, getPoints, d, i);
1123
+ if (!geometry) continue;
1124
+ const { body: rect, wickStart, wickEnd } = geometry;
1125
+ ctx.fillStyle = getCanvasOverColor($$, getFocusedCanvasDatum(focusData, d)) || getCandlestickColor($$, d, value);
1126
+ ctx.strokeStyle = strokeColor;
1127
+ ctx.globalAlpha = targetOpacity;
1128
+ lineWidth > 0 && painter.strokePath(() => {
1129
+ painter.traceLine(wickStart[0], wickStart[1], wickEnd[0], wickEnd[1]);
1130
+ });
1131
+ painter.fillRect(rect, { alpha: (isExpanded(d) ? style.shape.candlestickExpandedOpacity : 1) * targetOpacity });
1132
+ lineWidth > 0 && painter.strokeRect(rect, {
1133
+ alpha: targetOpacity,
1134
+ lineWidth,
1135
+ stroke: strokeColor
1136
+ });
1137
+ }
1138
+ }
1139
+ ctx.globalAlpha = 1;
1140
+ });
1141
+ }
1142
+ /**
1143
+ * Check whether focus data requires a full redraw to reflect _expanded_ shape styles.
1144
+ * @param {object} $$ ChartInternal instance
1145
+ * @param {Array} selectedData Focused data rows
1146
+ * @returns {boolean} Whether focused shapes need a main redraw
1147
+ * @private
1148
+ */
1149
+ hasExpandedShapeFocus($$, selectedData) {
1150
+ return !!selectedData?.some((d) => d && (isCanvasBarType($$, d) || isCanvasCandlestickType($$, d)));
1151
+ }
1152
+ /**
1153
+ * Draw line strokes on canvas.
1154
+ * @param {object} $$ ChartInternal instance
1155
+ * @param {object} shape Cached draw shape object
1156
+ * @private
1157
+ */
1158
+ drawLines($$, shape) {
1159
+ const { ctx, painter, theme: { style } } = this;
1160
+ const isLine = isCanvasLineType.bind(null, $$);
1161
+ const targets = $$.filterTargetsToShow().filter(isLine).filter(isCanvasRenderableTarget.bind(null, $$));
1162
+ const indices = getCanvasShapeIndices($$, shape, TYPE.LINE, isLine);
1163
+ const { margin } = $$.state;
1164
+ if (!$$.generateGetLinePoints) return;
1165
+ painter.withTranslation(margin.left, margin.top, () => {
1166
+ for (const target of targets) {
1167
+ const visibleTarget = getVisibleCanvasTarget($$, target);
1168
+ if (!visibleTarget.values.some(hasCanvasDrawableValue.bind(null, $$))) continue;
1169
+ ctx.globalAlpha = getCanvasTargetFocusOpacity($$, target);
1170
+ ctx.lineWidth = isCanvasTargetFocused($$, target) ? style.shape.lineFocusedWidth : style.shape.lineWidth;
1171
+ ctx.strokeStyle = $$.color(target.id);
1172
+ $$.config.data_regions?.[visibleTarget.id] ? drawCanvasLineWithDataRegions($$, visibleTarget, painter) : drawCanvasLine($$, visibleTarget, indices, painter);
1173
+ }
1174
+ ctx.globalAlpha = 1;
1175
+ });
1176
+ }
1177
+ /**
1178
+ * Draw area fills on canvas.
1179
+ * @param {object} $$ ChartInternal instance
1180
+ * @param {object} shape Cached draw shape object
1181
+ * @param {Array} focusData Focused data rows
1182
+ * @private
1183
+ */
1184
+ drawAreas($$, shape, focusData) {
1185
+ const { ctx, painter, theme: { style } } = this;
1186
+ const isArea = isCanvasAreaType.bind(null, $$);
1187
+ const targets = $$.filterTargetsToShow().filter(isArea).filter(isCanvasRenderableTarget.bind(null, $$));
1188
+ const indices = getCanvasShapeIndices($$, shape, TYPE.AREA, isArea);
1189
+ const { margin } = $$.state;
1190
+ if (!$$.generateGetAreaPoints) return;
1191
+ painter.withTranslation(margin.left, margin.top, () => {
1192
+ for (const target of targets) {
1193
+ const visibleTarget = getVisibleCanvasTarget($$, target);
1194
+ if (!visibleTarget.values.some(hasCanvasDrawableValue.bind(null, $$))) continue;
1195
+ const color = getCanvasRenderColor($$, visibleTarget, focusData);
1196
+ ctx.globalAlpha = style.shape.areaOpacity * getCanvasTargetFocusOpacity($$, target);
1197
+ ctx.fillStyle = getCanvasLinearGradientFill($$, ctx, visibleTarget, "area", $$.config.area_linearGradient ? getCanvasAreaBounds($$, target, indices) : null, color);
1198
+ drawCanvasArea($$, visibleTarget, indices, painter);
1199
+ }
1200
+ ctx.globalAlpha = 1;
1201
+ });
1202
+ }
1203
+ /**
1204
+ * Draw point circles on canvas.
1205
+ * @param {object} $$ ChartInternal instance
1206
+ * @param {object} shape Cached draw shape object
1207
+ * @param {Array} focusData Focused data rows
1208
+ * @private
1209
+ */
1210
+ drawCircles($$, shape, focusData) {
1211
+ if (!$$.config.point_show || $$.isPointFocusOnly?.()) return;
1212
+ const { ctx, painter, theme: { style } } = this;
1213
+ const { cx, cy } = shape.pos;
1214
+ const { margin } = $$.state;
1215
+ const isExpanded = getExpandedFocusMatcher($$, focusData, isCanvasPointType);
1216
+ const hasExpandedFocus = !!focusData?.some((d) => d && isCanvasPointType($$, d));
1217
+ if (!cx || !cy) return;
1218
+ painter.withTranslation(margin.left, margin.top, () => {
1219
+ for (const target of $$.filterTargetsToShow()) {
1220
+ if (!isCanvasPointType($$, target) || !isCanvasRenderableTarget($$, target) || isCanvasLineType($$, target) && !shouldDrawPoints($$, target)) continue;
1221
+ const color = $$.color(target.id);
1222
+ const pointType = getPointType($$, target);
1223
+ const targetOpacity = getCanvasTargetFocusOpacity($$, target);
1224
+ const hasGradient = !!$$.config.point_radialGradient;
1225
+ const cullDenseScatter = shouldCullDenseScatterPoints($$, target, pointType, hasGradient);
1226
+ const range = getCanvasTargetVisibleRange($$, target);
1227
+ const visibleCount = range.end - range.start;
1228
+ const occupancy = cullDenseScatter ? createCanvasPointOccupancyGrid($$.state.width, $$.state.height, getTargetPointRadius($$, target, target.values[range.start] || target.values[0])) : null;
1229
+ const pointAlpha = getPointOpacity($$, target) * targetOpacity;
1230
+ ctx.fillStyle = color;
1231
+ ctx.globalAlpha = pointAlpha;
1232
+ const pointFill = style.shape.pointFillColor || color;
1233
+ const pointStroke = style.shape.pointStrokeColor || color;
1234
+ const pointLineWidth = pointStroke ? style.shape.pointLineWidth ?? 1 : 0;
1235
+ const hasPointStroke = !!pointStroke && pointLineWidth > 0;
1236
+ const mergeSameColorStroke = hasPointStroke && pointType === "circle" && isNumber(pointAlpha) && pointAlpha < 1 && pointFill === pointStroke;
1237
+ if (!hasExpandedFocus && !hasGradient && pointType === "circle") {
1238
+ ctx.fillStyle = pointFill;
1239
+ if (hasPointStroke && !mergeSameColorStroke) {
1240
+ ctx.strokeStyle = pointStroke;
1241
+ ctx.lineWidth = pointLineWidth;
1242
+ }
1243
+ if (visibleCount > MAX_BATCHED_CIRCLE_POINTS) for (let i = range.start; i < range.end; i++) {
1244
+ const d = target.values[i];
1245
+ if (!hasCanvasDrawableValue($$, d)) continue;
1246
+ const baseR = getTargetPointRadius($$, target, d);
1247
+ const r = mergeSameColorStroke ? baseR + pointLineWidth / 2 : baseR;
1248
+ const x = cx(d, i);
1249
+ const y = cy(d, i);
1250
+ if (!isFiniteCanvasCoordinate(x, y)) continue;
1251
+ if (occupancy && !markCanvasPointOccupancy(occupancy, x, y)) continue;
1252
+ ctx.beginPath();
1253
+ ctx.arc(x, y, r, 0, Math.PI * 2);
1254
+ ctx.fill();
1255
+ hasPointStroke && !mergeSameColorStroke && ctx.stroke();
1256
+ }
1257
+ else {
1258
+ ctx.beginPath();
1259
+ for (let i = range.start; i < range.end; i++) {
1260
+ const d = target.values[i];
1261
+ if (!hasCanvasDrawableValue($$, d)) continue;
1262
+ const baseR = getTargetPointRadius($$, target, d);
1263
+ const r = mergeSameColorStroke ? baseR + pointLineWidth / 2 : baseR;
1264
+ const x = cx(d, i);
1265
+ const y = cy(d, i);
1266
+ if (!isFiniteCanvasCoordinate(x, y)) continue;
1267
+ painter.traceCircle(x, y, r);
1268
+ }
1269
+ ctx.fill();
1270
+ hasPointStroke && !mergeSameColorStroke && ctx.stroke();
1271
+ }
1272
+ } else for (let i = range.start; i < range.end; i++) {
1273
+ const d = target.values[i];
1274
+ if (!hasCanvasDrawableValue($$, d)) continue;
1275
+ const expanded = isExpanded(d);
1276
+ const baseR = getTargetPointRadius($$, target, d);
1277
+ const r = expanded ? $$.pointExpandedR?.(d) ?? baseR * 1.75 : baseR;
1278
+ const x = cx(d, i);
1279
+ const y = cy(d, i);
1280
+ const overColor = getCanvasOverColor($$, d);
1281
+ const renderColor = overColor || color;
1282
+ const renderPointFill = style.shape.pointFillColor || renderColor;
1283
+ const renderPointStroke = style.shape.pointStrokeColor || renderColor;
1284
+ const fill = expanded ? overColor || style.focusPoint.fill || renderPointFill : renderPointFill;
1285
+ const stroke = expanded ? overColor || style.focusPoint.stroke || renderColor : renderPointStroke;
1286
+ const lineWidth = expanded ? style.focusPoint.lineWidth : pointLineWidth;
1287
+ const alpha = getPointOpacity($$, d) * targetOpacity;
1288
+ const hasStroke = !!stroke && (lineWidth ?? 0) > 0;
1289
+ const mergeSameColorStroke = pointType === "circle" && hasStroke && isNumber(alpha) && alpha < 1 && fill === stroke;
1290
+ const drawR = mergeSameColorStroke ? r + (lineWidth || 0) / 2 : r;
1291
+ if (!isFiniteCanvasCoordinate(x, y)) continue;
1292
+ ctx.fillStyle = hasGradient ? getPointFillStyle($$, ctx, d, x, y, r, renderColor) : fill;
1293
+ ctx.globalAlpha = alpha;
1294
+ drawPointPattern(painter, pointType, x, y, drawR, hasStroke && !mergeSameColorStroke ? {
1295
+ fill: ctx.fillStyle,
1296
+ stroke,
1297
+ lineWidth
1298
+ } : void 0, baseR);
1299
+ }
1300
+ }
1301
+ });
1302
+ }
1303
+ /**
1304
+ * Draw selected canvas shapes.
1305
+ * @param {object} $$ ChartInternal instance
1306
+ * @param {object} shape Cached draw shape object
1307
+ * @private
1308
+ */
1309
+ drawSelections($$, shape) {
1310
+ const selectedData = $$.getCanvasSelectedData?.();
1311
+ if (!$$.config.data_selection_enabled || !selectedData?.length || $$.state.hasTreemap) return;
1312
+ const { ctx, painter, theme: { style } } = this;
1313
+ const { margin } = $$.state;
1314
+ const isBar = isCanvasBarType.bind(null, $$);
1315
+ const isCandlestick = isCanvasCandlestickType.bind(null, $$);
1316
+ const barPoints = selectedData.some(isBar) ? $$.generateGetBarPoints(getCanvasShapeIndices($$, shape, TYPE.BAR, isBar), false) : null;
1317
+ const candlestickPoints = selectedData.some(isCandlestick) ? $$.generateGetCandlestickPoints?.(getCanvasShapeIndices($$, shape, TYPE.CANDLESTICK, isCandlestick), false) : null;
1318
+ const getRadius = barPoints ? getBarRadiusResolver($$) : null;
1319
+ const stackingRadiusSet = getRadius ? getStackingBarRadiusSet($$) : /* @__PURE__ */ new Set();
1320
+ painter.withTranslation(margin.left, margin.top, () => {
1321
+ selectedData.forEach((d) => {
1322
+ const color = $$.color(d.id);
1323
+ if (isBar(d) && barPoints) {
1324
+ const geometry = getCanvasBarGeometry($$, barPoints, d, d.index);
1325
+ if (!geometry) return;
1326
+ const { points, rect } = geometry;
1327
+ const corners = getRadius ? getBarRadiusInfo($$, d, points, getRadius, stackingRadiusSet).corners : 0;
1328
+ getRadius ? painter.fillRoundRect(rect, corners, {
1329
+ fill: "#fff",
1330
+ alpha: .3
1331
+ }) : painter.fillRect(rect, {
1332
+ fill: "#fff",
1333
+ alpha: .3
1334
+ });
1335
+ getRadius ? painter.strokeRoundRect(rect, corners, {
1336
+ stroke: color,
1337
+ lineWidth: 2
1338
+ }) : painter.strokeRect(rect, {
1339
+ stroke: color,
1340
+ lineWidth: 2
1341
+ });
1342
+ } else if (isCandlestick(d) && candlestickPoints) {
1343
+ if (!$$.getCandlestickData?.(d)) return;
1344
+ const geometry = getCanvasCandlestickGeometry($$, candlestickPoints, d, d.index);
1345
+ if (!geometry) return;
1346
+ const { body, wickStart, wickEnd } = geometry;
1347
+ painter.strokePath(() => {
1348
+ painter.traceLine(wickStart[0], wickStart[1], wickEnd[0], wickEnd[1]);
1349
+ }, {
1350
+ stroke: color,
1351
+ lineWidth: 3
1352
+ });
1353
+ painter.fillRect(body, {
1354
+ fill: "#fff",
1355
+ alpha: .3
1356
+ });
1357
+ painter.strokeRect(body, {
1358
+ stroke: color,
1359
+ lineWidth: 2
1360
+ });
1361
+ } else if (isCanvasPointType($$, d)) {
1362
+ const pointType = getPointType($$, { id: d.id });
1363
+ const { x, y } = getRenderDataPoint($$, d);
1364
+ const r = $$.pointR?.(d) ?? 2.5;
1365
+ const selectR = $$.pointSelectR?.(d) ?? r * 2;
1366
+ if (!isFiniteCanvasCoordinate(x, y)) return;
1367
+ drawPointPattern(painter, "circle", x, y, selectR, {
1368
+ fill: style.selectedPoint.fill,
1369
+ lineWidth: style.selectedPoint.lineWidth,
1370
+ stroke: style.selectedPoint.stroke || color
1371
+ });
1372
+ if (!$$.isPointFocusOnly?.()) drawPointPattern(painter, pointType, x, y, r, {
1373
+ fill: style.shape.pointFillColor || color,
1374
+ stroke: style.shape.pointStrokeColor || color,
1375
+ lineWidth: style.shape.pointLineWidth ?? 1
1376
+ });
1377
+ }
1378
+ });
1379
+ ctx.globalAlpha = 1;
1380
+ });
1381
+ }
1382
+ /**
1383
+ * Draw data labels on canvas.
1384
+ * @param {object} $$ ChartInternal instance
1385
+ * @param {object} shape Cached draw shape object
1386
+ * @private
1387
+ */
1388
+ drawLabels($$, shape) {
1389
+ if (!$$.hasDataLabel?.()) return;
1390
+ const { ctx, painter, theme: { style } } = this;
1391
+ const { margin } = $$.state;
1392
+ const { cx, cy } = shape.pos;
1393
+ const barPoints = shape.indices[TYPE.BAR] ? $$.generateGetBarPoints(shape.indices[TYPE.BAR], false) : null;
1394
+ const candlestickPoints = $$.generateGetCandlestickPoints ? $$.generateGetCandlestickPoints(getCanvasShapeIndices($$, shape, TYPE.CANDLESTICK, isCanvasCandlestickType.bind(null, $$)), false) : null;
1395
+ const targets = $$.filterTargetsToShow().filter((target) => (isCanvasBarType($$, target) || isCanvasPointType($$, target) || isCanvasAreaType($$, target) || isCanvasCandlestickType($$, target)) && isCanvasRenderableTarget($$, target));
1396
+ const targetRows = [];
1397
+ let labelCount = 0;
1398
+ targets.forEach((target) => {
1399
+ const range = getCanvasTargetVisibleRange($$, target);
1400
+ const data = $$.labelishData(target);
1401
+ const rows = [];
1402
+ for (let i = 0; i < data.length; i++) {
1403
+ const d = data[i];
1404
+ const text = getLabelText($$, d);
1405
+ if (text && d.index >= range.start && d.index < range.end && hasCanvasDrawableValue($$, d)) rows.push({
1406
+ d,
1407
+ text
1408
+ });
1409
+ }
1410
+ labelCount += rows.length;
1411
+ rows.length && targetRows.push(rows);
1412
+ });
1413
+ const texts = { size: () => labelCount };
1414
+ painter.withTranslation(margin.left, margin.top, () => {
1415
+ ctx.font = style.label.font;
1416
+ ctx.textAlign = "center";
1417
+ targetRows.forEach((rows) => {
1418
+ for (const { d, text } of rows) {
1419
+ let x;
1420
+ let y;
1421
+ if (isCanvasBarType($$, d) && barPoints) {
1422
+ const geometry = getCanvasBarGeometry($$, barPoints, d, d.index);
1423
+ if (!geometry) continue;
1424
+ const { points, rect } = geometry;
1425
+ const value = $$.getBaseValue(d);
1426
+ const end = getRenderPoint($$, points[2]);
1427
+ const isNegative = value < 0;
1428
+ const labelHeight = getLabelDecorationBox(ctx, text, 0, 0).h;
1429
+ x = $$.config.axis_rotated && !$$.config.data_labels.centered ? end[0] + (isNegative ? -4 : 4) : rect.x + rect.w / 2;
1430
+ y = $$.config.data_labels.centered ? rect.y + rect.h / 2 : $$.config.axis_rotated ? rect.y + rect.h / 2 : end[1] + (isNegative ? labelHeight - 3 : -3);
1431
+ ctx.textAlign = $$.config.axis_rotated && !$$.config.data_labels.centered ? isNegative ? "right" : "left" : "center";
1432
+ ctx.textBaseline = $$.config.data_labels.centered ? "middle" : $$.config.axis_rotated ? "middle" : "alphabetic";
1433
+ } else if (isCanvasCandlestickType($$, d) && candlestickPoints) {
1434
+ const value = $$.getCandlestickData?.(d);
1435
+ const geometry = getCanvasCandlestickGeometry($$, candlestickPoints, d, d.index);
1436
+ const isUp = value?._isUp;
1437
+ if (!geometry) continue;
1438
+ const { wickEnd, wickStart } = geometry;
1439
+ const isInverted = $$.config[`axis_${$$.axis?.getId(d.id)}_inverted`];
1440
+ x = $$.config.axis_rotated ? isUp ? wickEnd[0] + 4 : wickStart[0] - 4 : wickStart[0];
1441
+ y = $$.config.axis_rotated ? wickStart[1] + 3 : isUp ? wickEnd[1] - 3 : wickStart[1] + 12;
1442
+ !$$.config.axis_rotated && isInverted && (y += 15 * (isUp ? 1 : -1));
1443
+ ctx.textAlign = $$.config.axis_rotated ? isUp ? "left" : "right" : "center";
1444
+ ctx.textBaseline = "alphabetic";
1445
+ } else if (cx && cy) {
1446
+ ({x, y} = getShapePoint(shape.pos, d, d.index));
1447
+ ({x, y} = getPointLabelAnchor($$, ctx, d, x, y));
1448
+ }
1449
+ x += getLabelPosition($$, d, "x", texts);
1450
+ y += getLabelPosition($$, d, "y", texts);
1451
+ if (!isFiniteCanvasCoordinate(x, y)) continue;
1452
+ this.drawDataLabel($$, d, text, x, y);
1453
+ }
1454
+ });
1455
+ });
1456
+ }
1457
+ /**
1458
+ * Draw empty-data label on canvas.
1459
+ * @param {object} $$ ChartInternal instance
1460
+ * @private
1461
+ */
1462
+ drawEmptyLabel($$) {
1463
+ const text = $$.config.data_empty_label_text;
1464
+ const targetsToShow = $$.filterTargetsToShow();
1465
+ if (!text || targetsToShow.length) return;
1466
+ const { margin, width, height } = $$.state;
1467
+ const { style } = this.theme;
1468
+ this.painter.text(String(text), margin.left + width / 2, margin.top + height / 2, {
1469
+ fill: style.emptyLabel.color,
1470
+ font: style.emptyLabel.font,
1471
+ textAlign: "center",
1472
+ textBaseline: "middle"
1473
+ });
1474
+ }
1475
+ /**
1476
+ * Draw drag-zoom brush overlay.
1477
+ * @param {object} $$ ChartInternal instance
1478
+ * @param {number} start Start coordinate
1479
+ * @param {number} end End coordinate
1480
+ * @private
1481
+ */
1482
+ drawZoomBrush($$, start, end) {
1483
+ const { axis_rotated } = $$.config;
1484
+ const { height, margin, width } = $$.state;
1485
+ const brushStart = Math.min(start, end);
1486
+ const brushSize = Math.abs(end - start);
1487
+ const rect = axis_rotated ? {
1488
+ x: margin.left,
1489
+ y: margin.top + brushStart,
1490
+ w: width,
1491
+ h: brushSize
1492
+ } : {
1493
+ x: margin.left + brushStart,
1494
+ y: margin.top,
1495
+ w: brushSize,
1496
+ h: height
1497
+ };
1498
+ if (brushSize <= 0) return;
1499
+ this.painter.fillRect(rect, {
1500
+ fill: this.theme.style.zoomBrush.fill,
1501
+ alpha: this.theme.style.zoomBrush.opacity
1502
+ });
1503
+ }
1504
+ /**
1505
+ * Draw data selection drag area.
1506
+ * @param {object} $$ ChartInternal instance
1507
+ * @param {object} rect Selection rectangle
1508
+ * @private
1509
+ */
1510
+ drawSelectionDragArea($$, rect) {
1511
+ const { margin, width, height } = $$.state;
1512
+ const plot = {
1513
+ x: margin.left,
1514
+ y: margin.top,
1515
+ w: width,
1516
+ h: height
1517
+ };
1518
+ const x = Math.max(plot.x, rect.x);
1519
+ const y = Math.max(plot.y, rect.y);
1520
+ const right = Math.min(plot.x + plot.w, rect.x + rect.w);
1521
+ const bottom = Math.min(plot.y + plot.h, rect.y + rect.h);
1522
+ if (right <= x || bottom <= y) return;
1523
+ this.painter.fillRect({
1524
+ x,
1525
+ y,
1526
+ w: right - x,
1527
+ h: bottom - y
1528
+ }, {
1529
+ fill: this.theme.style.zoomBrush.fill,
1530
+ alpha: this.theme.style.zoomBrush.opacity
1531
+ });
1532
+ }
1533
+ /**
1534
+ * Draw treemap tiles and labels on canvas.
1535
+ * @param {object} $$ ChartInternal instance
1536
+ * @private
1537
+ */
1538
+ drawTreemaps($$) {
1539
+ const { ctx, painter, theme: { style } } = this;
1540
+ const { config } = $$;
1541
+ const root = $$.getTreemapRoot?.($$.data.targets);
1542
+ const nodes = root?.children || [];
1543
+ painter.withState(() => {
1544
+ ctx.lineWidth = style.treemap.lineWidth;
1545
+ ctx.strokeStyle = style.treemap.stroke;
1546
+ for (const node of nodes) {
1547
+ const { data } = node;
1548
+ if (!isCanvasTreemapType($$, data)) continue;
1549
+ const { x, y, w, h } = getTreemapNodeRect($$, node, root, true);
1550
+ if (!isFiniteCanvasCoordinate(x, y) || !isFiniteCanvasCoordinate(x + w, y + h) || w <= 0 || h <= 0) continue;
1551
+ const rect = {
1552
+ x,
1553
+ y,
1554
+ w,
1555
+ h
1556
+ };
1557
+ ctx.fillStyle = $$.color(data.name);
1558
+ painter.fillRect(rect);
1559
+ painter.strokeRect(rect);
1560
+ if (!config.treemap_label_show || (data.ratio || 0) < (config.treemap_label_threshold || 0)) continue;
1561
+ const label = getTreemapLabelText($$, data, w, h);
1562
+ if (!label) continue;
1563
+ const lines = label.split("\n");
1564
+ const isCentered = !!config.data_labels.centered;
1565
+ ctx.font = style.label.font;
1566
+ const lineHeight = getFontSize(ctx.font);
1567
+ const metrics = ctx.measureText(lines[0] ?? "");
1568
+ const fontBoundingHeight = (metrics.fontBoundingBoxAscent || 0) + (metrics.fontBoundingBoxDescent || 0) || (metrics.actualBoundingBoxAscent || 0) + (metrics.actualBoundingBoxDescent || 0);
1569
+ const textHeight = Math.max(lineHeight, fontBoundingHeight) + (lines.length - 1) * lineHeight;
1570
+ const blockY = isCentered ? y + h / 2 : y + textHeight + 5;
1571
+ let textX = isCentered ? x + w / 2 : x + 5;
1572
+ let textY = blockY - (lines.length - 1) * lineHeight / (isCentered ? 2 : 1);
1573
+ ctx.textAlign = isCentered ? "center" : "left";
1574
+ ctx.textBaseline = isCentered ? "middle" : "alphabetic";
1575
+ ({x: textX, y: textY} = this.drawLabelImage($$, data, label, textX, textY));
1576
+ ctx.fillStyle = getLabelColor($$, data, style.label.color);
1577
+ drawLabelDecorations($$, painter, data, label, textX, isCentered ? textY + (lines.length - 1) * lineHeight / 2 : blockY);
1578
+ lines.forEach((line, i) => {
1579
+ painter.text(line, textX, textY + i * lineHeight, { maxWidth: Math.max(0, w - 8) });
1580
+ });
1581
+ }
1582
+ });
1583
+ }
1584
+ /**
1585
+ * Draw synchronized focus grid on the canvas subchart.
1586
+ * @param {object} $$ ChartInternal instance
1587
+ * @param {Array} selectedData Focused data rows
1588
+ * @private
1589
+ */
1590
+ drawSubchartFocus($$, selectedData) {
1591
+ const { config, scale, state } = $$;
1592
+ const focus = selectedData?.find((d) => d && hasCanvasDrawableValue($$, d));
1593
+ if (!focus || !config.subchart_show || config.subchart_grid_focus_continuous || config.subchart_brush_enabled !== false || config.grid_focus_show === false || config.subchart_grid_focus === false || !config.tooltip_show || config.axis_tooltip || !scale.subX || state.width2 <= 0 || state.height2 <= 0) return;
1594
+ const { margin2, width2, height2 } = state;
1595
+ const pos = scale.subX(focus.x);
1596
+ if (!Number.isFinite(pos)) return;
1597
+ const { painter, theme: { style } } = this;
1598
+ const lineWidth = style.axis.lineWidth;
1599
+ const x = painter.crisp(margin2.left + pos, lineWidth);
1600
+ const y = painter.crisp(margin2.top + pos, lineWidth);
1601
+ painter.strokePath(() => {
1602
+ config.axis_rotated ? painter.traceLine(margin2.left, y, margin2.left + width2, y) : painter.traceLine(x, margin2.top, x, margin2.top + height2);
1603
+ }, {
1604
+ lineDash: style.focusGrid.dashArray,
1605
+ lineWidth: style.focusGrid.lineWidth,
1606
+ stroke: style.focusGrid.lineColor
1607
+ });
1608
+ }
1609
+ /**
1610
+ * Draw focus grid and focused points on canvas.
1611
+ * @param {object} $$ ChartInternal instance
1612
+ * @param {Array} selectedData Focused data rows
1613
+ * @private
1614
+ */
1615
+ drawFocus($$, selectedData) {
1616
+ if (!selectedData?.length) return;
1617
+ const { painter } = this;
1618
+ const { style } = this.theme;
1619
+ const { margin } = $$.state;
1620
+ const focus = selectedData.find((d) => d && hasCanvasDrawableValue($$, d));
1621
+ $$.config.subchart_grid_focus !== false && !isContinuousGridFocusEnabled($$) && this.drawSubchartFocus($$, selectedData);
1622
+ painter.withTranslation(margin.left, margin.top, () => {
1623
+ if ($$.config.tooltip_show && $$.config.grid_focus_show !== false && !$$.config.axis_tooltip && focus) {
1624
+ const { x, y } = getRenderDataPoint($$, focus);
1625
+ const axisLineWidth = style.axis.lineWidth;
1626
+ const isRotated = $$.config.axis_rotated;
1627
+ const hasIndexCoordinate = Number.isFinite(isRotated ? y : x);
1628
+ const hasValueCoordinate = Number.isFinite(isRotated ? x : y);
1629
+ const crispEdgeX = (value) => painter.crisp(margin.left + value, axisLineWidth) - margin.left;
1630
+ const crispEdgeY = (value) => painter.crisp(margin.top + value, axisLineWidth) - margin.top;
1631
+ const isEdge = $$.config.grid_focus_edge && !$$.config.tooltip_grouped;
1632
+ const continuousFocus = isContinuousGridFocusEnabled($$);
1633
+ const focusEndX = continuousFocus ? $$.state.margin2.left - margin.left + $$.state.width2 : $$.state.width;
1634
+ const focusEndY = continuousFocus ? $$.state.margin2.top - margin.top + $$.state.height2 : $$.state.height;
1635
+ if (hasIndexCoordinate) painter.strokePath(() => {
1636
+ if (isRotated) {
1637
+ painter.traceLine(crispEdgeX(0), y, isEdge && hasValueCoordinate ? x : crispEdgeX(focusEndX), y);
1638
+ if (hasValueCoordinate && $$.config.grid_focus_y && !$$.config.tooltip_grouped) {
1639
+ const isY2 = $$.axis?.getId(focus.id) === "y2";
1640
+ painter.traceLine(x, isEdge && !isY2 ? y : crispEdgeY(0), x, isEdge && isY2 ? y : crispEdgeY($$.state.height));
1641
+ }
1642
+ } else {
1643
+ painter.traceLine(x, isEdge && hasValueCoordinate ? y : crispEdgeY(0), x, crispEdgeY(focusEndY));
1644
+ if (hasValueCoordinate && $$.config.grid_focus_y && !$$.config.tooltip_grouped) {
1645
+ const isY2 = $$.axis?.getId(focus.id) === "y2";
1646
+ painter.traceLine(isEdge && isY2 ? x : crispEdgeX(0), y, isEdge && !isY2 ? x : crispEdgeX($$.state.width), y);
1647
+ }
1648
+ }
1649
+ }, {
1650
+ lineDash: style.focusGrid.dashArray,
1651
+ lineWidth: style.focusGrid.lineWidth,
1652
+ stroke: style.focusGrid.lineColor
1653
+ });
1654
+ }
1655
+ if ($$.config.point_show && !$$.state.canvasFocusMainRedraw) selectedData.filter((d) => d && hasCanvasDrawableValue($$, d) && isCanvasPointType($$, d) && isCanvasRenderableTarget($$, { id: d.id })).forEach((d) => {
1656
+ const pointType = getPointType($$, { id: d.id });
1657
+ const { x, y } = getRenderDataPoint($$, d);
1658
+ const baseR = $$.pointR?.(d) ?? 2.5;
1659
+ const r = $$.pointExpandedR?.(d) ?? baseR * 1.75;
1660
+ const overColor = getCanvasOverColor($$, d);
1661
+ const color = overColor || $$.color(d.id);
1662
+ const fill = overColor || style.focusPoint.fill || style.shape.pointFillColor || color;
1663
+ const stroke = overColor || style.focusPoint.stroke || color;
1664
+ const lineWidth = style.focusPoint.lineWidth;
1665
+ const alpha = getPointOpacity($$, d);
1666
+ const hasStroke = !!stroke && (lineWidth ?? 0) > 0;
1667
+ const mergeSameColorStroke = pointType === "circle" && hasStroke && isNumber(alpha) && alpha < 1 && fill === stroke;
1668
+ if (!isFiniteCanvasCoordinate(x, y)) return;
1669
+ drawPointPattern(painter, pointType, x, y, mergeSameColorStroke ? r + (lineWidth || 0) / 2 : r, hasStroke && !mergeSameColorStroke ? {
1670
+ fill,
1671
+ lineWidth,
1672
+ stroke,
1673
+ alpha
1674
+ } : {
1675
+ fill,
1676
+ alpha
1677
+ }, baseR);
1678
+ });
1679
+ this.drawFocusLabels($$, selectedData);
1680
+ });
1681
+ }
1682
+ };
1683
+ //#endregion
1934
1684
  export { CanvasRenderer as default };