billboard.js 4.0.3 → 4.1.0-next.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (270) hide show
  1. package/README.md +82 -7
  2. package/dist/billboard.css +1 -1
  3. package/dist/billboard.js +1841 -491
  4. package/dist/billboard.min.css +1 -1
  5. package/dist/billboard.min.js +19 -24
  6. package/dist/billboard.pkgd.js +1837 -676
  7. package/dist/billboard.pkgd.min.js +24 -29
  8. package/dist/billboard.react.js +205 -0
  9. package/dist/billboard.react.min.js +9 -0
  10. package/dist/billboard.worker.js +10 -0
  11. package/dist/plugin/billboardjs-plugin-bubblecompare.js +2 -2
  12. package/dist/plugin/billboardjs-plugin-bubblecompare.min.js +2 -2
  13. package/dist/plugin/billboardjs-plugin-sparkline.js +3 -2
  14. package/dist/plugin/billboardjs-plugin-sparkline.min.js +3 -3
  15. package/dist/plugin/billboardjs-plugin-stanford.js +3 -2
  16. package/dist/plugin/billboardjs-plugin-stanford.min.js +2 -2
  17. package/dist/plugin/billboardjs-plugin-tableview.js +57 -27
  18. package/dist/plugin/billboardjs-plugin-tableview.min.js +5 -5
  19. package/dist/plugin/billboardjs-plugin-textoverlap.js +72 -54
  20. package/dist/plugin/billboardjs-plugin-textoverlap.min.js +2 -2
  21. package/dist/plugin/pkgd/billboardjs-plugin-bubblecompare.pkgd.js +2 -2
  22. package/dist/plugin/pkgd/billboardjs-plugin-bubblecompare.pkgd.min.js +2 -2
  23. package/dist/plugin/pkgd/billboardjs-plugin-sparkline.pkgd.js +3 -2
  24. package/dist/plugin/pkgd/billboardjs-plugin-sparkline.pkgd.min.js +3 -3
  25. package/dist/plugin/pkgd/billboardjs-plugin-stanford.pkgd.js +3 -2
  26. package/dist/plugin/pkgd/billboardjs-plugin-stanford.pkgd.min.js +2 -2
  27. package/dist/plugin/pkgd/billboardjs-plugin-tableview.pkgd.js +57 -27
  28. package/dist/plugin/pkgd/billboardjs-plugin-tableview.pkgd.min.js +11 -11
  29. package/dist/plugin/pkgd/billboardjs-plugin-textoverlap.pkgd.js +65 -3556
  30. package/dist/plugin/pkgd/billboardjs-plugin-textoverlap.pkgd.min.js +8 -8
  31. package/dist/theme/dark.css +1 -1
  32. package/dist/theme/dark.min.css +1 -1
  33. package/dist/theme/datalab.css +1 -1
  34. package/dist/theme/datalab.min.css +1 -1
  35. package/dist/theme/graph.css +1 -1
  36. package/dist/theme/graph.min.css +1 -1
  37. package/dist/theme/insight.css +1 -1
  38. package/dist/theme/insight.min.css +1 -1
  39. package/dist/theme/modern.css +1 -1
  40. package/dist/theme/modern.min.css +1 -1
  41. package/dist-esm/Chart/Chart.js +134 -152
  42. package/dist-esm/Chart/api/axis.js +217 -237
  43. package/dist-esm/Chart/api/category.js +54 -54
  44. package/dist-esm/Chart/api/chart.js +172 -215
  45. package/dist-esm/Chart/api/color.js +21 -21
  46. package/dist-esm/Chart/api/data.js +172 -173
  47. package/dist-esm/Chart/api/export.js +242 -262
  48. package/dist-esm/Chart/api/flow.js +198 -254
  49. package/dist-esm/Chart/api/focus.js +136 -143
  50. package/dist-esm/Chart/api/grid.js +191 -190
  51. package/dist-esm/Chart/api/group.js +31 -34
  52. package/dist-esm/Chart/api/legend.js +54 -63
  53. package/dist-esm/Chart/api/load.js +237 -257
  54. package/dist-esm/Chart/api/regions.js +148 -168
  55. package/dist-esm/Chart/api/selection.js +181 -139
  56. package/dist-esm/Chart/api/show.js +133 -147
  57. package/dist-esm/Chart/api/stubs.js +43 -42
  58. package/dist-esm/Chart/api/subchart.js +155 -172
  59. package/dist-esm/Chart/api/tooltip.js +132 -177
  60. package/dist-esm/Chart/api/x.js +67 -71
  61. package/dist-esm/Chart/api/zoom.js +183 -220
  62. package/dist-esm/ChartInternal/Axis/Axis.js +1059 -1266
  63. package/dist-esm/ChartInternal/Axis/AxisRenderer.js +408 -540
  64. package/dist-esm/ChartInternal/Axis/AxisRendererHelper.js +113 -149
  65. package/dist-esm/ChartInternal/ChartInternal.js +573 -749
  66. package/dist-esm/ChartInternal/data/convert.helper.js +133 -159
  67. package/dist-esm/ChartInternal/data/convert.js +200 -229
  68. package/dist-esm/ChartInternal/data/data.js +967 -1153
  69. package/dist-esm/ChartInternal/data/load.js +145 -197
  70. package/dist-esm/ChartInternal/interactions/drag.js +86 -122
  71. package/dist-esm/ChartInternal/interactions/eventrect.js +455 -621
  72. package/dist-esm/ChartInternal/interactions/flow.js +151 -215
  73. package/dist-esm/ChartInternal/interactions/interaction.js +214 -252
  74. package/dist-esm/ChartInternal/interactions/subchart.js +425 -300
  75. package/dist-esm/ChartInternal/interactions/zoom.js +321 -440
  76. package/dist-esm/ChartInternal/internals/category.js +18 -18
  77. package/dist-esm/ChartInternal/internals/class.js +89 -99
  78. package/dist-esm/ChartInternal/internals/clip.js +88 -119
  79. package/dist-esm/ChartInternal/internals/color.js +233 -288
  80. package/dist-esm/ChartInternal/internals/domain.js +452 -588
  81. package/dist-esm/ChartInternal/internals/format.js +64 -77
  82. package/dist-esm/ChartInternal/internals/grid.js +333 -457
  83. package/dist-esm/ChartInternal/internals/legend.js +675 -927
  84. package/dist-esm/ChartInternal/internals/redraw.js +200 -289
  85. package/dist-esm/ChartInternal/internals/region.js +106 -159
  86. package/dist-esm/ChartInternal/internals/scale.js +186 -215
  87. package/dist-esm/ChartInternal/internals/selection.js +132 -159
  88. package/dist-esm/ChartInternal/internals/size.axis.js +92 -139
  89. package/dist-esm/ChartInternal/internals/size.js +286 -394
  90. package/dist-esm/ChartInternal/internals/style.js +39 -41
  91. package/dist-esm/ChartInternal/internals/subchart.util.js +49 -0
  92. package/dist-esm/ChartInternal/internals/text.arc.js +155 -219
  93. package/dist-esm/ChartInternal/internals/text.js +368 -528
  94. package/dist-esm/ChartInternal/internals/text.util.js +179 -239
  95. package/dist-esm/ChartInternal/internals/title.js +96 -121
  96. package/dist-esm/ChartInternal/internals/tooltip.js +450 -586
  97. package/dist-esm/ChartInternal/internals/transform.js +79 -97
  98. package/dist-esm/ChartInternal/internals/type.js +285 -236
  99. package/dist-esm/ChartInternal/shape/arc.js +825 -1112
  100. package/dist-esm/ChartInternal/shape/area.js +73 -94
  101. package/dist-esm/ChartInternal/shape/bar.js +227 -288
  102. package/dist-esm/ChartInternal/shape/bubble.js +9 -8
  103. package/dist-esm/ChartInternal/shape/candlestick.js +110 -138
  104. package/dist-esm/ChartInternal/shape/core/barRadius.js +197 -133
  105. package/dist-esm/ChartInternal/shape/core/bubble.js +64 -73
  106. package/dist-esm/ChartInternal/shape/core/candlestick.js +93 -86
  107. package/dist-esm/ChartInternal/shape/core/dataRegion.js +68 -75
  108. package/dist-esm/ChartInternal/shape/core/geometry.js +112 -112
  109. package/dist-esm/ChartInternal/shape/core/path.js +142 -115
  110. package/dist-esm/ChartInternal/shape/core/point.js +105 -125
  111. package/dist-esm/ChartInternal/shape/core/treemap.js +83 -88
  112. package/dist-esm/ChartInternal/shape/funnel.js +310 -338
  113. package/dist-esm/ChartInternal/shape/gauge.js +71 -103
  114. package/dist-esm/ChartInternal/shape/line.js +208 -278
  115. package/dist-esm/ChartInternal/shape/point.js +310 -425
  116. package/dist-esm/ChartInternal/shape/polar.js +84 -109
  117. package/dist-esm/ChartInternal/shape/radar.js +228 -318
  118. package/dist-esm/ChartInternal/shape/shape.js +782 -843
  119. package/dist-esm/ChartInternal/shape/treemap.js +152 -190
  120. package/dist-esm/canvas/CanvasAxisRenderer.js +1180 -1328
  121. package/dist-esm/canvas/CanvasEngine.js +148 -166
  122. package/dist-esm/canvas/CanvasPainter.js +395 -411
  123. package/dist-esm/canvas/CanvasRenderer.js +1584 -1834
  124. package/dist-esm/canvas/CanvasTheme.js +899 -802
  125. package/dist-esm/canvas/HitDetector.js +394 -463
  126. package/dist-esm/canvas/axisTicks.js +672 -786
  127. package/dist-esm/canvas/classes.js +19 -17
  128. package/dist-esm/canvas/color.js +40 -59
  129. package/dist-esm/canvas/geometry.js +38 -38
  130. package/dist-esm/canvas/labels.js +318 -371
  131. package/dist-esm/canvas/pointPattern.js +578 -580
  132. package/dist-esm/canvas/util.js +242 -262
  133. package/dist-esm/config/Options/Options.js +29 -32
  134. package/dist-esm/config/Options/axis/axis.js +101 -101
  135. package/dist-esm/config/Options/axis/x.js +759 -755
  136. package/dist-esm/config/Options/axis/y.js +525 -522
  137. package/dist-esm/config/Options/axis/y2.js +501 -485
  138. package/dist-esm/config/Options/common/boost.js +93 -32
  139. package/dist-esm/config/Options/common/color.js +88 -87
  140. package/dist-esm/config/Options/common/grid.js +74 -73
  141. package/dist-esm/config/Options/common/legend.js +180 -179
  142. package/dist-esm/config/Options/common/main.js +474 -473
  143. package/dist-esm/config/Options/common/point.js +160 -159
  144. package/dist-esm/config/Options/common/title.js +47 -46
  145. package/dist-esm/config/Options/common/tooltip.js +253 -252
  146. package/dist-esm/config/Options/data/axis.js +173 -172
  147. package/dist-esm/config/Options/data/data.js +958 -957
  148. package/dist-esm/config/Options/data/selection.js +131 -130
  149. package/dist-esm/config/Options/interaction/interaction.js +53 -52
  150. package/dist-esm/config/Options/interaction/subchart.js +164 -73
  151. package/dist-esm/config/Options/interaction/zoom.js +86 -85
  152. package/dist-esm/config/Options/shape/arc.js +150 -149
  153. package/dist-esm/config/Options/shape/area.js +69 -68
  154. package/dist-esm/config/Options/shape/bar.js +147 -146
  155. package/dist-esm/config/Options/shape/bubble.js +35 -34
  156. package/dist-esm/config/Options/shape/candlestick.js +61 -60
  157. package/dist-esm/config/Options/shape/donut.js +181 -180
  158. package/dist-esm/config/Options/shape/funnel.js +49 -48
  159. package/dist-esm/config/Options/shape/gauge.js +237 -236
  160. package/dist-esm/config/Options/shape/line.js +58 -57
  161. package/dist-esm/config/Options/shape/pie.js +185 -184
  162. package/dist-esm/config/Options/shape/polar.js +167 -166
  163. package/dist-esm/config/Options/shape/radar.js +76 -75
  164. package/dist-esm/config/Options/shape/scatter.js +36 -36
  165. package/dist-esm/config/Options/shape/spline.js +46 -46
  166. package/dist-esm/config/Options/shape/treemap.js +80 -79
  167. package/dist-esm/config/Store/Element.js +64 -62
  168. package/dist-esm/config/Store/State.js +189 -200
  169. package/dist-esm/config/Store/Store.js +25 -26
  170. package/dist-esm/config/classes.js +157 -155
  171. package/dist-esm/config/config.js +26 -35
  172. package/dist-esm/config/const.js +120 -128
  173. package/dist-esm/config/resolver/axis.core.js +25 -25
  174. package/dist-esm/config/resolver/axis.js +26 -27
  175. package/dist-esm/config/resolver/canvas.js +2071 -2472
  176. package/dist-esm/config/resolver/canvasShape.js +201 -177
  177. package/dist-esm/config/resolver/category.js +26 -27
  178. package/dist-esm/config/resolver/export.js +24 -26
  179. package/dist-esm/config/resolver/flow.js +29 -31
  180. package/dist-esm/config/resolver/grid.js +34 -35
  181. package/dist-esm/config/resolver/interaction/selection.js +17 -17
  182. package/dist-esm/config/resolver/interaction/subchart.js +17 -17
  183. package/dist-esm/config/resolver/interaction/zoom.js +17 -17
  184. package/dist-esm/config/resolver/regions.js +30 -31
  185. package/dist-esm/config/resolver/shape/arc.helpers.js +19 -19
  186. package/dist-esm/config/resolver/shape/area.js +17 -17
  187. package/dist-esm/config/resolver/shape/axis.helpers.js +28 -25
  188. package/dist-esm/config/resolver/shape/bar.js +13 -13
  189. package/dist-esm/config/resolver/shape/bubble.js +18 -14
  190. package/dist-esm/config/resolver/shape/candlestick.js +13 -13
  191. package/dist-esm/config/resolver/shape/donut.js +11 -11
  192. package/dist-esm/config/resolver/shape/funnel.js +11 -11
  193. package/dist-esm/config/resolver/shape/gauge.js +12 -12
  194. package/dist-esm/config/resolver/shape/line.js +10 -10
  195. package/dist-esm/config/resolver/shape/pie.js +11 -11
  196. package/dist-esm/config/resolver/shape/polar.js +12 -12
  197. package/dist-esm/config/resolver/shape/radar.js +22 -14
  198. package/dist-esm/config/resolver/shape/scatter.js +13 -13
  199. package/dist-esm/config/resolver/shape/treemap.js +11 -11
  200. package/dist-esm/core.js +208 -211
  201. package/dist-esm/index.canvas.js +14 -13
  202. package/dist-esm/index.esm.js +25 -24
  203. package/dist-esm/module/Cache.js +117 -129
  204. package/dist-esm/module/browser.js +30 -35
  205. package/dist-esm/module/dsv.js +107 -127
  206. package/dist-esm/module/error.js +45 -52
  207. package/dist-esm/module/generator.js +75 -83
  208. package/dist-esm/module/sanitize.js +302 -399
  209. package/dist-esm/module/util/brush.js +27 -38
  210. package/dist-esm/module/util/dom.js +288 -348
  211. package/dist-esm/module/util/object.js +277 -341
  212. package/dist-esm/module/util/type-checks.js +21 -25
  213. package/dist-esm/module/worker.js +312 -130
  214. package/dist-esm/plugin/billboardjs-plugin-bubblecompare.js +185 -198
  215. package/dist-esm/plugin/billboardjs-plugin-sparkline.js +442 -450
  216. package/dist-esm/plugin/billboardjs-plugin-stanford.js +813 -950
  217. package/dist-esm/plugin/billboardjs-plugin-tableview.js +708 -788
  218. package/dist-esm/plugin/billboardjs-plugin-textoverlap.js +344 -301
  219. package/dist-esm/react/index.js +157 -0
  220. package/package.json +28 -8
  221. package/src/Chart/api/selection.ts +130 -5
  222. package/src/ChartInternal/Axis/Axis.ts +164 -65
  223. package/src/ChartInternal/Axis/AxisRenderer.ts +41 -8
  224. package/src/ChartInternal/Axis/AxisRendererHelper.ts +1 -1
  225. package/src/ChartInternal/ChartInternal.ts +1 -1
  226. package/src/ChartInternal/data/convert.ts +54 -4
  227. package/src/ChartInternal/data/data.ts +4 -1
  228. package/src/ChartInternal/interactions/eventrect.ts +6 -0
  229. package/src/ChartInternal/interactions/subchart.ts +437 -66
  230. package/src/ChartInternal/internals/color.ts +2 -2
  231. package/src/ChartInternal/internals/domain.ts +15 -3
  232. package/src/ChartInternal/internals/grid.ts +21 -1
  233. package/src/ChartInternal/internals/scale.ts +9 -2
  234. package/src/ChartInternal/internals/selection.ts +1 -1
  235. package/src/ChartInternal/internals/subchart.util.ts +55 -0
  236. package/src/ChartInternal/internals/text.arc.ts +3 -1
  237. package/src/ChartInternal/internals/text.ts +18 -5
  238. package/src/ChartInternal/internals/transform.ts +8 -2
  239. package/src/ChartInternal/internals/type.ts +105 -1
  240. package/src/ChartInternal/shape/IShape.ts +6 -3
  241. package/src/ChartInternal/shape/bar.ts +62 -31
  242. package/src/ChartInternal/shape/core/barRadius.ts +129 -0
  243. package/src/ChartInternal/shape/core/candlestick.ts +1 -1
  244. package/src/ChartInternal/shape/core/path.ts +76 -9
  245. package/src/ChartInternal/shape/funnel.ts +2 -3
  246. package/src/ChartInternal/shape/point.ts +2 -2
  247. package/src/ChartInternal/shape/shape.ts +149 -17
  248. package/src/Plugin/stanford/index.ts +0 -4
  249. package/src/Plugin/tableview/index.ts +85 -3
  250. package/src/Plugin/textoverlap/index.ts +9 -15
  251. package/src/canvas/CanvasAxisRenderer.ts +184 -36
  252. package/src/canvas/CanvasRenderer.ts +247 -160
  253. package/src/canvas/CanvasTheme.ts +212 -0
  254. package/src/canvas/axisTicks.ts +122 -40
  255. package/src/config/Options/axis/y2.ts +13 -0
  256. package/src/config/Options/common/boost.ts +64 -4
  257. package/src/config/Options/common/color.ts +2 -2
  258. package/src/config/Options/interaction/subchart.ts +98 -1
  259. package/src/config/Options/shape/gauge.ts +1 -1
  260. package/src/config/Options/shape/treemap.ts +2 -2
  261. package/src/config/Store/Element.ts +3 -1
  262. package/src/config/Store/State.ts +1 -0
  263. package/src/config/classes.ts +1 -0
  264. package/src/config/resolver/canvas.ts +92 -10
  265. package/src/module/voronoi.ts +119 -0
  266. package/src/module/worker.entry.ts +61 -0
  267. package/src/module/worker.ts +346 -63
  268. package/src/react/index.ts +193 -0
  269. package/types/options.d.ts +206 -3
  270. package/types/react/index.d.ts +27 -0
