billboard.js 3.16.0 → 3.17.0

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 (66) hide show
  1. package/dist/billboard.css +4 -4
  2. package/dist/billboard.js +645 -82
  3. package/dist/billboard.min.css +1 -1
  4. package/dist/billboard.min.js +12 -12
  5. package/dist/billboard.pkgd.js +1987 -786
  6. package/dist/billboard.pkgd.min.js +18 -17
  7. package/dist/plugin/billboardjs-plugin-bubblecompare.js +2 -2
  8. package/dist/plugin/billboardjs-plugin-bubblecompare.min.js +2 -2
  9. package/dist/plugin/billboardjs-plugin-sparkline.js +18 -2
  10. package/dist/plugin/billboardjs-plugin-sparkline.min.js +4 -4
  11. package/dist/plugin/billboardjs-plugin-stanford.js +18 -2
  12. package/dist/plugin/billboardjs-plugin-stanford.min.js +4 -4
  13. package/dist/plugin/billboardjs-plugin-tableview.js +16 -2
  14. package/dist/plugin/billboardjs-plugin-tableview.min.js +5 -5
  15. package/dist/plugin/billboardjs-plugin-textoverlap.js +16 -2
  16. package/dist/plugin/billboardjs-plugin-textoverlap.min.js +3 -3
  17. package/dist/plugin/pkgd/billboardjs-plugin-bubblecompare.pkgd.js +1344 -706
  18. package/dist/plugin/pkgd/billboardjs-plugin-bubblecompare.pkgd.min.js +6 -5
  19. package/dist/plugin/pkgd/billboardjs-plugin-sparkline.pkgd.js +1454 -800
  20. package/dist/plugin/pkgd/billboardjs-plugin-sparkline.pkgd.min.js +8 -7
  21. package/dist/plugin/pkgd/billboardjs-plugin-stanford.pkgd.js +1455 -801
  22. package/dist/plugin/pkgd/billboardjs-plugin-stanford.pkgd.min.js +13 -12
  23. package/dist/plugin/pkgd/billboardjs-plugin-tableview.pkgd.js +1454 -802
  24. package/dist/plugin/pkgd/billboardjs-plugin-tableview.pkgd.min.js +10 -9
  25. package/dist/plugin/pkgd/billboardjs-plugin-textoverlap.pkgd.js +1455 -803
  26. package/dist/plugin/pkgd/billboardjs-plugin-textoverlap.pkgd.min.js +7 -6
  27. package/dist/theme/dark.css +4 -4
  28. package/dist/theme/dark.min.css +1 -1
  29. package/dist/theme/datalab.css +4 -4
  30. package/dist/theme/datalab.min.css +1 -1
  31. package/dist/theme/graph.css +4 -4
  32. package/dist/theme/graph.min.css +1 -1
  33. package/dist/theme/insight.css +4 -4
  34. package/dist/theme/insight.min.css +1 -1
  35. package/dist/theme/modern.css +4 -4
  36. package/dist/theme/modern.min.css +1 -1
  37. package/dist-esm/billboard.js +697 -79
  38. package/dist-esm/plugin/billboardjs-plugin-bubblecompare.js +2 -2
  39. package/dist-esm/plugin/billboardjs-plugin-sparkline.js +4 -2
  40. package/dist-esm/plugin/billboardjs-plugin-stanford.js +4 -2
  41. package/dist-esm/plugin/billboardjs-plugin-tableview.js +2 -2
  42. package/dist-esm/plugin/billboardjs-plugin-textoverlap.js +2 -2
  43. package/package.json +21 -21
  44. package/src/ChartInternal/Axis/AxisRenderer.ts +20 -13
  45. package/src/ChartInternal/Axis/AxisRendererHelper.ts +9 -4
  46. package/src/ChartInternal/data/convert.ts +3 -3
  47. package/src/ChartInternal/data/data.ts +2 -2
  48. package/src/ChartInternal/internals/color.ts +20 -14
  49. package/src/ChartInternal/internals/text.ts +40 -125
  50. package/src/ChartInternal/internals/text.util.ts +320 -0
  51. package/src/ChartInternal/shape/arc.ts +41 -19
  52. package/src/ChartInternal/shape/treemap.ts +5 -6
  53. package/src/config/Options/axis/x.ts +18 -0
  54. package/src/config/Options/axis/y.ts +17 -0
  55. package/src/config/Options/axis/y2.ts +18 -0
  56. package/src/config/Options/common/boost.ts +1 -0
  57. package/src/config/Options/data/data.ts +125 -4
  58. package/src/config/Options/shape/donut.ts +77 -1
  59. package/src/config/Options/shape/gauge.ts +69 -2
  60. package/src/config/Options/shape/pie.ts +68 -1
  61. package/src/config/Options/shape/polar.ts +68 -1
  62. package/src/config/classes.ts +2 -0
  63. package/src/module/util.ts +26 -0
  64. package/types/axis.d.ts +10 -0
  65. package/types/options.d.ts +138 -1
  66. package/types/options.shape.d.ts +302 -2
