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,599 +5,463 @@
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 { select } from 'd3-selection';
11
- import { $TOOLTIP, $ARC } from '../../config/classes.js';
12
- import { document as doc } from '../../module/browser.js';
13
- import { callFn, tplProcess, parseDate } from '../../module/util/object.js';
14
- import { isValue, isFunction, isUndefined, isObject, isString, isEmpty } from '../../module/util/type-checks.js';
15
- import { getTransformCTM, getBoundingRect, getPointer, hasViewBox } from '../../module/util/dom.js';
16
- import { sanitize } from '../../module/sanitize.js';
17
-
10
+ import { document as doc } from "../../module/browser.js";
11
+ import { isEmpty, isFunction, isObject, isString, isUndefined, isValue } from "../../module/util/type-checks.js";
12
+ import { sanitize } from "../../module/sanitize.js";
13
+ import { callFn, parseDate, tplProcess } from "../../module/util/object.js";
14
+ import { getBoundingRect, getPointer, getTransformCTM, hasViewBox } from "../../module/util/dom.js";
15
+ import { $ARC, $TOOLTIP } from "../../config/classes.js";
16
+ import { select } from "d3-selection";
17
+ //#region src/ChartInternal/internals/tooltip.ts
18
18
  /**
19
- * Copyright (c) 2017 ~ present NAVER Corp.
20
- * billboard.js project is licensed under the MIT license
21
- */
22
- // Hoisted to module level to avoid recompilation on every getTooltipContentTemplate() call
19
+ * Copyright (c) 2017 ~ present NAVER Corp.
20
+ * billboard.js project is licensed under the MIT license
21
+ */
23
22
  const RE_WHITESPACE = /(\r?\n|\t)/g;
24
23
  const RE_TOOLTIP_TPL = /{{(.*)}}/;