@@ -5,1429 +5,1281 @@
5
5
  * billboard.js, JavaScript chart library
6
6
  * https://naver.github.io/billboard.js/
7
7
  *
8
- * @version 4.0.3
8
+ * @version 4.1.0-next.1
9
9
  */
10
- import { AXIS_TICK_SIZE, AXIS_TICK_PADDING } from '../config/const.js';
11
- import { getSubXTickValues, normalizeXValue, getXTickValues, getYGridTickValues, normalizeYValue, getAdditionalAxisScale, getAdditionalAxisTickFormat, getAdditionalAxisTickValues, getXScale, getXTickLineValues, getXTickLinePosition, isSameTickValue, getYTickValues } from './axisTicks.js';
12
- import CanvasPainter from './CanvasPainter.js';
13
- import { getFontSize } from './util.js';
14
-
10
+ import { getFontSize } from "./util.js";
11
+ import { getAdditionalAxisScale, getAdditionalAxisTickFormat, getAdditionalAxisTickValues, getSubXTickLineValues, getSubXTickValues, getXScale, getXTickLinePosition, getXTickLineValues, getXTickValues, getYGridTickValues, getYTickValues, isSameTickValue, normalizeXValue, normalizeYValue } from "./axisTicks.js";
12
+ import CanvasPainter from "./CanvasPainter.js";
13
+ //#region src/canvas/CanvasAxisRenderer.ts
15
14
  const X_AXIS_TICK_TEXT_HORIZONTAL_CLIP_PADDING = 20;
16
15
  const X_AXIS_TICK_TEXT_VERTICAL_CLIP_PADDING = 15;
17
16
  /**
18
- * Get x tick text direction. Text labels stay outside the chart even when tick lines are inner.
19
- * @param {boolean} isRotated Whether axis is rotated
20
- * @returns {number} Text direction multiplier
21
- * @private
22
- */
17
+ * Get x tick text direction. Text labels stay outside the chart even when tick lines are inner.
18
+ * @param {boolean} isRotated Whether axis is rotated
19
+ * @returns {number} Text direction multiplier
20
+ * @private
21
+ */
23
22
  function getXTickTextDirection(isRotated) {
24
- return isRotated ? -1 : 1;
23
+ return isRotated ? -1 : 1;
25
24
  }
26
25
  /**
27
- * Get y/y2 tick text direction. Text labels stay outside the chart even when tick lines are inner.
28
- * @param {boolean} isRotated Whether axis is rotated
29
- * @param {boolean} isY2 Whether axis is y2
30
- * @returns {number} Text direction multiplier
31
- * @private
32
- */
26
+ * Get y/y2 tick text direction. Text labels stay outside the chart even when tick lines are inner.
27
+ * @param {boolean} isRotated Whether axis is rotated
28
+ * @param {boolean} isY2 Whether axis is y2
29
+ * @returns {number} Text direction multiplier
30
+ * @private
31
+ */
33
32
  function getYTickTextDirection(isRotated, isY2) {
34
- return isRotated ? (isY2 ? -1 : 1) : (isY2 ? 1 : -1);
33
+ return isRotated ? isY2 ? -1 : 1 : isY2 ? 1 : -1;
34
+ }
35
+ /**
36
+ * Get tick formatter for the canvas subchart y/y2 axis.
37
+ * @param {object} $$ ChartInternal context
38
+ * @param {string} id Axis id
39
+ * @returns {function} Tick formatter
40
+ * @private
41
+ */
42
+ function getSubYTickFormat($$, id) {
43
+ const axisFormat = $$.axis?.[id === "y2" ? "subY2" : "subY"]?.tickFormat?.();
44
+ const configFormat = $$.config[`subchart_axis_${id}_tick_format`] || $$.config[`axis_${id}_tick_format`];
45
+ if (axisFormat) return axisFormat;
46
+ return typeof configFormat === "function" ? configFormat.bind($$.api) : ((v) => v);
47
+ }
48
+ /**
49
+ * Convert resolved grid line style to painter line style.
50
+ * @param {object} style Grid line style
51
+ * @returns {object|undefined} Canvas line style
52
+ * @private
53
+ */
54
+ function getGridLineCanvasStyle(style) {
55
+ return style ? {
56
+ stroke: style.lineColor,
57
+ lineWidth: style.lineWidth,
58
+ lineDash: style.dashArray
59
+ } : void 0;
60
+ }
61
+ /**
62
+ * Convert resolved grid text style to painter text style.
63
+ * @param {object} style Grid text style
64
+ * @returns {object|undefined} Canvas text style
65
+ * @private
66
+ */
67
+ function getGridTextCanvasStyle(style) {
68
+ return style ? {
69
+ fill: style.labelColor,
70
+ font: style.labelFont
71
+ } : void 0;
35
72
  }
36
73
  /**
37
- * Get horizontal x-axis clip rect with small text overflow tolerance.
38
- * @param {object} margin Chart margin
39
- * @param {number} width Plot width
40
- * @param {number} height Clip height
41
- * @returns {object} Clip rectangle
42
- * @private
43
- */
74
+ * Get horizontal x-axis clip rect with small text overflow tolerance.
75
+ * @param {object} margin Chart margin
76
+ * @param {number} width Plot width
77
+ * @param {number} height Clip height
78
+ * @returns {object} Clip rectangle
79
+ * @private
80
+ */
44
81
  function getHorizontalXAxisClipRect(margin, width, height) {
45
- return {
46
- x: margin.left - X_AXIS_TICK_TEXT_HORIZONTAL_CLIP_PADDING,
47
- y: 0,
48
- w: width + (X_AXIS_TICK_TEXT_HORIZONTAL_CLIP_PADDING * 2),
49
- h: height
50
- };
82
+ return {
83
+ x: margin.left - X_AXIS_TICK_TEXT_HORIZONTAL_CLIP_PADDING,
84
+ y: 0,
85
+ w: width + X_AXIS_TICK_TEXT_HORIZONTAL_CLIP_PADDING * 2,
86
+ h: height
87
+ };
51
88
  }
52
89
  /**
53
- * Get rotated x-axis clip rect with small text overflow tolerance.
54
- * @param {object} margin Chart margin
55
- * @param {number} width Clip width
56
- * @param {number} height Plot height
57
- * @returns {object} Clip rectangle
58
- * @private
59
- */
90
+ * Get rotated x-axis clip rect with small text overflow tolerance.
91
+ * @param {object} margin Chart margin
92
+ * @param {number} width Clip width
93
+ * @param {number} height Plot height
94
+ * @returns {object} Clip rectangle
95
+ * @private
96
+ */
60
97
  function getRotatedXAxisClipRect(margin, width, height) {
61
- return {
62
- x: 0,
63
- y: margin.top - X_AXIS_TICK_TEXT_VERTICAL_CLIP_PADDING,
64
- w: width,
65
- h: height + (X_AXIS_TICK_TEXT_VERTICAL_CLIP_PADDING * 2)
66
- };
98
+ return {
99
+ x: 0,
100
+ y: margin.top - X_AXIS_TICK_TEXT_VERTICAL_CLIP_PADDING,
101
+ w: width,
102
+ h: height + X_AXIS_TICK_TEXT_VERTICAL_CLIP_PADDING * 2
103
+ };
67
104
  }
68
105
  /**
69
- * Get configured axis tooltip background color.
70
- * @param {object} $$ ChartInternal context
71
- * @param {string} id Axis id
72
- * @returns {string|null} Background color
73
- * @private
74
- */
106
+ * Get configured axis tooltip background color.
107
+ * @param {object} $$ ChartInternal context
108
+ * @param {string} id Axis id
109
+ * @returns {string|null} Background color
110
+ * @private
111
+ */
75
112
  function getAxisTooltipBackgroundColor($$, id) {
76
- const bgColor = $$.config.axis_tooltip?.backgroundColor ?? "black";
77
- return typeof bgColor === "string" ? bgColor : (bgColor?.[id] || null);
113
+ const bgColor = $$.config.axis_tooltip?.backgroundColor ?? "black";
114
+ return typeof bgColor === "string" ? bgColor : bgColor?.[id] || null;
78
115
  }
79
116
  /**
80
- * Format canvas axis tooltip scale value.
81
- * @param {object} $$ ChartInternal context
82
- * @param {string} id Axis id
83
- * @param {number} value Scale coordinate
84
- * @returns {string|null} Formatted scale text
85
- * @private
86
- */
117
+ * Format canvas axis tooltip scale value.
118
+ * @param {object} $$ ChartInternal context
119
+ * @param {string} id Axis id
120
+ * @param {number} value Scale coordinate
121
+ * @returns {string|null} Formatted scale text
122
+ * @private
123
+ */
87
124
  function formatAxisTooltipValue($$, id, value) {
88
- const scale = $$.scale[id];
89
- if (!scale?.invert) {
90
- return null;
91
- }
92
- const scaleValue = scale.invert(value);
93
- if (scaleValue === null || scaleValue === undefined) {
94
- return null;
95
- }
96
- if (id === "x" && $$.axis?.isTimeSeries?.()) {
97
- return $$.format.xAxisTick(scaleValue);
98
- }
99
- const numeric = Number(scaleValue);
100
- return Number.isFinite(numeric) ? numeric.toFixed(2) : `${scaleValue}`;
125
+ const scale = $$.scale[id];
126
+ if (!scale?.invert) return null;
127
+ const scaleValue = scale.invert(value);
128
+ if (scaleValue === null || scaleValue === void 0) return null;
129
+ if (id === "x" && $$.axis?.isTimeSeries?.()) return $$.format.xAxisTick(scaleValue);
130
+ const numeric = Number(scaleValue);
131
+ return Number.isFinite(numeric) ? numeric.toFixed(2) : `${scaleValue}`;
101
132
  }
102
133
  /**
103
- * Get the axis group origin in canvas coordinates.
104
- * @param {object} $$ ChartInternal context
105
- * @param {string} id Axis id
106
- * @returns {object} Axis group origin
107
- * @private
108
- */
134
+ * Get the axis group origin in canvas coordinates.
135
+ * @param {object} $$ ChartInternal context
136
+ * @param {string} id Axis id
137
+ * @returns {object} Axis group origin
138
+ * @private
139
+ */
109
140
  function getAxisLabelBasePosition($$, id) {
110
- const { config, state: { height, margin, width } } = $$;
111
- const isRotated = config.axis_rotated;
112
- const base = {
113
- x: margin.left,
114
- y: margin.top
115
- };
116
- if (id === "x") {
117
- !isRotated && (base.y += height);
118
- }
119
- else if (id === "y") {
120
- isRotated && (base.y += height);
121
- }
122
- else if (isRotated) {
123
- base.y -= 1;
124
- }
125
- else {
126
- base.x += width;
127
- }
128
- return base;
141
+ const { config, state: { height, margin, width } } = $$;
142
+ const isRotated = config.axis_rotated;
143
+ const base = {
144
+ x: margin.left,
145
+ y: margin.top
146
+ };
147
+ if (id === "x") !isRotated && (base.y += height);
148
+ else if (id === "y") isRotated && (base.y += height);
149
+ else if (isRotated) base.y -= 1;
150
+ else base.x += width;
151
+ return base;
129
152
  }
130
153
  /**
131
- * Convert SVG axis label local coordinates to canvas coordinates.
132
- * @param {object} base Axis group origin
133
- * @param {number} base.x Axis group origin x
134
- * @param {number} base.y Axis group origin y
135
- * @param {number} localX SVG local x coordinate
136
- * @param {number} localY SVG local y coordinate
137
- * @param {boolean} isRotated Whether SVG label has rotate(-90)
138
- * @returns {object} Canvas coordinates
139
- * @private
140
- */
154
+ * Convert SVG axis label local coordinates to canvas coordinates.
155
+ * @param {object} base Axis group origin
156
+ * @param {number} base.x Axis group origin x
157
+ * @param {number} base.y Axis group origin y
158
+ * @param {number} localX SVG local x coordinate
159
+ * @param {number} localY SVG local y coordinate
160
+ * @param {boolean} isRotated Whether SVG label has rotate(-90)
161
+ * @returns {object} Canvas coordinates
162
+ * @private
163
+ */
141
164
  function getAxisLabelCanvasPosition(base, localX, localY, isRotated) {
142
- return isRotated ?
143
- {
144
- x: base.x + localY,
145
- y: base.y - localX
146
- } :
147
- {
148
- x: base.x + localX,
149
- y: base.y + localY
150
- };
165
+ return isRotated ? {
166
+ x: base.x + localY,
167
+ y: base.y - localX
168
+ } : {
169
+ x: base.x + localX,
170
+ y: base.y + localY
171
+ };
151
172
  }
152
173
  /**
153
- * Format tick value.
154
- * @param {function} format Format function
155
- * @param {number|Date|string} tick Tick value
156
- * @returns {string} Formatted tick
157
- * @private
158
- */
174
+ * Format tick value.
175
+ * @param {function} format Format function
176
+ * @param {number|Date|string} tick Tick value
177
+ * @returns {string} Formatted tick
178
+ * @private
179
+ */
159
180
  function formatTick(format, tick) {
160
- const value = format ? format(tick) : tick;
161
- return value == null ? "" : String(value);
181
+ const value = format ? format(tick) : tick;
182
+ return value == null ? "" : String(value);
162
183
  }
163
184
  /**
164
- * Get axis tick text font for a specific axis.
165
- * @param {object} axisStyle Canvas axis style
166
- * @param {string} id Axis id
167
- * @returns {string} Canvas font shorthand
168
- * @private
169
- */
185
+ * Get axis tick text font for a specific axis.
186
+ * @param {object} axisStyle Canvas axis style
187
+ * @param {string} id Axis id
188
+ * @returns {string} Canvas font shorthand
189
+ * @private
190
+ */
170
191
  function getAxisTickFont(axisStyle, id) {
171
- return axisStyle[`${id}TickFont`] || axisStyle.labelFont;
192
+ return axisStyle[`${id}TickFont`] || axisStyle.labelFont;
172
193
  }
173
194
  /**
174
- * Get SVG-like multiline x tick text line height.
175
- * @param {object} painter Canvas painter
176
- * @param {number} fontSize Current font size
177
- * @returns {number} Line height
178
- * @private
179
- */
195
+ * Get SVG-like multiline x tick text line height.
196
+ * @param {object} painter Canvas painter
197
+ * @param {number} fontSize Current font size
198
+ * @returns {number} Line height
199
+ * @private
200
+ */
180
201
  function getXTickTextLineHeight(painter, fontSize) {
181
- const metrics = painter.measureText("0");
182
- const fontBoxHeight = (metrics.fontBoundingBoxAscent || 0) +
183
- (metrics.fontBoundingBoxDescent || 0);
184
- const actualBoxHeight = (metrics.actualBoundingBoxAscent || 0) +
185
- (metrics.actualBoundingBoxDescent || 0);
186
- const svgFallbackHeight = (fontSize || 10) * (11.5 / 10);
187
- return Math.max(fontSize, fontBoxHeight, actualBoxHeight, svgFallbackHeight);
202
+ const metrics = painter.measureText("0");
203
+ const fontBoxHeight = (metrics.fontBoundingBoxAscent || 0) + (metrics.fontBoundingBoxDescent || 0);
204
+ const actualBoxHeight = (metrics.actualBoundingBoxAscent || 0) + (metrics.actualBoundingBoxDescent || 0);
205
+ const svgFallbackHeight = (fontSize || 10) * (11.5 / 10);
206
+ return Math.max(fontSize, fontBoxHeight, actualBoxHeight, svgFallbackHeight);
188
207
  }
189
208
  /**
190
- * Get SVG-compatible y position for rotated bottom x-axis tick text.
191
- * @param {number} rotate Tick text rotation
192
- * @returns {number} Local SVG text y coordinate
193
- * @private
194
- */
209
+ * Get SVG-compatible y position for rotated bottom x-axis tick text.
210
+ * @param {number} rotate Tick text rotation
211
+ * @returns {number} Local SVG text y coordinate
212
+ * @private
213
+ */
195
214
  function getRotatedXTickTextY(rotate) {
196
- const r2 = rotate / 15;
197
- return 11.5 - 2.5 * r2 * (rotate > 0 ? 1 : -1);
215
+ return 11.5 - 2.5 * (rotate / 15) * (rotate > 0 ? 1 : -1);
198
216
  }
