jmchart 1.2.12 → 1.2.13

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,234 @@
1
+ import jmSeries from './series.js';
2
+
3
+ /**
4
+ * 词云图
5
+ *
6
+ * 词云图用于展示文本数据中词语的频率或重要性,词语大小与其权重成正比。
7
+ * 常用于文本分析、舆情监控等场景。
8
+ *
9
+ * 数据格式要求:
10
+ * - field: 'weight' - 权重字段
11
+ * - xField: 'word' - 词语字段
12
+ *
13
+ * 样式配置:
14
+ * - minFontSize: 最小字体大小,默认 12
15
+ * - maxFontSize: 最大字体大小,默认 60
16
+ * - colors: 颜色数组
17
+ * - spiral: 是否使用螺旋布局,默认 true
18
+ *
19
+ * @class jmWordCloudSeries
20
+ * @module jmChart
21
+ * @extends jmSeries
22
+ *
23
+ * @example
24
+ * chart.createSeries('wordCloud', {
25
+ * field: 'weight',
26
+ * xField: 'word',
27
+ * style: {
28
+ * minFontSize: 14,
29
+ * maxFontSize: 48
30
+ * }
31
+ * });
32
+ */
33
+ export default class jmWordCloudSeries extends jmSeries {
34
+ constructor(options) {
35
+ options.style = options.style || options.graph.style.wordCloud;
36
+ super(options);
37
+ this.placedWords = [];
38
+ }
39
+
40
+ /**
41
+ * 初始化词云图
42
+ */
43
+ init() {
44
+ const data = this.data;
45
+ if(!data || !data.length) return;
46
+
47
+ this.placedWords = [];
48
+
49
+ const chartWidth = this.graph.chartArea.width;
50
+ const chartHeight = this.graph.chartArea.height;
51
+ const centerX = chartWidth / 2;
52
+ const centerY = chartHeight / 2;
53
+
54
+ const field = this.field || 'weight';
55
+ const xField = this.option.xField || 'word';
56
+
57
+ const weights = data.map(item => Math.abs(item[field] || 0));
58
+ const maxWeight = Math.max(...weights);
59
+ const minWeight = Math.min(...weights);
60
+
61
+ if(maxWeight === 0) return;
62
+
63
+ const minFontSize = this.style.minFontSize || 12;
64
+ const maxFontSize = this.style.maxFontSize || 60;
65
+
66
+ const sortedData = [...data].sort((a, b) => (b[field] || 0) - (a[field] || 0));
67
+
68
+ for(let i = 0; i < sortedData.length; i++) {
69
+ const item = sortedData[i];
70
+ const weight = item[field] || 0;
71
+
72
+ let fontSize;
73
+ if(maxWeight === minWeight) {
74
+ fontSize = (minFontSize + maxFontSize) / 2;
75
+ } else {
76
+ fontSize = minFontSize + (weight - minWeight) / (maxWeight - minWeight) * (maxFontSize - minFontSize);
77
+ }
78
+
79
+ const color = this.getColor(item, i);
80
+ const text = item[xField] || '';
81
+
82
+ if(!text) continue;
83
+
84
+ const position = this.findPosition(centerX, centerY, text, fontSize, chartWidth, chartHeight);
85
+
86
+ if(position) {
87
+ this.createWordLabel(text, position.x, position.y, fontSize, color, item);
88
+
89
+ this.placedWords.push({
90
+ x: position.x,
91
+ y: position.y,
92
+ width: position.width,
93
+ height: position.height
94
+ });
95
+ }
96
+ }
97
+ }
98
+
99
+ /**
100
+ * 查找词语位置
101
+ */
102
+ findPosition(centerX, centerY, text, fontSize, chartWidth, chartHeight) {
103
+ const estimatedWidth = text.length * fontSize * 0.6;
104
+ const estimatedHeight = fontSize * 1.2;
105
+
106
+ if(this.style.spiral !== false) {
107
+ return this.findSpiralPosition(centerX, centerY, estimatedWidth, estimatedHeight, chartWidth, chartHeight);
108
+ } else {
109
+ return this.findGridPosition(centerX, centerY, estimatedWidth, estimatedHeight, chartWidth, chartHeight);
110
+ }
111
+ }
112
+
113
+ /**
114
+ * 螺旋布局查找位置
115
+ */
116
+ findSpiralPosition(centerX, centerY, width, height, chartWidth, chartHeight) {
117
+ const spiralStep = 5;
118
+ const maxRadius = Math.max(chartWidth, chartHeight);
119
+ let angle = 0;
120
+ let radius = 0;
121
+
122
+ for(let i = 0; i < 1000; i++) {
123
+ const x = centerX + radius * Math.cos(angle) - width / 2;
124
+ const y = centerY + radius * Math.sin(angle) - height / 2;
125
+
126
+ if(this.canPlace(x, y, width, height, chartWidth, chartHeight)) {
127
+ return { x, y, width, height };
128
+ }
129
+
130
+ angle += 0.5;
131
+ radius += spiralStep * 0.02;
132
+
133
+ if(radius > maxRadius) break;
134
+ }
135
+
136
+ return null;
137
+ }
138
+
139
+ /**
140
+ * 网格布局查找位置
141
+ */
142
+ findGridPosition(centerX, centerY, width, height, chartWidth, chartHeight) {
143
+ const gridSize = 20;
144
+ const halfWidth = chartWidth / 2;
145
+ const halfHeight = chartHeight / 2;
146
+
147
+ for(let r = 0; r < Math.max(halfWidth, halfHeight); r += gridSize) {
148
+ for(let dx = -r; dx <= r; dx += gridSize) {
149
+ for(let dy = -r; dy <= r; dy += gridSize) {
150
+ if(Math.abs(dx) !== r && Math.abs(dy) !== r) continue;
151
+
152
+ const x = centerX + dx - width / 2;
153
+ const y = centerY + dy - height / 2;
154
+
155
+ if(this.canPlace(x, y, width, height, chartWidth, chartHeight)) {
156
+ return { x, y, width, height };
157
+ }
158
+ }
159
+ }
160
+ }
161
+
162
+ return null;
163
+ }
164
+
165
+ /**
166
+ * 检查位置是否可用
167
+ */
168
+ canPlace(x, y, width, height, chartWidth, chartHeight) {
169
+ if(x < 0 || y < 0 || x + width > chartWidth || y + height > chartHeight) {
170
+ return false;
171
+ }
172
+
173
+ for(const placed of this.placedWords) {
174
+ if(!(x + width < placed.x || x > placed.x + placed.width ||
175
+ y + height < placed.y || y > placed.y + placed.height)) {
176
+ return false;
177
+ }
178
+ }
179
+
180
+ return true;
181
+ }
182
+
183
+ /**
184
+ * 创建词语标签
185
+ */
186
+ createWordLabel(text, x, y, fontSize, color, item) {
187
+ const label = this.graph.createShape('label', {
188
+ style: {
189
+ fill: color,
190
+ font: `${fontSize}px Arial`,
191
+ textAlign: 'left',
192
+ textBaseline: 'top',
193
+ zIndex: 1
194
+ },
195
+ text: text,
196
+ position: { x, y },
197
+ data: item
198
+ });
199
+
200
+ this.addShape(label);
201
+ }
202
+
203
+ /**
204
+ * 获取颜色
205
+ */
206
+ getColor(item, index) {
207
+ if(this.style.colors && this.style.colors.length > 0) {
208
+ return this.style.colors[index % this.style.colors.length];
209
+ }
210
+ if(typeof this.style.color === 'function') {
211
+ return this.style.color.call(this, item, index);
212
+ }
213
+ return this.graph.getColor(index);
214
+ }
215
+
216
+ /**
217
+ * 生成图例
218
+ */
219
+ createLegend() {
220
+ const style = this.graph.utils.clone(this.style);
221
+ style.fill = this.style.color || this.graph.getColor(0);
222
+ style.stroke = style.fill;
223
+
224
+ const shape = this.graph.createShape('label', {
225
+ style: style,
226
+ text: 'Aa',
227
+ position: { x: 0, y: 0 }
228
+ });
229
+
230
+ this.graph.legend.append(this, shape);
231
+ }
232
+ }
233
+
234
+ export { jmWordCloudSeries };