@antv/g2 5.3.4-beta.2 → 5.3.4-beta.3
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.
- package/dist/g2.lite.min.js +1 -1
- package/dist/g2.min.js +1 -1
- package/esm/label-transform/contrastReverse.js +67 -4
- package/esm/label-transform/contrastReverse.js.map +1 -1
- package/esm/spec/labelTransform.d.ts +6 -0
- package/lib/label-transform/contrastReverse.js +67 -4
- package/lib/label-transform/contrastReverse.js.map +1 -1
- package/lib/spec/labelTransform.d.ts +6 -0
- package/package.json +1 -1
- package/src/label-transform/contrastReverse.ts +89 -3
- package/src/spec/labelTransform.ts +4 -0
|
@@ -1,5 +1,8 @@
|
|
|
1
1
|
import { maxIndex } from '@antv/vendor/d3-array';
|
|
2
|
+
import { isUndefined } from '@antv/util';
|
|
2
3
|
import { parseToRGB } from '../utils/color';
|
|
4
|
+
import { isOverflow, parseAABB } from '../utils/bounds';
|
|
5
|
+
import { bboxOf } from '../interaction/utils';
|
|
3
6
|
function getsRGB(s) {
|
|
4
7
|
let c = s / 255;
|
|
5
8
|
c = c <= 0.03928 ? c / 12.92 : Math.pow((c + 0.055) / 1.055, 2.4);
|
|
@@ -29,20 +32,80 @@ function mostContrast(color, palette) {
|
|
|
29
32
|
const i = maxIndex(palette, (c) => contrast(color, parseToRGB(c)));
|
|
30
33
|
return palette[i];
|
|
31
34
|
}
|
|
35
|
+
/**
|
|
36
|
+
* Get bounds of element considering animation state.
|
|
37
|
+
* If element has animations, get the final state bounds.
|
|
38
|
+
*/
|
|
39
|
+
function getBoundsWithAnimation(element) {
|
|
40
|
+
var _a;
|
|
41
|
+
const animations = element.getAnimations();
|
|
42
|
+
// If no animations, use regular bboxOf.
|
|
43
|
+
if (!animations || animations.length === 0) {
|
|
44
|
+
return bboxOf(element);
|
|
45
|
+
}
|
|
46
|
+
// Clone element and apply final animation state.
|
|
47
|
+
const cloneElement = element.cloneNode(true);
|
|
48
|
+
cloneElement.style.visibility = 'hidden';
|
|
49
|
+
animations.forEach((animation) => {
|
|
50
|
+
const keyframes = animation.effect.getKeyframes();
|
|
51
|
+
if (keyframes && keyframes.length > 0) {
|
|
52
|
+
cloneElement.attr(keyframes[keyframes.length - 1]);
|
|
53
|
+
}
|
|
54
|
+
});
|
|
55
|
+
(_a = element.parentNode) === null || _a === void 0 ? void 0 : _a.appendChild(cloneElement);
|
|
56
|
+
const bounds = bboxOf(cloneElement);
|
|
57
|
+
cloneElement.destroy();
|
|
58
|
+
return bounds;
|
|
59
|
+
}
|
|
32
60
|
/**
|
|
33
61
|
* Reverse the label color when the contrast is lower then `threshold`.
|
|
34
62
|
* The default value of `threshold` is 4.5.
|
|
35
63
|
* More about contract, see https://webaim.org/resources/contrastchecker/
|
|
36
64
|
*/
|
|
37
65
|
export const ContrastReverse = (options) => {
|
|
38
|
-
const { threshold = 4.5, palette = ['#000', '#fff'] } = options;
|
|
66
|
+
const { autoStroke = false, threshold = 4.5, palette = ['#000', '#fff'], } = options;
|
|
39
67
|
return (labels) => {
|
|
40
68
|
labels.forEach((l) => {
|
|
41
|
-
|
|
69
|
+
var _a;
|
|
70
|
+
const dependentElement = l.attr('dependentElement');
|
|
71
|
+
const background = dependentElement.parsedStyle.fill;
|
|
42
72
|
const foreground = l.parsedStyle.fill;
|
|
43
73
|
const c = contrast(foreground, background);
|
|
44
|
-
|
|
45
|
-
|
|
74
|
+
// Apply contrast reverse if needed.
|
|
75
|
+
if (c < threshold) {
|
|
76
|
+
const reversedColor = mostContrast(background, palette);
|
|
77
|
+
l.attr('fill', reversedColor);
|
|
78
|
+
// Check if text bbox overflows the dependent element.
|
|
79
|
+
if (autoStroke) {
|
|
80
|
+
// Save original stroke properties only if not already saved.
|
|
81
|
+
if (!l.attr('_originalStyleSaved')) {
|
|
82
|
+
l.attr('_originalStroke', l.attr('stroke'));
|
|
83
|
+
l.attr('_originalLineWidth', l.attr('lineWidth'));
|
|
84
|
+
l.attr('_originalStyleSaved', true);
|
|
85
|
+
}
|
|
86
|
+
const textBounds = parseAABB(getBoundsWithAnimation(l));
|
|
87
|
+
const elementBounds = parseAABB(getBoundsWithAnimation(dependentElement));
|
|
88
|
+
if (isOverflow(textBounds, elementBounds)) {
|
|
89
|
+
// Add stroke to make overflowing text more visible.
|
|
90
|
+
// Use the opposite color from palette for stroke.
|
|
91
|
+
const strokeColor = mostContrast(parseToRGB(reversedColor), palette);
|
|
92
|
+
l.attr('stroke', strokeColor);
|
|
93
|
+
l.attr('lineWidth', (_a = autoStroke === null || autoStroke === void 0 ? void 0 : autoStroke.lineWidth) !== null && _a !== void 0 ? _a : 1.6);
|
|
94
|
+
}
|
|
95
|
+
else {
|
|
96
|
+
// Restore original stroke properties if not overflowing.
|
|
97
|
+
l.attr({
|
|
98
|
+
stroke: l.attr('_originalStroke'),
|
|
99
|
+
lineWidth: l.attr('_originalLineWidth'),
|
|
100
|
+
});
|
|
101
|
+
// Undefined can't set to attrs, have to remove.
|
|
102
|
+
if (isUndefined(l.attr('_originalStroke')))
|
|
103
|
+
l.removeAttribute('stroke');
|
|
104
|
+
if (isUndefined(l.attr('_originalLineWidth')))
|
|
105
|
+
l.removeAttribute('lineWidth');
|
|
106
|
+
}
|
|
107
|
+
}
|
|
108
|
+
}
|
|
46
109
|
});
|
|
47
110
|
return labels;
|
|
48
111
|
};
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"contrastReverse.js","sourceRoot":"","sources":["../../src/label-transform/contrastReverse.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,QAAQ,EAAE,MAAM,uBAAuB,CAAC;
|
|
1
|
+
{"version":3,"file":"contrastReverse.js","sourceRoot":"","sources":["../../src/label-transform/contrastReverse.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,QAAQ,EAAE,MAAM,uBAAuB,CAAC;AACjD,OAAO,EAAE,WAAW,EAAE,MAAM,YAAY,CAAC;AAGzC,OAAO,EAAE,UAAU,EAAE,MAAM,gBAAgB,CAAC;AAC5C,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,CAAC,UAAU,EAAE,aAAa,CAAC,EAAE;wBACzC,oDAAoD;wBACpD,kDAAkD;wBAClD,MAAM,WAAW,GAAG,YAAY,CAC9B,UAAU,CAAC,aAAa,CAAC,EACzB,OAAO,CACR,CAAC;wBAEF,CAAC,CAAC,IAAI,CAAC,QAAQ,EAAE,WAAW,CAAC,CAAC;wBAC9B,CAAC,CAAC,IAAI,CACJ,WAAW,EACX,MAAC,UAAoC,aAApC,UAAU,uBAAV,UAAU,CAA4B,SAAS,mCAAI,GAAG,CACxD,CAAC;qBACH;yBAAM;wBACL,yDAAyD;wBACzD,CAAC,CAAC,IAAI,CAAC;4BACL,MAAM,EAAE,CAAC,CAAC,IAAI,CAAC,iBAAiB,CAAC;4BACjC,SAAS,EAAE,CAAC,CAAC,IAAI,CAAC,oBAAoB,CAAC;yBACxC,CAAC,CAAC;wBAEH,gDAAgD;wBAChD,IAAI,WAAW,CAAC,CAAC,CAAC,IAAI,CAAC,iBAAiB,CAAC,CAAC;4BACxC,CAAC,CAAC,eAAe,CAAC,QAAQ,CAAC,CAAC;wBAC9B,IAAI,WAAW,CAAC,CAAC,CAAC,IAAI,CAAC,oBAAoB,CAAC,CAAC;4BAC3C,CAAC,CAAC,eAAe,CAAC,WAAW,CAAC,CAAC;qBAClC;iBACF;aACF;QACH,CAAC,CAAC,CAAC;QACH,OAAO,MAAM,CAAC;IAChB,CAAC,CAAC;AACJ,CAAC,CAAC"}
|
|
@@ -24,6 +24,12 @@ export type ContrastReverseLabelTransform = {
|
|
|
24
24
|
* The optional color palette, default is [#000, #fff].
|
|
25
25
|
*/
|
|
26
26
|
palette?: string[];
|
|
27
|
+
/**
|
|
28
|
+
* Auto add stroke to make overflowing text more visible.
|
|
29
|
+
*/
|
|
30
|
+
autoStroke?: boolean | {
|
|
31
|
+
lineWidth: number;
|
|
32
|
+
};
|
|
27
33
|
};
|
|
28
34
|
export type OverflowHideLabelTransform = {
|
|
29
35
|
type: 'overflowHide';
|
|
@@ -2,7 +2,10 @@
|
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.ContrastReverse = void 0;
|
|
4
4
|
const d3_array_1 = require("@antv/vendor/d3-array");
|
|
5
|
+
const util_1 = require("@antv/util");
|
|
5
6
|
const color_1 = require("../utils/color");
|
|
7
|
+
const bounds_1 = require("../utils/bounds");
|
|
8
|
+
const utils_1 = require("../interaction/utils");
|
|
6
9
|
function getsRGB(s) {
|
|
7
10
|
let c = s / 255;
|
|
8
11
|
c = c <= 0.03928 ? c / 12.92 : Math.pow((c + 0.055) / 1.055, 2.4);
|
|
@@ -32,20 +35,80 @@ function mostContrast(color, palette) {
|
|
|
32
35
|
const i = (0, d3_array_1.maxIndex)(palette, (c) => contrast(color, (0, color_1.parseToRGB)(c)));
|
|
33
36
|
return palette[i];
|
|
34
37
|
}
|
|
38
|
+
/**
|
|
39
|
+
* Get bounds of element considering animation state.
|
|
40
|
+
* If element has animations, get the final state bounds.
|
|
41
|
+
*/
|
|
42
|
+
function getBoundsWithAnimation(element) {
|
|
43
|
+
var _a;
|
|
44
|
+
const animations = element.getAnimations();
|
|
45
|
+
// If no animations, use regular bboxOf.
|
|
46
|
+
if (!animations || animations.length === 0) {
|
|
47
|
+
return (0, utils_1.bboxOf)(element);
|
|
48
|
+
}
|
|
49
|
+
// Clone element and apply final animation state.
|
|
50
|
+
const cloneElement = element.cloneNode(true);
|
|
51
|
+
cloneElement.style.visibility = 'hidden';
|
|
52
|
+
animations.forEach((animation) => {
|
|
53
|
+
const keyframes = animation.effect.getKeyframes();
|
|
54
|
+
if (keyframes && keyframes.length > 0) {
|
|
55
|
+
cloneElement.attr(keyframes[keyframes.length - 1]);
|
|
56
|
+
}
|
|
57
|
+
});
|
|
58
|
+
(_a = element.parentNode) === null || _a === void 0 ? void 0 : _a.appendChild(cloneElement);
|
|
59
|
+
const bounds = (0, utils_1.bboxOf)(cloneElement);
|
|
60
|
+
cloneElement.destroy();
|
|
61
|
+
return bounds;
|
|
62
|
+
}
|
|
35
63
|
/**
|
|
36
64
|
* Reverse the label color when the contrast is lower then `threshold`.
|
|
37
65
|
* The default value of `threshold` is 4.5.
|
|
38
66
|
* More about contract, see https://webaim.org/resources/contrastchecker/
|
|
39
67
|
*/
|
|
40
68
|
const ContrastReverse = (options) => {
|
|
41
|
-
const { threshold = 4.5, palette = ['#000', '#fff'] } = options;
|
|
69
|
+
const { autoStroke = false, threshold = 4.5, palette = ['#000', '#fff'], } = options;
|
|
42
70
|
return (labels) => {
|
|
43
71
|
labels.forEach((l) => {
|
|
44
|
-
|
|
72
|
+
var _a;
|
|
73
|
+
const dependentElement = l.attr('dependentElement');
|
|
74
|
+
const background = dependentElement.parsedStyle.fill;
|
|
45
75
|
const foreground = l.parsedStyle.fill;
|
|
46
76
|
const c = contrast(foreground, background);
|
|
47
|
-
|
|
48
|
-
|
|
77
|
+
// Apply contrast reverse if needed.
|
|
78
|
+
if (c < threshold) {
|
|
79
|
+
const reversedColor = mostContrast(background, palette);
|
|
80
|
+
l.attr('fill', reversedColor);
|
|
81
|
+
// Check if text bbox overflows the dependent element.
|
|
82
|
+
if (autoStroke) {
|
|
83
|
+
// Save original stroke properties only if not already saved.
|
|
84
|
+
if (!l.attr('_originalStyleSaved')) {
|
|
85
|
+
l.attr('_originalStroke', l.attr('stroke'));
|
|
86
|
+
l.attr('_originalLineWidth', l.attr('lineWidth'));
|
|
87
|
+
l.attr('_originalStyleSaved', true);
|
|
88
|
+
}
|
|
89
|
+
const textBounds = (0, bounds_1.parseAABB)(getBoundsWithAnimation(l));
|
|
90
|
+
const elementBounds = (0, bounds_1.parseAABB)(getBoundsWithAnimation(dependentElement));
|
|
91
|
+
if ((0, bounds_1.isOverflow)(textBounds, elementBounds)) {
|
|
92
|
+
// Add stroke to make overflowing text more visible.
|
|
93
|
+
// Use the opposite color from palette for stroke.
|
|
94
|
+
const strokeColor = mostContrast((0, color_1.parseToRGB)(reversedColor), palette);
|
|
95
|
+
l.attr('stroke', strokeColor);
|
|
96
|
+
l.attr('lineWidth', (_a = autoStroke === null || autoStroke === void 0 ? void 0 : autoStroke.lineWidth) !== null && _a !== void 0 ? _a : 1.6);
|
|
97
|
+
}
|
|
98
|
+
else {
|
|
99
|
+
// Restore original stroke properties if not overflowing.
|
|
100
|
+
l.attr({
|
|
101
|
+
stroke: l.attr('_originalStroke'),
|
|
102
|
+
lineWidth: l.attr('_originalLineWidth'),
|
|
103
|
+
});
|
|
104
|
+
// Undefined can't set to attrs, have to remove.
|
|
105
|
+
if ((0, util_1.isUndefined)(l.attr('_originalStroke')))
|
|
106
|
+
l.removeAttribute('stroke');
|
|
107
|
+
if ((0, util_1.isUndefined)(l.attr('_originalLineWidth')))
|
|
108
|
+
l.removeAttribute('lineWidth');
|
|
109
|
+
}
|
|
110
|
+
}
|
|
111
|
+
}
|
|
49
112
|
});
|
|
50
113
|
return labels;
|
|
51
114
|
};
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"contrastReverse.js","sourceRoot":"","sources":["../../src/label-transform/contrastReverse.ts"],"names":[],"mappings":";;;AACA,oDAAiD;
|
|
1
|
+
{"version":3,"file":"contrastReverse.js","sourceRoot":"","sources":["../../src/label-transform/contrastReverse.ts"],"names":[],"mappings":";;;AACA,oDAAiD;AACjD,qCAAyC;AAGzC,0CAA4C;AAC5C,4CAAwD;AACxD,gDAA8C;AAO9C,SAAS,OAAO,CAAC,CAAS;IACxB,IAAI,CAAC,GAAG,CAAC,GAAG,GAAG,CAAC;IAChB,CAAC,GAAG,CAAC,IAAI,OAAO,CAAC,CAAC,CAAC,CAAC,GAAG,KAAK,CAAC,CAAC,CAAC,IAAI,CAAC,GAAG,CAAC,CAAC,CAAC,GAAG,KAAK,CAAC,GAAG,KAAK,EAAE,GAAG,CAAC,CAAC;IAClE,OAAO,CAAC,CAAC;AACX,CAAC;AAED,SAAS,IAAI,CAAC,CAAS,EAAE,CAAS,EAAE,CAAS;IAC3C,OAAO,MAAM,GAAG,OAAO,CAAC,CAAC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BACjC,SAAS,EAAE,CAAC,CAAC,IAAI,CAAC,oBAAoB,CAAC;yBACxC,CAAC,CAAC;wBAEH,gDAAgD;wBAChD,IAAI,IAAA,kBAAW,EAAC,CAAC,CAAC,IAAI,CAAC,iBAAiB,CAAC,CAAC;4BACxC,CAAC,CAAC,eAAe,CAAC,QAAQ,CAAC,CAAC;wBAC9B,IAAI,IAAA,kBAAW,EAAC,CAAC,CAAC,IAAI,CAAC,oBAAoB,CAAC,CAAC;4BAC3C,CAAC,CAAC,eAAe,CAAC,WAAW,CAAC,CAAC;qBAClC;iBACF;aACF;QACH,CAAC,CAAC,CAAC;QACH,OAAO,MAAM,CAAC;IAChB,CAAC,CAAC;AACJ,CAAC,CAAC;AAhEW,QAAA,eAAe,mBAgE1B"}
|
|
@@ -24,6 +24,12 @@ export type ContrastReverseLabelTransform = {
|
|
|
24
24
|
* The optional color palette, default is [#000, #fff].
|
|
25
25
|
*/
|
|
26
26
|
palette?: string[];
|
|
27
|
+
/**
|
|
28
|
+
* Auto add stroke to make overflowing text more visible.
|
|
29
|
+
*/
|
|
30
|
+
autoStroke?: boolean | {
|
|
31
|
+
lineWidth: number;
|
|
32
|
+
};
|
|
27
33
|
};
|
|
28
34
|
export type OverflowHideLabelTransform = {
|
|
29
35
|
type: 'overflowHide';
|
package/package.json
CHANGED
|
@@ -1,8 +1,11 @@
|
|
|
1
1
|
import { DisplayObject } from '@antv/g';
|
|
2
2
|
import { maxIndex } from '@antv/vendor/d3-array';
|
|
3
|
+
import { isUndefined } from '@antv/util';
|
|
3
4
|
import { ContrastReverseLabelTransform } from '../spec';
|
|
4
5
|
import { LabelTransformComponent as LLC } from '../runtime';
|
|
5
6
|
import { parseToRGB } from '../utils/color';
|
|
7
|
+
import { isOverflow, parseAABB } from '../utils/bounds';
|
|
8
|
+
import { bboxOf } from '../interaction/utils';
|
|
6
9
|
|
|
7
10
|
export type ContrastReverseOptions = Omit<
|
|
8
11
|
ContrastReverseLabelTransform,
|
|
@@ -41,19 +44,102 @@ function mostContrast(color, palette: string[]): string {
|
|
|
41
44
|
return palette[i];
|
|
42
45
|
}
|
|
43
46
|
|
|
47
|
+
/**
|
|
48
|
+
* Get bounds of element considering animation state.
|
|
49
|
+
* If element has animations, get the final state bounds.
|
|
50
|
+
*/
|
|
51
|
+
function getBoundsWithAnimation(element: DisplayObject) {
|
|
52
|
+
const animations = element.getAnimations();
|
|
53
|
+
|
|
54
|
+
// If no animations, use regular bboxOf.
|
|
55
|
+
if (!animations || animations.length === 0) {
|
|
56
|
+
return bboxOf(element);
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
// Clone element and apply final animation state.
|
|
60
|
+
const cloneElement = element.cloneNode(true) as DisplayObject;
|
|
61
|
+
cloneElement.style.visibility = 'hidden';
|
|
62
|
+
|
|
63
|
+
animations.forEach((animation) => {
|
|
64
|
+
const keyframes = animation.effect.getKeyframes();
|
|
65
|
+
if (keyframes && keyframes.length > 0) {
|
|
66
|
+
cloneElement.attr(keyframes[keyframes.length - 1]);
|
|
67
|
+
}
|
|
68
|
+
});
|
|
69
|
+
|
|
70
|
+
element.parentNode?.appendChild(cloneElement);
|
|
71
|
+
const bounds = bboxOf(cloneElement);
|
|
72
|
+
cloneElement.destroy();
|
|
73
|
+
|
|
74
|
+
return bounds;
|
|
75
|
+
}
|
|
76
|
+
|
|
44
77
|
/**
|
|
45
78
|
* Reverse the label color when the contrast is lower then `threshold`.
|
|
46
79
|
* The default value of `threshold` is 4.5.
|
|
47
80
|
* More about contract, see https://webaim.org/resources/contrastchecker/
|
|
48
81
|
*/
|
|
49
82
|
export const ContrastReverse: LLC<ContrastReverseOptions> = (options) => {
|
|
50
|
-
const {
|
|
83
|
+
const {
|
|
84
|
+
autoStroke = false,
|
|
85
|
+
threshold = 4.5,
|
|
86
|
+
palette = ['#000', '#fff'],
|
|
87
|
+
} = options;
|
|
88
|
+
|
|
51
89
|
return (labels: DisplayObject[]) => {
|
|
52
90
|
labels.forEach((l) => {
|
|
53
|
-
const
|
|
91
|
+
const dependentElement = l.attr('dependentElement');
|
|
92
|
+
const background = dependentElement.parsedStyle.fill;
|
|
54
93
|
const foreground = l.parsedStyle.fill;
|
|
55
94
|
const c = contrast(foreground, background);
|
|
56
|
-
|
|
95
|
+
|
|
96
|
+
// Apply contrast reverse if needed.
|
|
97
|
+
if (c < threshold) {
|
|
98
|
+
const reversedColor = mostContrast(background, palette);
|
|
99
|
+
l.attr('fill', reversedColor);
|
|
100
|
+
|
|
101
|
+
// Check if text bbox overflows the dependent element.
|
|
102
|
+
if (autoStroke) {
|
|
103
|
+
// Save original stroke properties only if not already saved.
|
|
104
|
+
if (!l.attr('_originalStyleSaved')) {
|
|
105
|
+
l.attr('_originalStroke', l.attr('stroke'));
|
|
106
|
+
l.attr('_originalLineWidth', l.attr('lineWidth'));
|
|
107
|
+
l.attr('_originalStyleSaved', true);
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
const textBounds = parseAABB(getBoundsWithAnimation(l));
|
|
111
|
+
const elementBounds = parseAABB(
|
|
112
|
+
getBoundsWithAnimation(dependentElement),
|
|
113
|
+
);
|
|
114
|
+
|
|
115
|
+
if (isOverflow(textBounds, elementBounds)) {
|
|
116
|
+
// Add stroke to make overflowing text more visible.
|
|
117
|
+
// Use the opposite color from palette for stroke.
|
|
118
|
+
const strokeColor = mostContrast(
|
|
119
|
+
parseToRGB(reversedColor),
|
|
120
|
+
palette,
|
|
121
|
+
);
|
|
122
|
+
|
|
123
|
+
l.attr('stroke', strokeColor);
|
|
124
|
+
l.attr(
|
|
125
|
+
'lineWidth',
|
|
126
|
+
(autoStroke as { lineWidth: number })?.lineWidth ?? 1.6,
|
|
127
|
+
);
|
|
128
|
+
} else {
|
|
129
|
+
// Restore original stroke properties if not overflowing.
|
|
130
|
+
l.attr({
|
|
131
|
+
stroke: l.attr('_originalStroke'),
|
|
132
|
+
lineWidth: l.attr('_originalLineWidth'),
|
|
133
|
+
});
|
|
134
|
+
|
|
135
|
+
// Undefined can't set to attrs, have to remove.
|
|
136
|
+
if (isUndefined(l.attr('_originalStroke')))
|
|
137
|
+
l.removeAttribute('stroke');
|
|
138
|
+
if (isUndefined(l.attr('_originalLineWidth')))
|
|
139
|
+
l.removeAttribute('lineWidth');
|
|
140
|
+
}
|
|
141
|
+
}
|
|
142
|
+
}
|
|
57
143
|
});
|
|
58
144
|
return labels;
|
|
59
145
|
};
|
|
@@ -32,6 +32,10 @@ export type ContrastReverseLabelTransform = {
|
|
|
32
32
|
* The optional color palette, default is [#000, #fff].
|
|
33
33
|
*/
|
|
34
34
|
palette?: string[];
|
|
35
|
+
/**
|
|
36
|
+
* Auto add stroke to make overflowing text more visible.
|
|
37
|
+
*/
|
|
38
|
+
autoStroke?: boolean | { lineWidth: number };
|
|
35
39
|
};
|
|
36
40
|
|
|
37
41
|
export type OverflowHideLabelTransform = {
|