vega-wordcloud
Advanced tools
Comparing version 4.1.2 to 4.1.3
@@ -35,3 +35,2 @@ (function (global, factory) { | ||
*/ | ||
// Word cloud layout by Jason Davies, https://www.jasondavies.com/wordcloud/ | ||
@@ -43,4 +42,3 @@ // Algorithm due to Jonathan Feinberg, http://static.mrfeinberg.com/bv_ch03.pdf | ||
ch = 1 << 11; | ||
function cloud() { | ||
function cloud () { | ||
var size = [256, 256], | ||
@@ -59,3 +57,3 @@ text, | ||
cloud.layout = function() { | ||
cloud.layout = function () { | ||
var contextAndRatio = getContext(vegaCanvas.canvas()), | ||
@@ -68,30 +66,36 @@ board = zeroArray((size[0] >> 5) * size[1]), | ||
data = words.map(d => ({ | ||
text: text(d), | ||
font: font(d), | ||
style: fontStyle(d), | ||
weight: fontWeight(d), | ||
rotate: rotate(d), | ||
size: ~~(fontSize(d) + 1e-14), | ||
padding: padding(d), | ||
xoff: 0, | ||
yoff: 0, | ||
x1: 0, | ||
y1: 0, | ||
x0: 0, | ||
y0: 0, | ||
hasText: false, | ||
sprite: null, | ||
datum: d | ||
})).sort((a, b) => b.size - a.size); | ||
text: text(d), | ||
font: font(d), | ||
style: fontStyle(d), | ||
weight: fontWeight(d), | ||
rotate: rotate(d), | ||
size: ~~(fontSize(d) + 1e-14), | ||
padding: padding(d), | ||
xoff: 0, | ||
yoff: 0, | ||
x1: 0, | ||
y1: 0, | ||
x0: 0, | ||
y0: 0, | ||
hasText: false, | ||
sprite: null, | ||
datum: d | ||
})).sort((a, b) => b.size - a.size); | ||
while (++i < n) { | ||
var d = data[i]; | ||
d.x = (size[0] * (random() + .5)) >> 1; | ||
d.y = (size[1] * (random() + .5)) >> 1; | ||
d.x = size[0] * (random() + .5) >> 1; | ||
d.y = size[1] * (random() + .5) >> 1; | ||
cloudSprite(contextAndRatio, d, data, i); | ||
if (d.hasText && place(board, d, bounds)) { | ||
tags.push(d); | ||
if (bounds) cloudBounds(bounds, d); | ||
else bounds = [{x: d.x + d.x0, y: d.y + d.y0}, {x: d.x + d.x1, y: d.y + d.y1}]; | ||
// Temporary hack | ||
if (bounds) cloudBounds(bounds, d);else bounds = [{ | ||
x: d.x + d.x0, | ||
y: d.y + d.y0 | ||
}, { | ||
x: d.x + d.x1, | ||
y: d.y + d.y1 | ||
}]; // Temporary hack | ||
d.x -= size[0] >> 1; | ||
@@ -110,8 +114,9 @@ d.y -= size[1] >> 1; | ||
canvas.height = ch / ratio; | ||
var context = canvas.getContext('2d'); | ||
context.fillStyle = context.strokeStyle = 'red'; | ||
context.textAlign = 'center'; | ||
return {context: context, ratio: ratio}; | ||
return { | ||
context: context, | ||
ratio: ratio | ||
}; | ||
} | ||
@@ -133,11 +138,7 @@ | ||
dy = ~~dxdy[1]; | ||
if (Math.min(Math.abs(dx), Math.abs(dy)) >= maxDelta) break; | ||
tag.x = startX + dx; | ||
tag.y = startY + dy; | ||
if (tag.x + tag.x0 < 0 || tag.y + tag.y0 < 0 || tag.x + tag.x1 > size[0] || tag.y + tag.y1 > size[1]) continue; // TODO only check for collisions within current bounds. | ||
if (tag.x + tag.x0 < 0 || tag.y + tag.y0 < 0 || | ||
tag.x + tag.x1 > size[0] || tag.y + tag.y1 > size[1]) continue; | ||
// TODO only check for collisions within current bounds. | ||
if (!bounds || !cloudCollide(tag, board, size[0])) { | ||
@@ -154,9 +155,13 @@ if (!bounds || collideRects(tag, bounds)) { | ||
last; | ||
for (var j = 0; j < h; j++) { | ||
last = 0; | ||
for (var i = 0; i <= w; i++) { | ||
board[x + i] |= (last << msx) | (i < w ? (last = sprite[j * w + i]) >>> sx : 0); | ||
board[x + i] |= last << msx | (i < w ? (last = sprite[j * w + i]) >>> sx : 0); | ||
} | ||
x += sw; | ||
} | ||
tag.sprite = null; | ||
@@ -167,6 +172,7 @@ return true; | ||
} | ||
return false; | ||
} | ||
cloud.words = function(_) { | ||
cloud.words = function (_) { | ||
if (arguments.length) { | ||
@@ -180,3 +186,3 @@ words = _; | ||
cloud.size = function(_) { | ||
cloud.size = function (_) { | ||
if (arguments.length) { | ||
@@ -190,3 +196,3 @@ size = [+_[0], +_[1]]; | ||
cloud.font = function(_) { | ||
cloud.font = function (_) { | ||
if (arguments.length) { | ||
@@ -200,3 +206,3 @@ font = functor(_); | ||
cloud.fontStyle = function(_) { | ||
cloud.fontStyle = function (_) { | ||
if (arguments.length) { | ||
@@ -210,3 +216,3 @@ fontStyle = functor(_); | ||
cloud.fontWeight = function(_) { | ||
cloud.fontWeight = function (_) { | ||
if (arguments.length) { | ||
@@ -220,3 +226,3 @@ fontWeight = functor(_); | ||
cloud.rotate = function(_) { | ||
cloud.rotate = function (_) { | ||
if (arguments.length) { | ||
@@ -230,3 +236,3 @@ rotate = functor(_); | ||
cloud.text = function(_) { | ||
cloud.text = function (_) { | ||
if (arguments.length) { | ||
@@ -240,3 +246,3 @@ text = functor(_); | ||
cloud.spiral = function(_) { | ||
cloud.spiral = function (_) { | ||
if (arguments.length) { | ||
@@ -250,3 +256,3 @@ spiral = spirals[_] || _; | ||
cloud.fontSize = function(_) { | ||
cloud.fontSize = function (_) { | ||
if (arguments.length) { | ||
@@ -260,3 +266,3 @@ fontSize = functor(_); | ||
cloud.padding = function(_) { | ||
cloud.padding = function (_) { | ||
if (arguments.length) { | ||
@@ -270,3 +276,3 @@ padding = functor(_); | ||
cloud.random = function(_) { | ||
cloud.random = function (_) { | ||
if (arguments.length) { | ||
@@ -281,6 +287,5 @@ random = _; | ||
return cloud; | ||
} | ||
} // Fetches a monochrome sprite bitmap for the specified text. | ||
// Load in batches for speed. | ||
// Fetches a monochrome sprite bitmap for the specified text. | ||
// Load in batches for speed. | ||
function cloudSprite(contextAndRatio, d, data, di) { | ||
@@ -290,3 +295,2 @@ if (d.sprite) return; | ||
ratio = contextAndRatio.ratio; | ||
c.clearRect(0, 0, (cw << 5) / ratio, ch / ratio); | ||
@@ -297,4 +301,9 @@ var x = 0, | ||
n = data.length, | ||
w, w32, h, i, j; | ||
w, | ||
w32, | ||
h, | ||
i, | ||
j; | ||
--di; | ||
while (++di < n) { | ||
@@ -306,2 +315,3 @@ d = data[di]; | ||
h = d.size << 1; | ||
if (d.rotate) { | ||
@@ -314,9 +324,11 @@ var sr = Math.sin(d.rotate * cloudRadians), | ||
hsr = h * sr; | ||
w = (Math.max(Math.abs(wcr + hsr), Math.abs(wcr - hsr)) + 0x1f) >> 5 << 5; | ||
w = Math.max(Math.abs(wcr + hsr), Math.abs(wcr - hsr)) + 0x1f >> 5 << 5; | ||
h = ~~Math.max(Math.abs(wsr + hcr), Math.abs(wsr - hcr)); | ||
} else { | ||
w = (w + 0x1f) >> 5 << 5; | ||
w = w + 0x1f >> 5 << 5; | ||
} | ||
if (h > maxh) maxh = h; | ||
if (x + w >= (cw << 5)) { | ||
if (x + w >= cw << 5) { | ||
x = 0; | ||
@@ -326,2 +338,3 @@ y += maxh; | ||
} | ||
if (y + h >= ch) break; | ||
@@ -331,2 +344,3 @@ c.translate((x + (w >> 1)) / ratio, (y + (h >> 1)) / ratio); | ||
c.fillText(d.text, 0, 0); | ||
if (d.padding) { | ||
@@ -336,2 +350,3 @@ c.lineWidth = 2 * d.padding; | ||
} | ||
c.restore(); | ||
@@ -349,4 +364,6 @@ d.width = w; | ||
} | ||
var pixels = c.getImageData(0, 0, (cw << 5) / ratio, ch / ratio).data, | ||
sprite = []; | ||
while (--di >= 0) { | ||
@@ -357,5 +374,6 @@ d = data[di]; | ||
w32 = w >> 5; | ||
h = d.y1 - d.y0; | ||
// Zero the buffer | ||
h = d.y1 - d.y0; // Zero the buffer | ||
for (i = 0; i < h * w32; i++) sprite[i] = 0; | ||
x = d.xoff; | ||
@@ -366,11 +384,12 @@ if (x == null) return; | ||
seenRow = -1; | ||
for (j = 0; j < h; j++) { | ||
for (i = 0; i < w; i++) { | ||
var k = w32 * j + (i >> 5), | ||
m = pixels[((y + j) * (cw << 5) + (x + i)) << 2] ? 1 << (31 - (i % 32)) : 0; | ||
m = pixels[(y + j) * (cw << 5) + (x + i) << 2] ? 1 << 31 - i % 32 : 0; | ||
sprite[k] |= m; | ||
seen |= m; | ||
} | ||
if (seen) seenRow = j; | ||
else { | ||
if (seen) seenRow = j;else { | ||
d.y0++; | ||
@@ -382,8 +401,9 @@ h--; | ||
} | ||
d.y1 = d.y0 + seenRow; | ||
d.sprite = sprite.slice(0, (d.y1 - d.y0) * w32); | ||
} | ||
} | ||
} // Use mask-based collision detection. | ||
// Use mask-based collision detection. | ||
function cloudCollide(tag, board, sw) { | ||
@@ -399,10 +419,13 @@ sw >>= 5; | ||
last; | ||
for (var j = 0; j < h; j++) { | ||
last = 0; | ||
for (var i = 0; i <= w; i++) { | ||
if (((last << msx) | (i < w ? (last = sprite[j * w + i]) >>> sx : 0)) | ||
& board[x + i]) return true; | ||
if ((last << msx | (i < w ? (last = sprite[j * w + i]) >>> sx : 0)) & board[x + i]) return true; | ||
} | ||
x += sw; | ||
} | ||
return false; | ||
@@ -426,3 +449,3 @@ } | ||
var e = size[0] / size[1]; | ||
return function(t) { | ||
return function (t) { | ||
return [e * (t *= .1) * Math.cos(t), t * Math.sin(t)]; | ||
@@ -437,20 +460,34 @@ }; | ||
y = 0; | ||
return function(t) { | ||
var sign = t < 0 ? -1 : 1; | ||
// See triangular numbers: T_n = n * (n + 1) / 2. | ||
switch ((Math.sqrt(1 + 4 * sign * t) - sign) & 3) { | ||
case 0: x += dx; break; | ||
case 1: y += dy; break; | ||
case 2: x -= dx; break; | ||
default: y -= dy; break; | ||
return function (t) { | ||
var sign = t < 0 ? -1 : 1; // See triangular numbers: T_n = n * (n + 1) / 2. | ||
switch (Math.sqrt(1 + 4 * sign * t) - sign & 3) { | ||
case 0: | ||
x += dx; | ||
break; | ||
case 1: | ||
y += dy; | ||
break; | ||
case 2: | ||
x -= dx; | ||
break; | ||
default: | ||
y -= dy; | ||
break; | ||
} | ||
return [x, y]; | ||
}; | ||
} | ||
} // TODO reuse arrays? | ||
// TODO reuse arrays? | ||
function zeroArray(n) { | ||
var a = [], | ||
i = -1; | ||
while (++i < n) a[i] = 0; | ||
return a; | ||
@@ -460,3 +497,5 @@ } | ||
function functor(d) { | ||
return typeof d === 'function' ? d : function() { return d; }; | ||
return typeof d === 'function' ? d : function () { | ||
return d; | ||
}; | ||
} | ||
@@ -469,28 +508,66 @@ | ||
var Output = ['x', 'y', 'font', 'fontSize', 'fontStyle', 'fontWeight', 'angle']; | ||
var Params = ['text', 'font', 'rotate', 'fontSize', 'fontStyle', 'fontWeight']; | ||
const Output = ['x', 'y', 'font', 'fontSize', 'fontStyle', 'fontWeight', 'angle']; | ||
const Params = ['text', 'font', 'rotate', 'fontSize', 'fontStyle', 'fontWeight']; | ||
function Wordcloud(params) { | ||
vegaDataflow.Transform.call(this, cloud(), params); | ||
} | ||
Wordcloud.Definition = { | ||
'type': 'Wordcloud', | ||
'metadata': {'modifies': true}, | ||
'params': [ | ||
{ 'name': 'size', 'type': 'number', 'array': true, 'length': 2 }, | ||
{ 'name': 'font', 'type': 'string', 'expr': true, 'default': 'sans-serif' }, | ||
{ 'name': 'fontStyle', 'type': 'string', 'expr': true, 'default': 'normal' }, | ||
{ 'name': 'fontWeight', 'type': 'string', 'expr': true, 'default': 'normal' }, | ||
{ 'name': 'fontSize', 'type': 'number', 'expr': true, 'default': 14 }, | ||
{ 'name': 'fontSizeRange', 'type': 'number', 'array': 'nullable', 'default': [10, 50] }, | ||
{ 'name': 'rotate', 'type': 'number', 'expr': true, 'default': 0 }, | ||
{ 'name': 'text', 'type': 'field' }, | ||
{ 'name': 'spiral', 'type': 'string', 'values': ['archimedean', 'rectangular'] }, | ||
{ 'name': 'padding', 'type': 'number', 'expr': true }, | ||
{ 'name': 'as', 'type': 'string', 'array': true, 'length': 7, 'default': Output } | ||
] | ||
'metadata': { | ||
'modifies': true | ||
}, | ||
'params': [{ | ||
'name': 'size', | ||
'type': 'number', | ||
'array': true, | ||
'length': 2 | ||
}, { | ||
'name': 'font', | ||
'type': 'string', | ||
'expr': true, | ||
'default': 'sans-serif' | ||
}, { | ||
'name': 'fontStyle', | ||
'type': 'string', | ||
'expr': true, | ||
'default': 'normal' | ||
}, { | ||
'name': 'fontWeight', | ||
'type': 'string', | ||
'expr': true, | ||
'default': 'normal' | ||
}, { | ||
'name': 'fontSize', | ||
'type': 'number', | ||
'expr': true, | ||
'default': 14 | ||
}, { | ||
'name': 'fontSizeRange', | ||
'type': 'number', | ||
'array': 'nullable', | ||
'default': [10, 50] | ||
}, { | ||
'name': 'rotate', | ||
'type': 'number', | ||
'expr': true, | ||
'default': 0 | ||
}, { | ||
'name': 'text', | ||
'type': 'field' | ||
}, { | ||
'name': 'spiral', | ||
'type': 'string', | ||
'values': ['archimedean', 'rectangular'] | ||
}, { | ||
'name': 'padding', | ||
'type': 'number', | ||
'expr': true | ||
}, { | ||
'name': 'as', | ||
'type': 'string', | ||
'array': true, | ||
'length': 7, | ||
'default': Output | ||
}] | ||
}; | ||
vegaUtil.inherits(Wordcloud, vegaDataflow.Transform, { | ||
@@ -503,3 +580,3 @@ transform(_, pulse) { | ||
function modp(param) { | ||
var p = _[param]; | ||
const p = _[param]; | ||
return vegaUtil.isFunction(p) && pulse.modified(p.fields); | ||
@@ -509,21 +586,15 @@ } | ||
const mod = _.modified(); | ||
if (!(mod || pulse.changed(pulse.ADD_REM) || Params.some(modp))) return; | ||
const data = pulse.materialize(pulse.SOURCE).source, | ||
layout = this.value, | ||
as = _.as || Output; | ||
let fontSize = _.fontSize || 14, | ||
range; | ||
vegaUtil.isFunction(fontSize) ? range = _.fontSizeRange : fontSize = vegaUtil.constant(fontSize); // create font size scaling function as needed | ||
vegaUtil.isFunction(fontSize) | ||
? (range = _.fontSizeRange) | ||
: (fontSize = vegaUtil.constant(fontSize)); | ||
// create font size scaling function as needed | ||
if (range) { | ||
const fsize = fontSize, | ||
sizeScale = vegaScale.scale('sqrt')() | ||
.domain(vegaUtil.extent(data, fsize)) | ||
.range(range); | ||
sizeScale = vegaScale.scale('sqrt')().domain(vegaUtil.extent(data, fsize)).range(range); | ||
fontSize = x => sizeScale(fsize(x)); | ||
@@ -536,25 +607,11 @@ } | ||
t[as[3]] = 0; | ||
}); | ||
}); // configure layout | ||
// configure layout | ||
const words = layout | ||
.words(data) | ||
.text(_.text) | ||
.size(_.size || [500, 500]) | ||
.padding(_.padding || 1) | ||
.spiral(_.spiral || 'archimedean') | ||
.rotate(_.rotate || 0) | ||
.font(_.font || 'sans-serif') | ||
.fontStyle(_.fontStyle || 'normal') | ||
.fontWeight(_.fontWeight || 'normal') | ||
.fontSize(fontSize) | ||
.random(vegaStatistics.random) | ||
.layout(); | ||
const words = layout.words(data).text(_.text).size(_.size || [500, 500]).padding(_.padding || 1).spiral(_.spiral || 'archimedean').rotate(_.rotate || 0).font(_.font || 'sans-serif').fontStyle(_.fontStyle || 'normal').fontWeight(_.fontWeight || 'normal').fontSize(fontSize).random(vegaStatistics.random).layout(); | ||
const size = layout.size(), | ||
dx = size[0] >> 1, | ||
dy = size[1] >> 1, | ||
n = words.length; | ||
dx = size[0] >> 1, | ||
dy = size[1] >> 1, | ||
n = words.length; | ||
for (let i = 0, w, t; i<n; ++i) { | ||
for (let i = 0, w, t; i < n; ++i) { | ||
w = words[i]; | ||
@@ -573,2 +630,3 @@ t = w.datum; | ||
} | ||
}); | ||
@@ -575,0 +633,0 @@ |
@@ -1,1 +0,2 @@ | ||
!function(t,e){"object"==typeof exports&&"undefined"!=typeof module?e(exports,require("vega-canvas"),require("vega-dataflow"),require("vega-util"),require("vega-scale"),require("vega-statistics")):"function"==typeof define&&define.amd?define(["exports","vega-canvas","vega-dataflow","vega-util","vega-scale","vega-statistics"],e):e(((t="undefined"!=typeof globalThis?globalThis:t||self).vega=t.vega||{},t.vega.transforms={}),t.vega,t.vega,t.vega,t.vega,t.vega)}(this,(function(t,e,n,r,a,i){"use strict";var o=Math.PI/180,f=2048;function s(){var t,n,r,a,i,o,s,h=[256,256],c=x,m=[],v=Math.random,p={};function z(t,e,n){for(var r,a,i,o,f,s=e.x,u=e.y,y=Math.sqrt(h[0]*h[0]+h[1]*h[1]),x=c(h),d=v()<.5?1:-1,g=-d;(r=x(g+=d))&&(a=~~r[0],i=~~r[1],!(Math.min(Math.abs(a),Math.abs(i))>=y));)if(e.x=s+a,e.y=u+i,!(e.x+e.x0<0||e.y+e.y0<0||e.x+e.x1>h[0]||e.y+e.y1>h[1]||n&&l(e,t,h[0])||n&&(f=n,!((o=e).x+o.x1>f[0].x&&o.x+o.x0<f[1].x&&o.y+o.y1>f[0].y&&o.y+o.y0<f[1].y)))){for(var m,p=e.sprite,z=e.width>>5,b=h[0]>>5,M=e.x-(z<<4),w=127&M,S=32-w,T=e.y1-e.y0,q=(e.y+e.y0)*b+(M>>5),W=0;W<T;W++){m=0;for(var k=0;k<=z;k++)t[q+k]|=m<<S|(k<z?(m=p[W*z+k])>>>w:0);q+=b}return e.sprite=null,!0}return!1}return p.layout=function(){for(var l=function(t){t.width=t.height=1;var e=Math.sqrt(t.getContext("2d").getImageData(0,0,1,1).data.length>>2);t.width=2048/e,t.height=f/e;var n=t.getContext("2d");return n.fillStyle=n.strokeStyle="red",n.textAlign="center",{context:n,ratio:e}}(e.canvas()),x=function(t){var e=[],n=-1;for(;++n<t;)e[n]=0;return e}((h[0]>>5)*h[1]),d=null,g=m.length,c=-1,p=[],b=m.map(e=>({text:t(e),font:n(e),style:a(e),weight:i(e),rotate:o(e),size:~~(r(e)+1e-14),padding:s(e),xoff:0,yoff:0,x1:0,y1:0,x0:0,y0:0,hasText:!1,sprite:null,datum:e})).sort((t,e)=>e.size-t.size);++c<g;){var M=b[c];M.x=h[0]*(v()+.5)>>1,M.y=h[1]*(v()+.5)>>1,u(l,M,b,c),M.hasText&&z(x,M,d)&&(p.push(M),d?y(d,M):d=[{x:M.x+M.x0,y:M.y+M.y0},{x:M.x+M.x1,y:M.y+M.y1}],M.x-=h[0]>>1,M.y-=h[1]>>1)}return p},p.words=function(t){return arguments.length?(m=t,p):m},p.size=function(t){return arguments.length?(h=[+t[0],+t[1]],p):h},p.font=function(t){return arguments.length?(n=d(t),p):n},p.fontStyle=function(t){return arguments.length?(a=d(t),p):a},p.fontWeight=function(t){return arguments.length?(i=d(t),p):i},p.rotate=function(t){return arguments.length?(o=d(t),p):o},p.text=function(e){return arguments.length?(t=d(e),p):t},p.spiral=function(t){return arguments.length?(c=g[t]||t,p):c},p.fontSize=function(t){return arguments.length?(r=d(t),p):r},p.padding=function(t){return arguments.length?(s=d(t),p):s},p.random=function(t){return arguments.length?(v=t,p):v},p}function u(t,e,n,r){if(!e.sprite){var a=t.context,i=t.ratio;a.clearRect(0,0,2048/i,f/i);var s,u,l,y,x,d=0,g=0,h=0,c=n.length;for(--r;++r<c;){if(e=n[r],a.save(),a.font=e.style+" "+e.weight+" "+~~((e.size+1)/i)+"px "+e.font,s=a.measureText(e.text+"m").width*i,l=e.size<<1,e.rotate){var m=Math.sin(e.rotate*o),v=Math.cos(e.rotate*o),p=s*v,z=s*m,b=l*v,M=l*m;s=Math.max(Math.abs(p+M),Math.abs(p-M))+31>>5<<5,l=~~Math.max(Math.abs(z+b),Math.abs(z-b))}else s=s+31>>5<<5;if(l>h&&(h=l),d+s>=2048&&(d=0,g+=h,h=0),g+l>=f)break;a.translate((d+(s>>1))/i,(g+(l>>1))/i),e.rotate&&a.rotate(e.rotate*o),a.fillText(e.text,0,0),e.padding&&(a.lineWidth=2*e.padding,a.strokeText(e.text,0,0)),a.restore(),e.width=s,e.height=l,e.xoff=d,e.yoff=g,e.x1=s>>1,e.y1=l>>1,e.x0=-e.x1,e.y0=-e.y1,e.hasText=!0,d+=s}for(var w=a.getImageData(0,0,2048/i,f/i).data,S=[];--r>=0;)if((e=n[r]).hasText){for(u=(s=e.width)>>5,l=e.y1-e.y0,y=0;y<l*u;y++)S[y]=0;if(null==(d=e.xoff))return;g=e.yoff;var T=0,q=-1;for(x=0;x<l;x++){for(y=0;y<s;y++){var W=u*x+(y>>5),k=w[2048*(g+x)+(d+y)<<2]?1<<31-y%32:0;S[W]|=k,T|=k}T?q=x:(e.y0++,l--,x--,g++)}e.y1=e.y0+q,e.sprite=S.slice(0,(e.y1-e.y0)*u)}}}function l(t,e,n){n>>=5;for(var r,a=t.sprite,i=t.width>>5,o=t.x-(i<<4),f=127&o,s=32-f,u=t.y1-t.y0,l=(t.y+t.y0)*n+(o>>5),y=0;y<u;y++){r=0;for(var x=0;x<=i;x++)if((r<<s|(x<i?(r=a[y*i+x])>>>f:0))&e[l+x])return!0;l+=n}return!1}function y(t,e){var n=t[0],r=t[1];e.x+e.x0<n.x&&(n.x=e.x+e.x0),e.y+e.y0<n.y&&(n.y=e.y+e.y0),e.x+e.x1>r.x&&(r.x=e.x+e.x1),e.y+e.y1>r.y&&(r.y=e.y+e.y1)}function x(t){var e=t[0]/t[1];return function(t){return[e*(t*=.1)*Math.cos(t),t*Math.sin(t)]}}function d(t){return"function"==typeof t?t:function(){return t}}var g={archimedean:x,rectangular:function(t){var e=4*t[0]/t[1],n=0,r=0;return function(t){var a=t<0?-1:1;switch(Math.sqrt(1+4*a*t)-a&3){case 0:n+=e;break;case 1:r+=4;break;case 2:n-=e;break;default:r-=4}return[n,r]}}},h=["x","y","font","fontSize","fontStyle","fontWeight","angle"],c=["text","font","rotate","fontSize","fontStyle","fontWeight"];function m(t){n.Transform.call(this,s(),t)}m.Definition={type:"Wordcloud",metadata:{modifies:!0},params:[{name:"size",type:"number",array:!0,length:2},{name:"font",type:"string",expr:!0,default:"sans-serif"},{name:"fontStyle",type:"string",expr:!0,default:"normal"},{name:"fontWeight",type:"string",expr:!0,default:"normal"},{name:"fontSize",type:"number",expr:!0,default:14},{name:"fontSizeRange",type:"number",array:"nullable",default:[10,50]},{name:"rotate",type:"number",expr:!0,default:0},{name:"text",type:"field"},{name:"spiral",type:"string",values:["archimedean","rectangular"]},{name:"padding",type:"number",expr:!0},{name:"as",type:"string",array:!0,length:7,default:h}]},r.inherits(m,n.Transform,{transform(t,e){!t.size||t.size[0]&&t.size[1]||r.error("Wordcloud size dimensions must be non-zero.");const n=t.modified();if(!(n||e.changed(e.ADD_REM)||c.some((function(n){var a=t[n];return r.isFunction(a)&&e.modified(a.fields)}))))return;const o=e.materialize(e.SOURCE).source,f=this.value,s=t.as||h;let u,l=t.fontSize||14;if(r.isFunction(l)?u=t.fontSizeRange:l=r.constant(l),u){const t=l,e=a.scale("sqrt")().domain(r.extent(o,t)).range(u);l=n=>e(t(n))}o.forEach(t=>{t[s[0]]=NaN,t[s[1]]=NaN,t[s[3]]=0});const y=f.words(o).text(t.text).size(t.size||[500,500]).padding(t.padding||1).spiral(t.spiral||"archimedean").rotate(t.rotate||0).font(t.font||"sans-serif").fontStyle(t.fontStyle||"normal").fontWeight(t.fontWeight||"normal").fontSize(l).random(i.random).layout(),x=f.size(),d=x[0]>>1,g=x[1]>>1,m=y.length;for(let t,e,n=0;n<m;++n)t=y[n],e=t.datum,e[s[0]]=t.x+d,e[s[1]]=t.y+g,e[s[2]]=t.font,e[s[3]]=t.size,e[s[4]]=t.style,e[s[5]]=t.weight,e[s[6]]=t.rotate;return e.reflow(n).modifies(s)}}),t.wordcloud=m,Object.defineProperty(t,"__esModule",{value:!0})})); | ||
this.vega=this.vega||{},this.vega.transforms=function(t,e,n,r,a,i){"use strict";var o=Math.PI/180,f=2048;function s(){var t,n,r,a,i,o,s,d=[256,256],g=h,m=[],p=Math.random,v={};function z(t,e,n){for(var r,a,i,o,f,s=e.x,u=e.y,l=Math.sqrt(d[0]*d[0]+d[1]*d[1]),h=g(d),x=p()<.5?1:-1,c=-x;(r=h(c+=x))&&(a=~~r[0],i=~~r[1],!(Math.min(Math.abs(a),Math.abs(i))>=l));)if(e.x=s+a,e.y=u+i,!(e.x+e.x0<0||e.y+e.y0<0||e.x+e.x1>d[0]||e.y+e.y1>d[1]||n&&y(e,t,d[0])||n&&(f=n,!((o=e).x+o.x1>f[0].x&&o.x+o.x0<f[1].x&&o.y+o.y1>f[0].y&&o.y+o.y0<f[1].y)))){for(var m,v=e.sprite,z=e.width>>5,M=d[0]>>5,b=e.x-(z<<4),S=127&b,w=32-S,T=e.y1-e.y0,W=(e.y+e.y0)*M+(b>>5),k=0;k<T;k++){m=0;for(var D=0;D<=z;D++)t[W+D]|=m<<w|(D<z?(m=v[k*z+D])>>>S:0);W+=M}return e.sprite=null,!0}return!1}return v.layout=function(){for(var y=function(t){t.width=t.height=1;var e=Math.sqrt(t.getContext("2d").getImageData(0,0,1,1).data.length>>2);t.width=2048/e,t.height=f/e;var n=t.getContext("2d");return n.fillStyle=n.strokeStyle="red",n.textAlign="center",{context:n,ratio:e}}(e.canvas()),h=function(t){var e=[],n=-1;for(;++n<t;)e[n]=0;return e}((d[0]>>5)*d[1]),x=null,c=m.length,g=-1,v=[],M=m.map(e=>({text:t(e),font:n(e),style:a(e),weight:i(e),rotate:o(e),size:~~(r(e)+1e-14),padding:s(e),xoff:0,yoff:0,x1:0,y1:0,x0:0,y0:0,hasText:!1,sprite:null,datum:e})).sort((t,e)=>e.size-t.size);++g<c;){var b=M[g];b.x=d[0]*(p()+.5)>>1,b.y=d[1]*(p()+.5)>>1,u(y,b,M,g),b.hasText&&z(h,b,x)&&(v.push(b),x?l(x,b):x=[{x:b.x+b.x0,y:b.y+b.y0},{x:b.x+b.x1,y:b.y+b.y1}],b.x-=d[0]>>1,b.y-=d[1]>>1)}return v},v.words=function(t){return arguments.length?(m=t,v):m},v.size=function(t){return arguments.length?(d=[+t[0],+t[1]],v):d},v.font=function(t){return arguments.length?(n=x(t),v):n},v.fontStyle=function(t){return arguments.length?(a=x(t),v):a},v.fontWeight=function(t){return arguments.length?(i=x(t),v):i},v.rotate=function(t){return arguments.length?(o=x(t),v):o},v.text=function(e){return arguments.length?(t=x(e),v):t},v.spiral=function(t){return arguments.length?(g=c[t]||t,v):g},v.fontSize=function(t){return arguments.length?(r=x(t),v):r},v.padding=function(t){return arguments.length?(s=x(t),v):s},v.random=function(t){return arguments.length?(p=t,v):p},v}function u(t,e,n,r){if(!e.sprite){var a=t.context,i=t.ratio;a.clearRect(0,0,2048/i,f/i);var s,u,y,l,h,x=0,c=0,d=0,g=n.length;for(--r;++r<g;){if(e=n[r],a.save(),a.font=e.style+" "+e.weight+" "+~~((e.size+1)/i)+"px "+e.font,s=a.measureText(e.text+"m").width*i,y=e.size<<1,e.rotate){var m=Math.sin(e.rotate*o),p=Math.cos(e.rotate*o),v=s*p,z=s*m,M=y*p,b=y*m;s=Math.max(Math.abs(v+b),Math.abs(v-b))+31>>5<<5,y=~~Math.max(Math.abs(z+M),Math.abs(z-M))}else s=s+31>>5<<5;if(y>d&&(d=y),x+s>=2048&&(x=0,c+=d,d=0),c+y>=f)break;a.translate((x+(s>>1))/i,(c+(y>>1))/i),e.rotate&&a.rotate(e.rotate*o),a.fillText(e.text,0,0),e.padding&&(a.lineWidth=2*e.padding,a.strokeText(e.text,0,0)),a.restore(),e.width=s,e.height=y,e.xoff=x,e.yoff=c,e.x1=s>>1,e.y1=y>>1,e.x0=-e.x1,e.y0=-e.y1,e.hasText=!0,x+=s}for(var S=a.getImageData(0,0,2048/i,f/i).data,w=[];--r>=0;)if((e=n[r]).hasText){for(u=(s=e.width)>>5,y=e.y1-e.y0,l=0;l<y*u;l++)w[l]=0;if(null==(x=e.xoff))return;c=e.yoff;var T=0,W=-1;for(h=0;h<y;h++){for(l=0;l<s;l++){var k=u*h+(l>>5),D=S[2048*(c+h)+(x+l)<<2]?1<<31-l%32:0;w[k]|=D,T|=D}T?W=h:(e.y0++,y--,h--,c++)}e.y1=e.y0+W,e.sprite=w.slice(0,(e.y1-e.y0)*u)}}}function y(t,e,n){n>>=5;for(var r,a=t.sprite,i=t.width>>5,o=t.x-(i<<4),f=127&o,s=32-f,u=t.y1-t.y0,y=(t.y+t.y0)*n+(o>>5),l=0;l<u;l++){r=0;for(var h=0;h<=i;h++)if((r<<s|(h<i?(r=a[l*i+h])>>>f:0))&e[y+h])return!0;y+=n}return!1}function l(t,e){var n=t[0],r=t[1];e.x+e.x0<n.x&&(n.x=e.x+e.x0),e.y+e.y0<n.y&&(n.y=e.y+e.y0),e.x+e.x1>r.x&&(r.x=e.x+e.x1),e.y+e.y1>r.y&&(r.y=e.y+e.y1)}function h(t){var e=t[0]/t[1];return function(t){return[e*(t*=.1)*Math.cos(t),t*Math.sin(t)]}}function x(t){return"function"==typeof t?t:function(){return t}}var c={archimedean:h,rectangular:function(t){var e=4*t[0]/t[1],n=0,r=0;return function(t){var a=t<0?-1:1;switch(Math.sqrt(1+4*a*t)-a&3){case 0:n+=e;break;case 1:r+=4;break;case 2:n-=e;break;default:r-=4}return[n,r]}}};const d=["x","y","font","fontSize","fontStyle","fontWeight","angle"],g=["text","font","rotate","fontSize","fontStyle","fontWeight"];function m(t){n.Transform.call(this,s(),t)}return m.Definition={type:"Wordcloud",metadata:{modifies:!0},params:[{name:"size",type:"number",array:!0,length:2},{name:"font",type:"string",expr:!0,default:"sans-serif"},{name:"fontStyle",type:"string",expr:!0,default:"normal"},{name:"fontWeight",type:"string",expr:!0,default:"normal"},{name:"fontSize",type:"number",expr:!0,default:14},{name:"fontSizeRange",type:"number",array:"nullable",default:[10,50]},{name:"rotate",type:"number",expr:!0,default:0},{name:"text",type:"field"},{name:"spiral",type:"string",values:["archimedean","rectangular"]},{name:"padding",type:"number",expr:!0},{name:"as",type:"string",array:!0,length:7,default:d}]},r.inherits(m,n.Transform,{transform(t,e){!t.size||t.size[0]&&t.size[1]||r.error("Wordcloud size dimensions must be non-zero.");const n=t.modified();if(!(n||e.changed(e.ADD_REM)||g.some((function(n){const a=t[n];return r.isFunction(a)&&e.modified(a.fields)}))))return;const o=e.materialize(e.SOURCE).source,f=this.value,s=t.as||d;let u,y=t.fontSize||14;if(r.isFunction(y)?u=t.fontSizeRange:y=r.constant(y),u){const t=y,e=a.scale("sqrt")().domain(r.extent(o,t)).range(u);y=n=>e(t(n))}o.forEach(t=>{t[s[0]]=NaN,t[s[1]]=NaN,t[s[3]]=0});const l=f.words(o).text(t.text).size(t.size||[500,500]).padding(t.padding||1).spiral(t.spiral||"archimedean").rotate(t.rotate||0).font(t.font||"sans-serif").fontStyle(t.fontStyle||"normal").fontWeight(t.fontWeight||"normal").fontSize(y).random(i.random).layout(),h=f.size(),x=h[0]>>1,c=h[1]>>1,m=l.length;for(let t,e,n=0;n<m;++n)t=l[n],e=t.datum,e[s[0]]=t.x+x,e[s[1]]=t.y+c,e[s[2]]=t.font,e[s[3]]=t.size,e[s[4]]=t.style,e[s[5]]=t.weight,e[s[6]]=t.rotate;return e.reflow(n).modifies(s)}}),t.wordcloud=m,t}({},vega,vega,vega,vega,vega); | ||
//# sourceMappingURL=vega-wordcloud.min.js.map |
{ | ||
"name": "vega-wordcloud", | ||
"version": "4.1.2", | ||
"version": "4.1.3", | ||
"description": "Wordcloud layout transform for Vega dataflows.", | ||
@@ -13,20 +13,18 @@ "keywords": [ | ||
"main": "build/vega-wordcloud.js", | ||
"module": "index", | ||
"module": "build/vega-wordcloud.module.js", | ||
"unpkg": "build/vega-wordcloud.min.js", | ||
"repository": "vega/vega", | ||
"scripts": { | ||
"rollup": "rollup -g vega-canvas:vega,vega-dataflow:vega,vega-scale:vega,vega-statistics:vega,vega-util:vega -f umd -n vega.transforms -o build/vega-wordcloud.js -- index.js", | ||
"prebuild": "rimraf build && mkdir build", | ||
"build": "yarn rollup", | ||
"postbuild": "terser build/vega-wordcloud.js -c -m -o build/vega-wordcloud.min.js", | ||
"pretest": "yarn prebuild && yarn rollup", | ||
"prebuild": "rimraf build", | ||
"build": "rollup -c --config-transform", | ||
"pretest": "yarn build --config-test", | ||
"test": "tape 'test/**/*-test.js'", | ||
"prepublishOnly": "yarn test && yarn build", | ||
"postpublish": "git push && git push --tags" | ||
"prepublishOnly": "yarn test && yarn build" | ||
}, | ||
"dependencies": { | ||
"vega-canvas": "^1.2.4", | ||
"vega-dataflow": "^5.7.2", | ||
"vega-scale": "^7.1.0", | ||
"vega-statistics": "^1.7.8", | ||
"vega-util": "^1.15.1" | ||
"vega-canvas": "^1.2.5", | ||
"vega-dataflow": "^5.7.3", | ||
"vega-scale": "^7.1.1", | ||
"vega-statistics": "^1.7.9", | ||
"vega-util": "^1.15.2" | ||
}, | ||
@@ -36,3 +34,3 @@ "devDependencies": { | ||
}, | ||
"gitHead": "8d6793f4ca7eaaf2d22186764e9ce2dae687cf52" | ||
"gitHead": "4affcbedb9d14815dbb6d3b250ed231b54fc95c0" | ||
} |
@@ -7,5 +7,5 @@ import cloud from './CloudLayout'; | ||
var Output = ['x', 'y', 'font', 'fontSize', 'fontStyle', 'fontWeight', 'angle']; | ||
const Output = ['x', 'y', 'font', 'fontSize', 'fontStyle', 'fontWeight', 'angle']; | ||
var Params = ['text', 'font', 'rotate', 'fontSize', 'fontStyle', 'fontWeight']; | ||
const Params = ['text', 'font', 'rotate', 'fontSize', 'fontStyle', 'fontWeight']; | ||
@@ -41,3 +41,3 @@ export default function Wordcloud(params) { | ||
function modp(param) { | ||
var p = _[param]; | ||
const p = _[param]; | ||
return isFunction(p) && pulse.modified(p.fields); | ||
@@ -109,2 +109,2 @@ } | ||
} | ||
}); | ||
}); |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Minified code
QualityThis package contains minified code. This may be harmless in some cases where minified code is included in packaged libraries, however packages on npm should not minify code.
Found 1 instance in 1 package
78228
11
1536
1
Updatedvega-canvas@^1.2.5
Updatedvega-dataflow@^5.7.3
Updatedvega-scale@^7.1.1
Updatedvega-statistics@^1.7.9
Updatedvega-util@^1.15.2