billboard.js 4.0.3 → 4.1.0-next.2

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 (271) hide show
  1. package/README.md +82 -7
  2. package/dist/billboard.css +1 -1
  3. package/dist/billboard.js +1848 -494
  4. package/dist/billboard.min.css +1 -1
  5. package/dist/billboard.min.js +19 -24
  6. package/dist/billboard.pkgd.js +1844 -679
  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 +5 -3
  12. package/dist/plugin/billboardjs-plugin-bubblecompare.min.js +2 -2
  13. package/dist/plugin/billboardjs-plugin-sparkline.js +6 -3
  14. package/dist/plugin/billboardjs-plugin-sparkline.min.js +3 -3
  15. package/dist/plugin/billboardjs-plugin-stanford.js +6 -3
  16. package/dist/plugin/billboardjs-plugin-stanford.min.js +3 -3
  17. package/dist/plugin/billboardjs-plugin-tableview.js +59 -27
  18. package/dist/plugin/billboardjs-plugin-tableview.min.js +5 -5
  19. package/dist/plugin/billboardjs-plugin-textoverlap.js +75 -55
  20. package/dist/plugin/billboardjs-plugin-textoverlap.min.js +2 -2
  21. package/dist/plugin/pkgd/billboardjs-plugin-bubblecompare.pkgd.js +5 -3
  22. package/dist/plugin/pkgd/billboardjs-plugin-bubblecompare.pkgd.min.js +2 -2
  23. package/dist/plugin/pkgd/billboardjs-plugin-sparkline.pkgd.js +6 -3
  24. package/dist/plugin/pkgd/billboardjs-plugin-sparkline.pkgd.min.js +3 -3
  25. package/dist/plugin/pkgd/billboardjs-plugin-stanford.pkgd.js +6 -3
  26. package/dist/plugin/pkgd/billboardjs-plugin-stanford.pkgd.min.js +2 -2
  27. package/dist/plugin/pkgd/billboardjs-plugin-tableview.pkgd.js +59 -27
  28. package/dist/plugin/pkgd/billboardjs-plugin-tableview.pkgd.min.js +11 -11
  29. package/dist/plugin/pkgd/billboardjs-plugin-textoverlap.pkgd.js +68 -3557
  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 +8 -2
  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/util/type-checks.ts +3 -1
  266. package/src/module/voronoi.ts +119 -0
  267. package/src/module/worker.entry.ts +61 -0
  268. package/src/module/worker.ts +346 -63
  269. package/src/react/index.ts +193 -0
  270. package/types/options.d.ts +206 -3
  271. package/types/react/index.d.ts +27 -0
@@ -5,418 +5,402 @@
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.2
9
9
  */
