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,965 +5,713 @@
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, namespaces } from 'd3-selection';
11
- import { $LEGEND, $FOCUS, $GAUGE } from '../../config/classes.js';
12
- import { document as doc } from '../../module/browser.js';
13
- import { KEY } from '../../module/Cache.js';
14
- import { isDefined, isFunction, isEmpty, isBoolean } from '../../module/util/type-checks.js';
15
- import { callFn, tplProcess, getOption, toMap } from '../../module/util/object.js';
16
- import { sanitize } from '../../module/sanitize.js';
17
-
10
+ import { document as doc } from "../../module/browser.js";
11
+ import { isBoolean, isDefined, isEmpty, isFunction } from "../../module/util/type-checks.js";
12
+ import { sanitize } from "../../module/sanitize.js";
13
+ import { callFn, getOption, toMap, tplProcess } from "../../module/util/object.js";
14
+ import { $FOCUS, $GAUGE, $LEGEND } from "../../config/classes.js";
15
+ import { KEY } from "../../module/Cache.js";
16
+ import { namespaces, select } from "d3-selection";
17
+ //#region src/ChartInternal/internals/legend.ts
18
18
  /**
19
- * Copyright (c) 2017 ~ present NAVER Corp.
20
- * billboard.js project is licensed under the MIT license
21
- */
19
+ * Copyright (c) 2017 ~ present NAVER Corp.
20
+ * billboard.js project is licensed under the MIT license
21
+ */
22
22
  const LEGEND_TOUCH_TAP_THRESHOLD = 10;
23
23
  const LEGEND_TOUCH_CLICK_TIMEOUT = 750;
24
24
  /**
25
- * Get color string for given data id
26
- * Internal helper used only within this file
27
- * @param {string} id Data id
28
- * @returns {string} Color string
29
- * @private
30
- */
25
+ * Get color string for given data id
26
+ * Internal helper used only within this file
27
+ * @param {string} id Data id
28
+ * @returns {string} Color string
29
+ * @private
30
+ */
31
31
  function _getLegendColor(id) {
32
- const $$ = this;
33
- const data = $$.getDataById(id);
34
- const color = $$.levelColor ? $$.levelColor(data.values[0].value) : $$.color(data);
35
- return color;
32
+ const $$ = this;
33
+ const data = $$.getDataById(id);
34
+ return $$.levelColor ? $$.levelColor(data.values[0].value) : $$.color(data);
36
35
  }
37
36
  /**
38
- * Get formatted text value
39
- * Internal helper used only within this file
40
- * @param {string} id Legend text id
41
- * @param {boolean} formatted Whether or not to format the text
42
- * @returns {string} Formatted legend text
43
- * @private
44
- */
37
+ * Get formatted text value
38
+ * Internal helper used only within this file
39
+ * @param {string} id Legend text id
40
+ * @param {boolean} formatted Whether or not to format the text
41
+ * @returns {string} Formatted legend text
42
+ * @private
43
+ */
45
44
  function _getFormattedText(id, formatted = true) {
46
- const { config } = this;
47
- let text = config.data_names[id] ?? id;
48
- if (formatted && isFunction(config.legend_format)) {
49
- text = config.legend_format(text, id !== text ? id : undefined);
50
- }
51
- return text;
45
+ const { config } = this;
46
+ let text = config.data_names[id] ?? id;
47
+ if (formatted && isFunction(config.legend_format)) text = config.legend_format(text, id !== text ? id : void 0);
48
+ return text;
52
49
  }
53
50
  /**
54
- * Build a Map of legend items for fast O(1) lookup by ID
55
- * Internal helper used only within this file
56
- * @param {object} $$ ChartInternal context
57
- * @param {d3.selection} legendItems D3 selection of legend items
58
- * @private
59
- */
51
+ * Build a Map of legend items for fast O(1) lookup by ID
52
+ * Internal helper used only within this file
53
+ * @param {object} $$ ChartInternal context
54
+ * @param {d3.selection} legendItems D3 selection of legend items
55
+ * @private
56
+ */
60
57
  function _buildLegendItemMap($$, legendItems) {
61
- if (!legendItems || legendItems.empty()) {
62
- return;
63
- }
64
- // rebuild from all rendered items, not the passed (possibly enter-only) selection,
65
- // so existing items aren't dropped from the map when new series are loaded
66
- const allItems = $$.$el.legend?.selectAll(`.${$LEGEND.legendItem}`);
67
- if (allItems && !allItems.empty()) {
68
- legendItems = allItems;
69
- }
70
- // Convert D3 selection to array of [id, node] pairs
71
- const items = [];
72
- legendItems.each(function (id) {
73
- items.push({ id, node: this });
74
- });
75
- // Create Map for O(1) lookups using toMap utility
76
- const itemMap = toMap(items, item => item.id, item => item.node);
77
- // Cache the map
78
- $$.cache.add(KEY.legendItemMap, itemMap);
58
+ if (!legendItems || legendItems.empty()) return;
59
+ const allItems = $$.$el.legend?.selectAll(`.${$LEGEND.legendItem}`);
60
+ if (allItems && !allItems.empty()) legendItems = allItems;
61
+ const items = [];
62
+ legendItems.each(function(id) {
63
+ items.push({
64
+ id,
65
+ node: this
66
+ });
67
+ });
68
+ const itemMap = toMap(items, (item) => item.id, (item) => item.node);
69
+ $$.cache.add(KEY.legendItemMap, itemMap);
79
70
  }
80
71
  /**
81
- * Get touch point from a touch event.
82
- * @param {TouchEvent} event Touch event
83
- * @returns {Touch | undefined} Touch point
84
- * @private
85
- */
72
+ * Get touch point from a touch event.
73
+ * @param {TouchEvent} event Touch event
74
+ * @returns {Touch | undefined} Touch point
75
+ * @private
76
+ */
86
77
  function _getLegendTouchPoint(event) {
87
- return event.changedTouches?.[0] || event.touches?.[0];
78
+ return event.changedTouches?.[0] || event.touches?.[0];
88
79
  }
89
80
  /**
90
- * Store the touch start position for legend tap detection.
91
- * @param {object} $$ ChartInternal context
92
- * @param {string} id Legend data id
93
- * @param {TouchEvent} event Touch event
94
- * @private
95
- */
81
+ * Store the touch start position for legend tap detection.
82
+ * @param {object} $$ ChartInternal context
83
+ * @param {string} id Legend data id
84
+ * @param {TouchEvent} event Touch event
85
+ * @private
86
+ */
96
87
  function _setLegendTouchStart($$, id, event) {
97
- const touch = _getLegendTouchPoint(event);
98
- $$.state.legendTouch = touch ?
99
- {
100
- id,
101
- x: touch.clientX,
102
- y: touch.clientY,
103
- moved: false
104
- } :
105
- null;
88
+ const touch = _getLegendTouchPoint(event);
89
+ $$.state.legendTouch = touch ? {
90
+ id,
91
+ x: touch.clientX,
92
+ y: touch.clientY,
93
+ moved: false
94
+ } : null;
106
95
  }
107
96
  /**
108
- * Update whether the current legend touch moved beyond tap tolerance.
109
- * @param {object} $$ ChartInternal context
110
- * @param {TouchEvent} event Touch event
111
- * @private
112
- */
97
+ * Update whether the current legend touch moved beyond tap tolerance.
98
+ * @param {object} $$ ChartInternal context
99
+ * @param {TouchEvent} event Touch event
100
+ * @private
101
+ */
113
102
  function _updateLegendTouchMove($$, event) {
114
- const start = $$.state.legendTouch;
115
- const touch = start && _getLegendTouchPoint(event);
116
- if (touch) {
117
- start.moved = start.moved ||
118
- Math.abs(touch.clientX - start.x) > LEGEND_TOUCH_TAP_THRESHOLD ||
119
- Math.abs(touch.clientY - start.y) > LEGEND_TOUCH_TAP_THRESHOLD;
120
- }
103
+ const start = $$.state.legendTouch;
104
+ const touch = start && _getLegendTouchPoint(event);
105
+ if (touch) start.moved = start.moved || Math.abs(touch.clientX - start.x) > LEGEND_TOUCH_TAP_THRESHOLD || Math.abs(touch.clientY - start.y) > LEGEND_TOUCH_TAP_THRESHOLD;
121
106
  }
122
107
  /**
123
- * Determine whether a touch sequence is a legend tap.
124
- * @param {object} $$ ChartInternal context
125
- * @param {string} id Legend data id
126
- * @param {TouchEvent} event Touch event
127
- * @returns {boolean} Whether the touch sequence is a tap
128
- * @private
129
- */
108
+ * Determine whether a touch sequence is a legend tap.
109
+ * @param {object} $$ ChartInternal context
110
+ * @param {string} id Legend data id
111
+ * @param {TouchEvent} event Touch event
112
+ * @returns {boolean} Whether the touch sequence is a tap
113
+ * @private
114
+ */
130
115
  function _isLegendTouchTap($$, id, event) {
131
- _updateLegendTouchMove($$, event);
132
- const start = $$.state.legendTouch;
133
- $$.state.legendTouch = null;
134
- return !!start && start.id === id && !start.moved;
116
+ _updateLegendTouchMove($$, event);
117
+ const start = $$.state.legendTouch;
118
+ $$.state.legendTouch = null;
119
+ return !!start && start.id === id && !start.moved;
135
120
  }
