@makechart/tagcloud 0.0.1 → 0.0.2

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.
Files changed (3) hide show
  1. package/CHANGELOG.md +13 -0
  2. package/index.html +55 -33
  3. package/package.json +1 -1
package/CHANGELOG.md CHANGED
@@ -1,5 +1,18 @@
1
1
  # Change Logs
2
2
 
3
+ ## v0.0.2
4
+
5
+ - extend the lite base ( `@makechart/base` `lite/index.html`, no d3 ): needs `@makechart/base` >= 0.2.0
6
+ and `@plotdb/chart` >= 3.7.0
7
+ its own d3@7 and d3-cloud loading are replaced by `d3-cloud` 1.2.5 as a dependency
8
+ - add `pkg.name`
9
+ - rotation and random colors are seeded, so the same data always gives the same result
10
+ - fix bug: it didn't render. the svg is now positioned at the top left instead of being pushed below
11
+ the base layout block
12
+ - fix bug: `xfl` was used without declaration
13
+ - add test cases ( `test/index.ls`, run with `npm test` via `@makechart/viewer` )
14
+
15
+
3
16
  ## v0.0.1
4
17
 
5
18
  init release
package/index.html CHANGED
@@ -1,5 +1,8 @@
1
1
  <div><script type="@plotdb/block">/* generated by claude */
2
2
 
3
+ // xl 字型的載入器 ( @xlfont/load ), init 時從 context 取得
4
+ var xfl;
5
+
3
6
 
4
7
  var loadfont = function() {
5
8
  var font, this$ = this;
@@ -32,27 +35,44 @@ var loadfont = function() {
32
35
  }
33
36
  };
34
37
 
38
+ // 同 d3.randomLcg ( 移植自 d3-random, ISC )
39
+ var lcg = function(seed) {
40
+ var mul = 0x19660D, inc = 0x3C6EF35F, eps = 1 / 0x100000000;
41
+ var state = (0 <= seed && seed < 1 ? seed / eps : Math.abs(seed)) | 0;
42
+ return function() { return eps * ((state = (mul * state + inc) | 0) >>> 0); };
43
+ };
44
+ var svgns = "http://www.w3.org/2000/svg";
45
+
35
46
  module.exports = {
36
47
  pkg: {
37
- extend: {name: "@makechart/base"},
48
+ name: "tagcloud",
49
+ // lite: 不帶 d3 的 base ( 見 @plotdb/chart utils ). d3-cloud 的 build 已內含 d3-dispatch, 不需要 d3 core
50
+ extend: {name: "@makechart/base", path: "lite/index.html"},
38
51
  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"}
52
+ {name: "d3-cloud", version: "1.2.5", path: "build/d3.layout.cloud.js", async: false},
53
+ {name: "ldcolor"},
54
+ {name: "@xlfont/load", version: "main"}
42
55
  ]
43
56
  },
