@antv/g2 5.4.6 → 5.4.7

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.
@@ -34,11 +34,92 @@ function getFunnelPoints(
34
34
  return [p0, newP1, newP2, p3];
35
35
  }
36
36
 
37
+ /**
38
+ * Calculate the offset
39
+ */
40
+ const getOffset = (
41
+ angle: number,
42
+ radius: number,
43
+ sign: 1 | -1,
44
+ ): { x: number; y: number } => {
45
+ const offsetX = Math.cos(angle) * radius;
46
+ const offsetY = Math.sin(angle) * radius;
47
+ return { x: offsetX * sign, y: offsetY };
48
+ };
49
+ /**
50
+ * Calculate the radius of the long side
51
+ */
52
+ const getRadiusOfLongerBase = (angle: number, radius: number): number => {
53
+ return radius / Math.tan(angle / 2);
54
+ };
55
+
56
+ /**
57
+ * Create a rounded rectangle path with given corner radius.
58
+ */
59
+ function createRoundedPath(
60
+ points: Vector2[],
61
+ radius: [number, number, number, number],
62
+ ): string {
63
+ const [p0, p1, p2, p3] = points;
64
+ const [r0, r1, r2, r3] = radius;
65
+ const isLongBaseOnTop = p1[0] - p0[0] > p2[0] - p3[0];
66
+
67
+ const angle = Math.atan2(Math.abs(p1[1] - p2[1]), Math.abs(p1[0] - p2[0]));
68
+
69
+ // The radius on the long side requires additional calculation
70
+ const rs = isLongBaseOnTop
71
+ ? [
72
+ getRadiusOfLongerBase(angle, r0),
73
+ getRadiusOfLongerBase(angle, r1),
74
+ r2,
75
+ r3,
76
+ ]
77
+ : [
78
+ r0,
79
+ r1,
80
+ getRadiusOfLongerBase(angle, r2),
81
+ getRadiusOfLongerBase(angle, r3),
82
+ ];
83
+
84
+ // Determine the direction of the X-axis offset based on the lengths of the upper and lower sides
85
+ const sign = isLongBaseOnTop ? 1 : -1;
86
+
87
+ const ofs = rs.map((r) => getOffset(angle, r, sign));
88
+
89
+ return [
90
+ `M${p0[0] + rs[0]} ${p0[1]}`,
91
+ `L${p1[0] - rs[1]} ${p1[1]}`,
92
+ `Q${p1[0]} ${p1[1]} ${p1[0] - ofs[1].x} ${p1[1] + ofs[1].y}`,
93
+ `L${p2[0] + ofs[2].x} ${p2[1] - ofs[2].y}`,
94
+ `Q${p2[0]} ${p2[1]} ${p2[0] - rs[2]} ${p2[1]}`,
95
+ `L${p3[0] + rs[3]} ${p3[1]}`,
96
+ `Q${p3[0]} ${p3[1]} ${p3[0] - ofs[3].x} ${p3[1] - ofs[3].y}`,
97
+ `L${p0[0] + ofs[0].x} ${p0[1] + ofs[0].y}`,
98
+ `Q${p0[0]} ${p0[1]} ${p0[0] + rs[0]} ${p0[1]}`,
99
+ 'Z',
100
+ ].join(' ');
101
+ }
102
+
37
103
  /**
38
104
  * Render funnel in different coordinate and using color channel for stroke and fill attribute.
39
105
  */