136
121
  /**
137
- * Mark a touch legend tap so the following compatibility click can be skipped.
138
- * @param {object} $$ ChartInternal context
139
- * @param {string} id Legend data id
140
- * @private
141
- */
122
+ * Mark a touch legend tap so the following compatibility click can be skipped.
123
+ * @param {object} $$ ChartInternal context
124
+ * @param {string} id Legend data id
125
+ * @private
126
+ */
142
127
  function _markLegendTouchClick($$, id) {
143
- $$.state.legendLastTouchClickId = id;
144
- $$.state.legendLastTouchClickTime = Date.now();
128
+ $$.state.legendLastTouchClickId = id;
129
+ $$.state.legendLastTouchClickTime = Date.now();
145
130
  }
146
131
  /**
147
- * Check if a click duplicates a recent touch legend tap.
148
- * @param {object} $$ ChartInternal context
149
- * @param {string} id Legend data id
150
- * @returns {boolean} Whether the click is duplicate
151
- * @private
152
- */
132
+ * Check if a click duplicates a recent touch legend tap.
133
+ * @param {object} $$ ChartInternal context
134
+ * @param {string} id Legend data id
135
+ * @returns {boolean} Whether the click is duplicate
136
+ * @private
137
+ */
153
138
  function _isDuplicateLegendTouchClick($$, id) {
154
- const { state } = $$;
155
- const duplicate = state.legendLastTouchClickId === id &&
156
- Date.now() - (state.legendLastTouchClickTime || 0) < LEGEND_TOUCH_CLICK_TIMEOUT;
157
- if (duplicate) {
158
- state.legendLastTouchClickId = null;
159
- state.legendLastTouchClickTime = 0;
160
- }
161
- return duplicate;
139
+ const { state } = $$;
140
+ const duplicate = state.legendLastTouchClickId === id && Date.now() - (state.legendLastTouchClickTime || 0) < LEGEND_TOUCH_CLICK_TIMEOUT;
141
+ if (duplicate) {
142
+ state.legendLastTouchClickId = null;
143
+ state.legendLastTouchClickTime = 0;
144
+ }
145
+ return duplicate;
162
146
  }
163
147
  /**
164
- * Get touch listener passive option following interaction.inputType.touch.preventDefault.
165
- * @param {object} $$ ChartInternal context
166
- * @returns {object} Touch listener option
167
- * @private
168
- */
148
+ * Get touch listener passive option following interaction.inputType.touch.preventDefault.
149
+ * @param {object} $$ ChartInternal context
150
+ * @returns {object} Touch listener option
151
+ * @private
152
+ */
169
153
  function _getLegendTouchOption($$) {
170
- const preventDefault = $$.config.interaction_inputType_touch?.preventDefault;
171
- const isPrevented = (isBoolean(preventDefault) && preventDefault) || false;
172
- const preventThreshold = (!isNaN(preventDefault) && preventDefault) || null;
173
- return {
174
- passive: !isPrevented && preventThreshold === null
175
- };
154
+ const preventDefault = $$.config.interaction_inputType_touch?.preventDefault;
155
+ return { passive: !(isBoolean(preventDefault) && preventDefault || false) && (!isNaN(preventDefault) && preventDefault || null) === null };
176
156
  }