199
217
  /**
200
- * Get SVG-compatible tspan dx for rotated bottom x-axis tick text.
201
- * @param {number} rotate Tick text rotation
202
- * @returns {number} Local tspan dx
203
- * @private
204
- */
218
+ * Get SVG-compatible tspan dx for rotated bottom x-axis tick text.
219
+ * @param {number} rotate Tick text rotation
220
+ * @returns {number} Local tspan dx
221
+ * @private
222
+ */
205
223
  function getRotatedXTickTextDx(rotate) {
206
- return 8 * Math.sin(Math.PI * (rotate / 180));
224
+ return 8 * Math.sin(Math.PI * (rotate / 180));
207
225
  }
208
226
  /**
209
- * Resolve SVG-like dx/dy values for canvas text.
210
- * @param {number|string} value Offset value
211
- * @param {number} fontSize Current font size
212
- * @returns {number} Pixel offset
213
- * @private
214
- */
227
+ * Resolve SVG-like dx/dy values for canvas text.
228
+ * @param {number|string} value Offset value
229
+ * @param {number} fontSize Current font size
230
+ * @returns {number} Pixel offset
231
+ * @private
232
+ */
215
233
  function resolveTextOffset(value, fontSize) {
216
- if (typeof value === "number") {
217
- return value;
218
- }
219
- if (typeof value === "string") {
220
- const parsed = parseFloat(value);
221
- if (!Number.isFinite(parsed)) {
222
- return 0;
223
- }
224
- return /em$/.test(value.trim()) ? parsed * fontSize : parsed;
225
- }
226
- return 0;
234
+ if (typeof value === "number") return value;
235
+ if (typeof value === "string") {
236
+ const parsed = parseFloat(value);
237
+ if (!Number.isFinite(parsed)) return 0;
238
+ return /em$/.test(value.trim()) ? parsed * fontSize : parsed;
239
+ }
240
+ return 0;
227
241
  }
228
242
  /**
229
- * Split tick text by configured width.
230
- * @param {string} text Tick text
231
- * @param {number} width Max width
232
- * @param {object} painter Canvas painter
233
- * @returns {Array} Text lines
234
- * @private
235
- */
243
+ * Split tick text by configured width.
244
+ * @param {string} text Tick text
245
+ * @param {number} width Max width
246
+ * @param {object} painter Canvas painter
247
+ * @returns {Array} Text lines
248
+ * @private
249
+ */
236
250
  function splitTickTextByWidth(text, width, painter) {
237
- if (!width) {
238
- return [text];
239
- }
240
- const charWidth = painter.measureText("0").width || 5.5;
241
- /**
242
- * Split a text fragment recursively.
243
- * @param {Array} lines Accumulated text lines
244
- * @param {string} value Remaining text fragment
245
- * @returns {Array} Text lines
246
- * @private
247
- */
248
- function split(lines, value) {
249
- let spaceIndex;
250
- for (let i = 1; i < value.length; i++) {
251
- if (value.charAt(i) === " ") {
252
- spaceIndex = i;
253
- }
254
- if (width < charWidth * (i + 1)) {
255
- const splitIndex = spaceIndex || i;
256
- return split(lines.concat(value.slice(0, splitIndex)), value.slice(spaceIndex ? spaceIndex + 1 : i));
257
- }
258
- }
259
- return lines.concat(value);
260
- }
261
- return split([], text);
251
+ if (!width) return [text];
252
+ const charWidth = painter.measureText("0").width || 5.5;
253
+ /**
254
+ * Split a text fragment recursively.
255
+ * @param {Array} lines Accumulated text lines
256
+ * @param {string} value Remaining text fragment
257
+ * @returns {Array} Text lines
258
+ * @private
259
+ */
260
+ function split(lines, value) {
261
+ let spaceIndex;
262
+ for (let i = 1; i < value.length; i++) {
263
+ if (value.charAt(i) === " ") spaceIndex = i;
264
+ if (width < charWidth * (i + 1)) return split(lines.concat(value.slice(0, spaceIndex || i)), value.slice(spaceIndex ? spaceIndex + 1 : i));
265
+ }
266
+ return lines.concat(value);
267
+ }
268
+ return split([], text);
262
269
  }
263
270
  /**
264
- * Get max width for splitting canvas x axis tick text.
265
- * @param {object} $$ ChartInternal context
266
- * @param {Array} ticks X-axis tick values
267
- * @param {boolean} isRotated Whether axis is rotated
268
- * @param {function} targetScale X scale
269
- * @returns {number} Max tick text width
270
- * @private
271
- */
271
+ * Get max width for splitting canvas x axis tick text.
272
+ * @param {object} $$ ChartInternal context
273
+ * @param {Array} ticks X-axis tick values
274
+ * @param {boolean} isRotated Whether axis is rotated
275
+ * @param {function} targetScale X scale
276
+ * @returns {number} Max tick text width
277
+ * @private
278
+ */
272
279
  function getXTickTextWidth($$, ticks, isRotated, targetScale) {
273
- const configured = $$.config.axis_x_tick_width;
274
- if (configured && configured > 0) {
275
- return configured;
276
- }
277
- if (isRotated) {
278
- return 95;
279
- }
280
- if ($$.axis?.isCategorized?.() && ticks.length > 1) {
281
- const start = targetScale(normalizeXValue($$, ticks[0]));
282
- const end = targetScale(normalizeXValue($$, ticks[1]));
283
- return Math.max(0, Math.abs(end - start) - 12);
284
- }
285
- return 110;
280
+ const configured = $$.config.axis_x_tick_width;
281
+ if (configured && configured > 0) return configured;
282
+ if (isRotated) return 95;
283
+ if ($$.axis?.isCategorized?.() && ticks.length > 1) {
284
+ const start = targetScale(normalizeXValue($$, ticks[0]));
285
+ const end = targetScale(normalizeXValue($$, ticks[1]));
286
+ return Math.max(0, Math.abs(end - start) - 12);
287
+ }
288
+ return 110;
286
289
  }
287
290
  /**
288
- * Get rendered x-axis tick text lines.
289
- * @param {object} $$ ChartInternal context
290
- * @param {object} painter Canvas painter
291
- * @param {function} format Tick formatter
292
- * @param {number|Date|string} tick Tick value
293
- * @param {Array} ticks X-axis tick values
294
- * @param {boolean} isRotated Whether axis is rotated
295
- * @param {function} targetScale X scale
296
- * @param {number} [maxWidth] Pre-computed max tick text width (invariant per draw pass)
297
- * @returns {Array} Tick text lines
298
- * @private
299
- */
291
+ * Get rendered x-axis tick text lines.
292
+ * @param {object} $$ ChartInternal context
293
+ * @param {object} painter Canvas painter
294
+ * @param {function} format Tick formatter
295
+ * @param {number|Date|string} tick Tick value
296
+ * @param {Array} ticks X-axis tick values
297
+ * @param {boolean} isRotated Whether axis is rotated
298
+ * @param {function} targetScale X scale
299
+ * @param {number} [maxWidth] Pre-computed max tick text width (invariant per draw pass)
300
+ * @returns {Array} Tick text lines
301
+ * @private
302
+ */
300
303
  function getXTickTextLines($$, painter, format, tick, ticks = [], isRotated = false, targetScale = getXScale($$), maxWidth) {
301
- const value = format ? format(tick) : tick;
302
- if (value == null) {
303
- return [""];
304
- }
305
- if (Array.isArray(value)) {
306
- return value.map(v => String(v));
307
- }
308
- const text = String(value);
309
- if (text.indexOf("\n") > -1) {
310
- return text.split("\n");
311
- }
312
- return $$.config.axis_x_tick_multiline ?
313
- splitTickTextByWidth(text, maxWidth ?? getXTickTextWidth($$, ticks, isRotated, targetScale), painter) :
314
- [text];
304
+ const value = format ? format(tick) : tick;
305
+ if (value == null) return [""];
306
+ if (Array.isArray(value)) return value.map((v) => String(v));
307
+ const text = String(value);
308
+ if (text.indexOf("\n") > -1) return text.split("\n");
309
+ return $$.config.axis_x_tick_multiline ? splitTickTextByWidth(text, maxWidth ?? getXTickTextWidth($$, ticks, isRotated, targetScale), painter) : [text];
315
310
  }
316
311
  /**
317
- * Get canvas text alignment for x-axis tick text.
318
- * @param {object} $$ ChartInternal context
319
- * @param {object} options X-axis drawing options
320
- * @returns {string} Canvas text alignment
321
- * @private
322
- */
312
+ * Get canvas text alignment for x-axis tick text.
313
+ * @param {object} $$ ChartInternal context
314
+ * @param {object} options X-axis drawing options
315
+ * @returns {string} Canvas text alignment
316
+ * @private
317
+ */
323
318
  function getXTickTextAlign($$, options) {
324
- const { isRotated, tickCount, tickIndex, tickRotate, tickTextDirection } = options;
325
- const inner = $$.config.axis_x_tick_text_inner;
326
- let align = isRotated ?
327
- (tickTextDirection > 0 ? "left" : "right") :
328
- (tickRotate ? (tickRotate > 0 ? "left" : "right") : "center");
329
- if (!isRotated && tickIndex === 0 && (inner === true || inner?.first)) {
330
- align = "left";
331
- }
332
- else if (!isRotated &&
333
- tickIndex === tickCount - 1 &&
334
- (inner === true || inner?.last)) {
335
- align = "right";
336
- }
337
- return align;
319
+ const { isRotated, tickCount, tickIndex, tickRotate, tickTextDirection } = options;
320
+ const inner = $$.config.axis_x_tick_text_inner;
321
+ let align = isRotated ? tickTextDirection > 0 ? "left" : "right" : tickRotate ? tickRotate > 0 ? "left" : "right" : "center";
322
+ if (!isRotated && tickIndex === 0 && (inner === true || inner?.first)) align = "left";
323
+ else if (!isRotated && tickIndex === tickCount - 1 && (inner === true || inner?.last)) align = "right";
324
+ return align;
338
325
  }
339
326
  /**
340
- * Get title x position and text alignment.
341
- * @param {string} position Title position option
342
- * @param {number} width Chart width
343
- * @returns {object} Position and alignment
344
- * @private
345
- */
327
+ * Get title x position and text alignment.
328
+ * @param {string} position Title position option
329
+ * @param {number} width Chart width
330
+ * @returns {object} Position and alignment
331
+ * @private
332
+ */
346
333
  function getTitleTextPosition(position, width) {
347
- if ((position?.indexOf("center") ?? -1) > -1) {
348
- return { x: width / 2, align: "center" };
349
- }
350
- if ((position?.indexOf("right") ?? -1) > -1) {
351
- return { x: width, align: "right" };
352
- }
353
- return { x: 0, align: "left" };
334
+ if ((position?.indexOf("center") ?? -1) > -1) return {
335
+ x: width / 2,
336
+ align: "center"
337
+ };
338
+ if ((position?.indexOf("right") ?? -1) > -1) return {
339
+ x: width,
340
+ align: "right"
341
+ };
342
+ return {
343
+ x: 0,
344
+ align: "left"
345
+ };
354
346
  }
355
347
  /**
356
- * Check if position can be drawn.
357
- * @param {number} value Coordinate value
358
- * @returns {boolean} Whether coordinate is finite
359
- * @private
360
- */
348
+ * Check if position can be drawn.
349
+ * @param {number} value Coordinate value
350
+ * @returns {boolean} Whether coordinate is finite
351
+ * @private
352
+ */
361
353
  function isDrawable(value) {
362
- return Number.isFinite(value);
354
+ return Number.isFinite(value);
363
355
  }
364
356
  /**
365
- * Check if position is within an axis drawing range.
366
- * @param {number} value Coordinate value
367
- * @param {number} start Range start
368
- * @param {number} end Range end
369
- * @returns {boolean} Whether coordinate is finite and in range
370
- * @private
371
- */
357
+ * Check if position is within an axis drawing range.
358
+ * @param {number} value Coordinate value
359
+ * @param {number} start Range start
360
+ * @param {number} end Range end
361
+ * @returns {boolean} Whether coordinate is finite and in range
362
+ * @private
363
+ */
372
364
  function isInAxisRange(value, start, end) {
373
- return isDrawable(value) &&
374
- value >= Math.min(start, end) &&
375
- value <= Math.max(start, end);
365
+ return isDrawable(value) && value >= Math.min(start, end) && value <= Math.max(start, end);
376
366
  }
377
367
  /**
378
- * Get outer x tick direction following SVG axis orientation.
379
- * @param {boolean} isRotated Whether axis is rotated
380
- * @returns {number} Outer tick direction
381
- * @private
382
- */
368
+ * Get outer x tick direction following SVG axis orientation.
369
+ * @param {boolean} isRotated Whether axis is rotated
370
+ * @returns {number} Outer tick direction
371
+ * @private
372
+ */
383
373
  function getXOuterTickDirection(isRotated) {
384
- return isRotated ? -1 : 1;
374
+ return isRotated ? -1 : 1;
385
375
  }
386
376
  /**
387
- * Get outer y/y2 tick direction following SVG axis orientation.
388
- * @param {object} config Chart config
389
- * @param {boolean} isRotated Whether axis is rotated
390
- * @param {boolean} isY2 Whether axis is y2
391
- * @returns {number} Outer tick direction
392
- * @private
393
- */
377
+ * Get outer y/y2 tick direction following SVG axis orientation.
378
+ * @param {object} config Chart config
379
+ * @param {boolean} isRotated Whether axis is rotated
380
+ * @param {boolean} isY2 Whether axis is y2
381
+ * @returns {number} Outer tick direction
382
+ * @private
383
+ */
394
384
  function getYOuterTickDirection(config, isRotated, isY2) {
395
- if (isRotated) {
396
- return isY2 ? (config.axis_y2_inner ? 1 : -1) : (config.axis_y_inner ? -1 : 1);
397
- }
398
- return isY2 ? (config.axis_y2_inner ? -1 : 1) : (config.axis_y_inner ? 1 : -1);
385
+ if (isRotated) return isY2 ? config.axis_y2_inner ? 1 : -1 : config.axis_y_inner ? -1 : 1;
386
+ return isY2 ? config.axis_y2_inner ? -1 : 1 : config.axis_y_inner ? 1 : -1;
399
387
  }
400
388
  /**
401
- * Resolve x coordinate from an x/category value.
402
- * @param {object} $$ ChartInternal context
403
- * @param {number|Date|string} value X value
404
- * @param {function} targetScale X scale
405
- * @returns {number} Pixel coordinate relative to plot area
406
- * @private
407
- */
389
+ * Resolve x coordinate from an x/category value.
390
+ * @param {object} $$ ChartInternal context
391
+ * @param {number|Date|string} value X value
392
+ * @param {function} targetScale X scale
393
+ * @returns {number} Pixel coordinate relative to plot area
394
+ * @private
395
+ */
408
396
  function getXPosition($$, value, targetScale = getXScale($$)) {
409
- return targetScale(normalizeXValue($$, value));
397
+ return targetScale(normalizeXValue($$, value));
410
398
  }
411
399
  /**
412
- * Resolve x region boundary value, matching SVG region category semantics.
413
- * @param {object} $$ ChartInternal context
414
- * @param {number|Date|string} value X boundary value
415
- * @returns {number|Date|string} Scale-compatible value
416
- * @private
417
- */
400
+ * Resolve x region boundary value, matching SVG region category semantics.
401
+ * @param {object} $$ ChartInternal context
402
+ * @param {number|Date|string} value X boundary value
403
+ * @returns {number|Date|string} Scale-compatible value
404
+ * @private
405
+ */
418
406
  function normalizeXRegionBoundaryValue($$, value) {
419
- if ($$.axis?.isCategorized?.() && typeof value === "string" && Number.isNaN(Number(value))) {
420
- return $$.config.axis_x_categories.indexOf(value);
421
- }
422
- return normalizeXValue($$, value);
407
+ if ($$.axis?.isCategorized?.() && typeof value === "string" && Number.isNaN(Number(value))) return $$.config.axis_x_categories.indexOf(value);
408
+ return normalizeXValue($$, value);
423
409
  }
424
410
  /**
425
- * Resolve whether category region boundary needs tick offset.
426
- * @param {object} $$ ChartInternal context
427
- * @param {number|Date|string} value X boundary value
428
- * @returns {boolean}
429
- * @private
430
- */
411
+ * Resolve whether category region boundary needs tick offset.
412
+ * @param {object} $$ ChartInternal context
413
+ * @param {number|Date|string} value X boundary value
414
+ * @returns {boolean}
415
+ * @private
416
+ */
431
417
  function hasCategoryRegionBoundaryOffset($$, value) {
432
- return Boolean($$.axis?.isCategorized?.() && Number.isNaN(Number(value)));
418
+ return Boolean($$.axis?.isCategorized?.() && Number.isNaN(Number(value)));
433
419
  }
434
420
  /**
435
- * Resolve x boundary coordinate for category-aware regions.
436
- * @param {object} $$ ChartInternal context
437
- * @param {number|Date|string|undefined} value X value
438
- * @param {number} fallback Fallback coordinate
439
- * @param {string} key Region boundary key
440
- * @returns {number} Pixel coordinate relative to plot area
441
- * @private
442
- */
421
+ * Resolve x boundary coordinate for category-aware regions.
422
+ * @param {object} $$ ChartInternal context
423
+ * @param {number|Date|string|undefined} value X value
424
+ * @param {number} fallback Fallback coordinate
425
+ * @param {string} key Region boundary key
426
+ * @returns {number} Pixel coordinate relative to plot area
427
+ * @private
428
+ */
443
429
  function getXBoundary($$, value, fallback, key) {
444
- if (value === undefined) {
445
- return fallback;
446
- }
447
- let pos = getXScale($$)(normalizeXRegionBoundaryValue($$, value));
448
- if (hasCategoryRegionBoundaryOffset($$, value)) {
449
- const xScale = getXScale($$);
450
- const tickOffset = $$.axis.x?.tickOffset?.() || ((xScale(1) - xScale(0)) / 2);
451
- pos += tickOffset * (key === "start" ? -1 : 1);
452
- }
453
- return pos;
430
+ if (value === void 0) return fallback;
431
+ let pos = getXScale($$)(normalizeXRegionBoundaryValue($$, value));
432
+ if (hasCategoryRegionBoundaryOffset($$, value)) {
433
+ const xScale = getXScale($$);
434
+ const tickOffset = $$.axis.x?.tickOffset?.() || (xScale(1) - xScale(0)) / 2;
435
+ pos += tickOffset * (key === "start" ? -1 : 1);
436
+ }
437
+ return pos;
454
438
  }