@@ -18,114 +18,22 @@ import {
18
18
  setTextValue
19
19
  } from "../../module/util";
20
20
  import type {IArcData, IDataRow} from "../data/IData";
21
-
22
- type Coord = {x: number, y: number};
23
- type Anchor = "start" | "middle" | "end";
24
-
25
- /**
26
- * Get text-anchor according text.labels.rotate angle
27
- * @param {number} angle Angle value
28
- * @returns {string} Anchor string value
29
- * @private
30
- */
31
- function getRotateAnchor(angle: number): Anchor {
32
- let anchor: Anchor = "middle";
33
-
34
- if (angle > 0 && angle <= 170) {
35
- anchor = "end";
36
- } else if (angle > 190 && angle <= 360) {
37
- anchor = "start";
38
- }
39
-
40
- return anchor;
41
- }
42
-
43
- /**
44
- * Set rotated position coordinate according text.labels.rotate angle
45
- * @param {object} d Data object
46
- * @param {object} pos Position object
47
- * @param {object} pos.x x coordinate
48
- * @param {object} pos.y y coordinate
49
- * @param {string} anchor string value
50
- * @param {boolean} isRotated If axis is rotated
51
- * @param {boolean} isInverted If axis is inverted
52
- * @returns {object} x, y coordinate
53
- * @private
54
- */
55
- function setRotatePos(
56
- d: IDataRow,
57
- pos: Coord,
58
- anchor: Anchor,
59
- isRotated: boolean,
60
- isInverted: boolean
61
- ): Coord {
62
- const $$ = this;
63
- const {value} = d;
64
- const isCandlestickType = $$.isCandlestickType(d);
65
- const isNegative = (isNumber(value) && value < 0) || (
66
- isCandlestickType && !$$.getCandlestickData(d)?._isUp
67
- );
68
-
69
- let {x, y} = pos;
70
- const gap = 4;
71
- const doubleGap = gap * 2;
72
-
73
- if (isRotated) {
74
- if (anchor === "start") {
75
- x += isNegative ? 0 : doubleGap;
76
- y += gap;
77
- } else if (anchor === "middle") {
78
- x += doubleGap;
79
- y -= doubleGap;
80
- } else if (anchor === "end") {
81
- isNegative && (x -= doubleGap);
82
- y += gap;
83
- }
84
- } else {
85
- if (anchor === "start") {
86
- x += gap;
87
- isNegative && (y += doubleGap * 2);
88
- } else if (anchor === "middle") {
89
- y -= doubleGap;
90
- } else if (anchor === "end") {
91
- x -= gap;
92
- isNegative && (y += doubleGap * 2);
93
- }
94
-
95
- if (isInverted) {
96
- y += isNegative ? -17 : (isCandlestickType ? 13 : 7);
97
- }
98
- }
99
-
100
- return {x, y};
101
- }
102
-
103
- /**
104
- * Get data.labels.position value
105
- * @param {object} d Data object
106
- * @param {string} type x | y
107
- * @returns {number} Position value
108
- * @private
109
- */
110
- function getTextPos(d, type): number {
111
- const position = this.config.data_labels_position;
112
- const {id, index, value} = d;
113
-
114
- return (
115
- isFunction(position) ?
116
- position.bind(this.api)(type, value, id, index, this.$el.text) :
117
- (id in position ? position[id] : position)[type]
118
- ) ?? 0;
119
- }
21
+ import {
22
+ getRotateAnchor,
23
+ getTextPos,
24
+ meetsLabelThreshold,
25
+ setRotatePos,
26
+ updateTextBorder,
27
+ updateTextImage,
28
+ updateTextImagePos
29
+ } from "./text.util";
120
30
 
