@beppla/tapas-ui 1.4.33 → 1.4.35

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.
@@ -0,0 +1,242 @@
1
+ # 依赖警告处理指南
2
+
3
+ ## 常见警告及解决方案
4
+
5
+ ### 1. `Module not found: Can't resolve 'd3-hierarchy'`
6
+
7
+ **原因:** WordCloud 的气泡模式需要 `d3-hierarchy`,但该依赖被标记为可选,不会自动安装。
8
+
9
+ **解决方案:**
10
+
11
+ #### 方案 1:安装依赖(推荐)
12
+
13
+ 如果你需要使用气泡词云功能:
14
+
15
+ ```bash
16
+ npm install d3-hierarchy
17
+ # 或
18
+ yarn add d3-hierarchy
19
+ ```
20
+
21
+ #### 方案 2:忽略警告
22
+
23
+ 如果你只使用传统词云,可以忽略此警告,或在 webpack 配置中屏蔽:
24
+
25
+ ```js
26
+ // webpack.config.js
27
+ module.exports = {
28
+ plugins: [
29
+ new webpack.IgnorePlugin({
30
+ resourceRegExp: /^d3-hierarchy$/,
31
+ contextRegExp: /@beppla\/tapas-ui/,
32
+ }),
33
+ ],
34
+ };
35
+ ```
36
+
37
+ #### 方案 3:条件导入
38
+
39
+ 在代码中使用时动态检测:
40
+
41
+ ```tsx
42
+ import { WordCloud } from '@beppla/tapas-ui';
43
+
44
+ // 只使用传统模式,不会触发 d3-hierarchy 加载
45
+ <WordCloud
46
+ variant="traditional" // 默认值
47
+ words={data}
48
+ />
49
+
50
+ // 如果使用气泡模式,确保已安装 d3-hierarchy
51
+ <WordCloud
52
+ variant="bubble" // 需要 d3-hierarchy
53
+ words={data}
54
+ />
55
+ ```
56
+
57
+ ---
58
+
59
+ ### 2. `Module not found: NestedPieChart` 路径错误
60
+
61
+ **错误示例:**
62
+
63
+ ```tsx
64
+ // ❌ 错误:不要使用相对路径访问 node_modules 内部文件
65
+ const m = require("../../../../../node_modules/@beppla/tapas-ui/commonjs/PieChart/NestedPieChart");
66
+ ```
67
+
68
+ **原因:** 使用了错误的导入路径。
69
+
70
+ **正确导入方式:**
71
+
72
+ ```tsx
73
+ // ✅ 正确:从包的根路径导入
74
+ import { NestedPieChart } from '@beppla/tapas-ui';
75
+
76
+ // ✅ 或者从子路径导入(如果需要)
77
+ import { NestedPieChart } from '@beppla/tapas-ui/PieChart';
78
+ ```
79
+
80
+ **如果需要动态导入:**
81
+
82
+ ```tsx
83
+ // ✅ 正确的动态导入
84
+ let NestedPieChart;
85
+ try {
86
+ if (Platform.OS === 'web') {
87
+ // 从包的正确导出路径导入
88
+ const module = await import('@beppla/tapas-ui');
89
+ NestedPieChart = module.NestedPieChart;
90
+ }
91
+ } catch (error) {
92
+ console.warn('NestedPieChart not available');
93
+ }
94
+ ```
95
+
96
+ ---
97
+
98
+ ## 完整依赖列表
99
+
100
+ ### WordCloud 组件
101
+
102
+ | 依赖 | 用途 | 平台 | 是否必需 |
103
+ |------|------|------|----------|
104
+ | `react-wordcloud` | 传统词云 | Web | 可选 |
105
+ | `d3-cloud` | 传统词云 | Web | 可选 |
106
+ | `d3-hierarchy` | 气泡词云 | Web | 可选 |
107
+ | `react-native-svg` | 所有模式 | RN | 可选 |
108
+
109
+ ### PieChart / NestedPieChart 组件
110
+
111
+ | 依赖 | 用途 | 平台 | 是否必需 |
112
+ |------|------|------|----------|
113
+ | `recharts` | 图表渲染 | Web | 可选 |
114
+ | `react-native-svg` | 图表渲染 | RN | 可选 |
115
+ | `react-native-svg-charts` | 图表渲染 | RN | 可选 |
116
+
117
+ ### LineChart / BarChart / ComboChart 组件
118
+
119
+ | 依赖 | 用途 | 平台 | 是否必需 |
120
+ |------|------|------|----------|
121
+ | `recharts` | 图表渲染 | Web | 可选 |
122
+ | `react-native-svg-charts` | 图表渲染 | RN | 可选 |
123
+
124
+ ---
125
+
126
+ ## 按需安装策略
127
+
128
+ ### 策略 1:最小安装
129
+
130
+ 只使用基础组件(不包含图表):
131
+
132
+ ```bash
133
+ npm install @beppla/tapas-ui react react-native
134
+ ```
135
+
136
+ ### 策略 2:Web 图表
137
+
138
+ 使用 Web 端图表组件:
139
+
140
+ ```bash
141
+ npm install @beppla/tapas-ui react react-native
142
+ npm install recharts d3-hierarchy react-wordcloud
143
+ ```
144
+
145
+ ### 策略 3:RN 图表
146
+
147
+ 使用 React Native 图表组件:
148
+
149
+ ```bash
150
+ npm install @beppla/tapas-ui react react-native
151
+ npm install react-native-svg react-native-svg-charts
152
+ ```
153
+
154
+ ### 策略 4:全平台图表(推荐)
155
+
156
+ 同时支持 Web 和 RN:
157
+
158
+ ```bash
159
+ npm install @beppla/tapas-ui react react-native
160
+
161
+ # Web 依赖
162
+ npm install recharts d3-hierarchy react-wordcloud d3-cloud
163
+
164
+ # RN 依赖
165
+ npm install react-native-svg react-native-svg-charts
166
+ ```
167
+
168
+ ---
169
+
170
+ ## 生产环境优化
171
+
172
+ ### Tree Shaking
173
+
174
+ 确保你的打包工具支持 tree-shaking,未使用的组件不会被打包:
175
+
176
+ ```js
177
+ // 只导入需要的组件
178
+ import { WordCloud, NestedPieChart } from '@beppla/tapas-ui';
179
+
180
+ // ✅ 只有 WordCloud 和 NestedPieChart 会被打包
181
+ // ✅ 未使用的组件(如 BarChart)不会被打包
182
+ ```
183
+
184
+ ### 按需加载
185
+
186
+ 对于大型应用,可以使用动态导入:
187
+
188
+ ```tsx
189
+ // 懒加载气泡词云
190
+ const BubbleCloudPage = lazy(() =>
191
+ import('./pages/BubbleCloud').then(module => ({
192
+ default: module.BubbleCloudPage
193
+ }))
194
+ );
195
+ ```
196
+
197
+ ---
198
+
199
+ ## 常见问题
200
+
201
+ ### Q: 为什么不将这些依赖设为必需依赖?
202
+
203
+ **A:** 这些依赖较大(如 `recharts` ~500KB),并且不是所有用户都需要所有功能。将它们设为可选依赖可以:
204
+ - 减小默认包体积
205
+ - 让用户按需安装
206
+ - 支持更灵活的使用场景
207
+
208
+ ### Q: 警告会影响应用运行吗?
209
+
210
+ **A:** 不会。这些警告只是提示缺少可选依赖,不影响:
211
+ - 已安装依赖的功能正常使用
212
+ - 应用的其他部分
213
+ - 构建和部署流程
214
+
215
+ ### Q: 如何确认依赖是否正确安装?
216
+
217
+ **A:** 运行以下命令:
218
+
219
+ ```bash
220
+ # 检查 d3-hierarchy
221
+ npm list d3-hierarchy
222
+
223
+ # 检查 recharts
224
+ npm list recharts
225
+
226
+ # 检查所有 @beppla/tapas-ui 依赖
227
+ npm list --depth=0 | grep -E "d3|recharts|svg"
228
+ ```
229
+
230
+ ---
231
+
232
+ ## 获取帮助
233
+
234
+ 如果遇到其他依赖问题:
235
+
236
+ 1. 查看 [完整文档](https://github.com/your-repo/tapas-ui)
237
+ 2. 搜索 [GitHub Issues](https://github.com/your-repo/tapas-ui/issues)
238
+ 3. 提交新的 Issue 并附上:
239
+ - 错误信息
240
+ - 你的 package.json
241
+ - 使用的平台(Web / RN)
242
+ - 组件代码示例
@@ -30,15 +30,86 @@
30
30
 
31
31
  ## 安装依赖
32
32
 
33
+ ### 必需依赖
34
+
35
+ ```bash
36
+ npm install react react-native
37
+ ```
38
+
39
+ ### WordCloud 可选依赖
40
+
41
+ 根据你的使用场景安装对应的依赖:
42
+
43
+ #### 传统词云(Web 端)
44
+
33
45
  ```bash
34
46
  # Web 端需要
35
- npm install react-wordcloud d3-hierarchy
47
+ npm install react-wordcloud d3-cloud
48
+ ```
49
+
50
+ #### 气泡词云(Web 端)
51
+
52
+ ```bash
53
+ # Web 端气泡模式需要
54
+ npm install d3-hierarchy
55
+ ```
56
+
57
+ #### React Native 端
58
+
59
+ ```bash
60
+ # RN 端需要(两种模式都需要)
61
+ npm install react-native-svg
62
+ ```
63
+
64
+ ### 完整安装(推荐)
65
+
66
+ 如果你想使用所有功能,建议一次性安装:
67
+
68
+ ```bash
69
+ # Web 端完整依赖
70
+ npm install react-wordcloud d3-cloud d3-hierarchy
36
71
 
37
- # React Native 端需要
72
+ # React Native 端依赖
38
73
  npm install react-native-svg
39
74
  ```
40
75
 
41
- ## 基本用法
76
+ ### ⚠️ 依赖说明
77
+
78
+ - 这些依赖被标记为 `optional`,不会自动安装
79
+ - 如果缺少依赖,组件会优雅降级并显示提示信息
80
+ - **警告处理**:如果你看到 `Module not found: Can't resolve 'd3-hierarchy'` 警告,这是正常的。你可以:
81
+ - **方案 1(推荐)**:安装 `d3-hierarchy` 以使用气泡词云功能
82
+ - **方案 2**:如果只使用传统词云,可以忽略这个警告(不影响使用)
83
+ - **方案 3**:在 webpack 配置中添加 `IgnorePlugin` 忽略警告(见下方)
84
+
85
+ ### 忽略警告(Webpack)
86
+
87
+ 如果你不需要气泡词云功能,可以在 webpack 配置中忽略警告:
88
+
89
+ ```js
90
+ // webpack.config.js
91
+ module.exports = {
92
+ plugins: [
93
+ new webpack.IgnorePlugin({
94
+ resourceRegExp: /^d3-hierarchy$/,
95
+ contextRegExp: /@beppla\/tapas-ui/,
96
+ }),
97
+ ],
98
+ };
99
+ ```
100
+
101
+ 或者使用 `externals`:
102
+
103
+ ```js
104
+ // webpack.config.js
105
+ module.exports = {
106
+ externals: {
107
+ 'd3-hierarchy': 'd3-hierarchy',
108
+ },
109
+ };
110
+ ```
111
+
112
+ ---
42
113
 
43
114
  ### 传统词云
44
115
 
@@ -21,7 +21,7 @@ function _interopRequireWildcard(e, t) { if ("function" == typeof WeakMap) var r
21
21
 
22
22
  // 动态导入依赖
23
23
  let Svg, Text, G, Circle;
24
- let D3WordCloud, d3Hierarchy, d3Pack;
24
+ let D3WordCloud;
25
25
  if (_reactNative.Platform.OS === 'web') {
26
26
  // Web 端使用 react-wordcloud 和 d3-hierarchy
27
27
  try {
@@ -30,13 +30,6 @@ if (_reactNative.Platform.OS === 'web') {
30
30
  } catch (e) {
31
31
  // console.warn('react-wordcloud not available for Web', e);
32
32
  }
33
- try {
34
- const d3 = require('d3-hierarchy');
35
- d3Hierarchy = d3.hierarchy;
36
- d3Pack = d3.pack;
37
- } catch (e) {
38
- // console.warn('d3-hierarchy not available for Web', e);
39
- }
40
33
  } else {
41
34
  // RN 端使用 react-native-svg
42
35
  try {
@@ -240,17 +233,86 @@ const WordCloud = ({
240
233
  const viewWidth = width || 600;
241
234
  const viewHeight = height;
242
235
 
243
- // Web 端使用 D3 pack 布局
244
- if (_reactNative.Platform.OS === 'web' && d3Hierarchy && d3Pack) {
245
- const packLayout = d3Pack().size([viewWidth, viewHeight]).padding(bubblePadding);
246
- const root = d3Hierarchy({
247
- children: bubbleData
248
- }).sum(d => d.radius * d.radius);
249
- const nodes = packLayout(root).leaves();
236
+ // Web 端使用类似传统词云的中心散开布局
237
+ if (_reactNative.Platform.OS === 'web') {
238
+ // 使用自定义算法:从中心向外螺旋式布局(类似传统词云)
239
+ const centerX = viewWidth / 2;
240
+ const centerY = viewHeight / 2;
241
+ const layoutBubbles = () => {
242
+ const positioned = [];
243
+
244
+ // 按 value 排序,大的先放置
245
+ const sortedData = [...bubbleData].sort((a, b) => b.value - a.value);
246
+ sortedData.forEach((data, index) => {
247
+ let x = centerX;
248
+ let y = centerY;
249
+ let placed = false;
250
+ if (index === 0) {
251
+ // 第一个气泡放在正中心
252
+ placed = true;
253
+ } else {
254
+ // 从中心开始螺旋向外寻找位置
255
+ for (let attempt = 0; attempt < 500; attempt++) {
256
+ const angle = attempt * 0.5; // 螺旋角度
257
+ const distance = Math.sqrt(attempt) * (data.radius * 0.6 + bubblePadding);
258
+ x = centerX + distance * Math.cos(angle);
259
+ y = centerY + distance * Math.sin(angle);
260
+
261
+ // 检查边界
262
+ if (x - data.radius < 0 || x + data.radius > viewWidth || y - data.radius < 0 || y + data.radius > viewHeight) {
263
+ continue;
264
+ }
265
+
266
+ // 检查是否与已有气泡重叠
267
+ const overlaps = positioned.some(p => {
268
+ const dx = x - p.x;
269
+ const dy = y - p.y;
270
+ const dist = Math.sqrt(dx * dx + dy * dy);
271
+ return dist < data.radius + p.r + bubblePadding;
272
+ });
273
+ if (!overlaps) {
274
+ placed = true;
275
+ break;
276
+ }
277
+ }
278
+
279
+ // 如果还是找不到位置,在可用空间随机放置
280
+ if (!placed) {
281
+ for (let i = 0; i < 100; i++) {
282
+ const randomAngle = Math.random() * Math.PI * 2;
283
+ const randomDistance = Math.random() * Math.min(viewWidth, viewHeight) * 0.4;
284
+ x = centerX + randomDistance * Math.cos(randomAngle);
285
+ y = centerY + randomDistance * Math.sin(randomAngle);
286
+ if (x - data.radius >= 0 && x + data.radius <= viewWidth && y - data.radius >= 0 && y + data.radius <= viewHeight) {
287
+ const overlaps = positioned.some(p => {
288
+ const dx = x - p.x;
289
+ const dy = y - p.y;
290
+ const dist = Math.sqrt(dx * dx + dy * dy);
291
+ return dist < data.radius + p.r + bubblePadding * 0.5;
292
+ });
293
+ if (!overlaps) {
294
+ placed = true;
295
+ break;
296
+ }
297
+ }
298
+ }
299
+ }
300
+ }
301
+ positioned.push({
302
+ data,
303
+ x: placed ? x : centerX,
304
+ y: placed ? y : centerY,
305
+ r: data.radius
306
+ });
307
+ });
308
+ return positioned;
309
+ };
310
+ const positionedBubbles = layoutBubbles();
250
311
  return /*#__PURE__*/(0, _jsxRuntime.jsx)(_reactNative.View, {
251
- style: [styles.container, {
312
+ style: [{
252
313
  height,
253
- width: viewWidth
314
+ width: viewWidth,
315
+ backgroundColor: 'transparent'
254
316
  }, style],
255
317
  testID: testID,
256
318
  children: /*#__PURE__*/(0, _jsxRuntime.jsx)("svg", {
@@ -259,34 +321,34 @@ const WordCloud = ({
259
321
  style: {
260
322
  backgroundColor: 'transparent'
261
323
  },
262
- children: nodes.map((node, index) => {
263
- const data = node.data;
324
+ children: positionedBubbles.map((bubble, index) => {
325
+ const adjustedFontSize = Math.max(10, Math.min(24, bubble.r * 0.35));
264
326
  return /*#__PURE__*/(0, _jsxRuntime.jsxs)("g", {
265
- onClick: () => onBubbleClick?.(data),
327
+ onClick: () => onBubbleClick?.(bubble.data),
266
328
  children: [/*#__PURE__*/(0, _jsxRuntime.jsx)("circle", {
267
- cx: node.x,
268
- cy: node.y,
269
- r: node.r,
270
- fill: data.bubbleColor,
329
+ cx: bubble.x,
330
+ cy: bubble.y,
331
+ r: bubble.r,
332
+ fill: bubble.data.bubbleColor,
271
333
  stroke: "none",
272
334
  style: {
273
335
  cursor: onBubbleClick ? 'pointer' : 'default'
274
336
  }
275
337
  }), /*#__PURE__*/(0, _jsxRuntime.jsx)("text", {
276
- x: node.x,
277
- y: node.y,
338
+ x: bubble.x,
339
+ y: bubble.y,
278
340
  textAnchor: "middle",
279
341
  dominantBaseline: "middle",
280
- fontSize: data.fontSize,
281
- fill: data.textColor,
342
+ fontSize: adjustedFontSize,
343
+ fill: bubble.data.textColor,
282
344
  fontFamily: fontFamily,
283
345
  fontWeight: "400",
284
346
  style: {
285
347
  userSelect: 'none',
286
348
  pointerEvents: 'none',
287
- maxWidth: node.r * 1.8
349
+ maxWidth: bubble.r * 1.8
288
350
  },
289
- children: data.displayText
351
+ children: bubble.data.displayText
290
352
  })]
291
353
  }, index);
292
354
  })
@@ -294,54 +356,75 @@ const WordCloud = ({
294
356
  });
295
357
  }
296
358
 
297
- // RN 端使用简化的圆形打包算法
298
- if (_reactNative.Platform.OS !== 'web' && Svg && Circle && Text) {
299
- // 简化的圆形打包布局算法
359
+ // RN 端使用相同的中心散开布局
360
+ if (Svg && Circle && Text) {
361
+ // 从中心向外螺旋式布局(与 Web 端一致)
362
+ const centerX = viewWidth / 2;
363
+ const centerY = viewHeight / 2;
300
364
  const layoutBubbles = () => {
301
365
  const positioned = [];
302
- const sortedData = [...bubbleData].sort((a, b) => b.radius - a.radius);
303
- const centerX = viewWidth / 2;
304
- const centerY = viewHeight / 2;
366
+
367
+ // value 排序,大的先放置
368
+ const sortedData = [...bubbleData].sort((a, b) => b.value - a.value);
305
369
  sortedData.forEach((data, index) => {
306
- let x = 0;
307
- let y = 0;
308
- let attempts = 0;
309
- const maxAttempts = 100;
370
+ let x = centerX;
371
+ let y = centerY;
372
+ let placed = false;
310
373
  if (index === 0) {
311
- // 第一个气泡放在中心
312
- x = centerX;
313
- y = centerY;
374
+ // 第一个气泡放在正中心
375
+ placed = true;
314
376
  } else {
315
- // 后续气泡使用螺旋算法寻找位置
316
- while (attempts < maxAttempts) {
317
- const angle = attempts * 0.5;
318
- const spiralRadius = Math.sqrt(attempts + 1) * (data.radius + bubblePadding);
319
- x = centerX + spiralRadius * Math.cos(angle);
320
- y = centerY + spiralRadius * Math.sin(angle);
377
+ // 从中心开始螺旋向外寻找位置
378
+ for (let attempt = 0; attempt < 500; attempt++) {
379
+ const angle = attempt * 0.5;
380
+ const distance = Math.sqrt(attempt) * (data.radius * 0.6 + bubblePadding);
381
+ x = centerX + distance * Math.cos(angle);
382
+ y = centerY + distance * Math.sin(angle);
383
+
384
+ // 检查边界
385
+ if (x - data.radius < 0 || x + data.radius > viewWidth || y - data.radius < 0 || y + data.radius > viewHeight) {
386
+ continue;
387
+ }
321
388
 
322
389
  // 检查是否与已有气泡重叠
323
390
  const overlaps = positioned.some(p => {
324
391
  const dx = x - p.x;
325
392
  const dy = y - p.y;
326
- const distance = Math.sqrt(dx * dx + dy * dy);
327
- return distance < data.radius + p.r + bubblePadding;
393
+ const dist = Math.sqrt(dx * dx + dy * dy);
394
+ return dist < data.radius + p.r + bubblePadding;
328
395
  });
329
396
  if (!overlaps) {
397
+ placed = true;
330
398
  break;
331
399
  }
332
- attempts++;
333
400
  }
334
401
 
335
- // 如果找不到合适位置,强制放置
336
- if (attempts >= maxAttempts) {
337
- x = centerX + (Math.random() * viewWidth * 0.3 - viewWidth * 0.15);
338
- y = centerY + (Math.random() * viewHeight * 0.3 - viewHeight * 0.15);
402
+ // 兜底:随机放置在中心周围
403
+ if (!placed) {
404
+ for (let i = 0; i < 100; i++) {
405
+ const randomAngle = Math.random() * Math.PI * 2;
406
+ const randomDistance = Math.random() * Math.min(viewWidth, viewHeight) * 0.4;
407
+ x = centerX + randomDistance * Math.cos(randomAngle);
408
+ y = centerY + randomDistance * Math.sin(randomAngle);
409
+ if (x - data.radius >= 0 && x + data.radius <= viewWidth && y - data.radius >= 0 && y + data.radius <= viewHeight) {
410
+ const overlaps = positioned.some(p => {
411
+ const dx = x - p.x;
412
+ const dy = y - p.y;
413
+ const dist = Math.sqrt(dx * dx + dy * dy);
414
+ return dist < data.radius + p.r + bubblePadding * 0.5;
415
+ });
416
+ if (!overlaps) {
417
+ placed = true;
418
+ break;
419
+ }
420
+ }
421
+ }
339
422
  }
340
423
  }
341
424
  positioned.push({
342
425
  data,
343
- x: x || 0,
344
- y: y || 0,
426
+ x: placed ? x : centerX,
427
+ y: placed ? y : centerY,
345
428
  r: data.radius
346
429
  });
347
430
  });