455
439
  /**
456
- * Resolve y coordinate from y/y2 value.
457
- * @param {object} $$ ChartInternal context
458
- * @param {number|Date|string} value Y value
459
- * @param {string} axis Axis id
460
- * @returns {number} Pixel coordinate relative to plot area
461
- * @private
462
- */
440
+ * Resolve y coordinate from y/y2 value.
441
+ * @param {object} $$ ChartInternal context
442
+ * @param {number|Date|string} value Y value
443
+ * @param {string} axis Axis id
444
+ * @returns {number} Pixel coordinate relative to plot area
445
+ * @private
446
+ */
463
447
  function getYPosition($$, value, axis = "y") {
464
- return $$.scale[axis || "y"](normalizeYValue($$, value, axis));
448
+ return $$.scale[axis || "y"](normalizeYValue($$, value, axis));
465
449
  }
466
450
  /**
467
- * Get a text position along a grid line.
468
- * @param {string} position Grid label position
469
- * @param {number} start Start coordinate
470
- * @param {number} end End coordinate
471
- * @returns {number} Label coordinate
472
- * @private
473
- */
451
+ * Get a text position along a grid line.
452
+ * @param {string} position Grid label position
453
+ * @param {number} start Start coordinate
454
+ * @param {number} end End coordinate
455
+ * @returns {number} Label coordinate
456
+ * @private
457
+ */
474
458
  function getLineTextPosition(position, start, end) {
475
- if (position === "start") {
476
- return start + 4;
477
- }
478
- if (position === "middle") {
479
- return (start + end) / 2;
480
- }
481
- return end - 4;
459
+ if (position === "start") return start + 4;
460
+ if (position === "middle") return (start + end) / 2;
461
+ return end - 4;
482
462
  }
483
463
  /**
484
- * Get a text position for labels drawn with SVG-compatible rotate(-90).
485
- * @param {string} position Grid label position
486
- * @param {number} start Start coordinate
487
- * @param {number} end End coordinate
488
- * @returns {number} Label coordinate
489
- * @private
490
- */
464
+ * Get a text position for labels drawn with SVG-compatible rotate(-90).
465
+ * @param {string} position Grid label position
466
+ * @param {number} start Start coordinate
467
+ * @param {number} end End coordinate
468
+ * @returns {number} Label coordinate
469
+ * @private
470
+ */
491
471
  function getRotatedLineTextPosition(position, start, end) {
492
- if (position === "start") {
493
- return end - 4;
494
- }
495
- if (position === "middle") {
496
- return (start + end) / 2;
497
- }
498
- return start + 4;
472
+ if (position === "start") return end - 4;
473
+ if (position === "middle") return (start + end) / 2;
474
+ return start + 4;
499
475
  }
500
476
  /**
501
- * Get global region rectangle.
502
- * @param {object} $$ ChartInternal context
503
- * @param {object} region Region option
504
- * @returns {object|null} Region rectangle relative to plot area
505
- * @private
506
- */
477
+ * Get global region rectangle.
478
+ * @param {object} $$ ChartInternal context
479
+ * @param {object} region Region option
480
+ * @returns {object|null} Region rectangle relative to plot area
481
+ * @private
482
+ */
507
483
  function getRegionRect($$, region) {
508
- const { config, scale, state: { width, height } } = $$;
509
- const axis = region.axis || "x";
510
- const isRotated = config.axis_rotated;
511
- if (axis === "x") {
512
- const start = getXBoundary($$, region.start, 0, "start");
513
- const end = getXBoundary($$, region.end, isRotated ? height : width, "end");
514
- const min = Math.min(start, end);
515
- const size = Math.abs(end - start);
516
- return isRotated ? { x: 0, y: min, w: width, h: size } : { x: min, y: 0, w: size, h: height };
517
- }
518
- if (!scale[axis]) {
519
- return null;
520
- }
521
- const start = region.start === undefined ?
522
- (isRotated ? 0 : height) :
523
- getYPosition($$, region.start, axis);
524
- const end = region.end === undefined ?
525
- (isRotated ? width : 0) :
526
- getYPosition($$, region.end, axis);
527
- const min = Math.min(start, end);
528
- const size = Math.abs(end - start);
529
- return isRotated ? { x: min, y: 0, w: size, h: height } : { x: 0, y: min, w: width, h: size };
484
+ const { config, scale, state: { width, height } } = $$;
485
+ const axis = region.axis || "x";
486
+ const isRotated = config.axis_rotated;
487
+ if (axis === "x") {
488
+ const start = getXBoundary($$, region.start, 0, "start");
489
+ const end = getXBoundary($$, region.end, isRotated ? height : width, "end");
490
+ const min = Math.min(start, end);
491
+ const size = Math.abs(end - start);
492
+ return isRotated ? {
493
+ x: 0,
494
+ y: min,
495
+ w: width,
496
+ h: size
497
+ } : {
498
+ x: min,
499
+ y: 0,
500
+ w: size,
501
+ h: height
502
+ };
503
+ }
504
+ if (!scale[axis]) return null;
505
+ const start = region.start === void 0 ? isRotated ? 0 : height : getYPosition($$, region.start, axis);
506
+ const end = region.end === void 0 ? isRotated ? width : 0 : getYPosition($$, region.end, axis);
507
+ const min = Math.min(start, end);
508
+ const size = Math.abs(end - start);
509
+ return isRotated ? {
510
+ x: min,
511
+ y: 0,
512
+ w: size,
513
+ h: height
514
+ } : {
515
+ x: 0,
516
+ y: min,
517
+ w: width,
518
+ h: size
519
+ };
530
520
  }
