@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
 
@@ -14,7 +14,7 @@ import { View, StyleSheet, Platform, Text as RNText } from 'react-native';
14
14
 
15
15
  // 动态导入依赖
16
16
  let Svg: any, Text: any, G: any, Circle: any;
17
- let D3WordCloud: any, d3Hierarchy: any, d3Pack: any;
17
+ let D3WordCloud: any;
18
18
 
19
19
  if (Platform.OS === 'web') {
20
20
  // Web 端使用 react-wordcloud 和 d3-hierarchy
@@ -24,14 +24,6 @@ if (Platform.OS === 'web') {
24
24
  } catch (e) {
25
25
  // console.warn('react-wordcloud not available for Web', e);
26
26
  }
27
-
28
- try {
29
- const d3 = require('d3-hierarchy');
30
- d3Hierarchy = d3.hierarchy;
31
- d3Pack = d3.pack;
32
- } catch (e) {
33
- // console.warn('d3-hierarchy not available for Web', e);
34
- }
35
27
  } else {
36
28
  // RN 端使用 react-native-svg
37
29
  try {
@@ -277,48 +269,130 @@ export const WordCloud: React.FC<WordCloudProps> = ({
277
269
  const viewWidth = width || 600;
278
270
  const viewHeight = height;
279
271
 
280
- // Web 端使用 D3 pack 布局
281
- if (Platform.OS === 'web' && d3Hierarchy && d3Pack) {
282
- const packLayout = d3Pack()
283
- .size([viewWidth, viewHeight])
284
- .padding(bubblePadding);
272
+ // Web 端使用类似传统词云的中心散开布局
273
+ if (Platform.OS === 'web') {
274
+ // 使用自定义算法:从中心向外螺旋式布局(类似传统词云)
275
+ const centerX = viewWidth / 2;
276
+ const centerY = viewHeight / 2;
277
+
278
+ const layoutBubbles = () => {
279
+ const positioned: Array<{
280
+ data: any;
281
+ x: number;
282
+ y: number;
283
+ r: number;
284
+ }> = [];
285
+
286
+ // 按 value 排序,大的先放置
287
+ const sortedData = [...bubbleData].sort((a, b) => b.value - a.value);
288
+
289
+ sortedData.forEach((data, index) => {
290
+ let x = centerX;
291
+ let y = centerY;
292
+ let placed = false;
293
+
294
+ if (index === 0) {
295
+ // 第一个气泡放在正中心
296
+ placed = true;
297
+ } else {
298
+ // 从中心开始螺旋向外寻找位置
299
+ for (let attempt = 0; attempt < 500; attempt++) {
300
+ const angle = attempt * 0.5; // 螺旋角度
301
+ const distance = Math.sqrt(attempt) * (data.radius * 0.6 + bubblePadding);
302
+ x = centerX + distance * Math.cos(angle);
303
+ y = centerY + distance * Math.sin(angle);
304
+
305
+ // 检查边界
306
+ if (x - data.radius < 0 || x + data.radius > viewWidth ||
307
+ y - data.radius < 0 || y + data.radius > viewHeight) {
308
+ continue;
309
+ }
310
+
311
+ // 检查是否与已有气泡重叠
312
+ const overlaps = positioned.some(p => {
313
+ const dx = x - p.x;
314
+ const dy = y - p.y;
315
+ const dist = Math.sqrt(dx * dx + dy * dy);
316
+ return dist < (data.radius + p.r + bubblePadding);
317
+ });
318
+
319
+ if (!overlaps) {
320
+ placed = true;
321
+ break;
322
+ }
323
+ }
324
+
325
+ // 如果还是找不到位置,在可用空间随机放置
326
+ if (!placed) {
327
+ for (let i = 0; i < 100; i++) {
328
+ const randomAngle = Math.random() * Math.PI * 2;
329
+ const randomDistance = Math.random() * Math.min(viewWidth, viewHeight) * 0.4;
330
+ x = centerX + randomDistance * Math.cos(randomAngle);
331
+ y = centerY + randomDistance * Math.sin(randomAngle);
332
+
333
+ if (x - data.radius >= 0 && x + data.radius <= viewWidth &&
334
+ y - data.radius >= 0 && y + data.radius <= viewHeight) {
335
+ const overlaps = positioned.some(p => {
336
+ const dx = x - p.x;
337
+ const dy = y - p.y;
338
+ const dist = Math.sqrt(dx * dx + dy * dy);
339
+ return dist < (data.radius + p.r + bubblePadding * 0.5);
340
+ });
341
+
342
+ if (!overlaps) {
343
+ placed = true;
344
+ break;
345
+ }
346
+ }
347
+ }
348
+ }
349
+ }
285
350
 
286
- const root = d3Hierarchy({ children: bubbleData })
287
- .sum((d: any) => d.radius * d.radius);
351
+ positioned.push({
352
+ data,
353
+ x: placed ? x : centerX,
354
+ y: placed ? y : centerY,
355
+ r: data.radius
356
+ });
357
+ });
288
358
 
289
- const nodes = packLayout(root).leaves();
359
+ return positioned;
360
+ };
361
+
362
+ const positionedBubbles = layoutBubbles();
290
363
 
291
364
  return (
292
- <View style={[styles.container, { height, width: viewWidth }, style]} testID={testID}>
365
+ <View style={[{ height, width: viewWidth, backgroundColor: 'transparent' }, style]} testID={testID}>
293
366
  <svg width={viewWidth} height={viewHeight} style={{ backgroundColor: 'transparent' }}>
294
- {nodes.map((node: any, index: number) => {
295
- const data = node.data;
367
+ {positionedBubbles.map((bubble, index) => {
368
+ const adjustedFontSize = Math.max(10, Math.min(24, bubble.r * 0.35));
369
+
296
370
  return (
297
- <g key={index} onClick={() => onBubbleClick?.(data)}>
371
+ <g key={index} onClick={() => onBubbleClick?.(bubble.data)}>
298
372
  <circle
299
- cx={node.x}
300
- cy={node.y}
301
- r={node.r}
302
- fill={data.bubbleColor}
373
+ cx={bubble.x}
374
+ cy={bubble.y}
375
+ r={bubble.r}
376
+ fill={bubble.data.bubbleColor}
303
377
  stroke="none"
304
378
  style={{ cursor: onBubbleClick ? 'pointer' : 'default' }}
305
379
  />
306
380
  <text
307
- x={node.x}
308
- y={node.y}
381
+ x={bubble.x}
382
+ y={bubble.y}
309
383
  textAnchor="middle"
310
384
  dominantBaseline="middle"
311
- fontSize={data.fontSize}
312
- fill={data.textColor}
385
+ fontSize={adjustedFontSize}
386
+ fill={bubble.data.textColor}
313
387
  fontFamily={fontFamily}
314
388
  fontWeight="400"
315
389
  style={{
316
390
  userSelect: 'none',
317
391
  pointerEvents: 'none',
318
- maxWidth: node.r * 1.8,
392
+ maxWidth: bubble.r * 1.8,
319
393
  }}
320
394
  >
321
- {data.displayText}
395
+ {bubble.data.displayText}
322
396
  </text>
323
397
  </g>
324
398
  );
@@ -328,9 +402,12 @@ export const WordCloud: React.FC<WordCloudProps> = ({
328
402
  );
329
403
  }
330
404
 
331
- // RN 端使用简化的圆形打包算法
332
- if (Platform.OS !== 'web' && Svg && Circle && Text) {
333
- // 简化的圆形打包布局算法
405
+ // RN 端使用相同的中心散开布局
406
+ if (Svg && Circle && Text) {
407
+ // 从中心向外螺旋式布局(与 Web 端一致)
408
+ const centerX = viewWidth / 2;
409
+ const centerY = viewHeight / 2;
410
+
334
411
  const layoutBubbles = () => {
335
412
  const positioned: Array<{
336
413
  data: any;
@@ -339,50 +416,77 @@ export const WordCloud: React.FC<WordCloudProps> = ({
339
416
  r: number;
340
417
  }> = [];
341
418
 
342
- const sortedData = [...bubbleData].sort((a, b) => b.radius - a.radius);
343
- const centerX = viewWidth / 2;
344
- const centerY = viewHeight / 2;
419
+ // value 排序,大的先放置
420
+ const sortedData = [...bubbleData].sort((a, b) => b.value - a.value);
345
421
 
346
422
  sortedData.forEach((data, index) => {
347
- let x: number = 0;
348
- let y: number = 0;
349
- let attempts = 0;
350
- const maxAttempts = 100;
423
+ let x = centerX;
424
+ let y = centerY;
425
+ let placed = false;
351
426
 
352
427
  if (index === 0) {
353
- // 第一个气泡放在中心
354
- x = centerX;
355
- y = centerY;
428
+ // 第一个气泡放在正中心
429
+ placed = true;
356
430
  } else {
357
- // 后续气泡使用螺旋算法寻找位置
358
- while (attempts < maxAttempts) {
359
- const angle = attempts * 0.5;
360
- const spiralRadius = Math.sqrt(attempts + 1) * (data.radius + bubblePadding);
361
- x = centerX + spiralRadius * Math.cos(angle);
362
- y = centerY + spiralRadius * Math.sin(angle);
431
+ // 从中心开始螺旋向外寻找位置
432
+ for (let attempt = 0; attempt < 500; attempt++) {
433
+ const angle = attempt * 0.5;
434
+ const distance = Math.sqrt(attempt) * (data.radius * 0.6 + bubblePadding);
435
+ x = centerX + distance * Math.cos(angle);
436
+ y = centerY + distance * Math.sin(angle);
437
+
438
+ // 检查边界
439
+ if (x - data.radius < 0 || x + data.radius > viewWidth ||
440
+ y - data.radius < 0 || y + data.radius > viewHeight) {
441
+ continue;
442
+ }
363
443
 
364
444
  // 检查是否与已有气泡重叠
365
445
  const overlaps = positioned.some(p => {
366
446
  const dx = x - p.x;
367
447
  const dy = y - p.y;
368
- const distance = Math.sqrt(dx * dx + dy * dy);
369
- return distance < (data.radius + p.r + bubblePadding);
448
+ const dist = Math.sqrt(dx * dx + dy * dy);
449
+ return dist < (data.radius + p.r + bubblePadding);
370
450
  });
371
451
 
372
452
  if (!overlaps) {
453
+ placed = true;
373
454
  break;
374
455
  }
375
- attempts++;
376
456
  }
377
457
 
378
- // 如果找不到合适位置,强制放置
379
- if (attempts >= maxAttempts) {
380
- x = centerX + (Math.random() * viewWidth * 0.3 - viewWidth * 0.15);
381
- y = centerY + (Math.random() * viewHeight * 0.3 - viewHeight * 0.15);
458
+ // 兜底:随机放置在中心周围
459
+ if (!placed) {
460
+ for (let i = 0; i < 100; i++) {
461
+ const randomAngle = Math.random() * Math.PI * 2;
462
+ const randomDistance = Math.random() * Math.min(viewWidth, viewHeight) * 0.4;
463
+ x = centerX + randomDistance * Math.cos(randomAngle);
464
+ y = centerY + randomDistance * Math.sin(randomAngle);
465
+
466
+ if (x - data.radius >= 0 && x + data.radius <= viewWidth &&
467
+ y - data.radius >= 0 && y + data.radius <= viewHeight) {
468
+ const overlaps = positioned.some(p => {
469
+ const dx = x - p.x;
470
+ const dy = y - p.y;
471
+ const dist = Math.sqrt(dx * dx + dy * dy);
472
+ return dist < (data.radius + p.r + bubblePadding * 0.5);
473
+ });
474
+
475
+ if (!overlaps) {
476
+ placed = true;
477
+ break;
478
+ }
479
+ }
480
+ }
382
481
  }
383
482
  }
384
483
 
385
- positioned.push({ data, x: x || 0, y: y || 0, r: data.radius });
484
+ positioned.push({
485
+ data,
486
+ x: placed ? x : centerX,
487
+ y: placed ? y : centerY,
488
+ r: data.radius
489
+ });
386
490
  });
387
491
 
388
492
  return positioned;