10
- import { getFontSize } from './util.js';
11
-
10
+ import { getFontSize } from "./util.js";
11
+ //#region src/canvas/CanvasPainter.ts
12
12
  /**
13
- * Copyright (c) 2017 ~ present NAVER Corp.
14
- * billboard.js project is licensed under the MIT license
15
- */
13
+ * Copyright (c) 2017 ~ present NAVER Corp.
14
+ * billboard.js project is licensed under the MIT license
15
+ */
16
16
  /**
17
- * Centralized canvas drawing gateway.
18
- * @private
19
- */
20
- class CanvasPainter {
21
- ctx;
22
- /**
23
- * Constructor.
24
- * @param {CanvasRenderingContext2D} ctx Canvas drawing context
25
- * @private
26
- */
27
- constructor(ctx) {
28
- this.ctx = ctx;
29
- }
30
- /**
31
- * Get current drawing context.
32
- * @returns {CanvasRenderingContext2D} Canvas drawing context
33
- * @private
34
- */
35
- get context() {
36
- return this.ctx;
37
- }
38
- /**
39
- * Run a draw operation on another canvas context.
40
- * @param {CanvasRenderingContext2D} ctx Canvas drawing context
41
- * @param {function} draw Draw callback
42
- * @private
43
- */
44
- withContext(ctx, draw) {
45
- const prev = this.ctx;
46
- this.ctx = ctx;
47
- try {
48
- draw();
49
- }
50
- finally {
51
- this.ctx = prev;
52
- }
53
- }
54
- /**
55
- * Run a draw operation in an isolated canvas state.
56
- * @param {DrawCallback} draw Draw callback
57
- * @private
58
- */
59
- withState(draw) {
60
- const { ctx } = this;
61
- ctx.save();
62
- try {
63
- draw(ctx);
64
- }
65
- finally {
66
- ctx.restore();
67
- }
68
- }
69
- /**
70
- * Run a draw operation with a translated origin.
71
- * @param {number} x Translation x
72
- * @param {number} y Translation y
73
- * @param {DrawCallback} draw Draw callback
74
- * @private
75
- */
76
- withTranslation(x, y, draw) {
77
- this.withState(ctx => {
78
- ctx.translate(x, y);
79
- draw(ctx);
80
- });
81
- }
82
- /**
83
- * Run a draw operation clipped to a rectangle.
84
- * @param {object} rect Clip rectangle
85
- * @param {DrawCallback} draw Draw callback
86
- * @private
87
- */
88
- clipRect(rect, draw) {
89
- this.withState(ctx => {
90
- ctx.beginPath();
91
- ctx.rect(rect.x, rect.y, rect.w, rect.h);
92
- ctx.clip();
93
- draw(ctx);
94
- });
95
- }
96
- /**
97
- * Measure text using the current canvas state.
98
- * @param {string} text Text value
99
- * @returns {TextMetrics} Text metrics
100
- * @private
101
- */
102
- measureText(text) {
103
- return this.ctx.measureText(text);
104
- }
105
- /**
106
- * Add a line segment to the current path.
107
- * @param {number} x1 Start x
108
- * @param {number} y1 Start y
109
- * @param {number} x2 End x
110
- * @param {number} y2 End y
111
- * @private
112
- */
113
- traceLine(x1, y1, x2, y2) {
114
- const { ctx } = this;
115
- ctx.moveTo(x1, y1);
116
- ctx.lineTo(x2, y2);
117
- }
118
- /**
119
- * Add a crisp line segment to the current path.
120
- * @param {number} x1 Start x
121
- * @param {number} y1 Start y
122
- * @param {number} x2 End x
123
- * @param {number} y2 End y
124
- * @param {number} lineWidth Stroke width
125
- * @private
126
- */
127
- traceCrispLine(x1, y1, x2, y2, lineWidth) {
128
- this.traceLine(this.crisp(x1, lineWidth), this.crisp(y1, lineWidth), this.crisp(x2, lineWidth), this.crisp(y2, lineWidth));
129
- }
130
- /**
131
- * Add a circle to the current path.
132
- * @param {number} x Center x
133
- * @param {number} y Center y
134
- * @param {number} r Radius
135
- * @private
136
- */
137
- traceCircle(x, y, r) {
138
- this.ctx.moveTo(x + r, y);
139
- this.ctx.arc(x, y, r, 0, Math.PI * 2);
140
- }
141
- /**
142
- * Stroke a path.
143
- * @param {DrawCallback} draw Path callback
144
- * @param {object} style Optional style
145
- * @private
146
- */
147
- strokePath(draw, style) {
148
- this.withStyle(style, ctx => {
149
- ctx.beginPath();
150
- draw(ctx);
151
- ctx.stroke();
152
- });
153
- }
154
- /**
155
- * Fill a path.
156
- * @param {DrawCallback} draw Path callback
157
- * @param {object} style Optional style
158
- * @private
159
- */
160
- fillPath(draw, style) {
161
- this.withStyle(style, ctx => {
162
- ctx.beginPath();
163
- draw(ctx);
164
- ctx.fill();
165
- });
166
- }
167
- /**
168
- * Fill a rectangle.
169
- * @param {object} rect Rectangle
170
- * @param {object} style Optional style
171
- * @private
172
- */
173
- fillRect(rect, style) {
174
- this.withStyle(style, ctx => {
175
- ctx.fillRect(rect.x, rect.y, rect.w, rect.h);
176
- });
177
- }
178
- /**
179
- * Fill a rectangle with optional corner radii.
180
- * @param {object} rect Rectangle
181
- * @param {number|object} radius Corner radius
182
- * @param {object} style Optional style
183
- * @private
184
- */
185
- fillRoundRect(rect, radius = 0, style) {
186
- const normalized = this.normalizeRect(rect);
187
- const corners = this.getRectRadii(normalized, radius);
188
- if (!corners.tl && !corners.tr && !corners.br && !corners.bl) {
189
- this.fillRect(normalized, style);
190
- return;
191
- }
192
- this.fillPath(ctx => {
193
- this.traceRoundRect(ctx, normalized, corners);
194
- }, style);
195
- }
196
- /**
197
- * Stroke a rectangle with optional corner radii.
198
- * @param {object} rect Rectangle
199
- * @param {number|object} radius Corner radius
200
- * @param {object} style Optional style
201
- * @private
202
- */
203
- strokeRoundRect(rect, radius = 0, style) {
204
- const normalized = this.normalizeRect(rect);
205
- const corners = this.getRectRadii(normalized, radius);
206
- if (!corners.tl && !corners.tr && !corners.br && !corners.bl) {
207
- this.strokeRect(normalized, style);
208
- return;
209
- }
210
- this.strokePath(ctx => {
211
- this.traceRoundRect(ctx, normalized, corners);
212
- }, style);
213
- }
214
- /**
215
- * Stroke a rectangle.
216
- * @param {object} rect Rectangle
217
- * @param {object} style Optional style
218
- * @private
219
- */
220
- strokeRect(rect, style) {
221
- this.withStyle(style, ctx => {
222
- ctx.strokeRect(rect.x, rect.y, rect.w, rect.h);
223
- });
224
- }
225
- /**
226
- * Draw text.
227
- * @param {string} text Text value
228
- * @param {number} x X coordinate
229
- * @param {number} y Y coordinate
230
- * @param {object} style Optional style
231
- * @private
232
- */
233
- text(text, x, y, style) {
234
- const fillText = (ctx, textX, textY) => {
235
- style?.maxWidth === undefined ?
236
- ctx.fillText(text, textX, textY) :
237
- ctx.fillText(text, textX, textY, style.maxWidth);
238
- };
239
- const draw = (ctx) => {
240
- if (style?.angle) {
241
- ctx.translate(x, y);
242
- ctx.rotate(style.angle * Math.PI / 180);
243
- fillText(ctx, 0, 0);
244
- }
245
- else {
246
- fillText(ctx, x, y);
247
- }
248
- };
249
- if (style?.angle) {
250
- this.withState(ctx => {
251
- this.applyStyle(style);
252
- draw(ctx);
253
- });
254
- }
255
- else {
256
- this.withStyle(style, draw);
257
- }
258
- }
259
- /**
260
- * Draw a possibly rotated multiline text block.
261
- * @param {string} text Text value
262
- * @param {number} x X coordinate
263
- * @param {number} y Y coordinate
264
- * @param {object} style Optional style
265
- * @private
266
- */
267
- textLines(text, x, y, style) {
268
- this.withState(ctx => {
269
- const lines = text.split("\n");
270
- const lineHeight = getFontSize(style?.font || ctx.font);
271
- const firstLineY = lines.length > 1 ? -((lines.length - 1) * lineHeight) : 0;
272
- this.applyStyle(style);
273
- ctx.translate(x, y);
274
- style?.angle && ctx.rotate(style.angle * Math.PI / 180);
275
- lines.forEach((line, i) => {
276
- const lineY = firstLineY + (i * lineHeight);
277
- style?.maxWidth === undefined ?
278
- ctx.fillText(line, 0, lineY) :
279
- ctx.fillText(line, 0, lineY, style.maxWidth);
280
- });
281
- });
282
- }
283
- /**
284
- * Draw a point shape.
285
- * @param {string} type Point shape
286
- * @param {number} x X coordinate
287
- * @param {number} y Y coordinate
288
- * @param {number} r Radius
289
- * @param {object} style Optional style
290
- * @private
291
- */
292
- point(type, x, y, r, style) {
293
- this.withStyle(style, ctx => {
294
- const shouldFill = !style?.stroke || style.fill !== undefined;
295
- const shouldStroke = style?.stroke !== undefined;
296
- if (type === "rectangle") {
297
- const size = r * 2;
298
- const rect = { x: x - r, y: y - r, w: size, h: size };
299
- shouldFill && ctx.fillRect(rect.x, rect.y, rect.w, rect.h);
300
- shouldStroke && ctx.strokeRect(rect.x, rect.y, rect.w, rect.h);
301
- }
302
- else {
303
- ctx.beginPath();
304
- ctx.arc(x, y, r, 0, Math.PI * 2);
305
- shouldFill && ctx.fill();
306
- shouldStroke && ctx.stroke();
307
- }
308
- });
309
- }
310
- /**
311
- * Normalize rounded rectangle corner radii.
312
- * @param {object} rect Rectangle
313
- * @param {number|object} radius Corner radius
314
- * @returns {object} Corner radii
315
- * @private
316
- */
317
- getRectRadii(rect, radius) {
318
- const base = typeof radius === "number" ?
319
- { tl: radius, tr: radius, br: radius, bl: radius } :
320
- {
321
- tl: radius.tl || 0,
322
- tr: radius.tr || 0,
323
- br: radius.br || 0,
324
- bl: radius.bl || 0
325
- };
326
- const max = Math.max(0, Math.min(Math.abs(rect.w), Math.abs(rect.h)) / 2);
327
- return {
328
- tl: Math.max(0, Math.min(base.tl, max)),
329
- tr: Math.max(0, Math.min(base.tr, max)),
330
- br: Math.max(0, Math.min(base.br, max)),
331
- bl: Math.max(0, Math.min(base.bl, max))
332
- };
333
- }
334
- /**
335
- * Add a rounded rectangle to the current path.
336
- * @param {CanvasRenderingContext2D} ctx Canvas context
337
- * @param {object} rect Rectangle
338
- * @param {object} radius Corner radii
339
- * @private
340
- */
341
- traceRoundRect(ctx, rect, radius) {
342
- const { x, y, w, h } = rect;
343
- const right = x + w;
344
- const bottom = y + h;
345
- ctx.moveTo(x + radius.tl, y);
346
- ctx.lineTo(right - radius.tr, y);
347
- radius.tr ? ctx.quadraticCurveTo(right, y, right, y + radius.tr) : ctx.lineTo(right, y);
348
- ctx.lineTo(right, bottom - radius.br);
349
- radius.br ?
350
- ctx.quadraticCurveTo(right, bottom, right - radius.br, bottom) :
351
- ctx.lineTo(right, bottom);
352
- ctx.lineTo(x + radius.bl, bottom);
353
- radius.bl ? ctx.quadraticCurveTo(x, bottom, x, bottom - radius.bl) : ctx.lineTo(x, bottom);
354
- ctx.lineTo(x, y + radius.tl);
355
- radius.tl ? ctx.quadraticCurveTo(x, y, x + radius.tl, y) : ctx.lineTo(x, y);
356
- ctx.closePath();
357
- }
358
- /**
359
- * Normalize rectangle coordinates for path drawing.
360
- * @param {object} rect Rectangle
361
- * @returns {object} Normalized rectangle
362
- * @private
363
- */
364
- normalizeRect(rect) {
365
- const x = rect.w < 0 ? rect.x + rect.w : rect.x;
366
- const y = rect.h < 0 ? rect.y + rect.h : rect.y;
367
- return {
368
- x,
369
- y,
370
- w: Math.abs(rect.w),
371
- h: Math.abs(rect.h)
372
- };
373
- }
374
- /**
375
- * Apply drawing style.
376
- * @param {object} style Drawing style
377
- * @private
378
- */
379
- applyStyle(style) {
380
- if (!style) {
381
- return;
382
- }
383
- const { ctx } = this;
384
- style.fill !== undefined && (ctx.fillStyle = style.fill);
385
- style.stroke !== undefined && (ctx.strokeStyle = style.stroke);
386
- style.lineWidth !== undefined && (ctx.lineWidth = style.lineWidth);
387
- style.alpha !== undefined && (ctx.globalAlpha = style.alpha);
388
- style.font !== undefined && (ctx.font = style.font);
389
- style.textAlign !== undefined && (ctx.textAlign = style.textAlign);
390
- style.textBaseline !== undefined && (ctx.textBaseline = style.textBaseline);
391
- style.lineDash !== undefined && ctx.setLineDash(style.lineDash);
392
- }
393
- /**
394
- * Run with optional style state.
395
- * @param {object} style Optional drawing style
396
- * @param {DrawCallback} draw Draw callback
397
- * @private
398
- */
399
- withStyle(style, draw) {
400
- if (style) {
401
- this.withState(ctx => {
402
- this.applyStyle(style);
403
- draw(ctx);
404
- });
405
- }
406
- else {
407
- draw(this.ctx);
408
- }
409
- }
410
- /**
411
- * Get crisp canvas coordinate for axis/grid strokes.
412
- * @param {number} value Coordinate value
413
- * @param {number} lineWidth Stroke width
414
- * @returns {number} Crisp coordinate
415
- * @private
416
- */
417
- crisp(value, lineWidth) {
418
- return lineWidth % 2 ? Math.round(value) + 0.5 : Math.round(value);
419
- }
420
- }
421
-
17
+ * Centralized canvas drawing gateway.
18
+ * @private
19
+ */
20
+ var CanvasPainter = class {
21
+ ctx;
22
+ /**
23
+ * Constructor.
24
+ * @param {CanvasRenderingContext2D} ctx Canvas drawing context
25
+ * @private
26
+ */
27
+ constructor(ctx) {
28
+ this.ctx = ctx;
29
+ }
30
+ /**
31
+ * Get current drawing context.
32
+ * @returns {CanvasRenderingContext2D} Canvas drawing context
33
+ * @private
34
+ */
35
+ get context() {
36
+ return this.ctx;
37
+ }
38
+ /**
39
+ * Run a draw operation on another canvas context.
40
+ * @param {CanvasRenderingContext2D} ctx Canvas drawing context
41
+ * @param {function} draw Draw callback
42
+ * @private
43
+ */
44
+ withContext(ctx, draw) {
45
+ const prev = this.ctx;
46
+ this.ctx = ctx;
47
+ try {
48
+ draw();
49
+ } finally {
50
+ this.ctx = prev;
51
+ }
52
+ }
53
+ /**
54
+ * Run a draw operation in an isolated canvas state.
55
+ * @param {DrawCallback} draw Draw callback
56
+ * @private
57
+ */
58
+ withState(draw) {
59
+ const { ctx } = this;
60
+ ctx.save();
61
+ try {
62
+ draw(ctx);
63
+ } finally {
64
+ ctx.restore();
65
+ }
66
+ }
67
+ /**
68
+ * Run a draw operation with a translated origin.
69
+ * @param {number} x Translation x
70
+ * @param {number} y Translation y
71
+ * @param {DrawCallback} draw Draw callback
72
+ * @private
73
+ */
74
+ withTranslation(x, y, draw) {
75
+ this.withState((ctx) => {
76
+ ctx.translate(x, y);
77
+ draw(ctx);
78
+ });
79
+ }
80
+ /**
81
+ * Run a draw operation clipped to a rectangle.
82
+ * @param {object} rect Clip rectangle
83
+ * @param {DrawCallback} draw Draw callback
84
+ * @private
85
+ */
86
+ clipRect(rect, draw) {
87
+ this.withState((ctx) => {
88
+ ctx.beginPath();
89
+ ctx.rect(rect.x, rect.y, rect.w, rect.h);
90
+ ctx.clip();
91
+ draw(ctx);
92
+ });
93
+ }
94
+ /**
95
+ * Measure text using the current canvas state.
96
+ * @param {string} text Text value
97
+ * @returns {TextMetrics} Text metrics
98
+ * @private
99
+ */
100
+ measureText(text) {
101
+ return this.ctx.measureText(text);
102
+ }
103
+ /**
104
+ * Add a line segment to the current path.
105
+ * @param {number} x1 Start x
106
+ * @param {number} y1 Start y
107
+ * @param {number} x2 End x
108
+ * @param {number} y2 End y
109
+ * @private
110
+ */
111
+ traceLine(x1, y1, x2, y2) {
112
+ const { ctx } = this;
113
+ ctx.moveTo(x1, y1);
114
+ ctx.lineTo(x2, y2);
115
+ }
116
+ /**
117
+ * Add a crisp line segment to the current path.
118
+ * @param {number} x1 Start x
119
+ * @param {number} y1 Start y
120
+ * @param {number} x2 End x
121
+ * @param {number} y2 End y
122
+ * @param {number} lineWidth Stroke width
123
+ * @private
124
+ */
125
+ traceCrispLine(x1, y1, x2, y2, lineWidth) {
126
+ this.traceLine(this.crisp(x1, lineWidth), this.crisp(y1, lineWidth), this.crisp(x2, lineWidth), this.crisp(y2, lineWidth));
127
+ }
128
+ /**
129
+ * Add a circle to the current path.
130
+ * @param {number} x Center x
131
+ * @param {number} y Center y
132
+ * @param {number} r Radius
133
+ * @private
134
+ */
135
+ traceCircle(x, y, r) {
136
+ this.ctx.moveTo(x + r, y);
137
+ this.ctx.arc(x, y, r, 0, Math.PI * 2);
138
+ }
139
+ /**
140
+ * Stroke a path.
141
+ * @param {DrawCallback} draw Path callback
142
+ * @param {object} style Optional style
143
+ * @private
144
+ */
145
+ strokePath(draw, style) {
146
+ this.withStyle(style, (ctx) => {
147
+ ctx.beginPath();
148
+ draw(ctx);
149
+ ctx.stroke();
150
+ });
151
+ }
152
+ /**
153
+ * Fill a path.
154
+ * @param {DrawCallback} draw Path callback
155
+ * @param {object} style Optional style
156
+ * @private
157
+ */
158
+ fillPath(draw, style) {
159
+ this.withStyle(style, (ctx) => {
160
+ ctx.beginPath();
161
+ draw(ctx);
162
+ ctx.fill();
163
+ });
164
+ }
165
+ /**
166
+ * Fill a rectangle.
167
+ * @param {object} rect Rectangle
168
+ * @param {object} style Optional style
169
+ * @private
170
+ */
171
+ fillRect(rect, style) {
172
+ this.withStyle(style, (ctx) => {
173
+ ctx.fillRect(rect.x, rect.y, rect.w, rect.h);
174
+ });
175
+ }
176
+ /**
177
+ * Fill a rectangle with optional corner radii.
178
+ * @param {object} rect Rectangle
179
+ * @param {number|object} radius Corner radius
180
+ * @param {object} style Optional style
181
+ * @private
182
+ */
183
+ fillRoundRect(rect, radius = 0, style) {
184
+ const normalized = this.normalizeRect(rect);
185
+ const corners = this.getRectRadii(normalized, radius);
186
+ if (!corners.tl && !corners.tr && !corners.br && !corners.bl) {
187
+ this.fillRect(normalized, style);
188
+ return;
189
+ }
190
+ this.fillPath((ctx) => {
191
+ this.traceRoundRect(ctx, normalized, corners);
192
+ }, style);
193
+ }
194
+ /**
195
+ * Stroke a rectangle with optional corner radii.
196
+ * @param {object} rect Rectangle
197
+ * @param {number|object} radius Corner radius
198
+ * @param {object} style Optional style
199
+ * @private
200
+ */
201
+ strokeRoundRect(rect, radius = 0, style) {
202
+ const normalized = this.normalizeRect(rect);
203
+ const corners = this.getRectRadii(normalized, radius);
204
+ if (!corners.tl && !corners.tr && !corners.br && !corners.bl) {
205
+ this.strokeRect(normalized, style);
206
+ return;
207
+ }
208
+ this.strokePath((ctx) => {
209
+ this.traceRoundRect(ctx, normalized, corners);
210
+ }, style);
211
+ }
212
+ /**
213
+ * Stroke a rectangle.
214
+ * @param {object} rect Rectangle
215
+ * @param {object} style Optional style
216
+ * @private
217
+ */
218
+ strokeRect(rect, style) {
219
+ this.withStyle(style, (ctx) => {
220
+ ctx.strokeRect(rect.x, rect.y, rect.w, rect.h);
221
+ });
222
+ }
223
+ /**
224
+ * Draw text.
225
+ * @param {string} text Text value
226
+ * @param {number} x X coordinate
227
+ * @param {number} y Y coordinate
228
+ * @param {object} style Optional style
229
+ * @private
230
+ */
231
+ text(text, x, y, style) {
232
+ const fillText = (ctx, textX, textY) => {
233
+ style?.maxWidth === void 0 ? ctx.fillText(text, textX, textY) : ctx.fillText(text, textX, textY, style.maxWidth);
234
+ };
235
+ const draw = (ctx) => {
236
+ if (style?.angle) {
237
+ ctx.translate(x, y);
238
+ ctx.rotate(style.angle * Math.PI / 180);
239
+ fillText(ctx, 0, 0);
240
+ } else fillText(ctx, x, y);
241
+ };
242
+ if (style?.angle) this.withState((ctx) => {
243
+ this.applyStyle(style);
244
+ draw(ctx);
245
+ });
246
+ else this.withStyle(style, draw);
247
+ }
248
+ /**
249
+ * Draw a possibly rotated multiline text block.
250
+ * @param {string} text Text value
251
+ * @param {number} x X coordinate
252
+ * @param {number} y Y coordinate
253
+ * @param {object} style Optional style
254
+ * @private
255
+ */
256
+ textLines(text, x, y, style) {
257
+ this.withState((ctx) => {
258
+ const lines = text.split("\n");
259
+ const lineHeight = getFontSize(style?.font || ctx.font);
260
+ const firstLineY = lines.length > 1 ? -((lines.length - 1) * lineHeight) : 0;
261
+ this.applyStyle(style);
262
+ ctx.translate(x, y);
263
+ style?.angle && ctx.rotate(style.angle * Math.PI / 180);
264
+ lines.forEach((line, i) => {
265
+ const lineY = firstLineY + i * lineHeight;
266
+ style?.maxWidth === void 0 ? ctx.fillText(line, 0, lineY) : ctx.fillText(line, 0, lineY, style.maxWidth);
267
+ });
268
+ });
269
+ }
270
+ /**
271
+ * Draw a point shape.
272
+ * @param {string} type Point shape
273
+ * @param {number} x X coordinate
274
+ * @param {number} y Y coordinate
275
+ * @param {number} r Radius
276
+ * @param {object} style Optional style
277
+ * @private
278
+ */
279
+ point(type, x, y, r, style) {
280
+ this.withStyle(style, (ctx) => {
281
+ const shouldFill = !style?.stroke || style.fill !== void 0;
282
+ const shouldStroke = style?.stroke !== void 0;
283
+ if (type === "rectangle") {
284
+ const size = r * 2;
285
+ const rect = {
286
+ x: x - r,
287
+ y: y - r,
288
+ w: size,
289
+ h: size
290
+ };
291
+ shouldFill && ctx.fillRect(rect.x, rect.y, rect.w, rect.h);
292
+ shouldStroke && ctx.strokeRect(rect.x, rect.y, rect.w, rect.h);
293
+ } else {
294
+ ctx.beginPath();
295
+ ctx.arc(x, y, r, 0, Math.PI * 2);
296
+ shouldFill && ctx.fill();
297
+ shouldStroke && ctx.stroke();
298
+ }
299
+ });
300
+ }
301
+ /**
302
+ * Normalize rounded rectangle corner radii.
303
+ * @param {object} rect Rectangle
304
+ * @param {number|object} radius Corner radius
305
+ * @returns {object} Corner radii
306
+ * @private
307
+ */
308
+ getRectRadii(rect, radius) {
309
+ const base = typeof radius === "number" ? {
310
+ tl: radius,
311
+ tr: radius,
312
+ br: radius,
313
+ bl: radius
314
+ } : {
315
+ tl: radius.tl || 0,
316
+ tr: radius.tr || 0,
317
+ br: radius.br || 0,
318
+ bl: radius.bl || 0
319
+ };
320
+ const max = Math.max(0, Math.min(Math.abs(rect.w), Math.abs(rect.h)) / 2);
321
+ return {
322
+ tl: Math.max(0, Math.min(base.tl, max)),
323
+ tr: Math.max(0, Math.min(base.tr, max)),
324
+ br: Math.max(0, Math.min(base.br, max)),
325
+ bl: Math.max(0, Math.min(base.bl, max))
326
+ };
327
+ }
328
+ /**
329
+ * Add a rounded rectangle to the current path.
330
+ * @param {CanvasRenderingContext2D} ctx Canvas context
331
+ * @param {object} rect Rectangle
332
+ * @param {object} radius Corner radii
333
+ * @private
334
+ */
335
+ traceRoundRect(ctx, rect, radius) {
336
+ const { x, y, w, h } = rect;
337
+ const right = x + w;
338
+ const bottom = y + h;
339
+ ctx.moveTo(x + radius.tl, y);
340
+ ctx.lineTo(right - radius.tr, y);
341
+ radius.tr ? ctx.quadraticCurveTo(right, y, right, y + radius.tr) : ctx.lineTo(right, y);
342
+ ctx.lineTo(right, bottom - radius.br);
343
+ radius.br ? ctx.quadraticCurveTo(right, bottom, right - radius.br, bottom) : ctx.lineTo(right, bottom);
344
+ ctx.lineTo(x + radius.bl, bottom);
345
+ radius.bl ? ctx.quadraticCurveTo(x, bottom, x, bottom - radius.bl) : ctx.lineTo(x, bottom);
346
+ ctx.lineTo(x, y + radius.tl);
347
+ radius.tl ? ctx.quadraticCurveTo(x, y, x + radius.tl, y) : ctx.lineTo(x, y);
348
+ ctx.closePath();
349
+ }
350
+ /**
351
+ * Normalize rectangle coordinates for path drawing.
352
+ * @param {object} rect Rectangle
353
+ * @returns {object} Normalized rectangle
354
+ * @private
355
+ */
356
+ normalizeRect(rect) {
357
+ return {
358
+ x: rect.w < 0 ? rect.x + rect.w : rect.x,
359
+ y: rect.h < 0 ? rect.y + rect.h : rect.y,
360
+ w: Math.abs(rect.w),
361
+ h: Math.abs(rect.h)
362
+ };
363
+ }
364
+ /**
365
+ * Apply drawing style.
366
+ * @param {object} style Drawing style
367
+ * @private
368
+ */
369
+ applyStyle(style) {
370
+ if (!style) return;
371
+ const { ctx } = this;
372
+ style.fill !== void 0 && (ctx.fillStyle = style.fill);
373
+ style.stroke !== void 0 && (ctx.strokeStyle = style.stroke);
374
+ style.lineWidth !== void 0 && (ctx.lineWidth = style.lineWidth);
375
+ style.alpha !== void 0 && (ctx.globalAlpha = style.alpha);
376
+ style.font !== void 0 && (ctx.font = style.font);
377
+ style.textAlign !== void 0 && (ctx.textAlign = style.textAlign);
378
+ style.textBaseline !== void 0 && (ctx.textBaseline = style.textBaseline);
379
+ style.lineDash !== void 0 && ctx.setLineDash(style.lineDash);
380
+ }
381
+ /**
382
+ * Run with optional style state.
383
+ * @param {object} style Optional drawing style
384
+ * @param {DrawCallback} draw Draw callback
385
+ * @private
386
+ */
387
+ withStyle(style, draw) {
388
+ if (style) this.withState((ctx) => {
389
+ this.applyStyle(style);
390
+ draw(ctx);
391
+ });
392
+ else draw(this.ctx);
393
+ }
394
+ /**
395
+ * Get crisp canvas coordinate for axis/grid strokes.
396
+ * @param {number} value Coordinate value
397
+ * @param {number} lineWidth Stroke width
398
+ * @returns {number} Crisp coordinate
399
+ * @private
400
+ */
401
+ crisp(value, lineWidth) {
402
+ return lineWidth % 2 ? Math.round(value) + .5 : Math.round(value);
403
+ }
404
+ };
405
+ //#endregion
422
406
  export { CanvasPainter as default };