531
521
  /**
532
- * Draw axes and grid lines on canvas.
533
- * @private
534
- */
535
- class CanvasAxisRenderer {
536
- engine;
537
- theme;
538
- painter;
539
- /**
540
- * Constructor.
541
- * @param {CanvasEngine} engine Canvas drawing engine
542
- * @param {CanvasTheme} theme Canvas theme resolver
543
- * @private
544
- */
545
- constructor(engine, theme) {
546
- this.engine = engine;
547
- this.theme = theme;
548
- this.painter = new CanvasPainter(engine.ctx);
549
- }
550
- /**
551
- * Get the drawing context for the main canvas.
552
- * @returns {CanvasRenderingContext2D} Canvas drawing context
553
- * @private
554
- */
555
- get ctx() {
556
- return this.painter.context;
557
- }
558
- /**
559
- * Run axis renderer draw calls on another canvas context.
560
- * @param {CanvasRenderingContext2D} ctx Canvas drawing context
561
- * @param {function} draw Draw callback
562
- * @private
563
- */
564
- withContext(ctx, draw) {
565
- this.painter.withContext(ctx, draw);
566
- }
567
- /**
568
- * Draw grid and axis layers.
569
- * @param {object} $$ ChartInternal instance
570
- * @private
571
- */
572
- draw($$) {
573
- this.drawRegions($$);
574
- this.drawGrid($$);
575
- this.drawAxis($$);
576
- }
577
- /**
578
- * Draw visible axes.
579
- * @param {object} $$ ChartInternal instance
580
- * @private
581
- */
582
- drawAxis($$) {
583
- const { config } = $$;
584
- config.axis_x_show && this.drawXAxis($$);
585
- config.axis_y_show && this.drawYAxis($$);
586
- config.axis_y2_show && $$.scale.y2 && this.drawYAxis($$, "y2");
587
- this.drawAdditionalAxes($$);
588
- this.drawAxisLabels($$);
589
- }
590
- /**
591
- * Draw the canvas subchart x axis.
592
- * @param {object} $$ ChartInternal instance
593
- * @private
594
- */
595
- drawSubXAxis($$) {
596
- const { ctx, painter, theme: { style: { axis } } } = this;
597
- const { config, format, scale, state: { current, margin2, width2, height2 } } = $$;
598
- if (!config.subchart_show ||
599
- !config.subchart_axis_x_show ||
600
- !scale.subX ||
601
- width2 <= 0 ||
602
- height2 <= 0) {
603
- return;
604
- }
605
- const isRotated = config.axis_rotated;
606
- const x = painter.crisp(margin2.left, axis.lineWidth);
607
- const y = painter.crisp(margin2.top + height2, axis.lineWidth);
608
- const x1 = margin2.left;
609
- const x2 = margin2.left + width2;
610
- const y1 = margin2.top;
611
- const y2 = margin2.top + height2;
612
- const rangeStart = isRotated ? y1 : x1;
613
- const rangeEnd = isRotated ? y2 : x2;
614
- const ticks = getSubXTickValues($$);
615
- const tickDirection = isRotated ?
616
- (config.axis_x_tick_inner ? 1 : -1) :
617
- (config.axis_x_tick_inner ? -1 : 1);
618
- const outerTickDirection = getXOuterTickDirection(isRotated);
619
- const tickTextDirection = getXTickTextDirection(isRotated);
620
- const tickTextPosition = config.axis_x_tick_text_position;
621
- const tickRotate = !isRotated ? ($$.getAxisTickRotate?.("x") || 0) : 0;
622
- const tickFormat = format.subXAxisTick || $$.axis?.getXAxisTickFormat?.(true);
623
- painter.clipRect(isRotated ? getRotatedXAxisClipRect(margin2, current.width, height2) : {
624
- ...getHorizontalXAxisClipRect(margin2, width2, current.height - margin2.top),
625
- y: margin2.top
626
- }, () => {
627
- ctx.strokeStyle = axis.lineColor;
628
- ctx.lineWidth = axis.lineWidth;
629
- painter.strokePath(() => {
630
- if (isRotated) {
631
- painter.traceLine(x, y1, x, y2);
632
- }
633
- else {
634
- painter.traceLine(x1, y, x2, y);
635
- }
636
- if (config.axis_x_tick_outer) {
637
- if (isRotated) {
638
- painter.traceLine(x, y1, x + (AXIS_TICK_SIZE * outerTickDirection), y1);
639
- painter.traceLine(x, y2, x + (AXIS_TICK_SIZE * outerTickDirection), y2);
640
- }
641
- else {
642
- painter.traceLine(x1, y, x1, y + (AXIS_TICK_SIZE * outerTickDirection));
643
- painter.traceLine(x2, y, x2, y + (AXIS_TICK_SIZE * outerTickDirection));
644
- }
645
- }
646
- });
647
- const tickFont = getAxisTickFont(axis, "x");
648
- ctx.font = tickFont;
649
- ctx.fillStyle = axis.labelColor;
650
- ctx.textAlign = isRotated ? (tickTextDirection > 0 ? "left" : "right") : "center";
651
- ctx.textBaseline = isRotated ?
652
- "middle" :
653
- (tickTextDirection > 0 ? "top" : "bottom");
654
- ctx.strokeStyle = axis.tickColor;
655
- ctx.lineWidth = axis.tickWidth;
656
- // invariant across ticks: measure/resolve once per draw pass
657
- const lineHeight = getXTickTextLineHeight(painter, getFontSize(tickFont));
658
- const tickTextWidth = getXTickTextWidth($$, ticks, isRotated, scale.subX);
659
- for (const tick of ticks) {
660
- const tickPos = scale.subX(normalizeXValue($$, tick));
661
- const tx = margin2.left + tickPos;
662
- const ty = margin2.top + tickPos;
663
- const pos = isRotated ? ty : tx;
664
- if (!isInAxisRange(pos, rangeStart, rangeEnd)) {
665
- continue;
666
- }
667
- if (config.subchart_axis_x_tick_show) {
668
- painter.strokePath(() => {
669
- if (isRotated) {
670
- painter.traceLine(x, ty, x + (AXIS_TICK_SIZE * tickDirection), ty);
671
- }
672
- else {
673
- painter.traceLine(tx, y, tx, y + (AXIS_TICK_SIZE * tickDirection));
674
- }
675
- });
676
- }
677
- if (!config.subchart_axis_x_tick_text_show) {
678
- continue;
679
- }
680
- const lines = getXTickTextLines($$, painter, tickFormat, tick, ticks, isRotated, scale.subX, tickTextWidth);
681
- let textX;
682
- let textY;
683
- if (isRotated) {
684
- textX = x + ((AXIS_TICK_SIZE + AXIS_TICK_PADDING) * tickTextDirection) +
685
- (tickTextPosition.x || 0);
686
- textY = ty + (tickTextPosition.y || 0);
687
- ctx.textAlign = tickTextDirection > 0 ? "left" : "right";
688
- ctx.textBaseline = "middle";
689
- }
690
- else {
691
- textX = tx + (tickTextPosition.x || 0);
692
- textY = y + ((AXIS_TICK_SIZE + AXIS_TICK_PADDING) * tickTextDirection) +
693
- (tickTextPosition.y || 0);
694
- ctx.textAlign = tickRotate ? (tickRotate > 0 ? "left" : "right") : "center";
695
- ctx.textBaseline = tickTextDirection > 0 ? "top" : "bottom";
696
- }
697
- painter.withState(textCtx => {
698
- textCtx.translate(textX, textY);
699
- tickRotate && textCtx.rotate(tickRotate * Math.PI / 180);
700
- lines.forEach((line, i) => {
701
- textCtx.fillText(line, 0, i * lineHeight);
702
- });
703
- });
704
- }
705
- });
706
- }
707
- /**
708
- * Draw chart title.
709
- * @param {object} $$ ChartInternal instance
710
- * @private
711
- */
712
- drawTitle($$) {
713
- const { ctx, painter, theme: { style: { title } } } = this;
714
- const { config, state: { current } } = $$;
715
- if (!config.title_text) {
716
- return;
717
- }
718
- const lines = String(config.title_text).split("\n");
719
- const fontSize = getFontSize(title.font);
720
- const lineHeight = fontSize * 1.5;
721
- const { x, align } = getTitleTextPosition(config.title_position, current.width);
722
- const titleHeight = $$.getCanvasTitleHeight?.() ?? fontSize;
723
- const y = (config.title_padding.top || 0) + titleHeight;
724
- painter.withState(() => {
725
- ctx.font = title.font;
726
- ctx.fillStyle = title.color;
727
- ctx.textAlign = align;
728
- ctx.textBaseline = "alphabetic";
729
- lines.forEach((line, i) => {
730
- ctx.fillText(line, x, y + (i ? fontSize + ((i - 1) * lineHeight) : 0));
731
- });
732
- });
733
- }
734
- /**
735
- * Draw axis labels.
736
- * @param {object} $$ ChartInternal instance
737
- * @private
738
- */
739
- drawAxisLabels($$) {
740
- const { ctx, painter, theme: { style: { axis: style } } } = this;
741
- const { axis, config } = $$;
742
- const fontSize = getFontSize(style.labelFont);
743
- const ids = ["x", "y", "y2"];
744
- const labelColorById = {
745
- x: style.xLabelColor,
746
- y: style.yLabelColor,
747
- y2: style.y2LabelColor
748
- };
749
- const alignMap = {
750
- start: "left",
751
- middle: "center",
752
- end: "right"
753
- };
754
- if (!axis) {
755
- return;
756
- }
757
- painter.withState(() => {
758
- ctx.font = style.labelFont;
759
- ctx.textBaseline = "alphabetic";
760
- ids.forEach(id => {
761
- const text = axis.getLabelText(id);
762
- if (!text ||
763
- !config[`axis_${id}_show`] ||
764
- (id === "y2" && !$$.scale.y2)) {
765
- return;
766
- }
767
- const isRotatedLabel = (id === "x" && config.axis_rotated) ||
768
- (id !== "x" && !config.axis_rotated);
769
- const base = getAxisLabelBasePosition($$, id);
770
- const localX = axis.xForAxisLabel(id) +
771
- resolveTextOffset(axis.dxForAxisLabel(id), fontSize);
772
- const localY = resolveTextOffset(axis.dyForAxisLabel(id), fontSize);
773
- const { x, y } = getAxisLabelCanvasPosition(base, localX, localY, isRotatedLabel);
774
- const anchor = axis.textAnchorForAxisLabel(id);
775
- ctx.fillStyle = labelColorById[id] || style.labelColor;
776
- ctx.textAlign = alignMap[anchor] || "center";
777
- painter.text(String(text), x, y, {
778
- angle: isRotatedLabel ? -90 : 0
779
- });
780
- });
781
- });
782
- }
783
- /**
784
- * Draw visible grid lines.
785
- * @param {object} $$ ChartInternal instance
786
- * @private
787
- */
788
- drawGrid($$) {
789
- const { ctx, painter, theme: { style: { grid } } } = this;
790
- const { config, scale, state: { height, margin, width } } = $$;
791
- if (!grid.lineColor) {
792
- return;
793
- }
794
- const isRotated = config.axis_rotated;
795
- const x1 = margin.left;
796
- const x2 = margin.left + width;
797
- const y1 = margin.top;
798
- const y2 = margin.top + height;
799
- painter.withState(() => {
800
- ctx.strokeStyle = grid.lineColor;
801
- ctx.lineWidth = grid.lineWidth;
802
- grid.dashArray.length && ctx.setLineDash(grid.dashArray);
803
- if (config.grid_x_show && scale.x) {
804
- painter.strokePath(() => {
805
- for (const tick of getXTickValues($$)) {
806
- const pos = getXPosition($$, tick);
807
- if (!isDrawable(pos)) {
808
- continue;
809
- }
810
- if (isRotated) {
811
- painter.traceCrispLine(x1, margin.top + pos, x2, margin.top + pos, grid.lineWidth);
812
- }
813
- else {
814
- painter.traceCrispLine(margin.left + pos, y1, margin.left + pos, y2, grid.lineWidth);
815
- }
816
- }
817
- });
818
- }
819
- if (config.grid_y_show && scale.y) {
820
- painter.strokePath(() => {
821
- for (const tick of getYGridTickValues($$)) {
822
- const value = normalizeYValue($$, tick);
823
- const pos = scale.y(value);
824
- if (!isDrawable(pos)) {
825
- continue;
826
- }
827
- if (isRotated) {
828
- painter.traceCrispLine(margin.left + pos, y1, margin.left + pos, y2, grid.lineWidth);
829
- }
830
- else {
831
- painter.traceCrispLine(x1, margin.top + pos, x2, margin.top + pos, grid.lineWidth);
832
- }
833
- }
834
- });
835
- }
836
- !config.grid_lines_front && this.drawGridLines($$);
837
- });
838
- }
839
- /**
840
- * Draw configured global regions.
841
- * @param {object} $$ ChartInternal instance
842
- * @private
843
- */
844
- drawRegions($$) {
845
- const { ctx, painter, theme: { style: { region: style } } } = this;
846
- const { config, state: { height, margin, width } } = $$;
847
- const regions = config.regions || [];
848
- if (!regions.length) {
849
- return;
850
- }
851
- painter.clipRect({ x: margin.left, y: margin.top, w: width, h: height }, () => {
852
- ctx.fillStyle = style.fill;
853
- ctx.font = style.labelFont;
854
- ctx.textBaseline = "top";
855
- for (const region of regions) {
856
- const rect = getRegionRect($$, region);
857
- if (!rect || !isDrawable(rect.x) || !isDrawable(rect.y) || !rect.w || !rect.h) {
858
- continue;
859
- }
860
- const x = margin.left + rect.x;
861
- const y = margin.top + rect.y;
862
- const w = rect.w;
863
- const h = rect.h;
864
- ctx.globalAlpha = Number.isFinite(region.opacity) ? region.opacity : style.opacity;
865
- painter.fillRect({ x, y, w, h });
866
- if (region.label?.text) {
867
- const label = region.label;
868
- const center = label.center || "";
869
- const text = String(label.text);
870
- const textWidth = painter.measureText(text).width;
871
- const lineHeight = parseFloat(ctx.font) || 12;
872
- let tx = x + (label.x || 0);
873
- let ty = y + (label.y || 0);
874
- if (center.indexOf("x") > -1) {
875
- tx += (w - textWidth) / 2;
876
- }
877
- if (center.indexOf("y") > -1) {
878
- ty += (h - lineHeight) / 2;
879
- }
880
- painter.text(text, tx, ty, {
881
- angle: label.rotated ? -90 : 0,
882
- alpha: 1,
883
- fill: label.color || style.labelColor
884
- });
885
- }
886
- }
887
- });
888
- }
889
- /**
890
- * Draw configured x/y grid lines and labels.
891
- * @param {object} $$ ChartInternal instance
892
- * @private
893
- */
894
- drawGridLines($$) {
895
- const { ctx, painter, theme: { style: { axis, grid } } } = this;
896
- const { config, scale, state: { height, margin, width } } = $$;
897
- const isRotated = config.axis_rotated;
898
- const x1 = margin.left;
899
- const x2 = margin.left + width;
900
- const y1 = margin.top;
901
- const y2 = margin.top + height;
902
- if (!grid.lineColor) {
903
- return;
904
- }
905
- painter.withState(() => {
906
- ctx.strokeStyle = grid.lineColor;
907
- ctx.lineWidth = grid.lineWidth;
908
- ctx.font = grid.labelFont || axis.labelFont;
909
- ctx.fillStyle = grid.labelColor;
910
- ctx.textBaseline = "middle";
911
- ctx.setLineDash([]);
912
- const drawLabel = (text, x, y, rotated = false) => {
913
- if (!text) {
914
- return;
915
- }
916
- painter.text(text, x, y, {
917
- angle: rotated ? -90 : 0
918
- });
919
- };
920
- const drawXLine = (line) => {
921
- if (line.value === undefined || !scale.x) {
922
- return;
923
- }
924
- const pos = getXPosition($$, line.value);
925
- if (!isDrawable(pos)) {
926
- return;
927
- }
928
- if (isRotated) {
929
- const y = margin.top + pos;
930
- painter.strokePath(() => {
931
- painter.traceLine(x1, y, x2, y);
932
- });
933
- ctx.textAlign = line.position === "start" ?
934
- "left" :
935
- (line.position === "middle" ? "center" : "right");
936
- drawLabel(line.text, getLineTextPosition(line.position, x1, x2), y - 5);
937
- }
938
- else {
939
- const x = margin.left + pos;
940
- painter.strokePath(() => {
941
- painter.traceLine(x, y1, x, y2);
942
- });
943
- ctx.textAlign = line.position === "start" ?
944
- "left" :
945
- (line.position === "middle" ? "center" : "right");
946
- drawLabel(line.text, x - 5, getRotatedLineTextPosition(line.position, y1, y2), true);
947
- }
948
- };
949
- const drawYLine = (line) => {
950
- const targetScale = line.axis === "y2" ? scale.y2 : scale.y;
951
- const axisId = line.axis === "y2" ? "y2" : "y";
952
- if (line.value === undefined || !targetScale) {
953
- return;
954
- }
955
- const value = normalizeYValue($$, line.value, axisId);
956
- const pos = targetScale(value);
957
- if (!isDrawable(pos)) {
958
- return;
959
- }
960
- if (isRotated) {
961
- const x = margin.left + pos;
962
- painter.strokePath(() => {
963
- painter.traceLine(x, y1, x, y2);
964
- });
965
- ctx.textAlign = line.position === "start" ?
966
- "left" :
967
- (line.position === "middle" ? "center" : "right");
968
- drawLabel(line.text, x - 5, getRotatedLineTextPosition(line.position, y1, y2), true);
969
- }
970
- else {
971
- const y = margin.top + pos;
972
- painter.strokePath(() => {
973
- painter.traceLine(x1, y, x2, y);
974
- });
975
- ctx.textAlign = line.position === "start" ?
976
- "left" :
977
- (line.position === "middle" ? "center" : "right");
978
- drawLabel(line.text, getLineTextPosition(line.position, x1, x2), y - 5);
979
- }
980
- };
981
- (config.grid_x_lines || []).forEach(drawXLine);
982
- (config.grid_y_lines || []).forEach(drawYLine);
983
- });
984
- }
985
- /**
986
- * Draw axes configured with axis.x/y/y2.axes.
987
- * @param {object} $$ ChartInternal instance
988
- * @private
989
- */
990
- drawAdditionalAxes($$) {
991
- ["x", "y", "y2"].forEach(id => {
992
- const axesConfig = $$.config[`axis_${id}_axes`] || [];
993
- if (!axesConfig.length || !$$.scale[id] || !$$.config[`axis_${id}_show`]) {
994
- return;
995
- }
996
- axesConfig.forEach((axisConfig, index) => {
997
- const scale = getAdditionalAxisScale($$, id, axisConfig);
998
- if (!scale) {
999
- return;
1000
- }
1001
- const options = {
1002
- scale,
1003
- ticks: getAdditionalAxisTickValues($$, id, scale, axisConfig),
1004
- format: getAdditionalAxisTickFormat($$, axisConfig),
1005
- index: index + 1,
1006
- outerTick: axisConfig.tick?.outer !== false
1007
- };
1008
- id === "x" ?
1009
- this.drawXAxis($$, options) :
1010
- this.drawYAxis($$, id, options);
1011
- });
1012
- });
1013
- }
1014
- /**
1015
- * Draw the x axis.
1016
- * @param {object} $$ ChartInternal instance
1017
- * @param {object} axisOptions Additional axis options
1018
- * @private
1019
- */
1020
- drawXAxis($$, axisOptions) {
1021
- const { ctx, painter, theme: { style: { axis } } } = this;
1022
- const { axis: axisInstance, config, state: { current, margin, width, height } } = $$;
1023
- const isRotated = config.axis_rotated;
1024
- const axisOffset = axisOptions?.index ? $$.getAxisSize("x") * axisOptions.index : 0;
1025
- const targetScale = axisOptions?.scale || getXScale($$);
1026
- const x = painter.crisp(margin.left - (isRotated ? axisOffset : 0), axis.lineWidth);
1027
- const y = painter.crisp(margin.top + height + (isRotated ? 0 : axisOffset), axis.lineWidth);
1028
- const x1 = margin.left;
1029
- const x2 = margin.left + width;
1030
- const y1 = margin.top;
1031
- const y2 = margin.top + height;
1032
- const rangeStart = isRotated ? y1 : x1;
1033
- const rangeEnd = isRotated ? y2 : x2;
1034
- const ticks = axisOptions?.ticks || getXTickValues($$);
1035
- const lineTicks = axisOptions?.ticks ||
1036
- getXTickLineValues($$, ticks, axis.tickWidth);
1037
- const format = axisOptions?.format || axisInstance.getXAxisTickFormat();
1038
- const outerTick = axisOptions ? axisOptions.outerTick : config.axis_x_tick_outer;
1039
- const tickDirection = isRotated ?
1040
- (config.axis_x_tick_inner ? 1 : -1) :
1041
- (config.axis_x_tick_inner ? -1 : 1);
1042
- const outerTickDirection = getXOuterTickDirection(isRotated);
1043
- const tickTextDirection = getXTickTextDirection(isRotated);
1044
- const tickTextPosition = config.axis_x_tick_text_position;
1045
- const tickRotate = !isRotated ? ($$.getAxisTickRotate?.("x") || 0) : 0;
1046
- painter.clipRect(isRotated ?
1047
- getRotatedXAxisClipRect(margin, current.width, height) :
1048
- getHorizontalXAxisClipRect(margin, width, current.height), () => {
1049
- ctx.strokeStyle = axis.lineColor;
1050
- ctx.lineWidth = axis.lineWidth;
1051
- painter.strokePath(() => {
1052
- if (isRotated) {
1053
- painter.traceLine(x, y1, x, y2);
1054
- }
1055
- else {
1056
- painter.traceLine(x1, y, x2, y);
1057
- }
1058
- if (outerTick) {
1059
- if (isRotated) {
1060
- painter.traceLine(x, y1, x + (AXIS_TICK_SIZE * outerTickDirection), y1);
1061
- painter.traceLine(x, y2, x + (AXIS_TICK_SIZE * outerTickDirection), y2);
1062
- }
1063
- else {
1064
- painter.traceLine(x1, y, x1, y + (AXIS_TICK_SIZE * outerTickDirection));
1065
- painter.traceLine(x2, y, x2, y + (AXIS_TICK_SIZE * outerTickDirection));
1066
- }
1067
- }
1068
- });
1069
- ctx.font = getAxisTickFont(axis, "x");
1070
- ctx.fillStyle = axis.labelColor;
1071
- ctx.textAlign = isRotated ? (tickTextDirection > 0 ? "left" : "right") : "center";
1072
- ctx.textBaseline = isRotated ?
1073
- "middle" :
1074
- (tickTextDirection > 0 ? "top" : "bottom");
1075
- ctx.strokeStyle = axis.tickColor;
1076
- ctx.lineWidth = axis.tickWidth;
1077
- if (config.axis_x_tick_show) {
1078
- painter.strokePath(() => {
1079
- for (const tick of lineTicks) {
1080
- const tickPos = getXTickLinePosition($$, tick, targetScale);
1081
- const tx = margin.left + tickPos;
1082
- const ty = margin.top + tickPos;
1083
- const pos = isRotated ? ty : tx;
1084
- if (!isInAxisRange(pos, rangeStart, rangeEnd)) {
1085
- continue;
1086
- }
1087
- if (isRotated) {
1088
- painter.traceLine(x, ty, x + (AXIS_TICK_SIZE * tickDirection), ty);
1089
- }
1090
- else {
1091
- painter.traceLine(tx, y, tx, y + (AXIS_TICK_SIZE * tickDirection));
1092
- }
1093
- }
1094
- });
1095
- }
1096
- if (!axisOptions && !config.axis_x_tick_text_show) {
1097
- return;
1098
- }
1099
- // invariant across ticks: measure/resolve once per draw pass
1100
- const tickFont = getAxisTickFont(axis, "x");
1101
- const tickLineHeight = getXTickTextLineHeight(painter, getFontSize(tickFont));
1102
- const tickTextWidth = getXTickTextWidth($$, ticks, isRotated, targetScale);
1103
- ticks.forEach((tick, tickIndex) => {
1104
- this.drawXAxisTickText($$, tick, format, axis.labelColor, {
1105
- isRotated,
1106
- rangeEnd,
1107
- rangeStart,
1108
- tickCount: ticks.length,
1109
- tickFont,
1110
- tickIndex,
1111
- tickLineHeight,
1112
- tickTextDirection,
1113
- tickTextWidth,
1114
- tickRotate,
1115
- tickTextPosition,
1116
- targetScale,
1117
- ticks,
1118
- x,
1119
- y
1120
- });
1121
- });
1122
- });
1123
- }
1124
- /**
1125
- * Draw the focused x-axis tick text with the SVG active tick color.
1126
- * @param {object} $$ ChartInternal instance
1127
- * @param {Array} focusData Focused data rows
1128
- * @private
1129
- */
1130
- drawFocusedXAxisTick($$, focusData) {
1131
- const { painter, theme: { style: { axis } } } = this;
1132
- const { axis: axisInstance, config, state: { current, margin, width, height } } = $$;
1133
- const focusX = focusData?.[0]?.x;
1134
- if (focusX === undefined ||
1135
- !axisInstance ||
1136
- !config.axis_x_show ||
1137
- !config.axis_x_tick_text_show ||
1138
- !axis.activeLabelColor ||
1139
- axis.activeLabelColor === axis.labelColor) {
1140
- return;
1141
- }
1142
- const ticks = getXTickValues($$);
1143
- const tickIndex = ticks.findIndex(value => isSameTickValue(value, focusX));
1144
- const tick = ticks[tickIndex];
1145
- if (tick === undefined) {
1146
- return;
1147
- }
1148
- const isRotated = config.axis_rotated;
1149
- const x = painter.crisp(margin.left, axis.lineWidth);
1150
- const y = painter.crisp(margin.top + height, axis.lineWidth);
1151
- const rangeStart = isRotated ? margin.top : margin.left;
1152
- const rangeEnd = isRotated ? margin.top + height : margin.left + width;
1153
- const format = axisInstance.getXAxisTickFormat();
1154
- const tickTextDirection = getXTickTextDirection(isRotated);
1155
- painter.clipRect(isRotated ?
1156
- getRotatedXAxisClipRect(margin, current.width, height) :
1157
- getHorizontalXAxisClipRect(margin, width, current.height), () => {
1158
- this.drawXAxisTickText($$, tick, format, axis.activeLabelColor, {
1159
- isRotated,
1160
- rangeEnd,
1161
- rangeStart,
1162
- targetScale: getXScale($$),
1163
- tickCount: ticks.length,
1164
- tickIndex,
1165
- tickTextDirection,
1166
- tickRotate: !isRotated ? ($$.getAxisTickRotate?.("x") || 0) : 0,
1167
- tickTextPosition: config.axis_x_tick_text_position,
1168
- ticks,
1169
- x,
1170
- y
1171
- });
1172
- });
1173
- }
1174
- /**
1175
- * Draw axis tooltip guide lines and scale labels on canvas overlay.
1176
- * @param {object} $$ ChartInternal instance
1177
- * @param {Array} point Canvas-local pointer coordinate
1178
- * @private
1179
- */
1180
- drawAxisTooltip($$, point) {
1181
- const { ctx, painter, theme: { style: { axis, grid } } } = this;
1182
- const { config, state: { margin, width, height } } = $$;
1183
- if (!config.axis_tooltip || !point) {
1184
- return;
1185
- }
1186
- const isRotated = config.axis_rotated;
1187
- const localX = point[0] - margin.left;
1188
- const localY = point[1] - margin.top;
1189
- const isInXRange = localX >= 0 && localX <= width;
1190
- const isInYRange = localY >= 0 && localY <= height;
1191
- if (!isInXRange && !isInYRange) {
1192
- return;
1193
- }
1194
- const absX = margin.left + localX;
1195
- const absY = margin.top + localY;
1196
- const fontSize = getFontSize(axis.labelFont);
1197
- const lineHeight = fontSize || 10;
1198
- const drawLabel = (id, scaleValue, x, y, textAlign) => {
1199
- const bg = getAxisTooltipBackgroundColor($$, id);
1200
- const text = bg && formatAxisTooltipValue($$, id, scaleValue);
1201
- if (!text) {
1202
- return;
1203
- }
1204
- ctx.font = axis.labelFont;
1205
- ctx.textAlign = textAlign;
1206
- ctx.textBaseline = "alphabetic";
1207
- const metrics = ctx.measureText(text);
1208
- const textWidth = metrics.width;
1209
- const ascent = metrics.actualBoundingBoxAscent || lineHeight * 0.8;
1210
- const descent = metrics.actualBoundingBoxDescent || lineHeight * 0.2;
1211
- const paddingX = Math.max(2, textWidth * 0.15);
1212
- const paddingY = Math.max(3, lineHeight * 0.25);
1213
- const textLeft = textAlign === "right" ?
1214
- x - textWidth :
1215
- textAlign === "center" ?
1216
- x - textWidth / 2 :
1217
- x;
1218
- const textTop = y - ascent;
1219
- ctx.fillStyle = bg;
1220
- ctx.fillRect(textLeft - paddingX, textTop - paddingY, textWidth + paddingX * 2, ascent + descent + paddingY * 2);
1221
- ctx.fillStyle = "#fff";
1222
- ctx.fillText(text, x, y);
1223
- };
1224
- painter.withState(() => {
1225
- ctx.strokeStyle = grid.lineColor;
1226
- ctx.lineWidth = grid.lineWidth;
1227
- ctx.setLineDash([]);
1228
- painter.strokePath(() => {
1229
- if (isInXRange) {
1230
- painter.traceLine(absX, margin.top, absX, margin.top + height);
1231
- }
1232
- if (isInYRange) {
1233
- painter.traceLine(margin.left, absY, margin.left + width, absY);
1234
- }
1235
- });
1236
- ctx.setLineDash([]);
1237
- if (isRotated) {
1238
- isInYRange && config.axis_x_show &&
1239
- drawLabel("x", localY, margin.left - fontSize * 0.3, absY + fontSize * 0.4, "right");
1240
- isInXRange && config.axis_y_show &&
1241
- drawLabel("y", localX, absX - fontSize * 1.3, margin.top + height + fontSize * 1.15, "left");
1242
- isInXRange && config.axis_y2_show && $$.scale.y2 &&
1243
- drawLabel("y2", localX, absX - fontSize * 1.3, margin.top - fontSize * 0.4, "left");
1244
- }
1245
- else {
1246
- isInXRange && config.axis_x_show &&
1247
- drawLabel("x", localX, absX - fontSize, margin.top + height + fontSize * 1.15, "left");
1248
- isInYRange && config.axis_y_show &&
1249
- drawLabel("y", localY, margin.left - fontSize * 0.4, absY + fontSize * 0.3, "right");
1250
- isInYRange && config.axis_y2_show && $$.scale.y2 &&
1251
- drawLabel("y2", localY, margin.left + width + fontSize * 0.4, absY + fontSize * 0.3, "left");
1252
- }
1253
- });
1254
- }
1255
- /**
1256
- * Draw one x-axis tick text using the same layout as SVG axis ticks.
1257
- * @param {object} $$ ChartInternal instance
1258
- * @param {number|string|Date} tick Tick value
1259
- * @param {function} format Tick formatter
1260
- * @param {string} fill Text fill color
1261
- * @param {object} options X-axis drawing options
1262
- * @private
1263
- */
1264
- drawXAxisTickText($$, tick, format, fill, options) {
1265
- const { ctx, painter, theme: { style: { axis } } } = this;
1266
- const { state: { margin } } = $$;
1267
- const { isRotated, rangeEnd, rangeStart, tickTextDirection, tickRotate, tickTextPosition, targetScale, ticks, x, y } = options;
1268
- const tickPos = getXPosition($$, tick, targetScale);
1269
- const tx = margin.left + tickPos;
1270
- const ty = margin.top + tickPos;
1271
- const pos = isRotated ? ty : tx;
1272
- if (!isInAxisRange(pos, rangeStart, rangeEnd)) {
1273
- return;
1274
- }
1275
- const tickFont = options.tickFont ?? getAxisTickFont(axis, "x");
1276
- ctx.font = tickFont;
1277
- ctx.fillStyle = fill;
1278
- const lines = getXTickTextLines($$, painter, format, tick, ticks, isRotated, targetScale, options.tickTextWidth);
1279
- const lineHeight = options.tickLineHeight ??
1280
- getXTickTextLineHeight(painter, getFontSize(tickFont));
1281
- let textX;
1282
- let textY;
1283
- if (isRotated) {
1284
- textX = x + ((AXIS_TICK_SIZE + AXIS_TICK_PADDING) * tickTextDirection) +
1285
- (tickTextPosition.x || 0);
1286
- textY = ty + (tickTextPosition.y || 0);
1287
- ctx.textAlign = getXTickTextAlign($$, options);
1288
- ctx.textBaseline = "middle";
1289
- }
1290
- else if (tickRotate) {
1291
- const fontSize = getFontSize(tickFont);
1292
- const firstDy = tickTextPosition.y ?
1293
- resolveTextOffset(tickTextPosition.y, fontSize) :
1294
- 0.71 * fontSize;
1295
- const textDx = getRotatedXTickTextDx(tickRotate) +
1296
- (tickTextPosition.x || 0);
1297
- const textY = getRotatedXTickTextY(tickRotate) + firstDy;
1298
- ctx.textAlign = getXTickTextAlign($$, options);
1299
- ctx.textBaseline = "alphabetic";
1300
- painter.withState(textCtx => {
1301
- textCtx.translate(tx, y);
1302
- textCtx.rotate(tickRotate * Math.PI / 180);
1303
- lines.forEach((line, i) => {
1304
- textCtx.fillText(line, textDx, textY + (i * lineHeight));
1305
- });
1306
- });
1307
- return;
1308
- }
1309
- else {
1310
- textX = tx + (tickTextPosition.x || 0);
1311
- textY = y + ((AXIS_TICK_SIZE + AXIS_TICK_PADDING) * tickTextDirection) +
1312
- (tickTextPosition.y || 0);
1313
- ctx.textAlign = getXTickTextAlign($$, options);
1314
- ctx.textBaseline = tickTextDirection > 0 ? "top" : "bottom";
1315
- }
1316
- painter.withState(textCtx => {
1317
- textCtx.translate(textX, textY);
1318
- tickRotate && textCtx.rotate(tickRotate * Math.PI / 180);
1319
- lines.forEach((line, i) => {
1320
- textCtx.fillText(line, 0, i * lineHeight);
1321
- });
1322
- });
1323
- }
1324
- /**
1325
- * Draw the y axis.
1326
- * @param {object} $$ ChartInternal instance
1327
- * @param {string} id Axis id
1328
- * @param {object} axisOptions Additional axis options
1329
- * @private
1330
- */
1331
- drawYAxis($$, id = "y", axisOptions) {
1332
- const { ctx, painter, theme: { style: { axis } } } = this;
1333
- const { config, scale, state: { margin, width, height } } = $$;
1334
- const prefix = `axis_${id}`;
1335
- const targetScale = axisOptions?.scale || scale[id];
1336
- const isY2 = id === "y2";
1337
- const isRotated = config.axis_rotated;
1338
- const axisOffset = axisOptions?.index ? $$.getAxisSize(id) * axisOptions.index : 0;
1339
- const x = painter.crisp(margin.left + (isY2 ? width + (isRotated ? 0 : axisOffset) : -axisOffset), axis.lineWidth);
1340
- const y = painter.crisp(margin.top + (isRotated ? (isY2 ? -axisOffset - 1 : height + axisOffset) : 0), axis.lineWidth);
1341
- const x1 = margin.left;
1342
- const x2 = margin.left + width;
1343
- const y1 = margin.top;
1344
- const y2 = margin.top + height;
1345
- const ticks = axisOptions?.ticks || getYTickValues($$, id);
1346
- const lineTicks = axisOptions?.ticks || (config[`${prefix}_tick_culling`] && config[`${prefix}_tick_culling_lines`] !== false ?
1347
- getYTickValues($$, id, undefined, false) :
1348
- ticks);
1349
- const format = axisOptions?.format || $$.axis?.[id]?.tickFormat?.() ||
1350
- config[`${prefix}_tick_format`]?.bind($$.api) ||
1351
- (v => v);
1352
- const outerTick = axisOptions ? axisOptions.outerTick : config[`${prefix}_tick_outer`];
1353
- const tickDirection = isRotated ?
1354
- (isY2 ? (config.axis_y2_tick_inner ? 1 : -1) : (config.axis_y_tick_inner ? -1 : 1)) :
1355
- (isY2 ? (config.axis_y2_tick_inner ? -1 : 1) : (config.axis_y_tick_inner ? 1 : -1));
1356
- const outerTickDirection = getYOuterTickDirection(config, isRotated, isY2);
1357
- const tickTextDirection = getYTickTextDirection(isRotated, isY2);
1358
- const tickTextPosition = config[`${prefix}_tick_text_position`];
1359
- painter.withState(() => {
1360
- ctx.strokeStyle = axis.lineColor;
1361
- ctx.lineWidth = axis.lineWidth;
1362
- painter.strokePath(() => {
1363
- if (isRotated) {
1364
- painter.traceLine(x1, y, x2, y);
1365
- }
1366
- else {
1367
- painter.traceLine(x, y1, x, y2);
1368
- }
1369
- if (outerTick) {
1370
- if (isRotated) {
1371
- painter.traceLine(x1, y, x1, y + (AXIS_TICK_SIZE * outerTickDirection));
1372
- painter.traceLine(x2, y, x2, y + (AXIS_TICK_SIZE * outerTickDirection));
1373
- }
1374
- else {
1375
- painter.traceLine(x, y1, x + (AXIS_TICK_SIZE * outerTickDirection), y1);
1376
- painter.traceLine(x, y2, x + (AXIS_TICK_SIZE * outerTickDirection), y2);
1377
- }
1378
- }
1379
- });
1380
- const tickFont = getAxisTickFont(axis, id);
1381
- ctx.font = tickFont;
1382
- ctx.fillStyle = axis.labelColor;
1383
- ctx.textAlign = isRotated ? "center" : (tickTextDirection > 0 ? "left" : "right");
1384
- ctx.textBaseline = isRotated ? (tickTextDirection > 0 ? "top" : "bottom") : "middle";
1385
- ctx.strokeStyle = axis.tickColor;
1386
- ctx.lineWidth = axis.tickWidth;
1387
- const drawableTicks = [];
1388
- const drawableLineTicks = [];
1389
- const addDrawableTick = (tick, target) => {
1390
- const value = normalizeYValue($$, tick, id);
1391
- const tx = margin.left + targetScale(value);
1392
- const ty = margin.top + targetScale(value);
1393
- const pos = isRotated ? tx : ty;
1394
- if (!isDrawable(pos)) {
1395
- return;
1396
- }
1397
- target.push({ tick, tx, ty });
1398
- };
1399
- for (const tick of ticks) {
1400
- addDrawableTick(tick, drawableTicks);
1401
- }
1402
- for (const tick of lineTicks) {
1403
- addDrawableTick(tick, drawableLineTicks);
1404
- }
1405
- if (axisOptions || config[`${prefix}_tick_show`]) {
1406
- painter.strokePath(() => {
1407
- for (const { tx, ty } of drawableLineTicks) {
1408
- if (isRotated) {
1409
- painter.traceLine(tx, y, tx, y + (AXIS_TICK_SIZE * tickDirection));
1410
- }
1411
- else {
1412
- painter.traceLine(x, ty, x + (AXIS_TICK_SIZE * tickDirection), ty);
1413
- }
1414
- }
1415
- });
1416
- }
1417
- if (axisOptions || config[`${prefix}_tick_text_show`]) {
1418
- for (const { tick, tx, ty } of drawableTicks) {
1419
- if (isRotated) {
1420
- painter.text(formatTick(format, tick), tx + (tickTextPosition.x || 0), y + (AXIS_TICK_SIZE * tickTextDirection) +
1421
- (tickTextPosition.y || 0));
1422
- }
1423
- else {
1424
- painter.text(formatTick(format, tick), x + ((AXIS_TICK_SIZE + AXIS_TICK_PADDING) * tickTextDirection) +
1425
- (tickTextPosition.x || 0), ty + (tickTextPosition.y || 0));
1426
- }
1427
- }
1428
- }
1429
- });
1430
- }
1431
- }
1432
-
522
+ * Draw axes and grid lines on canvas.
523
+ * @private
524
+ */
525
+ var CanvasAxisRenderer = class {
526
+ engine;
527
+ theme;
528
+ painter;
529
+ /**
530
+ * Constructor.
531
+ * @param {CanvasEngine} engine Canvas drawing engine
532
+ * @param {CanvasTheme} theme Canvas theme resolver
533
+ * @private
534
+ */
535
+ constructor(engine, theme) {
536
+ this.engine = engine;
537
+ this.theme = theme;
538
+ this.painter = new CanvasPainter(engine.ctx);
539
+ }
540
+ /**
541
+ * Get the drawing context for the main canvas.
542
+ * @returns {CanvasRenderingContext2D} Canvas drawing context
543
+ * @private
544
+ */
545
+ get ctx() {
546
+ return this.painter.context;
547
+ }
548
+ /**
549
+ * Run axis renderer draw calls on another canvas context.
550
+ * @param {CanvasRenderingContext2D} ctx Canvas drawing context
551
+ * @param {function} draw Draw callback
552
+ * @private
553
+ */
554
+ withContext(ctx, draw) {
555
+ this.painter.withContext(ctx, draw);
556
+ }
557
+ /**
558
+ * Draw grid and axis layers.
559
+ * @param {object} $$ ChartInternal instance
560
+ * @private
561
+ */
562
+ draw($$) {
563
+ this.drawRegions($$);
564
+ this.drawGrid($$);
565
+ this.drawAxis($$);
566
+ }
567
+ /**
568
+ * Draw visible axes.
569
+ * @param {object} $$ ChartInternal instance
570
+ * @private
571
+ */
572
+ drawAxis($$) {
573
+ const { config } = $$;
574
+ config.axis_x_show && this.drawXAxis($$);
575
+ config.axis_y_show && this.drawYAxis($$);
576
+ config.axis_y2_show && $$.scale.y2 && this.drawYAxis($$, "y2");
577
+ this.drawAdditionalAxes($$);
578
+ this.drawAxisLabels($$);
579
+ }
580
+ /**
581
+ * Draw the canvas subchart x axis.
582
+ * @param {object} $$ ChartInternal instance
583
+ * @private
584
+ */
585
+ drawSubXAxis($$) {
586
+ const { ctx, painter, theme: { style: { axis } } } = this;
587
+ const { config, format, scale, state: { current, margin2, width2, height2 } } = $$;
588
+ if (!config.subchart_show || !config.subchart_axis_x_show || !scale.subX || width2 <= 0 || height2 <= 0) return;
589
+ const isRotated = config.axis_rotated;
590
+ const x = painter.crisp(margin2.left, axis.lineWidth);
591
+ const y = painter.crisp(margin2.top + height2, axis.lineWidth);
592
+ const x1 = margin2.left;
593
+ const x2 = margin2.left + width2;
594
+ const y1 = margin2.top;
595
+ const y2 = margin2.top + height2;
596
+ const rangeStart = isRotated ? y1 : x1;
597
+ const rangeEnd = isRotated ? y2 : x2;
598
+ const ticks = getSubXTickValues($$);
599
+ const lineTicks = getSubXTickLineValues($$, ticks, axis.tickWidth);
600
+ const outerTick = config.subchart_axis_x_tick_outer ?? config.axis_x_tick_outer;
601
+ const tickDirection = isRotated ? config.axis_x_tick_inner ? 1 : -1 : config.axis_x_tick_inner ? -1 : 1;
602
+ const outerTickDirection = getXOuterTickDirection(isRotated);
603
+ const tickTextDirection = getXTickTextDirection(isRotated);
604
+ const tickTextPosition = config.axis_x_tick_text_position;
605
+ const tickRotate = !isRotated ? $$.getAxisTickRotate?.("x") || 0 : 0;
606
+ const tickFormat = format.subXAxisTick || $$.axis?.getXAxisTickFormat?.(true);
607
+ painter.clipRect(isRotated ? getRotatedXAxisClipRect(margin2, current.width, height2) : {
608
+ ...getHorizontalXAxisClipRect(margin2, width2, current.height - margin2.top),
609
+ y: margin2.top
610
+ }, () => {
611
+ ctx.globalAlpha = 1;
612
+ ctx.strokeStyle = axis.lineColor;
613
+ ctx.lineWidth = axis.lineWidth;
614
+ ctx.setLineDash([]);
615
+ painter.strokePath(() => {
616
+ if (isRotated) painter.traceLine(x, y1, x, y2);
617
+ else painter.traceLine(x1, y, x2, y);
618
+ if (outerTick) if (isRotated) {
619
+ painter.traceLine(x, y1, x + 6 * outerTickDirection, y1);
620
+ painter.traceLine(x, y2, x + 6 * outerTickDirection, y2);
621
+ } else {
622
+ painter.traceLine(x1, y, x1, y + 6 * outerTickDirection);
623
+ painter.traceLine(x2, y, x2, y + 6 * outerTickDirection);
624
+ }
625
+ });
626
+ const tickFont = getAxisTickFont(axis, "x");
627
+ ctx.font = tickFont;
628
+ ctx.fillStyle = axis.labelColor;
629
+ ctx.textAlign = isRotated ? tickTextDirection > 0 ? "left" : "right" : "center";
630
+ ctx.textBaseline = isRotated ? "middle" : tickTextDirection > 0 ? "top" : "bottom";
631
+ ctx.strokeStyle = axis.tickColor;
632
+ ctx.lineWidth = axis.tickWidth;
633
+ const lineHeight = getXTickTextLineHeight(painter, getFontSize(tickFont));
634
+ const tickTextWidth = getXTickTextWidth($$, ticks, isRotated, scale.subX);
635
+ if (config.subchart_axis_x_tick_show) for (const tick of lineTicks) {
636
+ const tickPos = scale.subX(normalizeXValue($$, tick));
637
+ const tx = margin2.left + tickPos;
638
+ const ty = margin2.top + tickPos;
639
+ if (!isInAxisRange(isRotated ? ty : tx, rangeStart, rangeEnd)) continue;
640
+ painter.strokePath(() => {
641
+ if (isRotated) painter.traceLine(x, ty, x + 6 * tickDirection, ty);
642
+ else painter.traceLine(tx, y, tx, y + 6 * tickDirection);
643
+ });
644
+ }
645
+ for (const tick of ticks) {
646
+ const tickPos = scale.subX(normalizeXValue($$, tick));
647
+ const tx = margin2.left + tickPos;
648
+ const ty = margin2.top + tickPos;
649
+ if (!isInAxisRange(isRotated ? ty : tx, rangeStart, rangeEnd)) continue;
650
+ if (!config.subchart_axis_x_tick_text_show) continue;
651
+ const lines = getXTickTextLines($$, painter, tickFormat, tick, ticks, isRotated, scale.subX, tickTextWidth);
652
+ let textX;
653
+ let textY;
654
+ if (isRotated) {
655
+ textX = x + 9 * tickTextDirection + (tickTextPosition.x || 0);
656
+ textY = ty + (tickTextPosition.y || 0);
657
+ ctx.textAlign = tickTextDirection > 0 ? "left" : "right";
658
+ ctx.textBaseline = "middle";
659
+ } else {
660
+ textX = tx + (tickTextPosition.x || 0);
661
+ textY = y + 9 * tickTextDirection + (tickTextPosition.y || 0);
662
+ ctx.textAlign = tickRotate ? tickRotate > 0 ? "left" : "right" : "center";
663
+ ctx.textBaseline = tickTextDirection > 0 ? "top" : "bottom";
664
+ }
665
+ painter.withState((textCtx) => {
666
+ textCtx.translate(textX, textY);
667
+ tickRotate && textCtx.rotate(tickRotate * Math.PI / 180);
668
+ lines.forEach((line, i) => {
669
+ textCtx.fillText(line, 0, i * lineHeight);
670
+ });
671
+ });
672
+ }
673
+ });
674
+ }
675
+ /**
676
+ * Draw the canvas subchart y/y2 axes.
677
+ * @param {object} $$ ChartInternal instance
678
+ * @private
679
+ */
680
+ drawSubYAxes($$) {
681
+ const { config, scale, state: { margin2, width2, height2 } } = $$;
682
+ if (!config.subchart_show || width2 <= 0 || height2 <= 0) return;
683
+ ["y", "y2"].forEach((id) => {
684
+ const subScale = id === "y2" ? scale.subY2 : scale.subY;
685
+ if (!config[`subchart_axis_${id}_show`] || !subScale) return;
686
+ this.drawYAxis($$, id, {
687
+ scale: subScale,
688
+ ticks: getYTickValues($$, id, void 0, true, subScale, `subchart_axis_${id}`),
689
+ format: getSubYTickFormat($$, id),
690
+ index: 0,
691
+ outerTick: config[`subchart_axis_${id}_tick_outer`] ?? config[`axis_${id}_tick_outer`],
692
+ margin: margin2,
693
+ width: width2,
694
+ height: height2,
695
+ prefix: `subchart_axis_${id}`,
696
+ tickShow: config[`subchart_axis_${id}_tick_show`],
697
+ tickTextShow: config[`subchart_axis_${id}_tick_text_show`],
698
+ tickTextPosition: config[`axis_${id}_tick_text_position`]
699
+ });
700
+ });
701
+ }
702
+ /**
703
+ * Draw chart title.
704
+ * @param {object} $$ ChartInternal instance
705
+ * @private
706
+ */
707
+ drawTitle($$) {
708
+ const { ctx, painter, theme: { style: { title } } } = this;
709
+ const { config, state: { current } } = $$;
710
+ if (!config.title_text) return;
711
+ const lines = String(config.title_text).split("\n");
712
+ const fontSize = getFontSize(title.font);
713
+ const lineHeight = fontSize * 1.5;
714
+ const { x, align } = getTitleTextPosition(config.title_position, current.width);
715
+ const titleHeight = $$.getCanvasTitleHeight?.() ?? fontSize;
716
+ const y = (config.title_padding.top || 0) + titleHeight;
717
+ painter.withState(() => {
718
+ ctx.font = title.font;
719
+ ctx.fillStyle = title.color;
720
+ ctx.textAlign = align;
721
+ ctx.textBaseline = "alphabetic";
722
+ lines.forEach((line, i) => {
723
+ ctx.fillText(line, x, y + (i ? fontSize + (i - 1) * lineHeight : 0));
724
+ });
725
+ });
726
+ }
727
+ /**
728
+ * Draw axis labels.
729
+ * @param {object} $$ ChartInternal instance
730
+ * @private
731
+ */
732
+ drawAxisLabels($$) {
733
+ const { ctx, painter, theme: { style: { axis: style } } } = this;
734
+ const { axis, config } = $$;
735
+ const fontSize = getFontSize(style.labelFont);
736
+ const ids = [
737
+ "x",
738
+ "y",
739
+ "y2"
740
+ ];
741
+ const labelColorById = {
742
+ x: style.xLabelColor,
743
+ y: style.yLabelColor,
744
+ y2: style.y2LabelColor
745
+ };
746
+ const alignMap = {
747
+ start: "left",
748
+ middle: "center",
749
+ end: "right"
750
+ };
751
+ if (!axis) return;
752
+ painter.withState(() => {
753
+ ctx.font = style.labelFont;
754
+ ctx.textBaseline = "alphabetic";
755
+ ids.forEach((id) => {
756
+ const text = axis.getLabelText(id);
757
+ if (!text || !config[`axis_${id}_show`] || id === "y2" && !$$.scale.y2) return;
758
+ const isRotatedLabel = id === "x" && config.axis_rotated || id !== "x" && !config.axis_rotated;
759
+ const { x, y } = getAxisLabelCanvasPosition(getAxisLabelBasePosition($$, id), axis.xForAxisLabel(id) + resolveTextOffset(axis.dxForAxisLabel(id), fontSize), resolveTextOffset(axis.dyForAxisLabel(id), fontSize), isRotatedLabel);
760
+ const anchor = axis.textAnchorForAxisLabel(id);
761
+ ctx.fillStyle = labelColorById[id] || style.labelColor;
762
+ ctx.textAlign = alignMap[anchor] || "center";
763
+ painter.text(String(text), x, y, { angle: isRotatedLabel ? -90 : 0 });
764
+ });
765
+ });
766
+ }
767
+ /**
768
+ * Draw visible grid lines.
769
+ * @param {object} $$ ChartInternal instance
770
+ * @private
771
+ */
772
+ drawGrid($$) {
773
+ const { ctx, painter, theme: { style: { grid } } } = this;
774
+ const { config, scale, state: { height, margin, width } } = $$;
775
+ if (!grid.lineColor) return;
776
+ const isRotated = config.axis_rotated;
777
+ const x1 = margin.left;
778
+ const x2 = margin.left + width;
779
+ const y1 = margin.top;
780
+ const y2 = margin.top + height;
781
+ painter.withState(() => {
782
+ ctx.strokeStyle = grid.lineColor;
783
+ ctx.lineWidth = grid.lineWidth;
784
+ grid.dashArray.length && ctx.setLineDash(grid.dashArray);
785
+ if (config.grid_x_show && scale.x) painter.strokePath(() => {
786
+ for (const tick of getXTickValues($$)) {
787
+ const pos = getXPosition($$, tick);
788
+ if (!isDrawable(pos)) continue;
789
+ if (isRotated) painter.traceCrispLine(x1, margin.top + pos, x2, margin.top + pos, grid.lineWidth);
790
+ else painter.traceCrispLine(margin.left + pos, y1, margin.left + pos, y2, grid.lineWidth);
791
+ }
792
+ });
793
+ if (config.grid_y_show && scale.y) painter.strokePath(() => {
794
+ for (const tick of getYGridTickValues($$)) {
795
+ const value = normalizeYValue($$, tick);
796
+ const pos = scale.y(value);
797
+ if (!isDrawable(pos)) continue;
798
+ if (isRotated) painter.traceCrispLine(margin.left + pos, y1, margin.left + pos, y2, grid.lineWidth);
799
+ else painter.traceCrispLine(x1, margin.top + pos, x2, margin.top + pos, grid.lineWidth);
800
+ }
801
+ });
802
+ !config.grid_lines_front && this.drawGridLines($$);
803
+ });
804
+ }
805
+ /**
806
+ * Draw configured global regions.
807
+ * @param {object} $$ ChartInternal instance
808
+ * @private
809
+ */
810
+ drawRegions($$) {
811
+ const { ctx, painter, theme: { style: { region: style } } } = this;
812
+ const { config, state: { height, margin, width } } = $$;
813
+ const regions = config.regions || [];
814
+ if (!regions.length) return;
815
+ painter.clipRect({
816
+ x: margin.left,
817
+ y: margin.top,
818
+ w: width,
819
+ h: height
820
+ }, () => {
821
+ ctx.fillStyle = style.fill;
822
+ ctx.font = style.labelFont;
823
+ ctx.textBaseline = "top";
824
+ for (const region of regions) {
825
+ const rect = getRegionRect($$, region);
826
+ if (!rect || !isDrawable(rect.x) || !isDrawable(rect.y) || !rect.w || !rect.h) continue;
827
+ const x = margin.left + rect.x;
828
+ const y = margin.top + rect.y;
829
+ const w = rect.w;
830
+ const h = rect.h;
831
+ ctx.globalAlpha = Number.isFinite(region.opacity) ? region.opacity : style.opacity;
832
+ painter.fillRect({
833
+ x,
834
+ y,
835
+ w,
836
+ h
837
+ });
838
+ if (region.label?.text) {
839
+ const label = region.label;
840
+ const center = label.center || "";
841
+ const text = String(label.text);
842
+ const textWidth = painter.measureText(text).width;
843
+ const lineHeight = parseFloat(ctx.font) || 12;
844
+ let tx = x + (label.x || 0);
845
+ let ty = y + (label.y || 0);
846
+ if (center.indexOf("x") > -1) tx += (w - textWidth) / 2;
847
+ if (center.indexOf("y") > -1) ty += (h - lineHeight) / 2;
848
+ painter.text(text, tx, ty, {
849
+ angle: label.rotated ? -90 : 0,
850
+ alpha: 1,
851
+ fill: label.color || style.labelColor
852
+ });
853
+ }
854
+ }
855
+ });
856
+ }
857
+ /**
858
+ * Draw configured x/y grid lines and labels.
859
+ * @param {object} $$ ChartInternal instance
860
+ * @private
861
+ */
862
+ drawGridLines($$) {
863
+ const { ctx, painter, theme: { style: { axis, grid } } } = this;
864
+ const { config, scale, state: { height, margin, width } } = $$;
865
+ const isRotated = config.axis_rotated;
866
+ const x1 = margin.left;
867
+ const x2 = margin.left + width;
868
+ const y1 = margin.top;
869
+ const y2 = margin.top + height;
870
+ if (!grid.lineColor) return;
871
+ painter.withState(() => {
872
+ ctx.strokeStyle = grid.lineColor;
873
+ ctx.lineWidth = grid.lineWidth;
874
+ ctx.font = grid.labelFont || axis.labelFont;
875
+ ctx.fillStyle = grid.labelColor;
876
+ ctx.textBaseline = "middle";
877
+ ctx.setLineDash([]);
878
+ const drawLabel = (text, x, y, rotated = false, style) => {
879
+ if (!text) return;
880
+ painter.text(text, x, y, {
881
+ ...style,
882
+ angle: rotated ? -90 : 0
883
+ });
884
+ };
885
+ const drawLine = (line, axisId, draw) => {
886
+ const style = getGridLineCanvasStyle(this.theme.getGridLineStyle(axisId, line, "line"));
887
+ painter.strokePath(draw, style);
888
+ };
889
+ const getTextStyle = (axisId, line) => getGridTextCanvasStyle(this.theme.getGridLineStyle(axisId, line, "text"));
890
+ const drawXLine = (line) => {
891
+ if (line.value === void 0 || !scale.x) return;
892
+ const pos = getXPosition($$, line.value);
893
+ if (!isDrawable(pos)) return;
894
+ if (isRotated) {
895
+ const y = margin.top + pos;
896
+ drawLine(line, "x", () => {
897
+ painter.traceLine(x1, y, x2, y);
898
+ });
899
+ ctx.textAlign = line.position === "start" ? "left" : line.position === "middle" ? "center" : "right";
900
+ drawLabel(line.text, getLineTextPosition(line.position, x1, x2), y - 5, false, getTextStyle("x", line));
901
+ } else {
902
+ const x = margin.left + pos;
903
+ drawLine(line, "x", () => {
904
+ painter.traceLine(x, y1, x, y2);
905
+ });
906
+ ctx.textAlign = line.position === "start" ? "left" : line.position === "middle" ? "center" : "right";
907
+ drawLabel(line.text, x - 5, getRotatedLineTextPosition(line.position, y1, y2), true, getTextStyle("x", line));
908
+ }
909
+ };
910
+ const drawYLine = (line) => {
911
+ const targetScale = line.axis === "y2" ? scale.y2 : scale.y;
912
+ const axisId = line.axis === "y2" ? "y2" : "y";
913
+ if (line.value === void 0 || !targetScale) return;
914
+ const pos = targetScale(normalizeYValue($$, line.value, axisId));
915
+ if (!isDrawable(pos)) return;
916
+ if (isRotated) {
917
+ const x = margin.left + pos;
918
+ drawLine(line, "y", () => {
919
+ painter.traceLine(x, y1, x, y2);
920
+ });
921
+ ctx.textAlign = line.position === "start" ? "left" : line.position === "middle" ? "center" : "right";
922
+ drawLabel(line.text, x - 5, getRotatedLineTextPosition(line.position, y1, y2), true, getTextStyle("y", line));
923
+ } else {
924
+ const y = margin.top + pos;
925
+ drawLine(line, "y", () => {
926
+ painter.traceLine(x1, y, x2, y);
927
+ });
928
+ ctx.textAlign = line.position === "start" ? "left" : line.position === "middle" ? "center" : "right";
929
+ drawLabel(line.text, getLineTextPosition(line.position, x1, x2), y - 5, false, getTextStyle("y", line));
930
+ }
931
+ };
932
+ (config.grid_x_lines || []).forEach(drawXLine);
933
+ (config.grid_y_lines || []).forEach(drawYLine);
934
+ });
935
+ }
936
+ /**
937
+ * Draw axes configured with axis.x/y/y2.axes.
938
+ * @param {object} $$ ChartInternal instance
939
+ * @private
940
+ */
941
+ drawAdditionalAxes($$) {
942
+ [
943
+ "x",
944
+ "y",
945
+ "y2"
946
+ ].forEach((id) => {
947
+ const axesConfig = $$.config[`axis_${id}_axes`] || [];
948
+ if (!axesConfig.length || !$$.scale[id] || !$$.config[`axis_${id}_show`]) return;
949
+ axesConfig.forEach((axisConfig, index) => {
950
+ const scale = getAdditionalAxisScale($$, id, axisConfig);
951
+ if (!scale) return;
952
+ const options = {
953
+ scale,
954
+ ticks: getAdditionalAxisTickValues($$, id, scale, axisConfig),
955
+ format: getAdditionalAxisTickFormat($$, axisConfig),
956
+ index: index + 1,
957
+ outerTick: axisConfig.tick?.outer !== false
958
+ };
959
+ id === "x" ? this.drawXAxis($$, options) : this.drawYAxis($$, id, options);
960
+ });
961
+ });
962
+ }
963
+ /**
964
+ * Draw the x axis.
965
+ * @param {object} $$ ChartInternal instance
966
+ * @param {object} axisOptions Additional axis options
967
+ * @private
968
+ */
969
+ drawXAxis($$, axisOptions) {
970
+ const { ctx, painter, theme: { style: { axis } } } = this;
971
+ const { axis: axisInstance, config, state: { current, margin, width, height } } = $$;
972
+ const isRotated = config.axis_rotated;
973
+ const axisOffset = axisOptions?.index ? $$.getAxisSize("x") * axisOptions.index : 0;
974
+ const targetScale = axisOptions?.scale || getXScale($$);
975
+ const x = painter.crisp(margin.left - (isRotated ? axisOffset : 0), axis.lineWidth);
976
+ const y = painter.crisp(margin.top + height + (isRotated ? 0 : axisOffset), axis.lineWidth);
977
+ const x1 = margin.left;
978
+ const x2 = margin.left + width;
979
+ const y1 = margin.top;
980
+ const y2 = margin.top + height;
981
+ const rangeStart = isRotated ? y1 : x1;
982
+ const rangeEnd = isRotated ? y2 : x2;
983
+ const ticks = axisOptions?.ticks || getXTickValues($$);
984
+ const lineTicks = axisOptions?.ticks || getXTickLineValues($$, ticks, axis.tickWidth);
985
+ const format = axisOptions?.format || axisInstance.getXAxisTickFormat();
986
+ const outerTick = axisOptions ? axisOptions.outerTick : config.axis_x_tick_outer;
987
+ const tickDirection = isRotated ? config.axis_x_tick_inner ? 1 : -1 : config.axis_x_tick_inner ? -1 : 1;
988
+ const outerTickDirection = getXOuterTickDirection(isRotated);
989
+ const tickTextDirection = getXTickTextDirection(isRotated);
990
+ const tickTextPosition = config.axis_x_tick_text_position;
991
+ const tickRotate = !isRotated ? $$.getAxisTickRotate?.("x") || 0 : 0;
992
+ painter.clipRect(isRotated ? getRotatedXAxisClipRect(margin, current.width, height) : getHorizontalXAxisClipRect(margin, width, current.height), () => {
993
+ ctx.strokeStyle = axis.lineColor;
994
+ ctx.lineWidth = axis.lineWidth;
995
+ painter.strokePath(() => {
996
+ if (isRotated) painter.traceLine(x, y1, x, y2);
997
+ else painter.traceLine(x1, y, x2, y);
998
+ if (outerTick) if (isRotated) {
999
+ painter.traceLine(x, y1, x + 6 * outerTickDirection, y1);
1000
+ painter.traceLine(x, y2, x + 6 * outerTickDirection, y2);
1001
+ } else {
1002
+ painter.traceLine(x1, y, x1, y + 6 * outerTickDirection);
1003
+ painter.traceLine(x2, y, x2, y + 6 * outerTickDirection);
1004
+ }
1005
+ });
1006
+ ctx.font = getAxisTickFont(axis, "x");
1007
+ ctx.fillStyle = axis.labelColor;
1008
+ ctx.textAlign = isRotated ? tickTextDirection > 0 ? "left" : "right" : "center";
1009
+ ctx.textBaseline = isRotated ? "middle" : tickTextDirection > 0 ? "top" : "bottom";
1010
+ ctx.strokeStyle = axis.tickColor;
1011
+ ctx.lineWidth = axis.tickWidth;
1012
+ if (config.axis_x_tick_show) painter.strokePath(() => {
1013
+ for (const tick of lineTicks) {
1014
+ const tickPos = getXTickLinePosition($$, tick, targetScale);
1015
+ const tx = margin.left + tickPos;
1016
+ const ty = margin.top + tickPos;
1017
+ if (!isInAxisRange(isRotated ? ty : tx, rangeStart, rangeEnd)) continue;
1018
+ if (isRotated) painter.traceLine(x, ty, x + 6 * tickDirection, ty);
1019
+ else painter.traceLine(tx, y, tx, y + 6 * tickDirection);
1020
+ }
1021
+ });
1022
+ if (!axisOptions && !config.axis_x_tick_text_show) return;
1023
+ const tickFont = getAxisTickFont(axis, "x");
1024
+ const tickLineHeight = getXTickTextLineHeight(painter, getFontSize(tickFont));
1025
+ const tickTextWidth = getXTickTextWidth($$, ticks, isRotated, targetScale);
1026
+ ticks.forEach((tick, tickIndex) => {
1027
+ this.drawXAxisTickText($$, tick, format, axis.labelColor, {
1028
+ isRotated,
1029
+ rangeEnd,
1030
+ rangeStart,
1031
+ tickCount: ticks.length,
1032
+ tickFont,
1033
+ tickIndex,
1034
+ tickLineHeight,
1035
+ tickTextDirection,
1036
+ tickTextWidth,
1037
+ tickRotate,
1038
+ tickTextPosition,
1039
+ targetScale,
1040
+ ticks,
1041
+ x,
1042
+ y
1043
+ });
1044
+ });
1045
+ });
1046
+ }
1047
+ /**
1048
+ * Draw the focused x-axis tick text with the SVG active tick color.
1049
+ * @param {object} $$ ChartInternal instance
1050
+ * @param {Array} focusData Focused data rows
1051
+ * @private
1052
+ */
1053
+ drawFocusedXAxisTick($$, focusData) {
1054
+ const { painter, theme: { style: { axis } } } = this;
1055
+ const { axis: axisInstance, config, state: { current, margin, width, height } } = $$;
1056
+ const focusX = focusData?.[0]?.x;
1057
+ if (focusX === void 0 || !axisInstance || !config.axis_x_show || !config.axis_x_tick_text_show || !axis.activeLabelColor || axis.activeLabelColor === axis.labelColor) return;
1058
+ const ticks = getXTickValues($$);
1059
+ const tickIndex = ticks.findIndex((value) => isSameTickValue(value, focusX));
1060
+ const tick = ticks[tickIndex];
1061
+ if (tick === void 0) return;
1062
+ const isRotated = config.axis_rotated;
1063
+ const x = painter.crisp(margin.left, axis.lineWidth);
1064
+ const y = painter.crisp(margin.top + height, axis.lineWidth);
1065
+ const rangeStart = isRotated ? margin.top : margin.left;
1066
+ const rangeEnd = isRotated ? margin.top + height : margin.left + width;
1067
+ const format = axisInstance.getXAxisTickFormat();
1068
+ const tickTextDirection = getXTickTextDirection(isRotated);
1069
+ painter.clipRect(isRotated ? getRotatedXAxisClipRect(margin, current.width, height) : getHorizontalXAxisClipRect(margin, width, current.height), () => {
1070
+ this.drawXAxisTickText($$, tick, format, axis.activeLabelColor, {
1071
+ isRotated,
1072
+ rangeEnd,
1073
+ rangeStart,
1074
+ targetScale: getXScale($$),
1075
+ tickCount: ticks.length,
1076
+ tickIndex,
1077
+ tickTextDirection,
1078
+ tickRotate: !isRotated ? $$.getAxisTickRotate?.("x") || 0 : 0,
1079
+ tickTextPosition: config.axis_x_tick_text_position,
1080
+ ticks,
1081
+ x,
1082
+ y
1083
+ });
1084
+ });
1085
+ }
1086
+ /**
1087
+ * Draw axis tooltip guide lines and scale labels on canvas overlay.
1088
+ * @param {object} $$ ChartInternal instance
1089
+ * @param {Array} point Canvas-local pointer coordinate
1090
+ * @private
1091
+ */
1092
+ drawAxisTooltip($$, point) {
1093
+ const { ctx, painter, theme: { style: { axis, grid } } } = this;
1094
+ const { config, state: { margin, width, height } } = $$;
1095
+ if (!config.axis_tooltip || !point) return;
1096
+ const isRotated = config.axis_rotated;
1097
+ const localX = point[0] - margin.left;
1098
+ const localY = point[1] - margin.top;
1099
+ const isInXRange = localX >= 0 && localX <= width;
1100
+ const isInYRange = localY >= 0 && localY <= height;
1101
+ if (!isInXRange && !isInYRange) return;
1102
+ const absX = margin.left + localX;
1103
+ const absY = margin.top + localY;
1104
+ const fontSize = getFontSize(axis.labelFont);
1105
+ const lineHeight = fontSize || 10;
1106
+ const drawLabel = (id, scaleValue, x, y, textAlign) => {
1107
+ const bg = getAxisTooltipBackgroundColor($$, id);
1108
+ const text = bg && formatAxisTooltipValue($$, id, scaleValue);
1109
+ if (!text) return;
1110
+ ctx.font = axis.labelFont;
1111
+ ctx.textAlign = textAlign;
1112
+ ctx.textBaseline = "alphabetic";
1113
+ const metrics = ctx.measureText(text);
1114
+ const textWidth = metrics.width;
1115
+ const ascent = metrics.actualBoundingBoxAscent || lineHeight * .8;
1116
+ const descent = metrics.actualBoundingBoxDescent || lineHeight * .2;
1117
+ const paddingX = Math.max(2, textWidth * .15);
1118
+ const paddingY = Math.max(3, lineHeight * .25);
1119
+ const textLeft = textAlign === "right" ? x - textWidth : textAlign === "center" ? x - textWidth / 2 : x;
1120
+ const textTop = y - ascent;
1121
+ ctx.fillStyle = bg;
1122
+ ctx.fillRect(textLeft - paddingX, textTop - paddingY, textWidth + paddingX * 2, ascent + descent + paddingY * 2);
1123
+ ctx.fillStyle = "#fff";
1124
+ ctx.fillText(text, x, y);
1125
+ };
1126
+ painter.withState(() => {
1127
+ ctx.strokeStyle = grid.lineColor;
1128
+ ctx.lineWidth = grid.lineWidth;
1129
+ ctx.setLineDash([]);
1130
+ painter.strokePath(() => {
1131
+ if (isInXRange) painter.traceLine(absX, margin.top, absX, margin.top + height);
1132
+ if (isInYRange) painter.traceLine(margin.left, absY, margin.left + width, absY);
1133
+ });
1134
+ ctx.setLineDash([]);
1135
+ if (isRotated) {
1136
+ isInYRange && config.axis_x_show && drawLabel("x", localY, margin.left - fontSize * .3, absY + fontSize * .4, "right");
1137
+ isInXRange && config.axis_y_show && drawLabel("y", localX, absX - fontSize * 1.3, margin.top + height + fontSize * 1.15, "left");
1138
+ isInXRange && config.axis_y2_show && $$.scale.y2 && drawLabel("y2", localX, absX - fontSize * 1.3, margin.top - fontSize * .4, "left");
1139
+ } else {
1140
+ isInXRange && config.axis_x_show && drawLabel("x", localX, absX - fontSize, margin.top + height + fontSize * 1.15, "left");
1141
+ isInYRange && config.axis_y_show && drawLabel("y", localY, margin.left - fontSize * .4, absY + fontSize * .3, "right");
1142
+ isInYRange && config.axis_y2_show && $$.scale.y2 && drawLabel("y2", localY, margin.left + width + fontSize * .4, absY + fontSize * .3, "left");
1143
+ }
1144
+ });
1145
+ }
1146
+ /**
1147
+ * Draw one x-axis tick text using the same layout as SVG axis ticks.
1148
+ * @param {object} $$ ChartInternal instance
1149
+ * @param {number|string|Date} tick Tick value
1150
+ * @param {function} format Tick formatter
1151
+ * @param {string} fill Text fill color
1152
+ * @param {object} options X-axis drawing options
1153
+ * @private
1154
+ */
1155
+ drawXAxisTickText($$, tick, format, fill, options) {
1156
+ const { ctx, painter, theme: { style: { axis } } } = this;
1157
+ const { state: { margin } } = $$;
1158
+ const { isRotated, rangeEnd, rangeStart, tickTextDirection, tickRotate, tickTextPosition, targetScale, ticks, x, y } = options;
1159
+ const tickPos = getXPosition($$, tick, targetScale);
1160
+ const tx = margin.left + tickPos;
1161
+ const ty = margin.top + tickPos;
1162
+ if (!isInAxisRange(isRotated ? ty : tx, rangeStart, rangeEnd)) return;
1163
+ const tickFont = options.tickFont ?? getAxisTickFont(axis, "x");
1164
+ ctx.font = tickFont;
1165
+ ctx.fillStyle = fill;
1166
+ const lines = getXTickTextLines($$, painter, format, tick, ticks, isRotated, targetScale, options.tickTextWidth);
1167
+ const lineHeight = options.tickLineHeight ?? getXTickTextLineHeight(painter, getFontSize(tickFont));
1168
+ let textX;
1169
+ let textY;
1170
+ if (isRotated) {
1171
+ textX = x + 9 * tickTextDirection + (tickTextPosition.x || 0);
1172
+ textY = ty + (tickTextPosition.y || 0);
1173
+ ctx.textAlign = getXTickTextAlign($$, options);
1174
+ ctx.textBaseline = "middle";
1175
+ } else if (tickRotate) {
1176
+ const fontSize = getFontSize(tickFont);
1177
+ const firstDy = tickTextPosition.y ? resolveTextOffset(tickTextPosition.y, fontSize) : .71 * fontSize;
1178
+ const textDx = getRotatedXTickTextDx(tickRotate) + (tickTextPosition.x || 0);
1179
+ const textY = getRotatedXTickTextY(tickRotate) + firstDy;
1180
+ ctx.textAlign = getXTickTextAlign($$, options);
1181
+ ctx.textBaseline = "alphabetic";
1182
+ painter.withState((textCtx) => {
1183
+ textCtx.translate(tx, y);
1184
+ textCtx.rotate(tickRotate * Math.PI / 180);
1185
+ lines.forEach((line, i) => {
1186
+ textCtx.fillText(line, textDx, textY + i * lineHeight);
1187
+ });
1188
+ });
1189
+ return;
1190
+ } else {
1191
+ textX = tx + (tickTextPosition.x || 0);
1192
+ textY = y + 9 * tickTextDirection + (tickTextPosition.y || 0);
1193
+ ctx.textAlign = getXTickTextAlign($$, options);
1194
+ ctx.textBaseline = tickTextDirection > 0 ? "top" : "bottom";
1195
+ }
1196
+ painter.withState((textCtx) => {
1197
+ textCtx.translate(textX, textY);
1198
+ tickRotate && textCtx.rotate(tickRotate * Math.PI / 180);
1199
+ lines.forEach((line, i) => {
1200
+ textCtx.fillText(line, 0, i * lineHeight);
1201
+ });
1202
+ });
1203
+ }
1204
+ /**
1205
+ * Draw the y axis.
1206
+ * @param {object} $$ ChartInternal instance
1207
+ * @param {string} id Axis id
1208
+ * @param {object} axisOptions Additional axis options
1209
+ * @private
1210
+ */
1211
+ drawYAxis($$, id = "y", axisOptions) {
1212
+ const { ctx, painter, theme: { style: { axis } } } = this;
1213
+ const { config, scale, state } = $$;
1214
+ const { margin, width, height } = state;
1215
+ const plotMargin = axisOptions?.margin || margin;
1216
+ const plotWidth = axisOptions?.width ?? width;
1217
+ const plotHeight = axisOptions?.height ?? height;
1218
+ const prefix = axisOptions?.prefix || `axis_${id}`;
1219
+ const targetScale = axisOptions?.scale || scale[id];
1220
+ const isY2 = id === "y2";
1221
+ const isRotated = config.axis_rotated;
1222
+ const axisOffset = axisOptions?.index ? $$.getAxisSize(id) * axisOptions.index : 0;
1223
+ const x = painter.crisp(plotMargin.left + (isY2 ? plotWidth + (isRotated ? 0 : axisOffset) : -axisOffset), axis.lineWidth);
1224
+ const y = painter.crisp(plotMargin.top + (isRotated ? isY2 ? -axisOffset - 1 : plotHeight + axisOffset : 0), axis.lineWidth);
1225
+ const x1 = plotMargin.left;
1226
+ const x2 = plotMargin.left + plotWidth;
1227
+ const y1 = plotMargin.top;
1228
+ const y2 = plotMargin.top + plotHeight;
1229
+ const ticks = axisOptions?.ticks || getYTickValues($$, id);
1230
+ const lineTicks = axisOptions?.ticks || (config[`${prefix}_tick_culling`] && config[`${prefix}_tick_culling_lines`] !== false ? getYTickValues($$, id, void 0, false) : ticks);
1231
+ const format = axisOptions?.format || $$.axis?.[id]?.tickFormat?.() || config[`${prefix}_tick_format`]?.bind($$.api) || ((v) => v);
1232
+ const outerTick = axisOptions ? axisOptions.outerTick : config[`${prefix}_tick_outer`];
1233
+ const tickDirection = isRotated ? isY2 ? config.axis_y2_tick_inner ? 1 : -1 : config.axis_y_tick_inner ? -1 : 1 : isY2 ? config.axis_y2_tick_inner ? -1 : 1 : config.axis_y_tick_inner ? 1 : -1;
1234
+ const outerTickDirection = getYOuterTickDirection(config, isRotated, isY2);
1235
+ const tickTextDirection = getYTickTextDirection(isRotated, isY2);
1236
+ const tickTextPosition = axisOptions?.tickTextPosition || config[`${prefix}_tick_text_position`] || config[`axis_${id}_tick_text_position`];
1237
+ const tickShow = axisOptions?.tickShow ?? (axisOptions ? true : config[`${prefix}_tick_show`]);
1238
+ const tickTextShow = axisOptions?.tickTextShow ?? (axisOptions ? true : config[`${prefix}_tick_text_show`]);
1239
+ painter.withState(() => {
1240
+ ctx.strokeStyle = axis.lineColor;
1241
+ ctx.lineWidth = axis.lineWidth;
1242
+ painter.strokePath(() => {
1243
+ if (isRotated) painter.traceLine(x1, y, x2, y);
1244
+ else painter.traceLine(x, y1, x, y2);
1245
+ if (outerTick) if (isRotated) {
1246
+ painter.traceLine(x1, y, x1, y + 6 * outerTickDirection);
1247
+ painter.traceLine(x2, y, x2, y + 6 * outerTickDirection);
1248
+ } else {
1249
+ painter.traceLine(x, y1, x + 6 * outerTickDirection, y1);
1250
+ painter.traceLine(x, y2, x + 6 * outerTickDirection, y2);
1251
+ }
1252
+ });
1253
+ const tickFont = getAxisTickFont(axis, id);
1254
+ ctx.font = tickFont;
1255
+ ctx.fillStyle = axis.labelColor;
1256
+ ctx.textAlign = isRotated ? "center" : tickTextDirection > 0 ? "left" : "right";
1257
+ ctx.textBaseline = isRotated ? tickTextDirection > 0 ? "top" : "bottom" : "middle";
1258
+ ctx.strokeStyle = axis.tickColor;
1259
+ ctx.lineWidth = axis.tickWidth;
1260
+ const drawableTicks = [];
1261
+ const drawableLineTicks = [];
1262
+ const addDrawableTick = (tick, target) => {
1263
+ const value = normalizeYValue($$, tick, id);
1264
+ const tx = plotMargin.left + targetScale(value);
1265
+ const ty = plotMargin.top + targetScale(value);
1266
+ if (!isDrawable(isRotated ? tx : ty)) return;
1267
+ target.push({
1268
+ tick,
1269
+ tx,
1270
+ ty
1271
+ });
1272
+ };
1273
+ for (const tick of ticks) addDrawableTick(tick, drawableTicks);
1274
+ for (const tick of lineTicks) addDrawableTick(tick, drawableLineTicks);
1275
+ if (tickShow) painter.strokePath(() => {
1276
+ for (const { tx, ty } of drawableLineTicks) if (isRotated) painter.traceLine(tx, y, tx, y + 6 * tickDirection);
1277
+ else painter.traceLine(x, ty, x + 6 * tickDirection, ty);
1278
+ });
1279
+ if (tickTextShow) for (const { tick, tx, ty } of drawableTicks) if (isRotated) painter.text(formatTick(format, tick), tx + (tickTextPosition.x || 0), y + 6 * tickTextDirection + (tickTextPosition.y || 0));
1280
+ else painter.text(formatTick(format, tick), x + 9 * tickTextDirection + (tickTextPosition.x || 0), ty + (tickTextPosition.y || 0));
1281
+ });
1282
+ }
1283
+ };
1284
+ //#endregion
1433
1285
  export { CanvasAxisRenderer as default };