177
- var legend = {
178
- /**
179
- * Initialize the legend.
180
- * @private
181
- */
182
- initLegend() {
183
- const $$ = this;
184
- const { config, $el } = $$;
185
- $$.legendItemTextBox = {};
186
- $$.state.legendHasRendered = false;
187
- if ($$.state.isCanvasMode) {
188
- if (config.legend_show) {
189
- $$.updateHtmlLegend?.();
190
- }
191
- else {
192
- $$.state.hiddenLegendIds = new Set($$.mapToIds($$.data.targets));
193
- }
194
- return;
195
- }
196
- if (config.legend_show) {
197
- if (!config.legend_contents_bindto) {
198
- $el.legend = $$.$el.svg.append("g")
199
- .classed($LEGEND.legend, true)
200
- .attr("transform", $$.getTranslate("legend"));
201
- }
202
- // MEMO: call here to update legend box and translate for all
203
- // MEMO: translate will be updated by this, so transform not needed in updateLegend()
204
- $$.updateLegend();
205
- }
206
- else {
207
- $$.state.hiddenLegendIds = new Set($$.mapToIds($$.data.targets));
208
- }
209
- },
210
- /**
211
- * Update legend element
212
- * @param {Array} targetIds ID's of target
213
- * @param {object} options withTransform : Whether to use the transform property / withTransitionForTransform: Whether transition is used when using the transform property / withTransition : whether or not to transition.
214
- * @param {object} transitions Return value of the generateTransitions
215
- * @private
216
- */
217
- updateLegend(targetIds, options, transitions) {
218
- const $$ = this;
219
- const { config, state, scale, $el } = $$;
220
- const optionz = options || {
221
- withTransform: false,
222
- withTransitionForTransform: false,
223
- withTransition: false
224
- };
225
- optionz.withTransition = getOption(optionz, "withTransition", true);
226
- optionz.withTransitionForTransform = getOption(optionz, "withTransitionForTransform", true);
227
- if (config.legend_contents_bindto && config.legend_contents_template) {
228
- $$.updateLegendTemplate();
229
- }
230
- else if (!state.hasTreemap) {
231
- $$.updateLegendElement(targetIds || $$.mapToIds($$.data.targets), optionz);
232
- }
233
- // toggle legend state
234
- $el.legend?.selectAll(`.${$LEGEND.legendItem}`)
235
- .classed($LEGEND.legendItemHidden, function (id) {
236
- const hide = !$$.isTargetToShow(id);
237
- if (hide) {
238
- this.style.opacity = null;
239
- }
240
- return hide;
241
- });
242
- // Update size and scale
243
- $$.updateScales(false, !scale.zoom);
244
- $$.updateSvgSize();
245
- // Update g positions
246
- $$.transformAll(optionz.withTransitionForTransform, transitions);
247
- state.legendHasRendered = true;
248
- },
249
- /**
250
- * Update legend using template option
251
- * @private
252
- */
253
- updateLegendTemplate() {
254
- const $$ = this;
255
- const { config, $el } = $$;
256
- const wrapper = select(config.legend_contents_bindto);
257
- const template = config.legend_contents_template;
258
- if (!wrapper.empty()) {
259
- const targets = $$.mapToIds($$.data.targets);
260
- const ids = [];
261
- let html = "";
262
- targets.forEach(v => {
263
- const content = isFunction(template) ?
264
- sanitize(template.call($$.api, v, $$.color(v), $$.api.data(v)[0].values)) :
265
- tplProcess(template, {
266
- COLOR: $$.color(v),
267
- TITLE: v
268
- });
269
- if (content) {
270
- ids.push(v);
271
- html += content;
272
- }
273
- });
274
- const legendItem = wrapper.html(html)
275
- .selectAll(function () {
276
- return this.childNodes;
277
- })
278
- .data(ids);
279
- $$.setLegendItem(legendItem);
280
- $el.legend = wrapper;
281
- }
282
- },
283
- /**
284
- * Update the size of the legend.
285
- * @param {Obejct} size Size object
286
- * @private
287
- */
288
- updateSizeForLegend(size) {
289
- const $$ = this;
290
- const { config, state: { isLegendTop, isLegendLeft, isLegendRight, isLegendInset, current } } = $$;
291
- const { width, height } = size;
292
- const insetLegendPosition = {
293
- top: isLegendTop ?
294
- $$.getCurrentPaddingByDirection("top") + config.legend_inset_y + 5.5 :
295
- current.height - height - $$.getCurrentPaddingByDirection("bottom") -
296
- config.legend_inset_y,
297
- left: isLegendLeft ?
298
- $$.getCurrentPaddingByDirection("left") + config.legend_inset_x + 0.5 :
299
- current.width - width - $$.getCurrentPaddingByDirection("right") -
300
- config.legend_inset_x + 0.5
301
- };
302
- $$.state.margin3 = {
303
- top: isLegendRight ?
304
- 0 :
305
- isLegendInset ?
306
- insetLegendPosition.top :
307
- current.height - height,
308
- right: NaN,
309
- bottom: 0,
310
- left: isLegendRight ?
311
- current.width - width :
312
- isLegendInset ?
313
- insetLegendPosition.left :
314
- 0
315
- };
316
- },
317
- /**
318
- * Transform Legend
319
- * @param {boolean} withTransition whether or not to transition.
320
- * @private
321
- */
322
- transformLegend(withTransition) {
323
- const $$ = this;
324
- const { $el: { legend }, $T } = $$;
325
- $T(legend, withTransition)
326
- .attr("transform", $$.getTranslate("legend"));
327
- },
328
- /**
329
- * Update legend item color
330
- * @param {string} id Corresponding data ID value
331
- * @param {string} color Color value
332
- * @private
333
- */
334
- updateLegendItemColor(id, color) {
335
- const $$ = this;
336
- const { legend } = $$.$el;
337
- if (legend) {
338
- // Use cached Map lookup for O(1) performance
339
- const legendItem = $$.getLegendItemById(id);
340
- if (legendItem) {
341
- select(legendItem).select("line")
342
- .style("stroke", color);
343
- }
344
- }
345
- },
346
- /**
347
- * Get the width of the legend
348
- * @returns {number} width
349
- * @private
350
- */
351
- getLegendWidth() {
352
- const $$ = this;
353
- const { current: { width }, isLegendRight, isLegendInset, legendItemWidth, legendStep } = $$.state;
354
- return $$.config.legend_show ?
355
- (isLegendRight || isLegendInset ? legendItemWidth * (legendStep + 1) : width) :
356
- 0;
357
- },
358
- /**
359
- * Get the height of the legend
360
- * @returns {number} height
361
- * @private
362
- */
363
- getLegendHeight() {
364
- const $$ = this;
365
- const { current, isLegendRight, legendItemHeight, legendStep } = $$.state;
366
- const isFitPadding = $$.config.padding?.mode === "fit";
367
- const minHeight = isFitPadding ? 10 : 20;
368
- const height = $$.config.legend_show ?
369
- (isLegendRight ? current.height : (Math.max(minHeight, legendItemHeight)) * (legendStep + 1)) :
370
- 0;
371
- return height;
372
- },
373
- /**
374
- * Get the opacity of the legend that is unfocused
375
- * @param {d3.selection} legendItem Legend item node
376
- * @returns {string|null} opacity
377
- * @private
378
- */
379
- opacityForUnfocusedLegend(legendItem) {
380
- return legendItem.classed($LEGEND.legendItemHidden) ? null : "0.3";
381
- },
382
- /**
383
- * Toggles the focus of the legend
384
- * @param {Array} targetIds ID's of target
385
- * @param {boolean} focus whether or not to focus.
386
- * @private
387
- */
388
- toggleFocusLegend(targetIds, focus) {
389
- const $$ = this;
390
- const { $el: { legend }, $T } = $$;
391
- const targetIdz = $$.mapToTargetIds(targetIds);
392
- legend && $T(legend.selectAll(`.${$LEGEND.legendItem}`)
393
- .filter(id => targetIdz.indexOf(id) >= 0)
394
- .classed($FOCUS.legendItemFocused, focus))
395
- .style("opacity", function () {
396
- return focus ? null : $$.opacityForUnfocusedLegend.call($$, select(this));
397
- });
398
- },
399
- /**
400
- * Revert the legend to its default state
401
- * @private
402
- */
403
- revertLegend() {
404
- const $$ = this;
405
- const { $el: { legend }, $T } = $$;
406
- legend && $T(legend.selectAll(`.${$LEGEND.legendItem}`)
407
- .classed($FOCUS.legendItemFocused, false))
408
- .style("opacity", null);
409
- },
410
- /**
411
- * Shows the legend
412
- * @param {Array} targetIds ID's of target
413
- * @private
414
- */
415
- showLegend(targetIds) {
416
- const $$ = this;
417
- const { config, $el, $T } = $$;
418
- if (!config.legend_show) {
419
- config.legend_show = true;
420
- $el.legend ? $el.legend.style("visibility", null) : $$.initLegend();
421
- !$$.state.legendHasRendered && $$.updateLegend();
422
- }
423
- $$.removeHiddenLegendIds(targetIds);
424
- $T($el.legend.selectAll($$.selectorLegends(targetIds))
425
- .style("visibility", null)).style("opacity", null);
426
- },
427
- /**
428
- * Hide the legend
429
- * @param {Array} targetIds ID's of target
430
- * @private
431
- */
432
- hideLegend(targetIds) {
433
- const $$ = this;
434
- const { config, $el: { legend } } = $$;
435
- if (config.legend_show && isEmpty(targetIds)) {
436
- config.legend_show = false;
437
- legend?.style("visibility", "hidden");
438
- }
439
- $$.addHiddenLegendIds(targetIds);
440
- // legend element isn't created when the chart was generated with legend.show=false
441
- legend?.selectAll($$.selectorLegends(targetIds))
442
- .style("opacity", "0")
443
- .style("visibility", "hidden");
444
- },
445
- /**
446
- * Get legend item textbox dimension
447
- * @param {string} id Data ID
448
- * @param {HTMLElement|d3.selection} textElement Text node element
449
- * @returns {object} Bounding rect
450
- * @private
451
- */
452
- getLegendItemTextBox(id, textElement) {
453
- const $$ = this;
454
- const { cache, state } = $$;
455
- let data;
456
- // do not prefix w/'$', to not be resetted cache in .load() call
457
- const cacheKey = KEY.legendItemTextBox;
458
- if (id) {
459
- data = (!state.redrawing && cache.get(cacheKey)) || {};
460
- if (!data[id]) {
461
- data[id] = $$.getTextRect(textElement, $LEGEND.legendItem);
462
- cache.add(cacheKey, data);
463
- }
464
- data = data[id];
465
- }
466
- return data;
467
- },
468
- /**
469
- * Set legend item style & bind events
470
- * @param {d3.selection} item Item node
471
- * @private
472
- */
473
- setLegendItem(item) {
474
- const $$ = this;
475
- const { $el, api, config, state } = $$;
476
- const isTouch = state.inputType === "touch";
477
- const hasGauge = $$.hasType("gauge");
478
- const useCssRule = config.boost_useCssRule;
479
- const interaction = config.legend_item_interaction;
480
- const eventType = interaction.dblclick ? "dblclick" : "click";
481
- const hasClickInteraction = interaction || isFunction(config.legend_item_onclick);
482
- const touchOption = isTouch ? _getLegendTouchOption($$) : undefined;
483
- const handleLegendToggle = function (event, id) {
484
- if (!callFn(config.legend_item_onclick, api, id, !state.hiddenTargetIds.has(id))) {
485
- const { altKey, type } = event;
486
- const selected = select(this);
487
- if (type === "dblclick" || altKey) {
488
- // when focused legend is clicked(with altKey or double clicked), reset all hiding.
489
- if (state.hiddenTargetIds.size &&
490
- !selected.classed($LEGEND.legendItemHidden)) {
491
- api.show();
492
- }
493
- else {
494
- api.hide();
495
- api.show(id);
496
- }
497
- }
498
- else {
499
- api.toggle(id);
500
- selected.classed($FOCUS.legendItemFocused, false);
501
- }
502
- }
503
- if (isTouch) {
504
- $$.hideTooltip();
505
- $$.hideGridFocus?.(true);
506
- }
507
- };
508
- item
509
- .attr("class", function (id) {
510
- const node = select(this);
511
- const itemClass = (!node.empty() && node.attr("class")) || "";
512
- return itemClass + $$.generateClass($LEGEND.legendItem, id);
513
- })
514
- .style("visibility", id => ($$.isLegendToShow(id) ? null : "hidden"));
515
- if (config.interaction_enabled) {
516
- if (useCssRule) {
517
- [
518
- [`.${$LEGEND.legendItem}`, "cursor:pointer"],
519
- [`.${$LEGEND.legendItem} text`, "pointer-events:none"],
520
- [`.${$LEGEND.legendItemPoint} text`, "pointer-events:none"],
521
- [`.${$LEGEND.legendItemTile}`, "pointer-events:none"],
522
- [`.${$LEGEND.legendItemEvent}`, "fill-opacity:0"]
523
- ].forEach(v => {
524
- const [selector, props] = v;
525
- $$.setCssRule(false, selector, [props])($el.legend);
526
- });
527
- }
528
- item
529
- .on(eventType, hasClickInteraction ?
530
- function (event, id) {
531
- if (isTouch && event.type === "click" &&
532
- _isDuplicateLegendTouchClick($$, id)) {
533
- return;
534
- }
535
- handleLegendToggle.call(this, event, id);
536
- } :
537
- null);
538
- isTouch && eventType === "click" && hasClickInteraction && item
539
- .on("touchstart", function (event, id) {
540
- _setLegendTouchStart($$, id, event);
541
- }, touchOption)
542
- .on("touchmove", event => {
543
- _updateLegendTouchMove($$, event);
544
- }, touchOption)
545
- .on("touchend", function (event, id) {
546
- if (_isLegendTouchTap($$, id, event)) {
547
- _markLegendTouchClick($$, id);
548
- handleLegendToggle.call(this, event, id);
549
- }
550
- }, touchOption);
551
- !isTouch && item
552
- .on("mouseout", interaction || isFunction(config.legend_item_onout) ?
553
- function (event, id) {
554
- if (!callFn(config.legend_item_onout, api, id, !state.hiddenTargetIds.has(id))) {
555
- select(this).classed($FOCUS.legendItemFocused, false);
556
- if (hasGauge) {
557
- $$.undoMarkOverlapped($$, `.${$GAUGE.gaugeValue}`);
558
- }
559
- $$.api.revert();
560
- }
561
- } :
562
- null)
563
- .on("mouseover", interaction || isFunction(config.legend_item_onover) ?
564
- function (event, id) {
565
- if (!callFn(config.legend_item_onover, api, id, !state.hiddenTargetIds.has(id))) {
566
- select(this).classed($FOCUS.legendItemFocused, true);
567
- if (hasGauge) {
568
- $$.markOverlapped(id, $$, `.${$GAUGE.gaugeValue}`);
569
- }
570
- if (!state.transiting && $$.isTargetToShow(id)) {
571
- api.focus(id);
572
- }
573
- }
574
- } :
575
- null);
576
- // set cursor when has some interaction
577
- !item.empty() && item.on("click mouseout mouseover") &&
578
- item.style("cursor", $$.getStylePropValue("pointer"));
579
- }
580
- // Build legend item map for O(1) lookup
581
- _buildLegendItemMap($$, item);
582
- },
583
- /**
584
- * Get legend item node by ID using cached Map for O(1) lookup
585
- * Falls back to D3 selection if map is not available
586
- * @param {string} id Data ID
587
- * @returns {HTMLElement | null} Legend item node
588
- * @private
589
- */
590
- getLegendItemById(id) {
591
- const $$ = this;
592
- const itemMap = $$.cache.get(KEY.legendItemMap);
593
- if (itemMap && itemMap instanceof Map) {
594
- return itemMap.get(id) || null;
595
- }
596
- // Fallback to D3 selection (slower)
597
- const item = $$.$el.legend?.selectAll(`.${$LEGEND.legendItem}`)
598
- .filter(d => d === id);
599
- return item?.node() || null;
600
- },
601
- /**
602
- * Update the legend
603
- * @param {Array} targetIds ID's of target
604
- * @param {object} options withTransform : Whether to use the transform property / withTransitionForTransform: Whether transition is used when using the transform property / withTransition : whether or not to transition.
605
- * @private
606
- */
607
- updateLegendElement(targetIds, options) {
608
- const $$ = this;
609
- const { config, state, $el: { legend }, $T } = $$;
610
- const legendType = config.legend_item_tile_type;
611
- const isRectangle = legendType !== "circle";
612
- const legendItemR = config.legend_item_tile_r;
613
- const itemTileSize = {
614
- width: isRectangle ? config.legend_item_tile_width : legendItemR * 2,
615
- height: isRectangle ? config.legend_item_tile_height : legendItemR * 2
616
- };
617
- const dimension = {
618
- padding: {
619
- top: 4,
620
- right: 10
621
- },
622
- max: {
623
- width: 0,
624
- height: 0
625
- },
626
- posMin: 10,
627
- step: 0,
628
- tileWidth: itemTileSize.width + 5,
629
- totalLength: 0
630
- };
631
- const sizes = {
632
- offsets: {},
633
- widths: {},
634
- heights: {},
635
- margins: [0],
636
- steps: {}
637
- };
638
- let xForLegend;
639
- let yForLegend;
640
- let background;
641
- // Skip elements when their name is set to null
642
- const targetIdz = targetIds
643
- .filter(id => !isDefined(config.data_names[id]) || config.data_names[id] !== null);
644
- const withTransition = options.withTransition;
645
- const isLegendRightOrInset = state.isLegendRight || state.isLegendInset;
646
- const getFormattedText = _getFormattedText.bind($$);
647
- const updatePositions = $$.getUpdateLegendPositions(targetIdz, dimension, sizes, isLegendRightOrInset);
648
- if (state.isLegendInset) {
649
- dimension.step = config.legend_inset_step ? config.legend_inset_step : targetIdz.length;
650
- state.legendStep = dimension.step;
651
- }
652
- if (state.isLegendRight) {
653
- xForLegend = id => dimension.max.width * sizes.steps[id];
654
- yForLegend = id => sizes.margins[sizes.steps[id]] + sizes.offsets[id];
655
- }
656
- else if (state.isLegendInset) {
657
- xForLegend = id => dimension.max.width * sizes.steps[id] + 10;
658
- yForLegend = id => sizes.margins[sizes.steps[id]] + sizes.offsets[id];
659
- }
660
- else {
661
- xForLegend = id => sizes.margins[sizes.steps[id]] + sizes.offsets[id];
662
- yForLegend = id => dimension.max.height * sizes.steps[id];
663
- }
664
- const posFn = {
665
- xText: (id, i) => xForLegend(id, i) + 4 + itemTileSize.width,
666
- xRect: (id, i) => xForLegend(id, i),
667
- x1Tile: (id, i) => xForLegend(id, i) - 2,
668
- x2Tile: (id, i) => xForLegend(id, i) - 2 + itemTileSize.width,
669
- yText: (id, i) => yForLegend(id, i) + 9,
670
- yRect: (id, i) => yForLegend(id, i) - 5,
671
- yTile: (id, i) => yForLegend(id, i) + 4
672
- };
673
- $$.generateLegendItem(targetIdz, itemTileSize, updatePositions, posFn, isLegendRightOrInset, getFormattedText);
674
- // Set background for inset legend
675
- background = legend.select(`.${$LEGEND.legendBackground} rect`);
676
- if (state.isLegendInset && dimension.max.width > 0 && background.size() === 0) {
677
- background = legend.insert("g", `.${$LEGEND.legendItem}`)
678
- .attr("class", $LEGEND.legendBackground)
679
- .append("rect");
680
- }
681
- if (config.legend_tooltip) {
682
- legend.selectAll("title")
683
- .data(targetIdz)
684
- .text(id => getFormattedText(id, false));
685
- }
686
- const texts = legend.selectAll("text")
687
- .data(targetIdz)
688
- .text(id => getFormattedText(id)) // MEMO: needed for update
689
- .each(function (id, i) {
690
- updatePositions(this, id, i);
691
- });
692
- $T(texts, withTransition)
693
- .attr("x", posFn.xText)
694
- .attr("y", posFn.yText);
695
- const rects = legend.selectAll(`rect.${$LEGEND.legendItemEvent}`)
696
- .data(targetIdz);
697
- $T(rects, withTransition)
698
- .attr("width", id => sizes.widths[id])
699
- .attr("height", id => sizes.heights[id])
700
- .attr("x", posFn.xRect)
701
- .attr("y", posFn.yRect);
702
- // update legend items position
703
- $$.updateLegendItemPos(targetIdz, withTransition, posFn);
704
- if (background) {
705
- $T(background, withTransition)
706
- .attr("height", $$.getLegendHeight() - 12)
707
- .attr("width", dimension.max.width * (dimension.step + 1) + 10);
708
- }
709
- // Update all to reflect change of legend
710
- state.legendItemWidth = dimension.max.width;
711
- state.legendItemHeight = dimension.max.height;
712
- state.legendStep = dimension.step;
713
- },
714
- /**
715
- * Get position update function
716
- * @param {Array} targetIdz Data ids
717
- * @param {object} dimension Dimension object
718
- * @param {object} sizes Size object
719
- * @param {boolean} isLegendRightOrInset Whether legend is right or inset
720
- * @returns {function} Update position function
721
- * @private
722
- */
723
- getUpdateLegendPositions(targetIdz, dimension, sizes, isLegendRightOrInset) {
724
- const $$ = this;
725
- const { config, state } = $$;
726
- return function (textElement, id, index) {
727
- const reset = index === 0;
728
- const isLast = index === targetIdz.length - 1;
729
- const box = $$.getLegendItemTextBox(id, textElement);
730
- const itemWidth = box.width + dimension.tileWidth +
731
- (isLast && !isLegendRightOrInset ? 0 : dimension.padding.right) +
732
- config.legend_padding;
733
- const itemHeight = box.height + dimension.padding.top;
734
- const itemLength = isLegendRightOrInset ? itemHeight : itemWidth;
735
- const areaLength = isLegendRightOrInset ? $$.getLegendHeight() : $$.getLegendWidth();
736
- let margin;
737
- // MEMO: care about condifion of step, totalLength
738
- const updateValues = function (id2, withoutStep) {
739
- if (!withoutStep) {
740
- margin = (areaLength - dimension.totalLength - itemLength) / 2;
741
- if (margin < dimension.posMin) {
742
- margin = (areaLength - itemLength) / 2;
743
- dimension.totalLength = 0;
744
- dimension.step++;
745
- }
746
- }
747
- sizes.steps[id2] = dimension.step;
748
- sizes.margins[dimension.step] = state.isLegendInset ? 10 : margin;
749
- sizes.offsets[id2] = dimension.totalLength;
750
- dimension.totalLength += itemLength;
751
- };
752
- if (reset) {
753
- dimension.totalLength = 0;
754
- dimension.step = 0;
755
- dimension.max.width = 0;
756
- dimension.max.height = 0;
757
- }
758
- if (config.legend_show && !$$.isLegendToShow(id)) {
759
- sizes.widths[id] = 0;
760
- sizes.heights[id] = 0;
761
- sizes.steps[id] = 0;
762
- sizes.offsets[id] = 0;
763
- return;
764
- }
765
- sizes.widths[id] = itemWidth;
766
- sizes.heights[id] = itemHeight;
767
- if (!dimension.max.width || itemWidth >= dimension.max.width) {
768
- dimension.max.width = itemWidth;
769
- }
770
- if (!dimension.max.height || itemHeight >= dimension.max.height) {
771
- dimension.max.height = itemHeight;
772
- }
773
- const maxLength = isLegendRightOrInset ? dimension.max.height : dimension.max.width;
774
- if (config.legend_equally) {
775
- for (const id2 in sizes.widths)
776
- sizes.widths[id2] = dimension.max.width;
777
- for (const id2 in sizes.heights)
778
- sizes.heights[id2] = dimension.max.height;
779
- margin = (areaLength - maxLength * targetIdz.length) / 2;
780
- if (margin < dimension.posMin) {
781
- dimension.totalLength = 0;
782
- dimension.step = 0;
783
- targetIdz.forEach(id2 => updateValues(id2));
784
- }
785
- else {
786
- updateValues(id, true);
787
- }
788
- }
789
- else {
790
- updateValues(id);
791
- }
792
- };
793
- },
794
- /**
795
- * Generate legend item elements
796
- * @param {Array} targetIdz Data ids
797
- * @param {object} itemTileSize Item tile size {width, height}
798
- * @param {function} updatePositions Update position function
799
- * @param {object} posFn Position functions
800
- * @param {boolean} isLegendRightOrInset Whether legend is right or inset
801
- * @param {function} getFormattedText Bound text formatter function
802
- * @private
803
- */
804
- generateLegendItem(targetIdz, itemTileSize, updatePositions, posFn, isLegendRightOrInset, getFormattedText) {
805
- const $$ = this;
806
- const { config, state, $el: { legend } } = $$;
807
- const usePoint = config.legend_usePoint;
808
- const legendItemR = config.legend_item_tile_r;
809
- const legendType = config.legend_item_tile_type;
810
- const isRectangle = legendType !== "circle";
811
- const pos = -200;
812
- // Define g for legend area
813
- const l = legend.selectAll(`.${$LEGEND.legendItem}`)
814
- .data(targetIdz)
815
- .enter()
816
- .append("g");
817
- $$.setLegendItem(l);
818
- if (config.legend_tooltip) {
819
- l.append("title").text(id => id);
820
- }
821
- l.append("text")
822
- .text(id => getFormattedText(id))
823
- .each(function (id, i) {
824
- updatePositions(this, id, i);
825
- })
826
- .style("pointer-events", $$.getStylePropValue("none"))
827
- .attr("x", isLegendRightOrInset ? posFn.xText : pos)
828
- .attr("y", isLegendRightOrInset ? pos : posFn.yText);
829
- l.append("rect")
830
- .attr("class", $LEGEND.legendItemEvent)
831
- .style("fill-opacity", $$.getStylePropValue("0"))
832
- .attr("x", isLegendRightOrInset ? posFn.xRect : pos)
833
- .attr("y", isLegendRightOrInset ? pos : posFn.yRect);
834
- if (usePoint) {
835
- const ids = [];
836
- const pattern = $$.getValidPointPattern();
837
- l.append(d => {
838
- ids.indexOf(d) === -1 && ids.push(d);
839
- let point = pattern[ids.indexOf(d) % pattern.length];
840
- if (point === "rectangle") {
841
- point = "rect";
842
- }
843
- return doc.createElementNS(namespaces.svg, ("hasValidPointType" in $$) && $$.hasValidPointType(point) ? point : "use");
844
- })
845
- .attr("class", $LEGEND.legendItemPoint)
846
- .style("fill", _getLegendColor.bind($$))
847
- .style("pointer-events", $$.getStylePropValue("none"))
848
- .attr("href", (data, idx, selection) => {
849
- const node = selection[idx];
850
- const nodeName = node.nodeName.toLowerCase();
851
- const id = $$.getTargetSelectorSuffix(data);
852
- return nodeName === "use" ? `#${state.datetimeId}-point${id}` : undefined;
853
- });
854
- }
855
- else {
856
- l.append(isRectangle ? "line" : legendType)
857
- .attr("class", $LEGEND.legendItemTile)
858
- .style("stroke", _getLegendColor.bind($$))
859
- .style("pointer-events", $$.getStylePropValue("none"))
860
- .call(selection => {
861
- if (legendType === "circle") {
862
- selection
863
- .attr("r", legendItemR)
864
- .style("fill", _getLegendColor.bind($$))
865
- .attr("cx", isLegendRightOrInset ? posFn.x2Tile : pos)
866
- .attr("cy", isLegendRightOrInset ? pos : posFn.yTile);
867
- }
868
- else if (isRectangle) {
869
- selection
870
- .attr("stroke-width", itemTileSize.height)
871
- .attr("x1", isLegendRightOrInset ? posFn.x1Tile : pos)
872
- .attr("y1", isLegendRightOrInset ? pos : posFn.yTile)
873
- .attr("x2", isLegendRightOrInset ? posFn.x2Tile : pos)
874
- .attr("y2", isLegendRightOrInset ? pos : posFn.yTile);
875
- }
876
- });
877
- }
878
- },
879
- /**
880
- * Update legend item position
881
- * @param {Array} targetIdz Data ids
882
- * @param {boolean} withTransition Whether or not to apply transition
883
- * @param {object} posFn Position functions
884
- * @private
885
- */
886
- updateLegendItemPos(targetIdz, withTransition, posFn) {
887
- const $$ = this;
888
- const { config, $el: { legend }, $T } = $$;
889
- const usePoint = config.legend_usePoint;
890
- const legendType = config.legend_item_tile_type;
891
- const isRectangle = legendType !== "circle";
892
- if (usePoint) {
893
- const tiles = legend.selectAll(`.${$LEGEND.legendItemPoint}`)
894
- .data(targetIdz);
895
- const isRectangleTile = config.legend_item_tile_type !== "circle";
896
- const tileWidth = isRectangleTile ?
897
- config.legend_item_tile_width :
898
- config.legend_item_tile_r * 2;
899
- const tileHeight = isRectangleTile ?
900
- config.legend_item_tile_height :
901
- config.legend_item_tile_r * 2;
902
- const iconWidth = tileWidth * 0.75;
903
- const iconHeight = tileHeight * 0.75;
904
- const customScaleX = tileWidth / 8;
905
- const customScaleY = tileHeight / 8;
906
- $T(tiles, withTransition)
907
- .each(function () {
908
- const nodeName = this.nodeName.toLowerCase();
909
- let x = "x";
910
- let y = "y";
911
- let radius = null;
912
- let width = null;
913
- let height = null;
914
- if (nodeName === "circle") {
915
- x = "cx";
916
- y = "cy";
917
- radius = Math.min(iconWidth, iconHeight) / 2;
918
- }
919
- else if (nodeName === "rect") {
920
- width = iconWidth;
921
- height = iconHeight;
922
- }
923
- const tile = select(this)
924
- .attr("transform", null)
925
- .attr("x", null)
926
- .attr("y", null)
927
- .attr("cx", null)
928
- .attr("cy", null)
929
- .attr("r", radius)
930
- .attr("width", width)
931
- .attr("height", height);
932
- if (nodeName === "use") {
933
- tile.attr("transform", d => `translate(${posFn.x1Tile(d)} ${posFn.yTile(d) - tileHeight / 2}) ` +
934
- `scale(${customScaleX} ${customScaleY})`);
935
- }
936
- else {
937
- tile
938
- .attr(x, d => posFn.x1Tile(d) + ((tileWidth - (width || 0)) / 2))
939
- .attr(y, d => posFn.yTile(d) - ((height || 0) / 2));
940
- }
941
- });
942
- }
943
- else {
944
- const tiles = legend.selectAll(`.${$LEGEND.legendItemTile}`)
945
- .data(targetIdz);
946
- $T(tiles, withTransition)
947
- .style("stroke", _getLegendColor.bind($$))
948
- .call(selection => {
949
- if (legendType === "circle") {
950
- selection
951
- .attr("cx", d => {
952
- const x2 = posFn.x2Tile(d);
953
- return x2 - ((x2 - posFn.x1Tile(d)) / 2);
954
- })
955
- .attr("cy", posFn.yTile);
956
- }
957
- else if (isRectangle) {
958
- selection
959
- .attr("x1", posFn.x1Tile)
960
- .attr("y1", posFn.yTile)
961
- .attr("x2", posFn.x2Tile)
962
- .attr("y2", posFn.yTile);
963
- }
964
- });
965
- }
966
- }
157
+ var legend_default = {
158
+ /**
159
+ * Initialize the legend.
160
+ * @private
161
+ */
162
+ initLegend() {
163
+ const $$ = this;
164
+ const { config, $el } = $$;
165
+ $$.legendItemTextBox = {};
166
+ $$.state.legendHasRendered = false;
167
+ if ($$.state.isCanvasMode) {
168
+ if (config.legend_show) $$.updateHtmlLegend?.();
169
+ else $$.state.hiddenLegendIds = new Set($$.mapToIds($$.data.targets));
170
+ return;
171
+ }
172
+ if (config.legend_show) {
173
+ if (!config.legend_contents_bindto) $el.legend = $$.$el.svg.append("g").classed($LEGEND.legend, true).attr("transform", $$.getTranslate("legend"));
174
+ $$.updateLegend();
175
+ } else $$.state.hiddenLegendIds = new Set($$.mapToIds($$.data.targets));
176
+ },
177
+ /**
178
+ * Update legend element
179
+ * @param {Array} targetIds ID's of target
180
+ * @param {object} options withTransform : Whether to use the transform property / withTransitionForTransform: Whether transition is used when using the transform property / withTransition : whether or not to transition.
181
+ * @param {object} transitions Return value of the generateTransitions
182
+ * @private
183
+ */
184
+ updateLegend(targetIds, options, transitions) {
185
+ const $$ = this;
186
+ const { config, state, scale, $el } = $$;
187
+ const optionz = options || {
188
+ withTransform: false,
189
+ withTransitionForTransform: false,
190
+ withTransition: false
191
+ };
192
+ optionz.withTransition = getOption(optionz, "withTransition", true);
193
+ optionz.withTransitionForTransform = getOption(optionz, "withTransitionForTransform", true);
194
+ if (config.legend_contents_bindto && config.legend_contents_template) $$.updateLegendTemplate();
195
+ else if (!state.hasTreemap) $$.updateLegendElement(targetIds || $$.mapToIds($$.data.targets), optionz);
196
+ $el.legend?.selectAll(`.${$LEGEND.legendItem}`).classed($LEGEND.legendItemHidden, function(id) {
197
+ const hide = !$$.isTargetToShow(id);
198
+ if (hide) this.style.opacity = null;
199
+ return hide;
200
+ });
201
+ $$.updateScales(false, !scale.zoom);
202
+ $$.updateSvgSize();
203
+ $$.transformAll(optionz.withTransitionForTransform, transitions);
204
+ state.legendHasRendered = true;
205
+ },
206
+ /**
207
+ * Update legend using template option
208
+ * @private
209
+ */
210
+ updateLegendTemplate() {
211
+ const $$ = this;
212
+ const { config, $el } = $$;
213
+ const wrapper = select(config.legend_contents_bindto);
214
+ const template = config.legend_contents_template;
215
+ if (!wrapper.empty()) {
216
+ const targets = $$.mapToIds($$.data.targets);
217
+ const ids = [];
218
+ let html = "";
219
+ targets.forEach((v) => {
220
+ const content = isFunction(template) ? sanitize(template.call($$.api, v, $$.color(v), $$.api.data(v)[0].values)) : tplProcess(template, {
221
+ COLOR: $$.color(v),
222
+ TITLE: v
223
+ });
224
+ if (content) {
225
+ ids.push(v);
226
+ html += content;
227
+ }
228
+ });
229
+ const legendItem = wrapper.html(html).selectAll(function() {
230
+ return this.childNodes;
231
+ }).data(ids);
232
+ $$.setLegendItem(legendItem);
233
+ $el.legend = wrapper;
234
+ }
235
+ },
236
+ /**
237
+ * Update the size of the legend.
238
+ * @param {Obejct} size Size object
239
+ * @private
240
+ */
241
+ updateSizeForLegend(size) {
242
+ const $$ = this;
243
+ const { config, state: { isLegendTop, isLegendLeft, isLegendRight, isLegendInset, current } } = $$;
244
+ const { width, height } = size;
245
+ const insetLegendPosition = {
246
+ top: isLegendTop ? $$.getCurrentPaddingByDirection("top") + config.legend_inset_y + 5.5 : current.height - height - $$.getCurrentPaddingByDirection("bottom") - config.legend_inset_y,
247
+ left: isLegendLeft ? $$.getCurrentPaddingByDirection("left") + config.legend_inset_x + .5 : current.width - width - $$.getCurrentPaddingByDirection("right") - config.legend_inset_x + .5
248
+ };
249
+ $$.state.margin3 = {
250
+ top: isLegendRight ? 0 : isLegendInset ? insetLegendPosition.top : current.height - height,
251
+ right: NaN,
252
+ bottom: 0,
253
+ left: isLegendRight ? current.width - width : isLegendInset ? insetLegendPosition.left : 0
254
+ };
255
+ },
256
+ /**
257
+ * Transform Legend
258
+ * @param {boolean} withTransition whether or not to transition.
259
+ * @private
260
+ */
261
+ transformLegend(withTransition) {
262
+ const $$ = this;
263
+ const { $el: { legend }, $T } = $$;
264
+ $T(legend, withTransition).attr("transform", $$.getTranslate("legend"));
265
+ },
266
+ /**
267
+ * Update legend item color
268
+ * @param {string} id Corresponding data ID value
269
+ * @param {string} color Color value
270
+ * @private
271
+ */
272
+ updateLegendItemColor(id, color) {
273
+ const $$ = this;
274
+ const { legend } = $$.$el;
275
+ if (legend) {
276
+ const legendItem = $$.getLegendItemById(id);
277
+ if (legendItem) select(legendItem).select("line").style("stroke", color);
278
+ }
279
+ },
280
+ /**
281
+ * Get the width of the legend
282
+ * @returns {number} width
283
+ * @private
284
+ */
285
+ getLegendWidth() {
286
+ const $$ = this;
287
+ const { current: { width }, isLegendRight, isLegendInset, legendItemWidth, legendStep } = $$.state;
288
+ return $$.config.legend_show ? isLegendRight || isLegendInset ? legendItemWidth * (legendStep + 1) : width : 0;
289
+ },
290
+ /**
291
+ * Get the height of the legend
292
+ * @returns {number} height
293
+ * @private
294
+ */
295
+ getLegendHeight() {
296
+ const $$ = this;
297
+ const { current, isLegendRight, legendItemHeight, legendStep } = $$.state;
298
+ return $$.config.legend_show ? isLegendRight ? current.height : Math.max($$.config.padding?.mode === "fit" ? 10 : 20, legendItemHeight) * (legendStep + 1) : 0;
299
+ },
300
+ /**
301
+ * Get the opacity of the legend that is unfocused
302
+ * @param {d3.selection} legendItem Legend item node
303
+ * @returns {string|null} opacity
304
+ * @private
305
+ */
306
+ opacityForUnfocusedLegend(legendItem) {
307
+ return legendItem.classed($LEGEND.legendItemHidden) ? null : "0.3";
308
+ },
309
+ /**
310
+ * Toggles the focus of the legend
311
+ * @param {Array} targetIds ID's of target
312
+ * @param {boolean} focus whether or not to focus.
313
+ * @private
314
+ */
315
+ toggleFocusLegend(targetIds, focus) {
316
+ const $$ = this;
317
+ const { $el: { legend }, $T } = $$;
318
+ const targetIdz = $$.mapToTargetIds(targetIds);
319
+ legend && $T(legend.selectAll(`.${$LEGEND.legendItem}`).filter((id) => targetIdz.indexOf(id) >= 0).classed($FOCUS.legendItemFocused, focus)).style("opacity", function() {
320
+ return focus ? null : $$.opacityForUnfocusedLegend.call($$, select(this));
321
+ });
322
+ },
323
+ /**
324
+ * Revert the legend to its default state
325
+ * @private
326
+ */
327
+ revertLegend() {
328
+ const { $el: { legend }, $T } = this;
329
+ legend && $T(legend.selectAll(`.${$LEGEND.legendItem}`).classed($FOCUS.legendItemFocused, false)).style("opacity", null);
330
+ },
331
+ /**
332
+ * Shows the legend
333
+ * @param {Array} targetIds ID's of target
334
+ * @private
335
+ */
336
+ showLegend(targetIds) {
337
+ const $$ = this;
338
+ const { config, $el, $T } = $$;
339
+ if (!config.legend_show) {
340
+ config.legend_show = true;
341
+ $el.legend ? $el.legend.style("visibility", null) : $$.initLegend();
342
+ !$$.state.legendHasRendered && $$.updateLegend();
343
+ }
344
+ $$.removeHiddenLegendIds(targetIds);
345
+ $T($el.legend.selectAll($$.selectorLegends(targetIds)).style("visibility", null)).style("opacity", null);
346
+ },
347
+ /**
348
+ * Hide the legend
349
+ * @param {Array} targetIds ID's of target
350
+ * @private
351
+ */
352
+ hideLegend(targetIds) {
353
+ const $$ = this;
354
+ const { config, $el: { legend } } = $$;
355
+ if (config.legend_show && isEmpty(targetIds)) {
356
+ config.legend_show = false;
357
+ legend?.style("visibility", "hidden");
358
+ }
359
+ $$.addHiddenLegendIds(targetIds);
360
+ legend?.selectAll($$.selectorLegends(targetIds)).style("opacity", "0").style("visibility", "hidden");
361
+ },
362
+ /**
363
+ * Get legend item textbox dimension
364
+ * @param {string} id Data ID
365
+ * @param {HTMLElement|d3.selection} textElement Text node element
366
+ * @returns {object} Bounding rect
367
+ * @private
368
+ */
369
+ getLegendItemTextBox(id, textElement) {
370
+ const $$ = this;
371
+ const { cache, state } = $$;
372
+ let data;
373
+ const cacheKey = KEY.legendItemTextBox;
374
+ if (id) {
375
+ data = !state.redrawing && cache.get(cacheKey) || {};
376
+ if (!data[id]) {
377
+ data[id] = $$.getTextRect(textElement, $LEGEND.legendItem);
378
+ cache.add(cacheKey, data);
379
+ }
380
+ data = data[id];
381
+ }
382
+ return data;
383
+ },
384
+ /**
385
+ * Set legend item style & bind events
386
+ * @param {d3.selection} item Item node
387
+ * @private
388
+ */
389
+ setLegendItem(item) {
390
+ const $$ = this;
391
+ const { $el, api, config, state } = $$;
392
+ const isTouch = state.inputType === "touch";
393
+ const hasGauge = $$.hasType("gauge");
394
+ const useCssRule = config.boost_useCssRule;
395
+ const interaction = config.legend_item_interaction;
396
+ const eventType = interaction.dblclick ? "dblclick" : "click";
397
+ const hasClickInteraction = interaction || isFunction(config.legend_item_onclick);
398
+ const touchOption = isTouch ? _getLegendTouchOption($$) : void 0;
399
+ const handleLegendToggle = function(event, id) {
400
+ if (!callFn(config.legend_item_onclick, api, id, !state.hiddenTargetIds.has(id))) {
401
+ const { altKey, type } = event;
402
+ const selected = select(this);
403
+ if (type === "dblclick" || altKey) if (state.hiddenTargetIds.size && !selected.classed($LEGEND.legendItemHidden)) api.show();
404
+ else {
405
+ api.hide();
406
+ api.show(id);
407
+ }
408
+ else {
409
+ api.toggle(id);
410
+ selected.classed($FOCUS.legendItemFocused, false);
411
+ }
412
+ }
413
+ if (isTouch) {
414
+ $$.hideTooltip();
415
+ $$.hideGridFocus?.(true);
416
+ }
417
+ };
418
+ item.attr("class", function(id) {
419
+ const node = select(this);
420
+ return (!node.empty() && node.attr("class") || "") + $$.generateClass($LEGEND.legendItem, id);
421
+ }).style("visibility", (id) => $$.isLegendToShow(id) ? null : "hidden");
422
+ if (config.interaction_enabled) {
423
+ if (useCssRule) [
424
+ [`.${$LEGEND.legendItem}`, "cursor:pointer"],
425
+ [`.${$LEGEND.legendItem} text`, "pointer-events:none"],
426
+ [`.${$LEGEND.legendItemPoint} text`, "pointer-events:none"],
427
+ [`.${$LEGEND.legendItemTile}`, "pointer-events:none"],
428
+ [`.${$LEGEND.legendItemEvent}`, "fill-opacity:0"]
429
+ ].forEach((v) => {
430
+ const [selector, props] = v;
431
+ $$.setCssRule(false, selector, [props])($el.legend);
432
+ });
433
+ item.on(eventType, hasClickInteraction ? function(event, id) {
434
+ if (isTouch && event.type === "click" && _isDuplicateLegendTouchClick($$, id)) return;
435
+ handleLegendToggle.call(this, event, id);
436
+ } : null);
437
+ isTouch && eventType === "click" && hasClickInteraction && item.on("touchstart", function(event, id) {
438
+ _setLegendTouchStart($$, id, event);
439
+ }, touchOption).on("touchmove", (event) => {
440
+ _updateLegendTouchMove($$, event);
441
+ }, touchOption).on("touchend", function(event, id) {
442
+ if (_isLegendTouchTap($$, id, event)) {
443
+ _markLegendTouchClick($$, id);
444
+ handleLegendToggle.call(this, event, id);
445
+ }
446
+ }, touchOption);
447
+ !isTouch && item.on("mouseout", interaction || isFunction(config.legend_item_onout) ? function(event, id) {
448
+ if (!callFn(config.legend_item_onout, api, id, !state.hiddenTargetIds.has(id))) {
449
+ select(this).classed($FOCUS.legendItemFocused, false);
450
+ if (hasGauge) $$.undoMarkOverlapped($$, `.${$GAUGE.gaugeValue}`);
451
+ $$.api.revert();
452
+ }
453
+ } : null).on("mouseover", interaction || isFunction(config.legend_item_onover) ? function(event, id) {
454
+ if (!callFn(config.legend_item_onover, api, id, !state.hiddenTargetIds.has(id))) {
455
+ select(this).classed($FOCUS.legendItemFocused, true);
456
+ if (hasGauge) $$.markOverlapped(id, $$, `.${$GAUGE.gaugeValue}`);
457
+ if (!state.transiting && $$.isTargetToShow(id)) api.focus(id);
458
+ }
459
+ } : null);
460
+ !item.empty() && item.on("click mouseout mouseover") && item.style("cursor", $$.getStylePropValue("pointer"));
461
+ }
462
+ _buildLegendItemMap($$, item);
463
+ },
464
+ /**
465
+ * Get legend item node by ID using cached Map for O(1) lookup
466
+ * Falls back to D3 selection if map is not available
467
+ * @param {string} id Data ID
468
+ * @returns {HTMLElement | null} Legend item node
469
+ * @private
470
+ */
471
+ getLegendItemById(id) {
472
+ const $$ = this;
473
+ const itemMap = $$.cache.get(KEY.legendItemMap);
474
+ if (itemMap && itemMap instanceof Map) return itemMap.get(id) || null;
475
+ return ($$.$el.legend?.selectAll(`.${$LEGEND.legendItem}`).filter((d) => d === id))?.node() || null;
476
+ },
477
+ /**
478
+ * Update the legend
479
+ * @param {Array} targetIds ID's of target
480
+ * @param {object} options withTransform : Whether to use the transform property / withTransitionForTransform: Whether transition is used when using the transform property / withTransition : whether or not to transition.
481
+ * @private
482
+ */
483
+ updateLegendElement(targetIds, options) {
484
+ const $$ = this;
485
+ const { config, state, $el: { legend }, $T } = $$;
486
+ const isRectangle = config.legend_item_tile_type !== "circle";
487
+ const legendItemR = config.legend_item_tile_r;
488
+ const itemTileSize = {
489
+ width: isRectangle ? config.legend_item_tile_width : legendItemR * 2,
490
+ height: isRectangle ? config.legend_item_tile_height : legendItemR * 2
491
+ };
492
+ const dimension = {
493
+ padding: {
494
+ top: 4,
495
+ right: 10
496
+ },
497
+ max: {
498
+ width: 0,
499
+ height: 0
500
+ },
501
+ posMin: 10,
502
+ step: 0,
503
+ tileWidth: itemTileSize.width + 5,
504
+ totalLength: 0
505
+ };
506
+ const sizes = {
507
+ offsets: {},
508
+ widths: {},
509
+ heights: {},
510
+ margins: [0],
511
+ steps: {}
512
+ };
513
+ let xForLegend;
514
+ let yForLegend;
515
+ let background;
516
+ const targetIdz = targetIds.filter((id) => !isDefined(config.data_names[id]) || config.data_names[id] !== null);
517
+ const withTransition = options.withTransition;
518
+ const isLegendRightOrInset = state.isLegendRight || state.isLegendInset;
519
+ const getFormattedText = _getFormattedText.bind($$);
520
+ const updatePositions = $$.getUpdateLegendPositions(targetIdz, dimension, sizes, isLegendRightOrInset);
521
+ if (state.isLegendInset) {
522
+ dimension.step = config.legend_inset_step ? config.legend_inset_step : targetIdz.length;
523
+ state.legendStep = dimension.step;
524
+ }
525
+ if (state.isLegendRight) {
526
+ xForLegend = (id) => dimension.max.width * sizes.steps[id];
527
+ yForLegend = (id) => sizes.margins[sizes.steps[id]] + sizes.offsets[id];
528
+ } else if (state.isLegendInset) {
529
+ xForLegend = (id) => dimension.max.width * sizes.steps[id] + 10;
530
+ yForLegend = (id) => sizes.margins[sizes.steps[id]] + sizes.offsets[id];
531
+ } else {
532
+ xForLegend = (id) => sizes.margins[sizes.steps[id]] + sizes.offsets[id];
533
+ yForLegend = (id) => dimension.max.height * sizes.steps[id];
534
+ }
535
+ const posFn = {
536
+ xText: (id, i) => xForLegend(id, i) + 4 + itemTileSize.width,
537
+ xRect: (id, i) => xForLegend(id, i),
538
+ x1Tile: (id, i) => xForLegend(id, i) - 2,
539
+ x2Tile: (id, i) => xForLegend(id, i) - 2 + itemTileSize.width,
540
+ yText: (id, i) => yForLegend(id, i) + 9,
541
+ yRect: (id, i) => yForLegend(id, i) - 5,
542
+ yTile: (id, i) => yForLegend(id, i) + 4
543
+ };
544
+ $$.generateLegendItem(targetIdz, itemTileSize, updatePositions, posFn, isLegendRightOrInset, getFormattedText);
545
+ background = legend.select(`.${$LEGEND.legendBackground} rect`);
546
+ if (state.isLegendInset && dimension.max.width > 0 && background.size() === 0) background = legend.insert("g", `.${$LEGEND.legendItem}`).attr("class", $LEGEND.legendBackground).append("rect");
547
+ if (config.legend_tooltip) legend.selectAll("title").data(targetIdz).text((id) => getFormattedText(id, false));
548
+ $T(legend.selectAll("text").data(targetIdz).text((id) => getFormattedText(id)).each(function(id, i) {
549
+ updatePositions(this, id, i);
550
+ }), withTransition).attr("x", posFn.xText).attr("y", posFn.yText);
551
+ $T(legend.selectAll(`rect.${$LEGEND.legendItemEvent}`).data(targetIdz), withTransition).attr("width", (id) => sizes.widths[id]).attr("height", (id) => sizes.heights[id]).attr("x", posFn.xRect).attr("y", posFn.yRect);
552
+ $$.updateLegendItemPos(targetIdz, withTransition, posFn);
553
+ if (background) $T(background, withTransition).attr("height", $$.getLegendHeight() - 12).attr("width", dimension.max.width * (dimension.step + 1) + 10);
554
+ state.legendItemWidth = dimension.max.width;
555
+ state.legendItemHeight = dimension.max.height;
556
+ state.legendStep = dimension.step;
557
+ },
558
+ /**
559
+ * Get position update function
560
+ * @param {Array} targetIdz Data ids
561
+ * @param {object} dimension Dimension object
562
+ * @param {object} sizes Size object
563
+ * @param {boolean} isLegendRightOrInset Whether legend is right or inset
564
+ * @returns {function} Update position function
565
+ * @private
566
+ */
567
+ getUpdateLegendPositions(targetIdz, dimension, sizes, isLegendRightOrInset) {
568
+ const $$ = this;
569
+ const { config, state } = $$;
570
+ return function(textElement, id, index) {
571
+ const reset = index === 0;
572
+ const isLast = index === targetIdz.length - 1;
573
+ const box = $$.getLegendItemTextBox(id, textElement);
574
+ const itemWidth = box.width + dimension.tileWidth + (isLast && !isLegendRightOrInset ? 0 : dimension.padding.right) + config.legend_padding;
575
+ const itemHeight = box.height + dimension.padding.top;
576
+ const itemLength = isLegendRightOrInset ? itemHeight : itemWidth;
577
+ const areaLength = isLegendRightOrInset ? $$.getLegendHeight() : $$.getLegendWidth();
578
+ let margin;
579
+ const updateValues = function(id2, withoutStep) {
580
+ if (!withoutStep) {
581
+ margin = (areaLength - dimension.totalLength - itemLength) / 2;
582
+ if (margin < dimension.posMin) {
583
+ margin = (areaLength - itemLength) / 2;
584
+ dimension.totalLength = 0;
585
+ dimension.step++;
586
+ }
587
+ }
588
+ sizes.steps[id2] = dimension.step;
589
+ sizes.margins[dimension.step] = state.isLegendInset ? 10 : margin;
590
+ sizes.offsets[id2] = dimension.totalLength;
591
+ dimension.totalLength += itemLength;
592
+ };
593
+ if (reset) {
594
+ dimension.totalLength = 0;
595
+ dimension.step = 0;
596
+ dimension.max.width = 0;
597
+ dimension.max.height = 0;
598
+ }
599
+ if (config.legend_show && !$$.isLegendToShow(id)) {
600
+ sizes.widths[id] = 0;
601
+ sizes.heights[id] = 0;
602
+ sizes.steps[id] = 0;
603
+ sizes.offsets[id] = 0;
604
+ return;
605
+ }
606
+ sizes.widths[id] = itemWidth;
607
+ sizes.heights[id] = itemHeight;
608
+ if (!dimension.max.width || itemWidth >= dimension.max.width) dimension.max.width = itemWidth;
609
+ if (!dimension.max.height || itemHeight >= dimension.max.height) dimension.max.height = itemHeight;
610
+ const maxLength = isLegendRightOrInset ? dimension.max.height : dimension.max.width;
611
+ if (config.legend_equally) {
612
+ for (const id2 in sizes.widths) sizes.widths[id2] = dimension.max.width;
613
+ for (const id2 in sizes.heights) sizes.heights[id2] = dimension.max.height;
614
+ margin = (areaLength - maxLength * targetIdz.length) / 2;
615
+ if (margin < dimension.posMin) {
616
+ dimension.totalLength = 0;
617
+ dimension.step = 0;
618
+ targetIdz.forEach((id2) => updateValues(id2));
619
+ } else updateValues(id, true);
620
+ } else updateValues(id);
621
+ };
622
+ },
623
+ /**
624
+ * Generate legend item elements
625
+ * @param {Array} targetIdz Data ids
626
+ * @param {object} itemTileSize Item tile size {width, height}
627
+ * @param {function} updatePositions Update position function
628
+ * @param {object} posFn Position functions
629
+ * @param {boolean} isLegendRightOrInset Whether legend is right or inset
630
+ * @param {function} getFormattedText Bound text formatter function
631
+ * @private
632
+ */
633
+ generateLegendItem(targetIdz, itemTileSize, updatePositions, posFn, isLegendRightOrInset, getFormattedText) {
634
+ const $$ = this;
635
+ const { config, state, $el: { legend } } = $$;
636
+ const usePoint = config.legend_usePoint;
637
+ const legendItemR = config.legend_item_tile_r;
638
+ const legendType = config.legend_item_tile_type;
639
+ const isRectangle = legendType !== "circle";
640
+ const pos = -200;
641
+ const l = legend.selectAll(`.${$LEGEND.legendItem}`).data(targetIdz).enter().append("g");
642
+ $$.setLegendItem(l);
643
+ if (config.legend_tooltip) l.append("title").text((id) => id);
644
+ l.append("text").text((id) => getFormattedText(id)).each(function(id, i) {
645
+ updatePositions(this, id, i);
646
+ }).style("pointer-events", $$.getStylePropValue("none")).attr("x", isLegendRightOrInset ? posFn.xText : pos).attr("y", isLegendRightOrInset ? pos : posFn.yText);
647
+ l.append("rect").attr("class", $LEGEND.legendItemEvent).style("fill-opacity", $$.getStylePropValue("0")).attr("x", isLegendRightOrInset ? posFn.xRect : pos).attr("y", isLegendRightOrInset ? pos : posFn.yRect);
648
+ if (usePoint) {
649
+ const ids = [];
650
+ const pattern = $$.getValidPointPattern();
651
+ l.append((d) => {
652
+ ids.indexOf(d) === -1 && ids.push(d);
653
+ let point = pattern[ids.indexOf(d) % pattern.length];
654
+ if (point === "rectangle") point = "rect";
655
+ return doc.createElementNS(namespaces.svg, "hasValidPointType" in $$ && $$.hasValidPointType(point) ? point : "use");
656
+ }).attr("class", $LEGEND.legendItemPoint).style("fill", _getLegendColor.bind($$)).style("pointer-events", $$.getStylePropValue("none")).attr("href", (data, idx, selection) => {
657
+ const nodeName = selection[idx].nodeName.toLowerCase();
658
+ const id = $$.getTargetSelectorSuffix(data);
659
+ return nodeName === "use" ? `#${state.datetimeId}-point${id}` : void 0;
660
+ });
661
+ } else l.append(isRectangle ? "line" : legendType).attr("class", $LEGEND.legendItemTile).style("stroke", _getLegendColor.bind($$)).style("pointer-events", $$.getStylePropValue("none")).call((selection) => {
662
+ if (legendType === "circle") selection.attr("r", legendItemR).style("fill", _getLegendColor.bind($$)).attr("cx", isLegendRightOrInset ? posFn.x2Tile : pos).attr("cy", isLegendRightOrInset ? pos : posFn.yTile);
663
+ else if (isRectangle) selection.attr("stroke-width", itemTileSize.height).attr("x1", isLegendRightOrInset ? posFn.x1Tile : pos).attr("y1", isLegendRightOrInset ? pos : posFn.yTile).attr("x2", isLegendRightOrInset ? posFn.x2Tile : pos).attr("y2", isLegendRightOrInset ? pos : posFn.yTile);
664
+ });
665
+ },
666
+ /**
667
+ * Update legend item position
668
+ * @param {Array} targetIdz Data ids
669
+ * @param {boolean} withTransition Whether or not to apply transition
670
+ * @param {object} posFn Position functions
671
+ * @private
672
+ */
673
+ updateLegendItemPos(targetIdz, withTransition, posFn) {
674
+ const $$ = this;
675
+ const { config, $el: { legend }, $T } = $$;
676
+ const usePoint = config.legend_usePoint;
677
+ const legendType = config.legend_item_tile_type;
678
+ const isRectangle = legendType !== "circle";
679
+ if (usePoint) {
680
+ const tiles = legend.selectAll(`.${$LEGEND.legendItemPoint}`).data(targetIdz);
681
+ const isRectangleTile = config.legend_item_tile_type !== "circle";
682
+ const tileWidth = isRectangleTile ? config.legend_item_tile_width : config.legend_item_tile_r * 2;
683
+ const tileHeight = isRectangleTile ? config.legend_item_tile_height : config.legend_item_tile_r * 2;
684
+ const iconWidth = tileWidth * .75;
685
+ const iconHeight = tileHeight * .75;
686
+ const customScaleX = tileWidth / 8;
687
+ const customScaleY = tileHeight / 8;
688
+ $T(tiles, withTransition).each(function() {
689
+ const nodeName = this.nodeName.toLowerCase();
690
+ let x = "x";
691
+ let y = "y";
692
+ let radius = null;
693
+ let width = null;
694
+ let height = null;
695
+ if (nodeName === "circle") {
696
+ x = "cx";
697
+ y = "cy";
698
+ radius = Math.min(iconWidth, iconHeight) / 2;
699
+ } else if (nodeName === "rect") {
700
+ width = iconWidth;
701
+ height = iconHeight;
702
+ }
703
+ const tile = select(this).attr("transform", null).attr("x", null).attr("y", null).attr("cx", null).attr("cy", null).attr("r", radius).attr("width", width).attr("height", height);
704
+ if (nodeName === "use") tile.attr("transform", (d) => `translate(${posFn.x1Tile(d)} ${posFn.yTile(d) - tileHeight / 2}) scale(${customScaleX} ${customScaleY})`);
705
+ else tile.attr(x, (d) => posFn.x1Tile(d) + (tileWidth - (width || 0)) / 2).attr(y, (d) => posFn.yTile(d) - (height || 0) / 2);
706
+ });
707
+ } else $T(legend.selectAll(`.${$LEGEND.legendItemTile}`).data(targetIdz), withTransition).style("stroke", _getLegendColor.bind($$)).call((selection) => {
708
+ if (legendType === "circle") selection.attr("cx", (d) => {
709
+ const x2 = posFn.x2Tile(d);
710
+ return x2 - (x2 - posFn.x1Tile(d)) / 2;
711
+ }).attr("cy", posFn.yTile);
712
+ else if (isRectangle) selection.attr("x1", posFn.x1Tile).attr("y1", posFn.yTile).attr("x2", posFn.x2Tile).attr("y2", posFn.yTile);
713
+ });
714
+ }
967
715
  };
968
-
969
- export { legend as default };
716
+ //#endregion
717
+ export { legend_default as default };