44
57
  init: function(opt) {
45
58
  var d3 = opt.ctx.d3;
46
59
  var cloud = opt.ctx.d3.layout.cloud;
47
60
  var ldcolor = opt.ctx.ldcolor;
61
+ xfl = opt.ctx.xfl;
62
+ var Scale = opt.ctx.chart.utils.scale;
48
63
  var root = opt.root;
49
-
64
+ // 旋轉與隨機顏色用固定種子的亂數: 同樣的資料與設定每次排出一樣的結果
65
+ var rng = lcg(1);
66
+
50
67
  // 創建 SVG 容器
51
- var svg = d3.select(root).append("svg")
52
- .attr("width", "100%")
53
- .attr("height", "100%");
54
-
55
- var g = svg.append("g");
68
+ var svg = root.appendChild(document.createElementNS(svgns, "svg"));
69
+ svg.setAttribute("width", "100%");
70
+ svg.setAttribute("height", "100%");
71
+ // svg 接在 base 的 layout 區塊後面, 會被擠到可視範圍外; 疊回左上角
72
+ svg.style.position = "absolute";
73
+ svg.style.left = svg.style.top = 0;
74
+
75
+ var g = svg.appendChild(document.createElementNS(svgns, "g"));
56
76
  var layout = cloud();
57
77
 
58
78
  opt.pubsub.fire("init", {
@@ -124,11 +144,11 @@ module.exports = {
124
144
  // 根據設定選擇比例尺
125
145
  var sizeScale;
126
146
  if (this.cfg.sizeScale === "sqrt") {
127
- sizeScale = d3.scaleSqrt()
147
+ sizeScale = Scale.sqrt()
128
148
  .domain([minSize, maxSize])
129
149
  .range([this.cfg.font.minsize, this.cfg.font.maxsize]);
130
150
  } else {
131
- sizeScale = d3.scaleLinear()
151
+ sizeScale = Scale.linear()
132
152
  .domain([minSize, maxSize])
133
153
  .range([this.cfg.font.minsize, this.cfg.font.maxsize]);
134
154
  }
@@ -152,6 +172,7 @@ module.exports = {
152
172
  });
153
173
  }
154
174
 
175
+ rng = lcg(1);
155
176
  this.parsed = this.data.map(function(d, i) {
156
177
  var color;
157
178
 
@@ -161,7 +182,7 @@ module.exports = {
161
182
  } else {
162
183
  // 沒有分類資料時,根據設定決定顏色
163
184
  if (that.cfg.randomColor === "true") {
164
- color = colors[Math.floor(Math.random() * colors.length)];
185
+ color = colors[Math.floor(rng() * colors.length)];
165
186
  } else {
166
187
  color = colors[0];
167
188
  }
@@ -179,7 +200,8 @@ module.exports = {
179
200
  var rect = root.getBoundingClientRect();
180
201
  this.width = rect.width;
181
202
  this.height = rect.height;
182
- svg.attr("width", this.width).attr("height", this.height);
203
+ svg.setAttribute("width", this.width);
204
+ svg.setAttribute("height", this.height);
183
205
  loadfont.apply(this, []);
184
206
  },
185
207
  render: function() {
@@ -193,21 +215,22 @@ module.exports = {
193
215
 
194
216
  // 準備旋轉函式
195
217
  var rotateFunction;
218
+ rng = lcg(2);
196
219
  switch (this.cfg.rotationMode) {
197
220
  case "none":
198
221
  rotateFunction = function() { return 0; };
199
222
  break;
200
223
  case "fixed":
201
224
  rotateFunction = function() {
202
- return ~~(Math.random() * 2) * that.cfg.rotationStep;
225
+ return ~~(rng() * 2) * that.cfg.rotationStep;
203
226
  };
204
227
  break;
205
228
  case "random":
206
229
  default:
207
230
  rotateFunction = function() {
208
231
  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;
232
+ var randomStep = Math.floor(rng() * steps);
233
+ return (rng() < 0.5 ? 1 : -1) * randomStep * that.cfg.rotationStep;
211
234
  };
212
235
  break;
213
236
  }
@@ -224,23 +247,22 @@ module.exports = {
224
247
  .random(function() { return 0.5; }) // 固定隨機種子讓結果更一致
225
248
  .on("end", function(words) {
226
249
  // 清除舊內容
227
- g.selectAll("*").remove();
228
-
250
+ g.textContent = "";
251
+
229
252
  // 居中
230
- g.attr("transform", "translate(" + that.width/2 + "," + that.height/2 + ")");
231
-
253
+ g.setAttribute("transform", "translate(" + that.width/2 + "," + that.height/2 + ")");
254
+
232
255
  // 繪製文字
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; });
256
+ words.forEach(function(d) {
257
+ var t = g.appendChild(document.createElementNS(svgns, "text"));
258
+ t.__data__ = d;
259
+ t.style.fontSize = d.size + "px";
260
+ t.style.fontFamily = (that.cfg.font.family || {}).name || 'sans-serif';
261
+ t.style.fill = d.color;
262
+ t.setAttribute("text-anchor", "middle");
263
+ t.setAttribute("transform", "translate(" + [d.x, d.y] + ")rotate(" + d.rotate + ")");
264
+ t.textContent = d.text;
265
+ });
244
266
  });
245
267
 
246
268
  // 開始佈局
@@ -250,7 +272,7 @@ module.exports = {
250
272
  if (layout) {
251
273
  layout.stop();
252
274
  }
253
- svg.remove();
275
+ if (svg.parentNode) svg.parentNode.removeChild(svg);
254
276
  }
255
277
  }
256
278
  });
package/package.json CHANGED
@@ -1 +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"}}
1
+ {"name":"@makechart/tagcloud","version":"0.0.2","description":"","scripts":{"test":"mctest","test:serve":"mctest serve -o","test:dev":"mctest -l \"@makechart/base:../base;@plotdb/chart:../../../plotdb/projects/chart;@plotdb/dash:../../../plotdb/projects/dash\"","test:dev:serve":"mctest serve -o -l \"@makechart/base:../base;@plotdb/chart:../../../plotdb/projects/chart;@plotdb/dash:../../../plotdb/projects/dash\""},"keywords":[],"author":"zbryikt","license":"MIT","dependencies":{"@plotdb/srcbuild":"^0.0.69","fedep":"1.6.0","livescript":"^1.6.0"},"devDependencies":{"@makechart/viewer":"^0.0.3"}}