25
- var tooltip = {
26
- /**
27
- * Initializes the tooltip
28
- * @private
29
- */
30
- initTooltip() {
31
- const $$ = this;
32
- const { config, $el } = $$;
33
- $el.tooltip = select(config.tooltip_contents.bindto);
34
- if ($el.tooltip.empty()) {
35
- $el.tooltip = $el.chart
36
- .append("div")
37
- .attr("class", $TOOLTIP.tooltipContainer)
38
- .style("position", "absolute")
39
- .style("pointer-events", "none")
40
- .style("display", "none");
41
- }
42
- $$.bindTooltipResizePos();
43
- },
44
- /**
45
- * Show tooltip at initialization.
46
- * Is called only when tooltip.init.show=true option is set
47
- * @private
48
- */
49
- initShowTooltip() {
50
- const $$ = this;
51
- const { config, $el, state: { hasAxis, hasRadar } } = $$;
52
- // Show tooltip if needed
53
- if (config.tooltip_init_show) {
54
- const isArc = !(hasAxis || hasRadar);
55
- if ($$.axis?.isTimeSeries() && isString(config.tooltip_init_x)) {
56
- config.tooltip_init_x = parseDate.call($$, config.tooltip_init_x);
57
- }
58
- $$.api.tooltip.show({
59
- data: {
60
- [isArc ? "index" : "x"]: config.tooltip_init_x
61
- }
62
- });
63
- const position = config.tooltip_init_position;
64
- if (!config.tooltip_contents.bindto && !isEmpty(position)) {
65
- const { top = 0, left = 50 } = position;
66
- $el.tooltip.style("top", isString(top) ? top : `${top}px`)
67
- .style("left", isString(left) ? left : `${left}px`)
68
- .style("display", null);
69
- }
70
- }
71
- },
72
- /**
73
- * Get the tooltip HTML string
74
- * @param {Array} args Arguments
75
- * @returns {string} Formatted HTML string
76
- * @private
77
- */
78
- getTooltipHTML(...args) {
79
- const $$ = this;
80
- const { api, config } = $$;
81
- return sanitize(isFunction(config.tooltip_contents) ?
82
- config.tooltip_contents.bind(api)(...args) :
83
- $$.getTooltipContent(...args));
84
- },
85
- /**
86
- * Returns the tooltip content(HTML string)
87
- * @param {object} d data
88
- * @param {function} defaultTitleFormat Default title format
89
- * @param {function} defaultValueFormat Default format for each data value in the tooltip.
90
- * @param {function} color Color function
91
- * @returns {string} html
92
- * @private
93
- */
94
- getTooltipContent(d, defaultTitleFormat, defaultValueFormat, color) {
95
- const $$ = this;
96
- const { api, config, state, $el } = $$;
97
- // get formatter function
98
- const titleFn = config.tooltip_format_title;
99
- const nameFn = config.tooltip_format_name;
100
- const valueFn = config.tooltip_format_value;
101
- // determine formatter function
102
- const titleFormat = isFunction(titleFn) ? titleFn.bind(api) : defaultTitleFormat;
103
- const nameFormat = isFunction(nameFn) ? nameFn.bind(api) : (name => name);
104
- const boundValueFn = isFunction(valueFn) ? valueFn.bind(api) : null;
105
- const valueFormat = (v, ratio, id, index) => {
106
- let fn = boundValueFn;
107
- if (!fn) {
108
- // For normalize per group, only show percentage for data in groups
109
- if (state.hasTreemap ||
110
- ($$.isStackNormalized() &&
111
- (!$$.isStackNormalizedPerGroup() || $$.isGrouped(id)))) {
112
- fn = (v, ratio) => `${(ratio * 100).toFixed(2)}%`;
113
- }
114
- else {
115
- fn = defaultValueFormat;
116
- }
117
- }
118
- return fn(v, ratio, id, index);
119
- };
120
- const order = config.tooltip_order;
121
- const getRowValue = row => ($$.axis && $$.isBubbleZType(row) ?
122
- $$.getBubbleZData(row.value, "z") :
123
- $$.getBaseValue(row));
124
- const getBgColor = $$.levelColor ? row => $$.levelColor(row.value) : row => color(row);
125
- const contents = config.tooltip_contents;
126
- const tplStr = contents.template;
127
- const targetIds = $$.mapToTargetIds();
128
- if (order === null && config.data_groups.length) {
129
- // for stacked data, order should aligned with the visually displayed data
130
- const ids = $$.orderTargets($$.data.targets)
131
- .map(i2 => i2.id)
132
- .reverse();
133
- d.sort((a, b) => {
134
- let v1 = a ? a.value : null;
135
- let v2 = b ? b.value : null;
136
- if (v1 > 0 && v2 > 0) {
137
- v1 = a.id ? ids.indexOf(a.id) : null;
138
- v2 = b.id ? ids.indexOf(b.id) : null;
139
- }
140
- return v1 - v2;
141
- });
142
- }
143
- else if (/^(asc|desc)$/.test(order)) {
144
- const isAscending = order === "asc";
145
- d.sort((a, b) => {
146
- const v1 = a ? getRowValue(a) : null;
147
- const v2 = b ? getRowValue(b) : null;
148
- return isAscending ? v1 - v2 : v2 - v1;
149
- });
150
- }
151
- else if (isFunction(order)) {
152
- d.sort(order.bind(api));
153
- }
154
- const tpl = $$.getTooltipContentTemplate(tplStr);
155
- const len = d.length;
156
- let text;
157
- let row;
158
- let param;
159
- let value;
160
- let i;
161
- for (i = 0; i < len; i++) {
162
- row = d[i];
163
- const rowValue = row && getRowValue(row);
164
- if (!row || !(rowValue || rowValue === 0)) {
165
- continue;
166
- }
167
- if (isUndefined(text)) {
168
- const title = (state.hasAxis || state.hasRadar) && titleFormat(row.x);
169
- text = tplProcess(tpl[0], {
170
- CLASS_TOOLTIP: $TOOLTIP.tooltip,
171
- TITLE: isValue(title) ?
172
- (tplStr ? title : `<tr><th colspan="2">${title}</th></tr>`) :
173
- ""
174
- });
175
- }
176
- if (!row.ratio && $el.arcs) {
177
- param = ["arc", $$.$el.arcs.select(`path.${$ARC.arc}-${row.id}`).data()[0]];
178
- row.ratio = $$.getRatio(...param);
179
- }
180
- // arrange param to be passed to formatter
181
- param = [row.ratio, row.id, row.index];
182
- if ($$.isAreaRangeType(row)) {
183
- const [high, low] = ["high", "low"].map(v => valueFormat($$.getRangedData(row, v), ...param));
184
- const mid = valueFormat(getRowValue(row), ...param);
185
- value = `<b>Mid:</b> ${mid} <b>High:</b> ${high} <b>Low:</b> ${low}`;
186
- }
187
- else if ($$.isCandlestickType(row)) {
188
- const [open, high, low, close, volume] = ["open", "high", "low", "close", "volume"]
189
- .map(v => {
190
- const value = $$.getRangedData(row, v, "candlestick");
191
- return value ?
192
- valueFormat($$.getRangedData(row, v, "candlestick"), ...param) :
193
- undefined;
194
- });
195
- value =
196
- `<b>Open:</b> ${open} <b>High:</b> ${high} <b>Low:</b> ${low} <b>Close:</b> ${close}${volume ? ` <b>Volume:</b> ${volume}` : ""}`;
197
- }
198
- else if ($$.isBarRangeType(row)) {
199
- const { value: rangeValue, id, index } = row;
200
- value = `${valueFormat(rangeValue, undefined, id, index)}`;
201
- }
202
- else {
203
- value = valueFormat(getRowValue(row), ...param);
204
- }
205
- if (value !== undefined) {
206
- // Skip elements when their name is set to null
207
- if (row.name === null) {
208
- continue;
209
- }
210
- const name = nameFormat(row.name ?? row.id, ...param);
211
- const color = getBgColor(row);
212
- const contentValue = {
213
- CLASS_TOOLTIP_NAME: $TOOLTIP.tooltipName + $$.getTargetSelectorSuffix(row.id),
214
- COLOR: (tplStr || !$$.patterns) ?
215
- color :
216
- `<svg><rect style="fill:${color}" width="10" height="10"></rect></svg>`,
217
- NAME: name,
218
- VALUE: value
219
- };
220
- if (tplStr && isObject(contents.text)) {
221
- const index = targetIds.indexOf(row.id);
222
- Object.keys(contents.text).forEach(key => {
223
- contentValue[key] = contents.text[key][index];
224
- });
225
- }
226
- text += tplProcess(tpl[1], contentValue);
227
- }
228
- }
229
- return `${text}</table>`;
230
- },
231
- /**
232
- * Get the content template string
233
- * @param {string} tplStr Tempalte string
234
- * @returns {Array} Template string
235
- * @private
236
- */
237
- getTooltipContentTemplate(tplStr) {
238
- return (tplStr || `<table class="{=CLASS_TOOLTIP}"><tbody>
24
+ var tooltip_default = {
25
+ /**
26
+ * Initializes the tooltip
27
+ * @private
28
+ */
29
+ initTooltip() {
30
+ const $$ = this;
31
+ const { config, $el } = $$;
32
+ $el.tooltip = select(config.tooltip_contents.bindto);
33
+ if ($el.tooltip.empty()) $el.tooltip = $el.chart.append("div").attr("class", $TOOLTIP.tooltipContainer).style("position", "absolute").style("pointer-events", "none").style("display", "none");
34
+ $$.bindTooltipResizePos();
35
+ },
36
+ /**
37
+ * Show tooltip at initialization.
38
+ * Is called only when tooltip.init.show=true option is set
39
+ * @private
40
+ */
41
+ initShowTooltip() {
42
+ const $$ = this;
43
+ const { config, $el, state: { hasAxis, hasRadar } } = $$;
44
+ if (config.tooltip_init_show) {
45
+ const isArc = !(hasAxis || hasRadar);
46
+ if ($$.axis?.isTimeSeries() && isString(config.tooltip_init_x)) config.tooltip_init_x = parseDate.call($$, config.tooltip_init_x);
47
+ $$.api.tooltip.show({ data: { [isArc ? "index" : "x"]: config.tooltip_init_x } });
48
+ const position = config.tooltip_init_position;
49
+ if (!config.tooltip_contents.bindto && !isEmpty(position)) {
50
+ const { top = 0, left = 50 } = position;
51
+ $el.tooltip.style("top", isString(top) ? top : `${top}px`).style("left", isString(left) ? left : `${left}px`).style("display", null);
52
+ }
53
+ }
54
+ },
55
+ /**
56
+ * Get the tooltip HTML string
57
+ * @param {Array} args Arguments
58
+ * @returns {string} Formatted HTML string
59
+ * @private
60
+ */
61
+ getTooltipHTML(...args) {
62
+ const $$ = this;
63
+ const { api, config } = $$;
64
+ return sanitize(isFunction(config.tooltip_contents) ? config.tooltip_contents.bind(api)(...args) : $$.getTooltipContent(...args));
65
+ },
66
+ /**
67
+ * Returns the tooltip content(HTML string)
68
+ * @param {object} d data
69
+ * @param {function} defaultTitleFormat Default title format
70
+ * @param {function} defaultValueFormat Default format for each data value in the tooltip.
71
+ * @param {function} color Color function
72
+ * @returns {string} html
73
+ * @private
74
+ */
75
+ getTooltipContent(d, defaultTitleFormat, defaultValueFormat, color) {
76
+ const $$ = this;
77
+ const { api, config, state, $el } = $$;
78
+ const titleFn = config.tooltip_format_title;
79
+ const nameFn = config.tooltip_format_name;
80
+ const valueFn = config.tooltip_format_value;
81
+ const titleFormat = isFunction(titleFn) ? titleFn.bind(api) : defaultTitleFormat;
82
+ const nameFormat = isFunction(nameFn) ? nameFn.bind(api) : ((name) => name);
83
+ const boundValueFn = isFunction(valueFn) ? valueFn.bind(api) : null;
84
+ const valueFormat = (v, ratio, id, index) => {
85
+ let fn = boundValueFn;
86
+ if (!fn) if (state.hasTreemap || $$.isStackNormalized() && (!$$.isStackNormalizedPerGroup() || $$.isGrouped(id))) fn = (v, ratio) => `${(ratio * 100).toFixed(2)}%`;
87
+ else fn = defaultValueFormat;
88
+ return fn(v, ratio, id, index);
89
+ };
90
+ const order = config.tooltip_order;
91
+ const getRowValue = (row) => $$.axis && $$.isBubbleZType(row) ? $$.getBubbleZData(row.value, "z") : $$.getBaseValue(row);
92
+ const getBgColor = $$.levelColor ? (row) => $$.levelColor(row.value) : (row) => color(row);
93
+ const contents = config.tooltip_contents;
94
+ const tplStr = contents.template;
95
+ const targetIds = $$.mapToTargetIds();
96
+ if (order === null && config.data_groups.length) {
97
+ const ids = $$.orderTargets($$.data.targets).map((i2) => i2.id).reverse();
98
+ d.sort((a, b) => {
99
+ let v1 = a ? a.value : null;
100
+ let v2 = b ? b.value : null;
101
+ if (v1 > 0 && v2 > 0) {
102
+ v1 = a.id ? ids.indexOf(a.id) : null;
103
+ v2 = b.id ? ids.indexOf(b.id) : null;
104
+ }
105
+ return v1 - v2;
106
+ });
107
+ } else if (/^(asc|desc)$/.test(order)) {
108
+ const isAscending = order === "asc";
109
+ d.sort((a, b) => {
110
+ const v1 = a ? getRowValue(a) : null;
111
+ const v2 = b ? getRowValue(b) : null;
112
+ return isAscending ? v1 - v2 : v2 - v1;
113
+ });
114
+ } else if (isFunction(order)) d.sort(order.bind(api));
115
+ const tpl = $$.getTooltipContentTemplate(tplStr);
116
+ const len = d.length;
117
+ let text;
118
+ let row;
119
+ let param;
120
+ let value;
121
+ let i;
122
+ for (i = 0; i < len; i++) {
123
+ row = d[i];
124
+ const rowValue = row && getRowValue(row);
125
+ if (!row || !(rowValue || rowValue === 0)) continue;
126
+ if (isUndefined(text)) {
127
+ const title = (state.hasAxis || state.hasRadar) && titleFormat(row.x);
128
+ text = tplProcess(tpl[0], {
129
+ CLASS_TOOLTIP: $TOOLTIP.tooltip,
130
+ TITLE: isValue(title) ? tplStr ? title : `<tr><th colspan="2">${title}</th></tr>` : ""
131
+ });
132
+ }
133
+ if (!row.ratio && $el.arcs) {
134
+ param = ["arc", $$.$el.arcs.select(`path.${$ARC.arc}-${row.id}`).data()[0]];
135
+ row.ratio = $$.getRatio(...param);
136
+ }
137
+ param = [
138
+ row.ratio,
139
+ row.id,
140
+ row.index
141
+ ];
142
+ if ($$.isAreaRangeType(row)) {
143
+ const [high, low] = ["high", "low"].map((v) => valueFormat($$.getRangedData(row, v), ...param));
144
+ value = `<b>Mid:</b> ${valueFormat(getRowValue(row), ...param)} <b>High:</b> ${high} <b>Low:</b> ${low}`;
145
+ } else if ($$.isCandlestickType(row)) {
146
+ const [open, high, low, close, volume] = [
147
+ "open",
148
+ "high",
149
+ "low",
150
+ "close",
151
+ "volume"
152
+ ].map((v) => {
153
+ return $$.getRangedData(row, v, "candlestick") ? valueFormat($$.getRangedData(row, v, "candlestick"), ...param) : void 0;
154
+ });
155
+ value = `<b>Open:</b> ${open} <b>High:</b> ${high} <b>Low:</b> ${low} <b>Close:</b> ${close}${volume ? ` <b>Volume:</b> ${volume}` : ""}`;
156
+ } else if ($$.isBarRangeType(row)) {
157
+ const { value: rangeValue, id, index } = row;
158
+ value = `${valueFormat(rangeValue, void 0, id, index)}`;
159
+ } else value = valueFormat(getRowValue(row), ...param);
160
+ if (value !== void 0) {
161
+ if (row.name === null) continue;
162
+ const name = nameFormat(row.name ?? row.id, ...param);
163
+ const color = getBgColor(row);
164
+ const contentValue = {
165
+ CLASS_TOOLTIP_NAME: $TOOLTIP.tooltipName + $$.getTargetSelectorSuffix(row.id),
166
+ COLOR: tplStr || !$$.patterns ? color : `<svg><rect style="fill:${color}" width="10" height="10"></rect></svg>`,
167
+ NAME: name,
168
+ VALUE: value
169
+ };
170
+ if (tplStr && isObject(contents.text)) {
171
+ const index = targetIds.indexOf(row.id);
172
+ Object.keys(contents.text).forEach((key) => {
173
+ contentValue[key] = contents.text[key][index];
174
+ });
175
+ }
176
+ text += tplProcess(tpl[1], contentValue);
177
+ }
178
+ }
179
+ return `${text}</table>`;
180
+ },
181
+ /**
182
+ * Get the content template string
183
+ * @param {string} tplStr Tempalte string
184
+ * @returns {Array} Template string
185
+ * @private
186
+ */
187
+ getTooltipContentTemplate(tplStr) {
188
+ return (tplStr || `<table class="{=CLASS_TOOLTIP}"><tbody>
239
189
  {=TITLE}
240
190
  {{<tr class="{=CLASS_TOOLTIP_NAME}">
241
191
  <td class="name">${this.patterns ? `{=COLOR}` : `<span style="background-color:{=COLOR}"></span>`}{=NAME}</td>
242
192
  <td class="value">{=VALUE}</td>
243
193
  </tr>}}
244
- </tbody></table>`)
245
- .replace(RE_WHITESPACE, "")
246
- .split(RE_TOOLTIP_TPL);
247
- },
248
- /**
249
- * Update tooltip position coordinate
250
- * @param {object} dataToShow Data object
251
- * @param {SVGElement} eventTarget Event element
252
- * @private
253
- */
254
- setTooltipPosition(dataToShow, eventTarget) {
255
- const $$ = this;
256
- const { config, scale, state, $el: { eventRect, tooltip, svg } } = $$;
257
- const { bindto } = config.tooltip_contents;
258
- const isRotated = config.axis_rotated;
259
- const datum = tooltip?.datum();
260
- if (!bindto && datum) {
261
- const data = dataToShow ?? datum.data;
262
- const [x, y] = getPointer(state.event, eventTarget ?? eventRect?.node()); // get mouse event position
263
- const currPos = { x, y };
264
- if (state.hasAxis && scale.x && datum && "x" in datum) {
265
- const getYPos = (value = 0, id, axisId = "y") => {
266
- const scaleFn = scale[id ? $$.axis?.getId(id) : axisId];
267
- return scaleFn ?
268
- scaleFn(value) + (isRotated ? state.margin.left : state.margin.top) :
269
- 0;
270
- };
271
- currPos.xAxis = scale.x(datum.x) + (
272
- // add margin only when user specified tooltip.position function
273
- config.tooltip_position ? (isRotated ? state.margin.top : state.margin.left) : 0);
274
- if (data.length === 1) {
275
- currPos.yAxis = getYPos(data[0].value, data[0].id);
276
- }
277
- else {
278
- currPos.yAxis = getYPos;
279
- }
280
- }
281
- const { width = 0, height = 0 } = datum;
282
- // Get tooltip position
283
- const pos = config.tooltip_position?.bind($$.api)(data, width, height, eventRect?.node(), currPos) ?? ((svg && hasViewBox(svg)) ?
284
- $$.getTooltipPositionViewBox.bind($$)(width, height, currPos) :
285
- $$.getTooltipPosition.bind($$)(width, height, currPos));
286
- ["top", "left"].forEach(v => {
287
- const value = pos[v];
288
- tooltip.style(v, `${value}px`);
289
- // Remember left pos in percentage to be used on resize call
290
- if (v === "left" && !datum.xPosInPercent) {
291
- datum.xPosInPercent = value / state.current.width * 100;
292
- }
293
- });
294
- }
295
- },
296
- /**
297
- * Get tooltip position when svg has vieBox attribute
298
- * @param {number} tWidth Tooltip width value
299
- * @param {number} tHeight Tooltip height value
300
- * @param {object} currPos Current event position value from SVG coordinate
301
- * @returns {object} top, left value
302
- */
303
- getTooltipPositionViewBox(tWidth, tHeight, currPos) {
304
- const $$ = this;
305
- const { $el: { eventRect, svg }, config, state } = $$;
306
- const isRotated = config.axis_rotated;
307
- const hasArcType = $$.hasArcType() || state.hasFunnel || state.hasTreemap;
308
- const target = (hasArcType ? svg : eventRect)?.node() ?? state.event.target;
309
- let { x, y } = currPos;
310
- if (state.hasAxis) {
311
- x = isRotated ? x : currPos.xAxis;
312
- y = isRotated ? currPos.xAxis : y;
313
- }
314
- // currPos value based on SVG coordinate
315
- const ctm = getTransformCTM(target, x, y, false);
316
- const rect = getBoundingRect(target);
317
- const size = getTransformCTM(target, 20, 0, false).x;
318
- let top = ctm.y;
319
- let left = ctm.x + (tWidth / 2) + size;
320
- if (hasArcType) {
321
- if (state.hasFunnel || state.hasTreemap || state.hasRadar) {
322
- left -= (tWidth / 2) + size;
323
- top += tHeight;
324
- }
325
- else {
326
- top += rect.height / 2;
327
- left += (rect.width / 2) - (tWidth - size);
328
- }
329
- }
330
- if (left + tWidth > rect.width) {
331
- left = rect.width - tWidth - size;
332
- }
333
- if (top + tHeight > rect.height) {
334
- top -= tHeight * 2;
335
- }
336
- return {
337
- top,
338
- left
339
- };
340
- },
341
- /**
342
- * Returns the position of the tooltip
343
- * @param {string} tWidth Width value of tooltip element
344
- * @param {string} tHeight Height value of tooltip element
345
- * @param {object} currPos Current mouse position
346
- * @returns {object} top, left value
347
- * @private
348
- */
349
- getTooltipPosition(tWidth, tHeight, currPos) {
350
- const $$ = this;
351
- const { config, scale, state } = $$;
352
- const { width, height, current, hasFunnel, hasRadar, hasTreemap, isLegendRight, inputType } = state;
353
- const hasGauge = $$.hasType("gauge") && !config.gauge_fullCircle;
354
- const isRotated = config.axis_rotated;
355
- const hasArcType = $$.hasArcType();
356
- const svgLeft = state.isCanvasMode ? 0 : $$.getSvgLeft(true);
357
- let chartRight = svgLeft + current.width - $$.getCurrentPaddingByDirection("right");
358
- const size = 20;
359
- let { x, y } = currPos;
360
- // Determine tooltip position
361
- if (hasRadar) {
362
- x += x >= (width / 2) ? 15 : -(tWidth + 15);
363
- y += 15;
364
- }
365
- else if (hasArcType) {
366
- const notTouch = inputType !== "touch";
367
- if (notTouch) {
368
- let titlePadding = $$.getTitlePadding?.() ?? 0;
369
- if (titlePadding && hasGauge && config.arc_rangeText_values?.length) {
370
- titlePadding += 10;
371
- }
372
- x += (width - (isLegendRight ? $$.getLegendWidth() : 0)) / 2;
373
- y += (hasGauge ? height : (height / 2) + tHeight) + titlePadding;
374
- }
375
- }
376
- else if (hasFunnel || hasTreemap) {
377
- y += tHeight;
378
- }
379
- else {
380
- const padding = {
381
- top: $$.getCurrentPaddingByDirection("top", true),
382
- left: $$.getCurrentPaddingByDirection("left", true)
383
- };
384
- if (isRotated) {
385
- x += svgLeft + padding.left + size;
386
- y = padding.top + currPos.xAxis + size;
387
- chartRight -= svgLeft;
388
- }
389
- else {
390
- x = svgLeft + padding.left + size + (scale.zoom ? x : currPos.xAxis);
391
- y += padding.top - 5;
392
- }
393
- }
394
- // when tooltip left + tWidth > chart's width
395
- if ((x + tWidth + 15) > chartRight) {
396
- x -= tWidth + (hasFunnel || hasTreemap || hasArcType ? 0 : (isRotated ? size * 2 : 38));
397
- }
398
- if (y + tHeight > current.height) {
399
- const gap = hasTreemap ? tHeight + 10 : 30;
400
- y -= hasGauge ? tHeight * 1.5 : tHeight + gap;
401
- }
402
- const pos = { top: y, left: x };
403
- // make sure to not be positioned out of viewport
404
- if (pos.top < 0)
405
- pos.top = 0;
406
- if (pos.left < 0)
407
- pos.left = 0;
408
- return pos;
409
- },
410
- /**
411
- * Show the tooltip
412
- * @param {object} selectedData Data object
413
- * @param {SVGElement} eventTarget Event element
414
- * @private
415
- */
416
- showTooltip(selectedData, eventTarget) {
417
- const $$ = this;
418
- const { config, $el: { tooltip } } = $$;
419
- if (!tooltip || !config.tooltip_show) {
420
- return;
421
- }
422
- let dataToShow = null;
423
- let dataToShowCount = 0;
424
- let dataStr = `${$$.data.targets.length}:`;
425
- let firstData;
426
- for (let i = 0; i < selectedData.length; i++) {
427
- const d = selectedData[i];
428
- i > 0 && (dataStr += ",");
429
- dataStr += `${d?.index}|${d?.id}|${d?.value}`;
430
- !firstData && d && (firstData = d);
431
- if (d && isValue($$.getBaseValue(d))) {
432
- dataToShowCount++;
433
- dataToShow?.push(d);
434
- }
435
- else if (!dataToShow) {
436
- dataToShow = selectedData.slice(0, i);
437
- }
438
- }
439
- if (dataToShowCount === 0 || !firstData) {
440
- return;
441
- }
442
- const datum = tooltip.datum();
443
- let positionData = dataToShow;
444
- if (!datum || datum.current !== dataStr) {
445
- const { index, x } = firstData;
446
- // getTooltipHTML() may sort selectedData in-place for display ordering.
447
- // Keep the original order for tooltip.position without copying on repeated hovers.
448
- if (!positionData) {
449
- positionData = selectedData.slice();
450
- }
451
- callFn(config.tooltip_onshow, $$.api, selectedData);
452
- // set tooltip content
453
- tooltip
454
- .html($$.getTooltipHTML(selectedData, // data
455
- $$.axis ? $$.axis.getXAxisTickFormat() : $$.categoryName.bind($$), // defaultTitleFormat
456
- $$.getDefaultValueFormat(), // defaultValueFormat
457
- $$.color // color
458
- ))
459
- .style("display", null)
460
- .style("visibility", null); // for IE9
461
- tooltip.datum({
462
- index,
463
- x,
464
- current: dataStr,
465
- data: selectedData,
466
- width: tooltip.property("offsetWidth"),
467
- height: tooltip.property("offsetHeight")
468
- });
469
- callFn(config.tooltip_onshown, $$.api, selectedData);
470
- $$._handleLinkedCharts(true, index);
471
- }
472
- $$.setTooltipPosition(positionData || selectedData, eventTarget);
473
- },
474
- /**
475
- * Adjust tooltip position on resize event
476
- * @private
477
- */
478
- bindTooltipResizePos() {
479
- const $$ = this;
480
- const { resizeFunction, state, $el: { tooltip } } = $$;
481
- resizeFunction.add(() => {
482
- if (tooltip.style("display") === "block") {
483
- const { current } = state;
484
- const { width, xPosInPercent } = tooltip.datum();
485
- let value = current.width / 100 * xPosInPercent;
486
- const diff = current.width - (value + width);
487
- // if tooltip size overs current viewport size
488
- if (diff < 0) {
489
- value += diff;
490
- }
491
- tooltip.style("left", `${value}px`);
492
- }
493
- });
494
- },
495
- /**
496
- * Hide the tooltip
497
- * @param {boolean} force Force to hide
498
- * @private
499
- */
500
- hideTooltip(force) {
501
- const $$ = this;
502
- const { api, config, $el: { tooltip } } = $$;
503
- if (tooltip && tooltip.style("display") !== "none" && (!config.tooltip_doNotHide || force)) {
504
- const selectedData = tooltip.datum().data ?? [];
505
- callFn(config.tooltip_onhide, api, selectedData);
506
- // hide tooltip
507
- tooltip
508
- .style("display", "none")
509
- .datum(null);
510
- callFn(config.tooltip_onhidden, api, selectedData);
511
- }
512
- },
513
- /**
514
- * Toggle display for linked chart instances
515
- * @param {boolean} show true: show, false: hide
516
- * @param {number} index x Axis index
517
- * @private
518
- */
519
- _handleLinkedCharts(show, index) {
520
- const $$ = this;
521
- const { charts, config, state: { event } } = $$;
522
- // Prevent propagation among instances if isn't instantiated from the user's event
523
- // https://github.com/naver/billboard.js/issues/1979
524
- if (event?.isTrusted && config.tooltip_linked && charts.length > 1) {
525
- const linkedName = config.tooltip_linked_name;
526
- charts
527
- .filter(c => c !== $$.api)
528
- .forEach(c => {
529
- const { config, $el, state } = c.internal;
530
- const isLinked = config.tooltip_linked;
531
- const name = config.tooltip_linked_name;
532
- const isInDom = doc.body.contains($el.chart.node());
533
- if (isLinked && linkedName === name && isInDom) {
534
- const data = $el.tooltip.data()[0];
535
- const isNotSameIndex = index !== data?.index;
536
- try {
537
- if (show && isNotSameIndex && state.isCanvasMode) {
538
- c.internal.showCanvasLinkedTooltip?.(index);
539
- }
540
- else {
541
- c.tooltip[show && isNotSameIndex ? "show" : "hide"]({ index });
542
- }
543
- }
544
- catch { }
545
- }
546
- });
547
- }
548
- },
549
- /**
550
- * Update tooltip content on redraw
551
- * - In a situation where tooltip is displayed and data load happens, it should reflect loaded data to tooltip
552
- * @param {d3Selection} context Event rect element
553
- * @param {number} index Data index
554
- * @private
555
- */
556
- updateTooltipOnRedraw(context, index) {
557
- const $$ = this;
558
- const { config, $el: { eventRect, svg, tooltip }, state: { event, hasAxis, hasRadar, hasTreemap } } = $$;
559
- if ($$.state.isCanvasMode) {
560
- return;
561
- }
562
- // Update tooltip, when tooltip is in shown state
563
- if (tooltip?.style("display") === "block" && event) {
564
- const rect = context ?? (hasRadar ? svg : eventRect)?.node();
565
- // for Axis based & Radar
566
- if (hasAxis || hasRadar) {
567
- if ($$.isMultipleX()) {
568
- $$.selectRectForMultipleXs(rect, false);
569
- }
570
- else {
571
- const idx = index ?? $$.getDataIndexFromEvent(event);
572
- if (index === -1) {
573
- $$.api.tooltip.hide();
574
- }
575
- else {
576
- $$.selectRectForSingle(rect, idx);
577
- $$.setExpand(idx, null, true);
578
- }
579
- }
580
- }
581
- else { // for Arc & Treemap
582
- const { clientX, clientY } = event;
583
- setTimeout(() => {
584
- let target = [clientX, clientY].every(Number.isFinite) &&
585
- doc.elementFromPoint(clientX, clientY);
586
- const data = target && select(target).datum();
587
- if (data) {
588
- const d = $$.hasArcType() ?
589
- $$.convertToArcData($$.updateAngle(data)) :
590
- data?.data;
591
- hasTreemap && (target = svg.node());
592
- d && $$.showTooltip([d], target);
593
- }
594
- else {
595
- $$.api.tooltip.hide();
596
- }
597
- }, config.transition_duration);
598
- }
599
- }
600
- }
194
+ </tbody></table>`).replace(RE_WHITESPACE, "").split(RE_TOOLTIP_TPL);
195
+ },
196
+ /**
197
+ * Update tooltip position coordinate
198
+ * @param {object} dataToShow Data object
199
+ * @param {SVGElement} eventTarget Event element
200
+ * @private
201
+ */
202
+ setTooltipPosition(dataToShow, eventTarget) {
203
+ const $$ = this;
204
+ const { config, scale, state, $el: { eventRect, tooltip, svg } } = $$;
205
+ const { bindto } = config.tooltip_contents;
206
+ const isRotated = config.axis_rotated;
207
+ const datum = tooltip?.datum();
208
+ if (!bindto && datum) {
209
+ const data = dataToShow ?? datum.data;
210
+ const [x, y] = getPointer(state.event, eventTarget ?? eventRect?.node());
211
+ const currPos = {
212
+ x,
213
+ y
214
+ };
215
+ if (state.hasAxis && scale.x && datum && "x" in datum) {
216
+ const getYPos = (value = 0, id, axisId = "y") => {
217
+ const scaleFn = scale[id ? $$.axis?.getId(id) : axisId];
218
+ return scaleFn ? scaleFn(value) + (isRotated ? state.margin.left : state.margin.top) : 0;
219
+ };
220
+ currPos.xAxis = scale.x(datum.x) + (config.tooltip_position ? isRotated ? state.margin.top : state.margin.left : 0);
221
+ if (data.length === 1) currPos.yAxis = getYPos(data[0].value, data[0].id);
222
+ else currPos.yAxis = getYPos;
223
+ }
224
+ const { width = 0, height = 0 } = datum;
225
+ const pos = config.tooltip_position?.bind($$.api)(data, width, height, eventRect?.node(), currPos) ?? (svg && hasViewBox(svg) ? $$.getTooltipPositionViewBox.bind($$)(width, height, currPos) : $$.getTooltipPosition.bind($$)(width, height, currPos));
226
+ ["top", "left"].forEach((v) => {
227
+ const value = pos[v];
228
+ tooltip.style(v, `${value}px`);
229
+ if (v === "left" && !datum.xPosInPercent) datum.xPosInPercent = value / state.current.width * 100;
230
+ });
231
+ }
232
+ },
233
+ /**
234
+ * Get tooltip position when svg has vieBox attribute
235
+ * @param {number} tWidth Tooltip width value
236
+ * @param {number} tHeight Tooltip height value
237
+ * @param {object} currPos Current event position value from SVG coordinate
238
+ * @returns {object} top, left value
239
+ */
240
+ getTooltipPositionViewBox(tWidth, tHeight, currPos) {
241
+ const $$ = this;
242
+ const { $el: { eventRect, svg }, config, state } = $$;
243
+ const isRotated = config.axis_rotated;
244
+ const hasArcType = $$.hasArcType() || state.hasFunnel || state.hasTreemap;
245
+ const target = (hasArcType ? svg : eventRect)?.node() ?? state.event.target;
246
+ let { x, y } = currPos;
247
+ if (state.hasAxis) {
248
+ x = isRotated ? x : currPos.xAxis;
249
+ y = isRotated ? currPos.xAxis : y;
250
+ }
251
+ const ctm = getTransformCTM(target, x, y, false);
252
+ const rect = getBoundingRect(target);
253
+ const size = getTransformCTM(target, 20, 0, false).x;
254
+ let top = ctm.y;
255
+ let left = ctm.x + tWidth / 2 + size;
256
+ if (hasArcType) if (state.hasFunnel || state.hasTreemap || state.hasRadar) {
257
+ left -= tWidth / 2 + size;
258
+ top += tHeight;
259
+ } else {
260
+ top += rect.height / 2;
261
+ left += rect.width / 2 - (tWidth - size);
262
+ }
263
+ if (left + tWidth > rect.width) left = rect.width - tWidth - size;
264
+ if (top + tHeight > rect.height) top -= tHeight * 2;
265
+ return {
266
+ top,
267
+ left
268
+ };
269
+ },
270
+ /**
271
+ * Returns the position of the tooltip
272
+ * @param {string} tWidth Width value of tooltip element
273
+ * @param {string} tHeight Height value of tooltip element
274
+ * @param {object} currPos Current mouse position
275
+ * @returns {object} top, left value
276
+ * @private
277
+ */
278
+ getTooltipPosition(tWidth, tHeight, currPos) {
279
+ const $$ = this;
280
+ const { config, scale, state } = $$;
281
+ const { width, height, current, hasFunnel, hasRadar, hasTreemap, isLegendRight, inputType } = state;
282
+ const hasGauge = $$.hasType("gauge") && !config.gauge_fullCircle;
283
+ const isRotated = config.axis_rotated;
284
+ const hasArcType = $$.hasArcType();
285
+ const svgLeft = state.isCanvasMode ? 0 : $$.getSvgLeft(true);
286
+ let chartRight = svgLeft + current.width - $$.getCurrentPaddingByDirection("right");
287
+ const size = 20;
288
+ let { x, y } = currPos;
289
+ if (hasRadar) {
290
+ x += x >= width / 2 ? 15 : -(tWidth + 15);
291
+ y += 15;
292
+ } else if (hasArcType) {
293
+ if (inputType !== "touch") {
294
+ let titlePadding = $$.getTitlePadding?.() ?? 0;
295
+ if (titlePadding && hasGauge && config.arc_rangeText_values?.length) titlePadding += 10;
296
+ x += (width - (isLegendRight ? $$.getLegendWidth() : 0)) / 2;
297
+ y += (hasGauge ? height : height / 2 + tHeight) + titlePadding;
298
+ }
299
+ } else if (hasFunnel || hasTreemap) y += tHeight;
300
+ else {
301
+ const padding = {
302
+ top: $$.getCurrentPaddingByDirection("top", true),
303
+ left: $$.getCurrentPaddingByDirection("left", true)
304
+ };
305
+ if (isRotated) {
306
+ x += svgLeft + padding.left + size;
307
+ y = padding.top + currPos.xAxis + size;
308
+ chartRight -= svgLeft;
309
+ } else {
310
+ x = svgLeft + padding.left + size + (scale.zoom ? x : currPos.xAxis);
311
+ y += padding.top - 5;
312
+ }
313
+ }
314
+ if (x + tWidth + 15 > chartRight) x -= tWidth + (hasFunnel || hasTreemap || hasArcType ? 0 : isRotated ? size * 2 : 38);
315
+ if (y + tHeight > current.height) {
316
+ const gap = hasTreemap ? tHeight + 10 : 30;
317
+ y -= hasGauge ? tHeight * 1.5 : tHeight + gap;
318
+ }
319
+ const pos = {
320
+ top: y,
321
+ left: x
322
+ };
323
+ if (pos.top < 0) pos.top = 0;
324
+ if (pos.left < 0) pos.left = 0;
325
+ return pos;
326
+ },
327
+ /**
328
+ * Show the tooltip
329
+ * @param {object} selectedData Data object
330
+ * @param {SVGElement} eventTarget Event element
331
+ * @private
332
+ */
333
+ showTooltip(selectedData, eventTarget) {
334
+ const $$ = this;
335
+ const { config, $el: { tooltip } } = $$;
336
+ if (!tooltip || !config.tooltip_show) return;
337
+ let dataToShow = null;
338
+ let dataToShowCount = 0;
339
+ let dataStr = `${$$.data.targets.length}:`;
340
+ let firstData;
341
+ for (let i = 0; i < selectedData.length; i++) {
342
+ const d = selectedData[i];
343
+ i > 0 && (dataStr += ",");
344
+ dataStr += `${d?.index}|${d?.id}|${d?.value}`;
345
+ !firstData && d && (firstData = d);
346
+ if (d && isValue($$.getBaseValue(d))) {
347
+ dataToShowCount++;
348
+ dataToShow?.push(d);
349
+ } else if (!dataToShow) dataToShow = selectedData.slice(0, i);
350
+ }
351
+ if (dataToShowCount === 0 || !firstData) return;
352
+ const datum = tooltip.datum();
353
+ let positionData = dataToShow;
354
+ if (!datum || datum.current !== dataStr) {
355
+ const { index, x } = firstData;
356
+ if (!positionData) positionData = selectedData.slice();
357
+ callFn(config.tooltip_onshow, $$.api, selectedData);
358
+ tooltip.html($$.getTooltipHTML(selectedData, $$.axis ? $$.axis.getXAxisTickFormat() : $$.categoryName.bind($$), $$.getDefaultValueFormat(), $$.color)).style("display", null).style("visibility", null);
359
+ tooltip.datum({
360
+ index,
361
+ x,
362
+ current: dataStr,
363
+ data: selectedData,
364
+ width: tooltip.property("offsetWidth"),
365
+ height: tooltip.property("offsetHeight")
366
+ });
367
+ callFn(config.tooltip_onshown, $$.api, selectedData);
368
+ $$._handleLinkedCharts(true, index);
369
+ }
370
+ $$.setTooltipPosition(positionData || selectedData, eventTarget);
371
+ },
372
+ /**
373
+ * Adjust tooltip position on resize event
374
+ * @private
375
+ */
376
+ bindTooltipResizePos() {
377
+ const { resizeFunction, state, $el: { tooltip } } = this;
378
+ resizeFunction.add(() => {
379
+ if (tooltip.style("display") === "block") {
380
+ const { current } = state;
381
+ const { width, xPosInPercent } = tooltip.datum();
382
+ let value = current.width / 100 * xPosInPercent;
383
+ const diff = current.width - (value + width);
384
+ if (diff < 0) value += diff;
385
+ tooltip.style("left", `${value}px`);
386
+ }
387
+ });
388
+ },
389
+ /**
390
+ * Hide the tooltip
391
+ * @param {boolean} force Force to hide
392
+ * @private
393
+ */
394
+ hideTooltip(force) {
395
+ const { api, config, $el: { tooltip } } = this;
396
+ if (tooltip && tooltip.style("display") !== "none" && (!config.tooltip_doNotHide || force)) {
397
+ const selectedData = tooltip.datum().data ?? [];
398
+ callFn(config.tooltip_onhide, api, selectedData);
399
+ tooltip.style("display", "none").datum(null);
400
+ callFn(config.tooltip_onhidden, api, selectedData);
401
+ }
402
+ },
403
+ /**
404
+ * Toggle display for linked chart instances
405
+ * @param {boolean} show true: show, false: hide
406
+ * @param {number} index x Axis index
407
+ * @private
408
+ */
409
+ _handleLinkedCharts(show, index) {
410
+ const $$ = this;
411
+ const { charts, config, state: { event } } = $$;
412
+ if (event?.isTrusted && config.tooltip_linked && charts.length > 1) {
413
+ const linkedName = config.tooltip_linked_name;
414
+ charts.filter((c) => c !== $$.api).forEach((c) => {
415
+ const { config, $el, state } = c.internal;
416
+ const isLinked = config.tooltip_linked;
417
+ const name = config.tooltip_linked_name;
418
+ const isInDom = doc.body.contains($el.chart.node());
419
+ if (isLinked && linkedName === name && isInDom) {
420
+ const isNotSameIndex = index !== $el.tooltip.data()[0]?.index;
421
+ try {
422
+ if (show && isNotSameIndex && state.isCanvasMode) c.internal.showCanvasLinkedTooltip?.(index);
423
+ else c.tooltip[show && isNotSameIndex ? "show" : "hide"]({ index });
424
+ } catch {}
425
+ }
426
+ });
427
+ }
428
+ },
429
+ /**
430
+ * Update tooltip content on redraw
431
+ * - In a situation where tooltip is displayed and data load happens, it should reflect loaded data to tooltip
432
+ * @param {d3Selection} context Event rect element
433
+ * @param {number} index Data index
434
+ * @private
435
+ */
436
+ updateTooltipOnRedraw(context, index) {
437
+ const $$ = this;
438
+ const { config, $el: { eventRect, svg, tooltip }, state: { event, hasAxis, hasRadar, hasTreemap } } = $$;
439
+ if ($$.state.isCanvasMode) return;
440
+ if (tooltip?.style("display") === "block" && event) {
441
+ const rect = context ?? (hasRadar ? svg : eventRect)?.node();
442
+ if (hasAxis || hasRadar) if ($$.isMultipleX()) $$.selectRectForMultipleXs(rect, false);
443
+ else {
444
+ const idx = index ?? $$.getDataIndexFromEvent(event);
445
+ if (index === -1) $$.api.tooltip.hide();
446
+ else {
447
+ $$.selectRectForSingle(rect, idx);
448
+ $$.setExpand(idx, null, true);
449
+ }
450
+ }
451
+ else {
452
+ const { clientX, clientY } = event;
453
+ setTimeout(() => {
454
+ let target = [clientX, clientY].every(Number.isFinite) && doc.elementFromPoint(clientX, clientY);
455
+ const data = target && select(target).datum();
456
+ if (data) {
457
+ const d = $$.hasArcType() ? $$.convertToArcData($$.updateAngle(data)) : data?.data;
458
+ hasTreemap && (target = svg.node());
459
+ d && $$.showTooltip([d], target);
460
+ } else $$.api.tooltip.hide();
461
+ }, config.transition_duration);
462
+ }
463
+ }
464
+ }
601
465
  };
602
-
603
- export { tooltip as default };
466
+ //#endregion
467
+ export { tooltip_default as default };