@makechart/tagcloud 0.0.1
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/CHANGELOG.md +5 -0
- package/LICENSE +21 -0
- package/index.html +259 -0
- package/package.json +1 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2025 zbryikt
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/index.html
ADDED
|
@@ -0,0 +1,259 @@
|
|
|
1
|
+
<div><script type="@plotdb/block">/* generated by claude */
|
|
2
|
+
|
|
3
|
+
|
|
4
|
+
var loadfont = function() {
|
|
5
|
+
var font, this$ = this;
|
|
6
|
+
if (this.cfg.font.family && this.cfg.font.family.isXl) {
|
|
7
|
+
if (!(font = this.fonts.h[this.cfg.font.family.path])) {
|
|
8
|
+
this.fonts.h[this.cfg.font.family.path] = {
|
|
9
|
+
loading: true
|
|
10
|
+
};
|
|
11
|
+
xfl.load(this.cfg.font.family.path).then(function(font){
|
|
12
|
+
this$.fonts.h[this$.cfg.font.family.path] = this$.fonts.active = font;
|
|
13
|
+
if (!font.sync) {
|
|
14
|
+
return;
|
|
15
|
+
}
|
|
16
|
+
return font.sync(this$.parsed.map(function(it){
|
|
17
|
+
return it.text;
|
|
18
|
+
}).join('')).then(function(){
|
|
19
|
+
this$.resize();
|
|
20
|
+
return this$.render();
|
|
21
|
+
});
|
|
22
|
+
});
|
|
23
|
+
}
|
|
24
|
+
if (font && font.sync) {
|
|
25
|
+
font.sync(this.parsed.map(function(it){
|
|
26
|
+
return it.text;
|
|
27
|
+
}).join('')).then(function(){
|
|
28
|
+
this$.resize();
|
|
29
|
+
return this$.render();
|
|
30
|
+
});
|
|
31
|
+
}
|
|
32
|
+
}
|
|
33
|
+
};
|
|
34
|
+
|
|
35
|
+
module.exports = {
|
|
36
|
+
pkg: {
|
|
37
|
+
extend: {name: "@makechart/base"},
|
|
38
|
+
dependencies: [
|
|
39
|
+
{url: "https://cdn.jsdelivr.net/npm/d3@7/dist/d3.min.js"},
|
|
40
|
+
{url: "https://cdn.jsdelivr.net/npm/d3-cloud@1/build/d3.layout.cloud.js"},
|
|
41
|
+
{name: "ldcolor"}
|
|
42
|
+
]
|
|
43
|
+
},
|
|
44
|
+
init: function(opt) {
|
|
45
|
+
var d3 = opt.ctx.d3;
|
|
46
|
+
var cloud = opt.ctx.d3.layout.cloud;
|
|
47
|
+
var ldcolor = opt.ctx.ldcolor;
|
|
48
|
+
var root = opt.root;
|
|
49
|
+
|
|
50
|
+
// 創建 SVG 容器
|
|
51
|
+
var svg = d3.select(root).append("svg")
|
|
52
|
+
.attr("width", "100%")
|
|
53
|
+
.attr("height", "100%");
|
|
54
|
+
|
|
55
|
+
var g = svg.append("g");
|
|
56
|
+
var layout = cloud();
|
|
57
|
+
|
|
58
|
+
opt.pubsub.fire("init", {
|
|
59
|
+
layout: false,
|
|
60
|
+
mod: {
|
|
61
|
+
config: {
|
|
62
|
+
font: {
|
|
63
|
+
family: {name: "font", type: "font"},
|
|
64
|
+
minsize: {name: "min size", type: "number", min: 8, max: 50, default: 12, step: 1},
|
|
65
|
+
maxsize: {name: "max size", type: "number", min: 20, max: 200, default: 60, step: 1}
|
|
66
|
+
},
|
|
67
|
+
sizeScale: {name: "scale", type: "choice", values: ["sqrt", "linear"], default: "sqrt"},
|
|
68
|
+
padding: {name: "padding", type: "number", min: 0, max: 10, default: 2, step: 0.5},
|
|
69
|
+
spiral: {name: "shape", type: "choice", values: ["archimedean", "rectangular"], default: "archimedean"},
|
|
70
|
+
rotationMode: {name: "rotation mode", type: "choice", values: ["none", "fixed", "random"], default: "random"},
|
|
71
|
+
maxRotation: {name: "max rotation", type: "number", min: 0, max: 180, default: 90, step: 15},
|
|
72
|
+
rotationStep: {name: "rotation step", type: "number", min: 15, max: 90, default: 90, step: 15},
|
|
73
|
+
randomColor: {name: "random color without category", type: "choice", values: ["true", "false"], default: "false"},
|
|
74
|
+
palette: {name: "palette", type: "palette", default: {colors: ["#1f77b4", "#ff7f0e", "#2ca02c", "#d62728", "#9467bd", "#8c564b", "#e377c2", "#7f7f7f", "#bcbd22", "#17becf"]}}
|
|
75
|
+
},
|
|
76
|
+
dimension: {
|
|
77
|
+
text: {},
|
|
78
|
+
size: {},
|
|
79
|
+
category: {}
|
|
80
|
+
},
|
|
81
|
+
init: function() {
|
|
82
|
+
// 初始化完成
|
|
83
|
+
},
|
|
84
|
+
sample: function() {
|
|
85
|
+
return {
|
|
86
|
+
binding: {
|
|
87
|
+
text: {key: "word"},
|
|
88
|
+
size: {key: "frequency"},
|
|
89
|
+
category: {key: "type"}
|
|
90
|
+
},
|
|
91
|
+
raw: [
|
|
92
|
+
{word: "JavaScript", frequency: 100, type: "language"},
|
|
93
|
+
{word: "Python", frequency: 85, type: "language"},
|
|
94
|
+
{word: "React", frequency: 75, type: "framework"},
|
|
95
|
+
{word: "Node.js", frequency: 65, type: "environment"},
|
|
96
|
+
{word: "HTML", frequency: 60, type: "language"},
|
|
97
|
+
{word: "CSS", frequency: 55, type: "language"},
|
|
98
|
+
{word: "Vue", frequency: 50, type: "framework"},
|
|
99
|
+
{word: "TypeScript", frequency: 45, type: "language"},
|
|
100
|
+
{word: "Angular", frequency: 40, type: "framework"},
|
|
101
|
+
{word: "MongoDB", frequency: 35, type: "database"},
|
|
102
|
+
{word: "Express", frequency: 30, type: "framework"},
|
|
103
|
+
{word: "GraphQL", frequency: 25, type: "tool"},
|
|
104
|
+
{word: "Docker", frequency: 20, type: "tool"},
|
|
105
|
+
{word: "AWS", frequency: 18, type: "cloud"},
|
|
106
|
+
{word: "Git", frequency: 15, type: "tool"}
|
|
107
|
+
]
|
|
108
|
+
};
|
|
109
|
+
},
|
|
110
|
+
bind: function() {
|
|
111
|
+
var that = this;
|
|
112
|
+
if (!this.data || !this.data.length) return;
|
|
113
|
+
|
|
114
|
+
// 準備顏色陣列
|
|
115
|
+
var colors = this.cfg.palette.colors.map(function(v) {
|
|
116
|
+
return ldcolor.hex(v);
|
|
117
|
+
});
|
|
118
|
+
|
|
119
|
+
// 處理數據,計算字體大小範圍
|
|
120
|
+
var sizeValues = this.data.map(function(d) { return d.size; });
|
|
121
|
+
var minSize = Math.min.apply(Math, sizeValues);
|
|
122
|
+
var maxSize = Math.max.apply(Math, sizeValues);
|
|
123
|
+
|
|
124
|
+
// 根據設定選擇比例尺
|
|
125
|
+
var sizeScale;
|
|
126
|
+
if (this.cfg.sizeScale === "sqrt") {
|
|
127
|
+
sizeScale = d3.scaleSqrt()
|
|
128
|
+
.domain([minSize, maxSize])
|
|
129
|
+
.range([this.cfg.font.minsize, this.cfg.font.maxsize]);
|
|
130
|
+
} else {
|
|
131
|
+
sizeScale = d3.scaleLinear()
|
|
132
|
+
.domain([minSize, maxSize])
|
|
133
|
+
.range([this.cfg.font.minsize, this.cfg.font.maxsize]);
|
|
134
|
+
}
|
|
135
|
+
|
|
136
|
+
// 建立分類到顏色的對應
|
|
137
|
+
var categoryColorMap = {};
|
|
138
|
+
var hasCategory = this.data.some(function(d) { return d.category !== undefined && d.category !== null && d.category !== ""; });
|
|
139
|
+
|
|
140
|
+
if (hasCategory) {
|
|
141
|
+
// 取得所有獨特的分類
|
|
142
|
+
var categories = [];
|
|
143
|
+
this.data.forEach(function(d) {
|
|
144
|
+
if (d.category !== undefined && d.category !== null && d.category !== "" && categories.indexOf(d.category) === -1) {
|
|
145
|
+
categories.push(d.category);
|
|
146
|
+
}
|
|
147
|
+
});
|
|
148
|
+
|
|
149
|
+
// 為每個分類分配顏色
|
|
150
|
+
categories.forEach(function(category, index) {
|
|
151
|
+
categoryColorMap[category] = colors[index % colors.length];
|
|
152
|
+
});
|
|
153
|
+
}
|
|
154
|
+
|
|
155
|
+
this.parsed = this.data.map(function(d, i) {
|
|
156
|
+
var color;
|
|
157
|
+
|
|
158
|
+
if (hasCategory && d.category !== undefined && d.category !== null && d.category !== "") {
|
|
159
|
+
// 有分類資料時,使用分類對應的顏色
|
|
160
|
+
color = categoryColorMap[d.category];
|
|
161
|
+
} else {
|
|
162
|
+
// 沒有分類資料時,根據設定決定顏色
|
|
163
|
+
if (that.cfg.randomColor === "true") {
|
|
164
|
+
color = colors[Math.floor(Math.random() * colors.length)];
|
|
165
|
+
} else {
|
|
166
|
+
color = colors[0];
|
|
167
|
+
}
|
|
168
|
+
}
|
|
169
|
+
|
|
170
|
+
return {
|
|
171
|
+
text: d.text,
|
|
172
|
+
size: sizeScale(d.size),
|
|
173
|
+
color: color,
|
|
174
|
+
category: d.category || null
|
|
175
|
+
};
|
|
176
|
+
});
|
|
177
|
+
},
|
|
178
|
+
resize: function() {
|
|
179
|
+
var rect = root.getBoundingClientRect();
|
|
180
|
+
this.width = rect.width;
|
|
181
|
+
this.height = rect.height;
|
|
182
|
+
svg.attr("width", this.width).attr("height", this.height);
|
|
183
|
+
loadfont.apply(this, []);
|
|
184
|
+
},
|
|
185
|
+
render: function() {
|
|
186
|
+
var that = this;
|
|
187
|
+
if (!this.parsed || !this.parsed.length) return;
|
|
188
|
+
|
|
189
|
+
// 確保尺寸已設定
|
|
190
|
+
if (!this.width || !this.height) {
|
|
191
|
+
this.resize();
|
|
192
|
+
}
|
|
193
|
+
|
|
194
|
+
// 準備旋轉函式
|
|
195
|
+
var rotateFunction;
|
|
196
|
+
switch (this.cfg.rotationMode) {
|
|
197
|
+
case "none":
|
|
198
|
+
rotateFunction = function() { return 0; };
|
|
199
|
+
break;
|
|
200
|
+
case "fixed":
|
|
201
|
+
rotateFunction = function() {
|
|
202
|
+
return ~~(Math.random() * 2) * that.cfg.rotationStep;
|
|
203
|
+
};
|
|
204
|
+
break;
|
|
205
|
+
case "random":
|
|
206
|
+
default:
|
|
207
|
+
rotateFunction = function() {
|
|
208
|
+
var steps = Math.floor(that.cfg.maxRotation / that.cfg.rotationStep) + 1;
|
|
209
|
+
var randomStep = Math.floor(Math.random() * steps);
|
|
210
|
+
return (Math.random() < 0.5 ? 1 : -1) * randomStep * that.cfg.rotationStep;
|
|
211
|
+
};
|
|
212
|
+
break;
|
|
213
|
+
}
|
|
214
|
+
|
|
215
|
+
// 配置文字雲佈局,增加迭代次數提升緊實度
|
|
216
|
+
layout
|
|
217
|
+
.size([this.width, this.height])
|
|
218
|
+
.words(this.parsed.slice()) // 使用副本避免修改原數據
|
|
219
|
+
.padding(this.cfg.padding)
|
|
220
|
+
.rotate(rotateFunction)
|
|
221
|
+
.font((this.cfg.font.family || {}).name || 'sans-serif')
|
|
222
|
+
.fontSize(function(d) { return d.size; })
|
|
223
|
+
.spiral(this.cfg.spiral)
|
|
224
|
+
.random(function() { return 0.5; }) // 固定隨機種子讓結果更一致
|
|
225
|
+
.on("end", function(words) {
|
|
226
|
+
// 清除舊內容
|
|
227
|
+
g.selectAll("*").remove();
|
|
228
|
+
|
|
229
|
+
// 居中
|
|
230
|
+
g.attr("transform", "translate(" + that.width/2 + "," + that.height/2 + ")");
|
|
231
|
+
|
|
232
|
+
// 繪製文字
|
|
233
|
+
g.selectAll("text")
|
|
234
|
+
.data(words)
|
|
235
|
+
.enter().append("text")
|
|
236
|
+
.style("font-size", function(d) { return d.size + "px"; })
|
|
237
|
+
.style("font-family", (that.cfg.font.family || {}).name || 'sans-serif')
|
|
238
|
+
.style("fill", function(d) { return d.color; })
|
|
239
|
+
.attr("text-anchor", "middle")
|
|
240
|
+
.attr("transform", function(d) {
|
|
241
|
+
return "translate(" + [d.x, d.y] + ")rotate(" + d.rotate + ")";
|
|
242
|
+
})
|
|
243
|
+
.text(function(d) { return d.text; });
|
|
244
|
+
});
|
|
245
|
+
|
|
246
|
+
// 開始佈局
|
|
247
|
+
layout.start();
|
|
248
|
+
},
|
|
249
|
+
destroy: function() {
|
|
250
|
+
if (layout) {
|
|
251
|
+
layout.stop();
|
|
252
|
+
}
|
|
253
|
+
svg.remove();
|
|
254
|
+
}
|
|
255
|
+
}
|
|
256
|
+
});
|
|
257
|
+
}
|
|
258
|
+
};
|
|
259
|
+
</script></div>
|
package/package.json
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"name":"@makechart/tagcloud","version":"0.0.1","description":"","scripts":{"test":"echo \"Error: no test specified\" && exit 1"},"keywords":[],"author":"zbryikt","license":"MIT","dependencies":{"@plotdb/srcbuild":"^0.0.69","fedep":"1.6.0","livescript":"^1.6.0"}}
|