40
106
  export const Funnel: SC<FunnelOptions> = (options, context) => {
41
- const { adjustPoints = getFunnelPoints, ...style } = options;
107
+ const {
108
+ adjustPoints = getFunnelPoints,
109
+ radius,
110
+ radiusTopLeft = radius,
111
+ radiusTopRight = radius,
112
+ radiusBottomRight = radius,
113
+ radiusBottomLeft = radius,
114
+ innerRadius = 0,
115
+ innerRadiusTopLeft = innerRadius,
116
+ innerRadiusTopRight = innerRadius,
117
+ innerRadiusBottomRight = innerRadius,
118
+ innerRadiusBottomLeft = innerRadius,
119
+ first = true,
120
+ last = true,
121
+ ...style
122
+ } = options;
42
123
  const { coordinate, document } = context;
43
124
  return (points, value, defaults, point2d) => {
44
125
  const { index } = value;
@@ -48,10 +129,22 @@ export const Funnel: SC<FunnelOptions> = (options, context) => {
48
129
  const tpShape = !!isTranspose(coordinate);
49
130
  const [p0, p1, p2, p3] = tpShape ? reorder(funnelPoints) : funnelPoints;
50
131
  const { color = defaultColor, opacity } = value;
51
- const b = line().curve(curveLinearClosed)([p0, p1, p2, p3]);
132
+ const standardDirRadius: [number, number, number, number] = [
133
+ first ? radiusTopLeft ?? innerRadiusTopLeft : innerRadiusTopLeft,
134
+ first ? radiusTopRight ?? innerRadiusTopRight : innerRadiusTopRight,
135
+ last
136
+ ? radiusBottomRight ?? innerRadiusBottomRight
137
+ : innerRadiusBottomRight,
138
+ last ? radiusBottomLeft ?? innerRadiusBottomLeft : innerRadiusBottomLeft,
139
+ ];
140
+
141
+ const path = standardDirRadius.find((r) => r > 0)
142
+ ? createRoundedPath([p0, p1, p2, p3], standardDirRadius)
143
+ : line().curve(curveLinearClosed)([p0, p1, p2, p3]);
144
+
52
145
  return select(document.createElement('path', {}))
53
146
  .call(applyStyle, rest)
54
- .style('d', b)
147
+ .style('d', path)
55
148
  .style('fill', color)
56
149
  .style('fillOpacity', opacity)
57
150
  .call(applyStyle, style)
@@ -65,6 +65,9 @@ function filterWhenNoElements(mark: G2Mark) {
65
65
  export const Filter: TC<FilterOptions> = (options = {}) => {
66
66
  return (I, mark) => {
67
67
  const { encode, data } = mark;
68
+
69
+ const hasChannelOptions = Object.keys(options).length > 0;
70
+
68
71
  const filters = Object.entries(options)
69
72
  .map(([key, v]) => {
70
73
  const [V] = columnOf(encode, key);
@@ -88,9 +91,10 @@ export const Filter: TC<FilterOptions> = (options = {}) => {
88
91
  const FI = I.filter(totalFilter);
89
92
  const newIndex = FI.map((_, i) => i);
90
93
 
91
- if (filters.length === 0) {
94
+ if (filters.length === 0 && hasChannelOptions) {
92
95
  const targetMark = filterWhenNoElements(mark);
93
- return [I, targetMark];
96
+ // Return empty index array to ensure no elements are rendered
97
+ return [[], targetMark];
94
98
  }
95
99
 
96
100
  const newEncodes = Object.entries(encode).map(([key, encode]) => {
@@ -14,7 +14,7 @@ export type SymmetryYOptions = Omit<SymmetryYTransform, 'type'>;
14
14
  export const SymmetryY: TC<SymmetryYOptions> = (options = {}) => {
15
15
  const { groupBy = 'x' } = options;
16
16
  return (I, mark) => {
17
- const { encode } = mark;
17
+ const { encode, style = {} } = mark;
18
18
  const { x, ...rest } = encode;
19
19
 
20
20
  // Extract and create new channels starts with y, such as y, y1.
@@ -22,7 +22,6 @@ export const SymmetryY: TC<SymmetryYOptions> = (options = {}) => {
22
22
  .filter(([k]) => k.startsWith('y'))
23
23
  .map(([k]) => [k, columnOf(encode, k)[0]] as const);
24
24
  const newYn = Yn.map(([k]) => [k, new Array(I.length)] as const);
25
-
26
25
  // Group marks into series by specified keys.
27
26
  const groups = createGroups(groupBy, I, mark);
28
27
  const MY = new Array(groups.length);
@@ -52,6 +51,11 @@ export const SymmetryY: TC<SymmetryYOptions> = (options = {}) => {
52
51
  encode: Object.fromEntries(
53
52
  newYn.map(([k, v]) => [k, column(v, columnOf(encode, k)[1])]),
54
53
  ),
54
+ style: {
55
+ first: (_, i) => i === 0,
56
+ last: (_, i) => i === groups.length - 1,
57
+ ...style,
58
+ },
55
59
  }),
56
60
  ];
57
61
  };