121
31
  export default {
122
32
  opacityForText(d): null | "0" {
123
33
  const $$ = this;
124
34
 
125
- return $$.isBarType(d) && !$$.meetsLabelThreshold(
126
- Math.abs($$.getRatio("bar", d)),
127
- "bar"
128
- ) ?
35
+ return $$.isBarType(d) &&
36
+ !meetsLabelThreshold.call($$, Math.abs($$.getRatio("bar", d)), "bar") ?
129
37
  "0" :
130
38
  ($$.hasDataLabel ? null : "0");
131
39
  },
@@ -234,6 +142,9 @@ export default {
234
142
  setTextValue(node, value);
235
143
  }
236
144
  });
145
+
146
+ // Add images if imgUrl is specified
147
+ updateTextImage.call($$);
237
148
  },
238
149
 
239
150
  updateTextColor(d): null | object | string {
@@ -272,24 +183,38 @@ export default {
272
183
  /**
273
184
  * Update data label text background color
274
185
  * @param {object} d Data object
275
- * @param {object|string} option option object
186
+ * @param {object|string|Function} option option object
276
187
  * @returns {string|null}
277
188
  * @private
278
189
  */
279
190
  updateTextBGColor(d: IDataRow | IArcData, option): string | null {
280
191
  const $$ = this;
281
- const {$el} = $$;
192
+ const {$el: {defs}} = $$;
282
193
  let color: string = "";
283
194
 
284
- if (isString(option) || isObject(option)) {
195
+ if (option) {
285
196
  const id = isString(option) ?
286
197
  "" :
287
198
  $$.getTargetSelectorSuffix("id" in d ? d.id : d.data.id);
288
- const filter = $el.defs.select(["filter[id*='labels-bg", "']"].join(id));
199
+ const filter = defs.select(["filter[id*='labels-bg", "']"].join(id));
289
200
 
290
201
  if (filter.size()) {
291
202
  color = `url(#${filter.attr("id")})`;
292
203
  }
204
+
205
+ if (isFunction(option)) {
206
+ $$.generateTextBGColorFilter(option);
207
+
208
+ // Get default color and call function
209
+ const defaultColor = $$.color(d);
210
+ const bgColor = option.bind($$.api)(defaultColor, d);
211
+
212
+ if (bgColor) {
213
+ filter.select("feFlood").attr("flood-color", bgColor);
214
+ } else {
215
+ color = "";
216
+ }
217
+ }
293
218
  }
294
219
 
295
220
  return color || null;
@@ -321,7 +246,8 @@ export default {
321
246
  .each(function(d: IDataRow, i: number) {
322
247
  // do not apply transition for newly added text elements
323
248
  const node = $T(hasTreemap && this.childElementCount ? this.parentNode : this,
324
- !!(withTransition && this.getAttribute("x")), t);
249
+ !!(withTransition &&
250
+ (this.getAttribute("x") || this.getAttribute("transform"))), t);
325
251
  const isInverted = config[`axis_${axis?.getId(d.id)}_inverted`];
326
252
  let pos = {
327
253
  x: getX.bind(this)(d, i),
@@ -333,12 +259,17 @@ export default {
333
259
  node.attr("text-anchor", anchorString);
334
260
  }
335
261
 
262
+ updateTextImagePos.call($$, this, pos);
263
+
336
264
  // when is multiline
337
265
  if (this.childElementCount || angle) {
338
266
  node.attr("transform", `translate(${pos.x} ${pos.y}) ${rotateString}`);
339
267
  } else {
340
268
  node.attr("x", pos.x).attr("y", pos.y);
341
269
  }
270
+
271
+ config.data_labels.border &&
272
+ updateTextBorder.call($$, node.node(), pos, `${$TEXT.textBorderRect}-${i}`);
342
273
  });
343
274
 
344
275
  // need to return 'true' as of being pushed to the redraw list
@@ -438,7 +369,7 @@ export default {
438
369
  * @returns {number} Position value
439
370
  * @private
440
371
  */
441
- getCenteredTextPos(d, points, textElement, type: "x" | "y"): number {
372
+ getCenteredTextPos(d, points, textElement: SVGTextElement, type: "x" | "y"): number {
442
373
  const $$ = this;
443
374
  const {config} = $$;
444
375
  const isRotated = config.axis_rotated;
@@ -658,21 +589,5 @@ export default {
658
589
  d3SelectAll([this, this.previousSibling])
659
590
  .classed($TEXT.TextOverlapping, false);
660
591
  });
661
- },
662
-
663
- /**
664
- * Check if meets the ratio to show data label text
665
- * @param {number} ratio ratio to meet
666
- * @param {string} type chart type
667
- * @returns {boolean}
668
- * @private
669
- */
670
- meetsLabelThreshold(ratio: number = 0,
671
- type: "bar" | "donut" | "gauge" | "pie" | "polar" | "treemap"): boolean {
672
- const $$ = this;
673
- const {config} = $$;
674
- const threshold = config[`${type}_label_threshold`] || 0;
675
-
676
- return ratio >= threshold;
677
592
  }
678
593
  };
@@ -0,0 +1,320 @@
1
+ /**
2
+ * Copyright (c) 2017 ~ present NAVER Corp.
3
+ * billboard.js project is licensed under the MIT license
4
+ */
5
+ import {select as d3Select} from "d3-selection";
6
+ import {$TEXT} from "../../config/classes";
7
+ import {
8
+ getBBox,
9
+ getBoundingRect,
10
+ isFunction,
11
+ isNumber,
12
+ parseShorthand,
13
+ tplProcess
14
+ } from "../../module/util";
15
+ import type {IArcData, IData, IDataRow} from "../data/IData";
16
+
17
+ type TCoord = {x: number, y: number};
18
+ type TAnchor = "start" | "middle" | "end";
19
+ type TImage = {url: string, width: number, height: number, pos: {x: number, y: number}} | null;
20
+ type TLabelThresholdType = "bar" | "donut" | "gauge" | "pie" | "polar" | "treemap";
21
+
22
+ /**
23
+ * Get text-anchor according text.labels.rotate angle
24
+ * @param {number} angle Angle value
25
+ * @returns {string} Anchor string value
26
+ * @private
27
+ */
28
+ function getRotateAnchor(angle: number): TAnchor {
29
+ let anchor: TAnchor = "middle";
30
+
31
+ if (angle > 0 && angle <= 170) {
32
+ anchor = "end";
33
+ } else if (angle > 190 && angle <= 360) {
34
+ anchor = "start";
35
+ }
36
+
37
+ return anchor;
38
+ }
39
+
40
+ /**
41
+ * Set rotated position coordinate according text.labels.rotate angle
42
+ * @param {object} d Data object
43
+ * @param {object} pos Position object
44
+ * @param {object} pos.x x coordinate
45
+ * @param {object} pos.y y coordinate
46
+ * @param {string} anchor string value
47
+ * @param {boolean} isRotated If axis is rotated
48
+ * @param {boolean} isInverted If axis is inverted
49
+ * @returns {object} x, y coordinate
50
+ * @private
51
+ */
52
+ function setRotatePos(
53
+ d: IDataRow,
54
+ pos: TCoord,
55
+ anchor: TAnchor,
56
+ isRotated: boolean,
57
+ isInverted: boolean
58
+ ): TCoord {
59
+ const $$ = this;
60
+ const {value} = d;
61
+ const isCandlestickType = $$.isCandlestickType(d);
62
+ const isNegative = (isNumber(value) && value < 0) || (
63
+ isCandlestickType && !$$.getCandlestickData(d)?._isUp
64
+ );
65
+
66
+ let {x, y} = pos;
67
+ const gap = 4;
68
+ const doubleGap = gap * 2;
69
+
70
+ if (isRotated) {
71
+ if (anchor === "start") {
72
+ x += isNegative ? 0 : doubleGap;
73
+ y += gap;
74
+ } else if (anchor === "middle") {
75
+ x += doubleGap;
76
+ y -= doubleGap;
77
+ } else if (anchor === "end") {
78
+ isNegative && (x -= doubleGap);
79
+ y += gap;
80
+ }
81
+ } else {
82
+ if (anchor === "start") {
83
+ x += gap;
84
+ isNegative && (y += doubleGap * 2);
85
+ } else if (anchor === "middle") {
86
+ y -= doubleGap;
87
+ } else if (anchor === "end") {
88
+ x -= gap;
89
+ isNegative && (y += doubleGap * 2);
90
+ }
91
+
92
+ if (isInverted) {
93
+ y += isNegative ? -17 : (isCandlestickType ? 13 : 7);
94
+ }
95
+ }
96
+
97
+ return {x, y};
98
+ }
99
+
100
+ /**
101
+ * Get data.labels.position value
102
+ * @param {object} d Data object
103
+ * @param {string} type x | y
104
+ * @returns {number} Position value
105
+ * @private
106
+ */
107
+ function getTextPos(d: IDataRow, type: string): number {
108
+ const position = this.config.data_labels_position;
109
+ const {id, index, value} = d;
110
+
111
+ return (
112
+ isFunction(position) ?
113
+ position.bind(this.api)(type, value, id, index, this.$el.text) :
114
+ (id in position ? position[id] : position)[type]
115
+ ) ?? 0;
116
+ }
117
+
118
+ /**
119
+ * Update text border
120
+ * @param {SVGTextElement} text Text element
121
+ * @param {Coord} pos Position object
122
+ * @param {string} rectClass Class name
123
+ * @private
124
+ */
125
+ function updateTextBorder(text: SVGTextElement, pos: TCoord, rectClass: string): void {
126
+ const $$ = this;
127
+ const {config, $T} = $$;
128
+ const isRotated = config.axis_rotated;
129
+ const {
130
+ border: {
131
+ padding = "3 5",
132
+ radius = 10,
133
+ stroke = "#000",
134
+ strokeWidth = 1,
135
+ fill = "none"
136
+ }
137
+ } = config.data_labels;
138
+
139
+ const borderPadding = parseShorthand(padding);
140
+ const applyStyle = config.data_labels.border !== true;
141
+ const textRect = getBBox(text);
142
+ let borderRect = d3Select(text.previousElementSibling) as any;
143
+
144
+ if (
145
+ borderRect.empty() ||
146
+ borderRect.node()?.tagName !== "rect" ||
147
+ !borderRect.attr("class")?.includes(rectClass)
148
+ ) {
149
+ borderRect = d3Select(text.parentNode as Element)
150
+ .insert("rect", () => text)
151
+ .attr("class", `${$TEXT.textBorderRect} ${rectClass}`)
152
+ .attr("width",
153
+ textRect.width + (applyStyle ? borderPadding.left + borderPadding.right : 0))
154
+ .attr("height",
155
+ textRect.height + (applyStyle ? borderPadding.top + borderPadding.bottom : 0));
156
+
157
+ if (applyStyle) {
158
+ borderRect
159
+ .style("fill", fill)
160
+ .style("stroke", stroke)
161
+ .style("stroke-width", `${strokeWidth}px`)
162
+ .attr("rx", radius)
163
+ .attr("ry", radius);
164
+ }
165
+ }
166
+
167
+ $T(borderRect)
168
+ .attr("x",
169
+ pos.x - (applyStyle ? borderPadding.left : 0) - (isRotated ? 0 : textRect.width / 2))
170
+ .attr("y", pos.y - (applyStyle ? borderPadding.top : 0) - (textRect.height / 4 * 3.2));
171
+ }
172
+
173
+ /**
174
+ * Check if meets the ratio to show data label text
175
+ * @param {number} ratio ratio to meet
176
+ * @param {string} type chart type
177
+ * @returns {boolean}
178
+ * @private
179
+ */
180
+ function meetsLabelThreshold(ratio: number = 0, type: TLabelThresholdType): boolean {
181
+ const $$ = this;
182
+ const {config} = $$;
183
+ const threshold = config[`${type}_label_threshold`] || 0;
184
+
185
+ return ratio >= threshold;
186
+ }
187
+
188
+ /**
189
+ * Update text image
190
+ * @private
191
+ */
192
+ function updateTextImage(): void {
193
+ const $$ = this;
194
+ const {$el: {text}, config} = $$;
195
+ const isArc = $$.state.arcWidth;
196
+
197
+ if (isArc ? $$.getArcLabelConfig("image") : config.data_labels.image) {
198
+ text.filter(function() {
199
+ const prev = this.previousElementSibling;
200
+
201
+ if (prev) {
202
+ return prev.tagName !== "image" || !prev.classList.contains($TEXT.textLabelImage);
203
+ }
204
+
205
+ return true;
206
+ }).each(function(d) {
207
+ const {url, width, height, pos} = getDataLabelImgUrl.call($$, d);
208
+
209
+ if (url) {
210
+ const parentNode = d3Select(this.parentNode);
211
+
212
+ // Insert image before text
213
+ parentNode?.insert("image",
214
+ `${this.getAttribute("class")?.replace(/(?:^(.)|\s)/g, ".$1") ?? "text"}`)
215
+ .style("opacity", "0")
216
+ .attr("href", (d: IData | IArcData) =>
217
+ tplProcess(url, {
218
+ ID: ("id" in d) ? d.id : d.data.id
219
+ }))
220
+ .attr("class", $TEXT.textLabelImage)
221
+ .style("pointer-events", "none")
222
+ .attr("width", width)
223
+ .attr("height", height)
224
+ .attr("transform", pos ? `translate(${pos.x ?? 0} ${pos.y ?? 0})` : null);
225
+ }
226
+ });
227
+ }
228
+ }
229
+
230
+ /**
231
+ * Get image URL for data label
232
+ * @param {object} d Data object
233
+ * @returns {string|null} Image URL
234
+ * @private
235
+ */
236
+ function getDataLabelImgUrl(
237
+ d: IDataRow
238
+ ): TImage | null {
239
+ const $$ = this;
240
+ const {config, state} = $$;
241
+ const image = state.arcWidth ? $$.getArcLabelConfig("image") : config.data_labels.image;
242
+
243
+ if (isFunction(image)) {
244
+ return image.call($$.api, d.value, d.id, d.index) ?? {
245
+ url: "",
246
+ width: 0,
247
+ height: 0,
248
+ pos: {x: 0, y: 0}
249
+ };
250
+ } else if (image) {
251
+ const {url = "", width = 0, height = 0, pos} = image;
252
+
253
+ return {
254
+ url,
255
+ width,
256
+ height,
257
+ pos
258
+ };
259
+ }
260
+
261
+ return null;
262
+ }
263
+
264
+ /**
265
+ * Update text image position
266
+ * @param {SVGTextElement} textNode Text element
267
+ * @param {object} pos Position object
268
+ * @param {number} pos.x X coordinate
269
+ * @param {number} pos.y Y coordinate
270
+ * @private
271
+ */
272
+ function updateTextImagePos(textNode: SVGTextElement, pos: {x: number, y: number}): void {
273
+ const $$ = this;
274
+ const {config, state: {arcWidth, hasTreemap}} = $$;
275
+ const isRotated = config.axis_rotated;
276
+ const image = d3Select(textNode.previousElementSibling);
277
+ const isShown = textNode => {
278
+ const isShown = textNode.style.opacity !== "0" && textNode.style.fillOpacity !== "0";
279
+
280
+ return (arcWidth ? textNode.textContent : isShown) &&
281
+ this.previousElementSibling?.tagName !== "image";
282
+ };
283
+
284
+ if (!image.empty() && image.node()?.tagName === "image") {
285
+ const textRect = getBoundingRect(textNode);
286
+ const w = +image.attr("width") / 2;
287
+ const h = +image.attr("height") / 2;
288
+ let x = pos.x - w;
289
+ let y = pos.y - h - textRect.height / 2;
290
+
291
+ if (isRotated) {
292
+ pos.x += w;
293
+ } else {
294
+ if (hasTreemap) {
295
+ x = -w;
296
+ y = -(h * 2 + textRect.height);
297
+ }
298
+
299
+ // exclude pie & polar type
300
+ if (!($$.hasType("pie") || $$.hasType("polar"))) {
301
+ pos.y += h;
302
+ }
303
+ }
304
+
305
+ $$.$T(image)
306
+ .style("opacity", isShown(textNode) ? null : "0")
307
+ .attr("x", x)
308
+ .attr("y", y);
309
+ }
310
+ }
311
+
312
+ export {
313
+ getRotateAnchor,
314
+ getTextPos,
315
+ meetsLabelThreshold,
316
+ setRotatePos,
317
+ updateTextBorder,
318
+ updateTextImage,
319
+ updateTextImagePos
320
+ };
@@ -20,6 +20,7 @@ import {
20
20
  tplProcess
21
21
  } from "../../module/util";
22
22
  import type {IArcData, IArcDataRow, IData} from "../data/IData";
23
+ import {meetsLabelThreshold, updateTextImage, updateTextImagePos} from "../internals/text.util";
23
24
 
24
25
  /**
25
26
  * Get radius functions
@@ -446,7 +447,9 @@ export default {
446
447
  .merge(rangeText));
447
448
 
448
449
  if ((!state.rendered || (state.rendered && !fixed)) && totalSum > 0) {
449
- rangeText.attr("transform", (d, i) => $$.transformForArcLabel(pieData[i], true));
450
+ rangeText.attr("transform", function(d, i) {
451
+ return $$.transformForArcLabel(this, pieData[i], true);
452
+ });
450
453
  }
451
454
 
452
455
  rangeText.style("opacity",
@@ -456,36 +459,40 @@ export default {
456
459
 
457
460
  /**
458
461
  * Set transform attributes to arc label text
462
+ * @param {SVGTextElement} textNode Text node element
459
463
  * @param {object} d Data object
460
464
  * @param {boolean} forRange Weather is for ranged text option(arc.rangeText.values)
461
465
  * @returns {string} Translate attribute string
462
466
  * @private
463
467
  */
464
- transformForArcLabel(d: IArcData, forRange = false): string {
468
+ transformForArcLabel(textNode: SVGTextElement, d: IArcData, forRange = false): string {
465
469
  const $$ = this;
466
470
  const {config, state: {radiusExpanded}} = $$;
467
471
  const updated = $$.updateAngle(d, forRange);
472
+ const pos = {x: 0, y: 0};
468
473
  let translate = "";
474
+ let ratio = 1;
469
475
 
470
476
  if (updated) {
471
477
  if (forRange || $$.hasMultiArcGauge()) {
472
478
  const y1 = Math.sin(updated.endAngle - Math.PI / 2);
473
479
  const rangeTextPosition = config.arc_rangeText_position;
474
- let x = Math.cos(updated.endAngle - Math.PI / 2) *
480
+
481
+ pos.x = Math.cos(updated.endAngle - Math.PI / 2) *
475
482
  (radiusExpanded + (forRange ? 5 : 25));
476
- let y = y1 * (radiusExpanded + 15 - Math.abs(y1 * 10)) + 3;
483
+ pos.y = y1 * (radiusExpanded + 15 - Math.abs(y1 * 10)) + 3;
477
484
 
478
485
  if (forRange && rangeTextPosition) {
479
486
  const rangeValues = config.arc_rangeText_values;
480
- const pos = isFunction(rangeTextPosition) ?
487
+ const position = isFunction(rangeTextPosition) ?
481
488
  rangeTextPosition(rangeValues[d.index]) :
482
489
  rangeTextPosition;
483
490
 
484
- x += pos?.x ?? 0;
485
- y += pos?.y ?? 0;
491
+ pos.x += position?.x ?? 0;
492
+ pos.y += position?.y ?? 0;
486
493
  }
487
494
 
488
- translate = `translate(${x},${y})`;
495
+ // translate = `translate(${x},${y})`;
489
496
  } else if (!$$.hasType("gauge") || $$.data.targets.length > 1) {
490
497
  let {outerRadius} = $$.getRadius(d);
491
498
 
@@ -497,7 +504,10 @@ export default {
497
504
  const [x, y] = c.map(v => (isNaN(v) ? 0 : v));
498
505
  const h = Math.sqrt(x * x + y * y);
499
506
 
500
- let ratio = ["donut", "gauge", "pie", "polar"]
507
+ pos.x = x;
508
+ pos.y = y;
509
+
510
+ ratio = ["donut", "gauge", "pie", "polar"]
501
511
  .filter($$.hasType.bind($$))
502
512
  .map(v => config[`${v}_label_ratio`])?.[0];
503
513
 
@@ -511,9 +521,10 @@ export default {
511
521
  0
512
522
  );
513
523
  }
514
-
515
- translate = `translate(${x * ratio},${y * ratio})`;
516
524
  }
525
+
526
+ updateTextImagePos.call($$, textNode, pos);
527
+ translate = `translate(${pos.x * ratio},${pos.y * ratio})`;
517
528
  }
518
529
 
519
530
  return translate;
@@ -541,13 +552,13 @@ export default {
541
552
  const node = d3Select(this);
542
553
  const updated = $$.updateAngle(d);
543
554
  const ratio = $$.getRatio("arc", updated);
544
- const isUnderThreshold = $$.meetsLabelThreshold(ratio,
555
+ const isUnderThreshold = meetsLabelThreshold.call($$, ratio,
545
556
  ["donut", "gauge", "pie", "polar"].filter($$.hasType.bind($$))?.[0]);
546
557
 
547
558
  if (isUnderThreshold) {
548
559
  const {value} = updated || d;
549
560
  const text = (
550
- $$.getArcLabelFormat() || $$.defaultArcValueFormat
561
+ $$.getArcLabelConfig("format") || $$.defaultArcValueFormat
551
562
  )(value, ratio, d.data.id).toString();
552
563
 
553
564
  setTextValue(node, text, [-1, 1], hasGauge);
@@ -664,18 +675,22 @@ export default {
664
675
  .some(v => $$.hasType(v) && config[`${v}_label_show`]);
665
676
  },
666
677
 
667
- getArcLabelFormat(): number | string {
678
+ getArcLabelConfig(name = "format"): number | string | Function | object {
668
679
  const $$ = this;
669
680
  const {config} = $$;
670
- let format = v => v;
681
+ let fn = v => v;
671
682
 
672
683
  ["donut", "gauge", "pie", "polar"]
673
684
  .filter($$.hasType.bind($$))
674
685
  .forEach(v => {
675
- format = config[`${v}_label_format`];
686
+ fn = config[`${v}_label_${name}`];
676
687
  });
677
688
 
678
- return isFunction(format) ? format.bind($$.api) : format;
689
+ if (name === "format") {
690
+ return isFunction(fn) ? fn.bind($$.api) : fn;
691
+ } else {
692
+ return fn;
693
+ }
679
694
  },
680
695
 
681
696
  updateTargetsForArc(targets: IData): void {
@@ -901,6 +916,7 @@ export default {
901
916
  $$.hasType("gauge") && $$.redrawBackgroundArcs();
902
917
 
903
918
  config.arc_needle_show && $$.redrawNeedle();
919
+
904
920
  $$.redrawArcText(duration);
905
921
  $$.redrawArcRangeText();
906
922
  },
@@ -1187,12 +1203,18 @@ export default {
1187
1203
  .style("opacity", "0")
1188
1204
  .attr("class", d => ($$.isGaugeType(d.data) ? $GAUGE.gaugeValue : null))
1189
1205
  .call($$.textForArcLabel.bind($$))
1190
- .attr("transform", d => $$.transformForArcLabel.bind($$)(d))
1191
1206
  .style("font-size", d => (
1192
1207
  $$.isGaugeType(d.data) && $$.data.targets.length === 1 && !hasMultiArcGauge ?
1193
1208
  `${Math.round(state.radius / 5)}px` :
1194
1209
  null
1195
- ))
1210
+ ));
1211
+
1212
+ updateTextImage.call($$);
1213
+
1214
+ text
1215
+ .attr("transform", function(d) {
1216
+ return $$.transformForArcLabel.bind($$)(this, d);
1217
+ })
1196
1218
  .transition()
1197
1219
  .duration(duration)
1198
1220
  .style("opacity",