vega-wordcloud
Advanced tools
Comparing version 3.0.0 to 4.0.0
(function (global, factory) { | ||
typeof exports === 'object' && typeof module !== 'undefined' ? factory(exports, require('vega-canvas'), require('vega-dataflow'), require('vega-util'), require('vega-scale'), require('vega-statistics')) : | ||
typeof define === 'function' && define.amd ? define(['exports', 'vega-canvas', 'vega-dataflow', 'vega-util', 'vega-scale', 'vega-statistics'], factory) : | ||
(factory((global.vega = global.vega || {}, global.vega.transforms = global.vega.transforms || {}),global.vega,global.vega,global.vega,global.vega,global.vega)); | ||
}(this, (function (exports,vegaCanvas,vegaDataflow,vegaUtil,vegaScale,vegaStatistics) { 'use strict'; | ||
typeof exports === 'object' && typeof module !== 'undefined' ? factory(exports, require('vega-canvas'), require('vega-dataflow'), require('vega-util'), require('vega-scale'), require('vega-statistics')) : | ||
typeof define === 'function' && define.amd ? define(['exports', 'vega-canvas', 'vega-dataflow', 'vega-util', 'vega-scale', 'vega-statistics'], factory) : | ||
(global = global || self, factory((global.vega = global.vega || {}, global.vega.transforms = {}), global.vega, global.vega, global.vega, global.vega, global.vega)); | ||
}(this, function (exports, vegaCanvas, vegaDataflow, vegaUtil, vegaScale, vegaStatistics) { 'use strict'; | ||
/* | ||
Copyright (c) 2013, Jason Davies. | ||
All rights reserved. | ||
/* | ||
Copyright (c) 2013, Jason Davies. | ||
All rights reserved. | ||
Redistribution and use in source and binary forms, with or without | ||
modification, are permitted provided that the following conditions are met: | ||
Redistribution and use in source and binary forms, with or without | ||
modification, are permitted provided that the following conditions are met: | ||
* Redistributions of source code must retain the above copyright notice, this | ||
list of conditions and the following disclaimer. | ||
* Redistributions of source code must retain the above copyright notice, this | ||
list of conditions and the following disclaimer. | ||
* Redistributions in binary form must reproduce the above copyright notice, | ||
this list of conditions and the following disclaimer in the documentation | ||
and/or other materials provided with the distribution. | ||
* Redistributions in binary form must reproduce the above copyright notice, | ||
this list of conditions and the following disclaimer in the documentation | ||
and/or other materials provided with the distribution. | ||
* The name Jason Davies may not be used to endorse or promote products | ||
derived from this software without specific prior written permission. | ||
* The name Jason Davies may not be used to endorse or promote products | ||
derived from this software without specific prior written permission. | ||
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND | ||
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED | ||
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE | ||
DISCLAIMED. IN NO EVENT SHALL JASON DAVIES BE LIABLE FOR ANY DIRECT, INDIRECT, | ||
INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT | ||
LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR | ||
PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF | ||
LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE | ||
OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF | ||
ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | ||
*/ | ||
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND | ||
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED | ||
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE | ||
DISCLAIMED. IN NO EVENT SHALL JASON DAVIES BE LIABLE FOR ANY DIRECT, INDIRECT, | ||
INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT | ||
LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR | ||
PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF | ||
LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE | ||
OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF | ||
ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | ||
*/ | ||
// Word cloud layout by Jason Davies, https://www.jasondavies.com/wordcloud/ | ||
// Algorithm due to Jonathan Feinberg, http://static.mrfeinberg.com/bv_ch03.pdf | ||
// Word cloud layout by Jason Davies, https://www.jasondavies.com/wordcloud/ | ||
// Algorithm due to Jonathan Feinberg, http://static.mrfeinberg.com/bv_ch03.pdf | ||
var cloudRadians = Math.PI / 180; | ||
var cw = 1 << 11 >> 5; | ||
var ch = 1 << 11; | ||
var cloudRadians = Math.PI / 180, | ||
cw = 1 << 11 >> 5, | ||
ch = 1 << 11; | ||
var cloud = function() { | ||
var size = [256, 256], | ||
text, | ||
font, | ||
fontSize, | ||
fontStyle, | ||
fontWeight, | ||
rotate, | ||
padding, | ||
spiral = archimedeanSpiral, | ||
words = [], | ||
random$$1 = Math.random, | ||
cloud = {}; | ||
function cloud() { | ||
var size = [256, 256], | ||
text, | ||
font, | ||
fontSize, | ||
fontStyle, | ||
fontWeight, | ||
rotate, | ||
padding, | ||
spiral = archimedeanSpiral, | ||
words = [], | ||
random = Math.random, | ||
cloud = {}; | ||
cloud.layout = function() { | ||
var contextAndRatio = getContext(vegaCanvas.canvas()), | ||
board = zeroArray((size[0] >> 5) * size[1]), | ||
bounds = null, | ||
n = words.length, | ||
i = -1, | ||
tags = [], | ||
data = words.map(function(d) { | ||
return { | ||
text: text(d), | ||
font: font(d), | ||
style: fontStyle(d), | ||
weight: fontWeight(d), | ||
rotate: rotate(d), | ||
size: ~~fontSize(d), | ||
padding: padding(d), | ||
xoff: 0, | ||
yoff: 0, | ||
x1: 0, | ||
y1: 0, | ||
x0: 0, | ||
y0: 0, | ||
hasText: false, | ||
sprite: null, | ||
datum: d | ||
}; | ||
}).sort(function(a, b) { return b.size - a.size; }); | ||
cloud.layout = function() { | ||
var contextAndRatio = getContext(vegaCanvas.canvas()), | ||
board = zeroArray((size[0] >> 5) * size[1]), | ||
bounds = null, | ||
n = words.length, | ||
i = -1, | ||
tags = [], | ||
data = words.map(function(d) { | ||
return { | ||
text: text(d), | ||
font: font(d), | ||
style: fontStyle(d), | ||
weight: fontWeight(d), | ||
rotate: rotate(d), | ||
size: ~~fontSize(d), | ||
padding: padding(d), | ||
xoff: 0, | ||
yoff: 0, | ||
x1: 0, | ||
y1: 0, | ||
x0: 0, | ||
y0: 0, | ||
hasText: false, | ||
sprite: null, | ||
datum: d | ||
}; | ||
}).sort(function(a, b) { return b.size - a.size; }); | ||
while (++i < n) { | ||
var d = data[i]; | ||
d.x = (size[0] * (random$$1() + .5)) >> 1; | ||
d.y = (size[1] * (random$$1() + .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 | ||
d.x -= size[0] >> 1; | ||
d.y -= size[1] >> 1; | ||
while (++i < n) { | ||
var d = data[i]; | ||
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 | ||
d.x -= size[0] >> 1; | ||
d.y -= size[1] >> 1; | ||
} | ||
} | ||
} | ||
return tags; | ||
}; | ||
return tags; | ||
}; | ||
function getContext(canvas$$1) { | ||
canvas$$1.width = canvas$$1.height = 1; | ||
var ratio = Math.sqrt(canvas$$1.getContext("2d").getImageData(0, 0, 1, 1).data.length >> 2); | ||
canvas$$1.width = (cw << 5) / ratio; | ||
canvas$$1.height = ch / ratio; | ||
function getContext(canvas) { | ||
canvas.width = canvas.height = 1; | ||
var ratio = Math.sqrt(canvas.getContext("2d").getImageData(0, 0, 1, 1).data.length >> 2); | ||
canvas.width = (cw << 5) / ratio; | ||
canvas.height = ch / ratio; | ||
var context = canvas$$1.getContext("2d"); | ||
context.fillStyle = context.strokeStyle = "red"; | ||
context.textAlign = "center"; | ||
var context = canvas.getContext("2d"); | ||
context.fillStyle = context.strokeStyle = "red"; | ||
context.textAlign = "center"; | ||
return {context: context, ratio: ratio}; | ||
} | ||
return {context: context, ratio: ratio}; | ||
} | ||
function place(board, tag, bounds) { | ||
var startX = tag.x, | ||
startY = tag.y, | ||
maxDelta = Math.sqrt(size[0] * size[0] + size[1] * size[1]), | ||
s = spiral(size), | ||
dt = random$$1() < .5 ? 1 : -1, | ||
t = -dt, | ||
dxdy, | ||
dx, | ||
dy; | ||
function place(board, tag, bounds) { | ||
var startX = tag.x, | ||
startY = tag.y, | ||
maxDelta = Math.sqrt(size[0] * size[0] + size[1] * size[1]), | ||
s = spiral(size), | ||
dt = random() < .5 ? 1 : -1, | ||
t = -dt, | ||
dxdy, | ||
dx, | ||
dy; | ||
while (dxdy = s(t += dt)) { | ||
dx = ~~dxdy[0]; | ||
dy = ~~dxdy[1]; | ||
while (dxdy = s(t += dt)) { | ||
dx = ~~dxdy[0]; | ||
dy = ~~dxdy[1]; | ||
if (Math.min(Math.abs(dx), Math.abs(dy)) >= maxDelta) break; | ||
if (Math.min(Math.abs(dx), Math.abs(dy)) >= maxDelta) break; | ||
tag.x = startX + dx; | ||
tag.y = startY + dy; | ||
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 (!bounds || !cloudCollide(tag, board, size[0])) { | ||
if (!bounds || collideRects(tag, bounds)) { | ||
var sprite = tag.sprite, | ||
w = tag.width >> 5, | ||
sw = size[0] >> 5, | ||
lx = tag.x - (w << 4), | ||
sx = lx & 0x7f, | ||
msx = 32 - sx, | ||
h = tag.y1 - tag.y0, | ||
x = (tag.y + tag.y0) * sw + (lx >> 5), | ||
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); | ||
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])) { | ||
if (!bounds || collideRects(tag, bounds)) { | ||
var sprite = tag.sprite, | ||
w = tag.width >> 5, | ||
sw = size[0] >> 5, | ||
lx = tag.x - (w << 4), | ||
sx = lx & 0x7f, | ||
msx = 32 - sx, | ||
h = tag.y1 - tag.y0, | ||
x = (tag.y + tag.y0) * sw + (lx >> 5), | ||
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); | ||
} | ||
x += sw; | ||
} | ||
x += sw; | ||
tag.sprite = null; | ||
return true; | ||
} | ||
tag.sprite = null; | ||
return true; | ||
} | ||
} | ||
return false; | ||
} | ||
return false; | ||
} | ||
cloud.words = function(_) { | ||
if (arguments.length) { | ||
words = _; | ||
return cloud; | ||
} else { | ||
return words; | ||
} | ||
}; | ||
cloud.words = function(_) { | ||
if (arguments.length) { | ||
words = _; | ||
return cloud; | ||
} else { | ||
return words; | ||
} | ||
}; | ||
cloud.size = function(_) { | ||
if (arguments.length) { | ||
size = [+_[0], +_[1]]; | ||
return cloud; | ||
} else { | ||
return size; | ||
} | ||
}; | ||
cloud.size = function(_) { | ||
if (arguments.length) { | ||
size = [+_[0], +_[1]]; | ||
return cloud; | ||
} else { | ||
return size; | ||
} | ||
}; | ||
cloud.font = function(_) { | ||
if (arguments.length) { | ||
font = functor(_); | ||
return cloud; | ||
} else { | ||
return font; | ||
} | ||
}; | ||
cloud.font = function(_) { | ||
if (arguments.length) { | ||
font = functor(_); | ||
return cloud; | ||
} else { | ||
return font; | ||
} | ||
}; | ||
cloud.fontStyle = function(_) { | ||
if (arguments.length) { | ||
fontStyle = functor(_); | ||
return cloud; | ||
} else { | ||
return fontStyle; | ||
} | ||
}; | ||
cloud.fontStyle = function(_) { | ||
if (arguments.length) { | ||
fontStyle = functor(_); | ||
return cloud; | ||
} else { | ||
return fontStyle; | ||
} | ||
}; | ||
cloud.fontWeight = function(_) { | ||
if (arguments.length) { | ||
fontWeight = functor(_); | ||
return cloud; | ||
} else { | ||
return fontWeight; | ||
} | ||
}; | ||
cloud.fontWeight = function(_) { | ||
if (arguments.length) { | ||
fontWeight = functor(_); | ||
return cloud; | ||
} else { | ||
return fontWeight; | ||
} | ||
}; | ||
cloud.rotate = function(_) { | ||
if (arguments.length) { | ||
rotate = functor(_); | ||
return cloud; | ||
} else { | ||
return rotate; | ||
} | ||
}; | ||
cloud.rotate = function(_) { | ||
if (arguments.length) { | ||
rotate = functor(_); | ||
return cloud; | ||
} else { | ||
return rotate; | ||
} | ||
}; | ||
cloud.text = function(_) { | ||
if (arguments.length) { | ||
text = functor(_); | ||
return cloud; | ||
} else { | ||
return text; | ||
} | ||
}; | ||
cloud.text = function(_) { | ||
if (arguments.length) { | ||
text = functor(_); | ||
return cloud; | ||
} else { | ||
return text; | ||
} | ||
}; | ||
cloud.spiral = function(_) { | ||
if (arguments.length) { | ||
spiral = spirals[_] || _; | ||
return cloud; | ||
} else { | ||
return spiral; | ||
} | ||
}; | ||
cloud.spiral = function(_) { | ||
if (arguments.length) { | ||
spiral = spirals[_] || _; | ||
return cloud; | ||
} else { | ||
return spiral; | ||
} | ||
}; | ||
cloud.fontSize = function(_) { | ||
if (arguments.length) { | ||
fontSize = functor(_); | ||
return cloud; | ||
} else { | ||
return fontSize; | ||
} | ||
}; | ||
cloud.fontSize = function(_) { | ||
if (arguments.length) { | ||
fontSize = functor(_); | ||
return cloud; | ||
} else { | ||
return fontSize; | ||
} | ||
}; | ||
cloud.padding = function(_) { | ||
if (arguments.length) { | ||
padding = functor(_); | ||
return cloud; | ||
} else { | ||
return padding; | ||
} | ||
}; | ||
cloud.padding = function(_) { | ||
if (arguments.length) { | ||
padding = functor(_); | ||
return cloud; | ||
} else { | ||
return padding; | ||
} | ||
}; | ||
cloud.random = function(_) { | ||
if (arguments.length) { | ||
random$$1 = _; | ||
return cloud; | ||
} else { | ||
return random$$1; | ||
} | ||
}; | ||
cloud.random = function(_) { | ||
if (arguments.length) { | ||
random = _; | ||
return cloud; | ||
} else { | ||
return random; | ||
} | ||
}; | ||
return cloud; | ||
}; | ||
return cloud; | ||
} | ||
// Fetches a monochrome sprite bitmap for the specified text. | ||
// Load in batches for speed. | ||
function cloudSprite(contextAndRatio, d, data, di) { | ||
if (d.sprite) return; | ||
var c = contextAndRatio.context, | ||
ratio = contextAndRatio.ratio; | ||
// Fetches a monochrome sprite bitmap for the specified text. | ||
// Load in batches for speed. | ||
function cloudSprite(contextAndRatio, d, data, di) { | ||
if (d.sprite) return; | ||
var c = contextAndRatio.context, | ||
ratio = contextAndRatio.ratio; | ||
c.clearRect(0, 0, (cw << 5) / ratio, ch / ratio); | ||
var x = 0, | ||
y = 0, | ||
maxh = 0, | ||
n = data.length, | ||
w, w32, h, i, j; | ||
--di; | ||
while (++di < n) { | ||
d = data[di]; | ||
c.save(); | ||
c.font = d.style + " " + d.weight + " " + ~~((d.size + 1) / ratio) + "px " + d.font; | ||
w = c.measureText(d.text + "m").width * ratio; | ||
h = d.size << 1; | ||
if (d.rotate) { | ||
var sr = Math.sin(d.rotate * cloudRadians), | ||
cr = Math.cos(d.rotate * cloudRadians), | ||
wcr = w * cr, | ||
wsr = w * sr, | ||
hcr = h * cr, | ||
hsr = h * sr; | ||
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; | ||
c.clearRect(0, 0, (cw << 5) / ratio, ch / ratio); | ||
var x = 0, | ||
y = 0, | ||
maxh = 0, | ||
n = data.length, | ||
w, w32, h, i, j; | ||
--di; | ||
while (++di < n) { | ||
d = data[di]; | ||
c.save(); | ||
c.font = d.style + " " + d.weight + " " + ~~((d.size + 1) / ratio) + "px " + d.font; | ||
w = c.measureText(d.text + "m").width * ratio; | ||
h = d.size << 1; | ||
if (d.rotate) { | ||
var sr = Math.sin(d.rotate * cloudRadians), | ||
cr = Math.cos(d.rotate * cloudRadians), | ||
wcr = w * cr, | ||
wsr = w * sr, | ||
hcr = h * cr, | ||
hsr = h * sr; | ||
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; | ||
} | ||
if (h > maxh) maxh = h; | ||
if (x + w >= (cw << 5)) { | ||
x = 0; | ||
y += maxh; | ||
maxh = 0; | ||
} | ||
if (y + h >= ch) break; | ||
c.translate((x + (w >> 1)) / ratio, (y + (h >> 1)) / ratio); | ||
if (d.rotate) c.rotate(d.rotate * cloudRadians); | ||
c.fillText(d.text, 0, 0); | ||
if (d.padding) { | ||
c.lineWidth = 2 * d.padding; | ||
c.strokeText(d.text, 0, 0); | ||
} | ||
c.restore(); | ||
d.width = w; | ||
d.height = h; | ||
d.xoff = x; | ||
d.yoff = y; | ||
d.x1 = w >> 1; | ||
d.y1 = h >> 1; | ||
d.x0 = -d.x1; | ||
d.y0 = -d.y1; | ||
d.hasText = true; | ||
x += w; | ||
} | ||
if (h > maxh) maxh = h; | ||
if (x + w >= (cw << 5)) { | ||
x = 0; | ||
y += maxh; | ||
maxh = 0; | ||
var pixels = c.getImageData(0, 0, (cw << 5) / ratio, ch / ratio).data, | ||
sprite = []; | ||
while (--di >= 0) { | ||
d = data[di]; | ||
if (!d.hasText) continue; | ||
w = d.width; | ||
w32 = w >> 5; | ||
h = d.y1 - d.y0; | ||
// Zero the buffer | ||
for (i = 0; i < h * w32; i++) sprite[i] = 0; | ||
x = d.xoff; | ||
if (x == null) return; | ||
y = d.yoff; | ||
var seen = 0, | ||
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; | ||
sprite[k] |= m; | ||
seen |= m; | ||
} | ||
if (seen) seenRow = j; | ||
else { | ||
d.y0++; | ||
h--; | ||
j--; | ||
y++; | ||
} | ||
} | ||
d.y1 = d.y0 + seenRow; | ||
d.sprite = sprite.slice(0, (d.y1 - d.y0) * w32); | ||
} | ||
if (y + h >= ch) break; | ||
c.translate((x + (w >> 1)) / ratio, (y + (h >> 1)) / ratio); | ||
if (d.rotate) c.rotate(d.rotate * cloudRadians); | ||
c.fillText(d.text, 0, 0); | ||
if (d.padding) { | ||
c.lineWidth = 2 * d.padding; | ||
c.strokeText(d.text, 0, 0); | ||
} | ||
c.restore(); | ||
d.width = w; | ||
d.height = h; | ||
d.xoff = x; | ||
d.yoff = y; | ||
d.x1 = w >> 1; | ||
d.y1 = h >> 1; | ||
d.x0 = -d.x1; | ||
d.y0 = -d.y1; | ||
d.hasText = true; | ||
x += w; | ||
} | ||
var pixels = c.getImageData(0, 0, (cw << 5) / ratio, ch / ratio).data, | ||
sprite = []; | ||
while (--di >= 0) { | ||
d = data[di]; | ||
if (!d.hasText) continue; | ||
w = d.width; | ||
w32 = w >> 5; | ||
h = d.y1 - d.y0; | ||
// Zero the buffer | ||
for (i = 0; i < h * w32; i++) sprite[i] = 0; | ||
x = d.xoff; | ||
if (x == null) return; | ||
y = d.yoff; | ||
var seen = 0, | ||
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; | ||
sprite[k] |= m; | ||
seen |= m; | ||
// Use mask-based collision detection. | ||
function cloudCollide(tag, board, sw) { | ||
sw >>= 5; | ||
var sprite = tag.sprite, | ||
w = tag.width >> 5, | ||
lx = tag.x - (w << 4), | ||
sx = lx & 0x7f, | ||
msx = 32 - sx, | ||
h = tag.y1 - tag.y0, | ||
x = (tag.y + tag.y0) * sw + (lx >> 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 (seen) seenRow = j; | ||
else { | ||
d.y0++; | ||
h--; | ||
j--; | ||
y++; | ||
} | ||
x += sw; | ||
} | ||
d.y1 = d.y0 + seenRow; | ||
d.sprite = sprite.slice(0, (d.y1 - d.y0) * w32); | ||
return false; | ||
} | ||
} | ||
// Use mask-based collision detection. | ||
function cloudCollide(tag, board, sw) { | ||
sw >>= 5; | ||
var sprite = tag.sprite, | ||
w = tag.width >> 5, | ||
lx = tag.x - (w << 4), | ||
sx = lx & 0x7f, | ||
msx = 32 - sx, | ||
h = tag.y1 - tag.y0, | ||
x = (tag.y + tag.y0) * sw + (lx >> 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; | ||
} | ||
x += sw; | ||
function cloudBounds(bounds, d) { | ||
var b0 = bounds[0], | ||
b1 = bounds[1]; | ||
if (d.x + d.x0 < b0.x) b0.x = d.x + d.x0; | ||
if (d.y + d.y0 < b0.y) b0.y = d.y + d.y0; | ||
if (d.x + d.x1 > b1.x) b1.x = d.x + d.x1; | ||
if (d.y + d.y1 > b1.y) b1.y = d.y + d.y1; | ||
} | ||
return false; | ||
} | ||
function cloudBounds(bounds, d) { | ||
var b0 = bounds[0], | ||
b1 = bounds[1]; | ||
if (d.x + d.x0 < b0.x) b0.x = d.x + d.x0; | ||
if (d.y + d.y0 < b0.y) b0.y = d.y + d.y0; | ||
if (d.x + d.x1 > b1.x) b1.x = d.x + d.x1; | ||
if (d.y + d.y1 > b1.y) b1.y = d.y + d.y1; | ||
} | ||
function collideRects(a, b) { | ||
return a.x + a.x1 > b[0].x && a.x + a.x0 < b[1].x && a.y + a.y1 > b[0].y && a.y + a.y0 < b[1].y; | ||
} | ||
function collideRects(a, b) { | ||
return a.x + a.x1 > b[0].x && a.x + a.x0 < b[1].x && a.y + a.y1 > b[0].y && a.y + a.y0 < b[1].y; | ||
} | ||
function archimedeanSpiral(size) { | ||
var e = size[0] / size[1]; | ||
return function(t) { | ||
return [e * (t *= .1) * Math.cos(t), t * Math.sin(t)]; | ||
}; | ||
} | ||
function archimedeanSpiral(size) { | ||
var e = size[0] / size[1]; | ||
return function(t) { | ||
return [e * (t *= .1) * Math.cos(t), t * Math.sin(t)]; | ||
}; | ||
} | ||
function rectangularSpiral(size) { | ||
var dy = 4, | ||
dx = dy * size[0] / size[1], | ||
x = 0, | ||
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 [x, y]; | ||
}; | ||
} | ||
function rectangularSpiral(size) { | ||
var dy = 4, | ||
dx = dy * size[0] / size[1], | ||
x = 0, | ||
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 [x, y]; | ||
}; | ||
} | ||
// TODO reuse arrays? | ||
function zeroArray(n) { | ||
var a = [], | ||
i = -1; | ||
while (++i < n) a[i] = 0; | ||
return a; | ||
} | ||
// TODO reuse arrays? | ||
function zeroArray(n) { | ||
var a = [], | ||
i = -1; | ||
while (++i < n) a[i] = 0; | ||
return a; | ||
} | ||
function functor(d) { | ||
return typeof d === "function" ? d : function() { return d; }; | ||
} | ||
function functor(d) { | ||
return typeof d === "function" ? d : function() { return d; }; | ||
} | ||
var spirals = { | ||
archimedean: archimedeanSpiral, | ||
rectangular: rectangularSpiral | ||
}; | ||
var spirals = { | ||
archimedean: archimedeanSpiral, | ||
rectangular: rectangularSpiral | ||
}; | ||
var Output = ['x', 'y', 'font', 'fontSize', 'fontStyle', 'fontWeight', 'angle']; | ||
var Output = ['x', 'y', 'font', 'fontSize', 'fontStyle', 'fontWeight', 'angle']; | ||
var Params = ['text', 'font', 'rotate', 'fontSize', 'fontStyle', 'fontWeight']; | ||
var Params = ['text', 'font', 'rotate', 'fontSize', 'fontStyle', 'fontWeight']; | ||
function Wordcloud(params) { | ||
vegaDataflow.Transform.call(this, cloud(), params); | ||
} | ||
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 } | ||
] | ||
}; | ||
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 } | ||
] | ||
}; | ||
var prototype = vegaUtil.inherits(Wordcloud, vegaDataflow.Transform); | ||
var prototype = vegaUtil.inherits(Wordcloud, vegaDataflow.Transform); | ||
prototype.transform = function(_, pulse) { | ||
function modp(param) { | ||
var p = _[param]; | ||
return vegaUtil.isFunction(p) && pulse.modified(p.fields); | ||
} | ||
prototype.transform = function(_, pulse) { | ||
function modp(param) { | ||
var p = _[param]; | ||
return vegaUtil.isFunction(p) && pulse.modified(p.fields); | ||
} | ||
var mod = _.modified(); | ||
if (!(mod || pulse.changed(pulse.ADD_REM) || Params.some(modp))) return; | ||
var mod = _.modified(); | ||
if (!(mod || pulse.changed(pulse.ADD_REM) || Params.some(modp))) return; | ||
var data = pulse.materialize(pulse.SOURCE).source, | ||
layout = this.value, | ||
as = _.as || Output, | ||
fontSize = _.fontSize || 14, | ||
range; | ||
var data = pulse.materialize(pulse.SOURCE).source, | ||
layout = this.value, | ||
as = _.as || Output, | ||
fontSize = _.fontSize || 14, | ||
range; | ||
vegaUtil.isFunction(fontSize) | ||
? (range = _.fontSizeRange) | ||
: (fontSize = vegaUtil.constant(fontSize)); | ||
vegaUtil.isFunction(fontSize) | ||
? (range = _.fontSizeRange) | ||
: (fontSize = vegaUtil.constant(fontSize)); | ||
// create font size scaling function as needed | ||
if (range) { | ||
var fsize = fontSize, | ||
sizeScale = vegaScale.scale('sqrt')() | ||
.domain(extent(fsize, data)) | ||
.range(range); | ||
fontSize = function(x) { return sizeScale(fsize(x)); }; | ||
} | ||
// create font size scaling function as needed | ||
if (range) { | ||
var fsize = fontSize, | ||
sizeScale = vegaScale.scale('sqrt')() | ||
.domain(extent(fsize, data)) | ||
.range(range); | ||
fontSize = function(x) { return sizeScale(fsize(x)); }; | ||
} | ||
data.forEach(function(t) { | ||
t[as[0]] = NaN; | ||
t[as[1]] = NaN; | ||
t[as[3]] = 0; | ||
}); | ||
data.forEach(function(t) { | ||
t[as[0]] = NaN; | ||
t[as[1]] = NaN; | ||
t[as[3]] = 0; | ||
}); | ||
// configure layout | ||
var 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(); | ||
// configure layout | ||
var 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(); | ||
var size = layout.size(), | ||
dx = size[0] >> 1, | ||
dy = size[1] >> 1, | ||
i = 0, | ||
n = words.length, | ||
w, t; | ||
var size = layout.size(), | ||
dx = size[0] >> 1, | ||
dy = size[1] >> 1, | ||
i = 0, | ||
n = words.length, | ||
w, t; | ||
for (; i<n; ++i) { | ||
w = words[i]; | ||
t = w.datum; | ||
t[as[0]] = w.x + dx; | ||
t[as[1]] = w.y + dy; | ||
t[as[2]] = w.font; | ||
t[as[3]] = w.size; | ||
t[as[4]] = w.style; | ||
t[as[5]] = w.weight; | ||
t[as[6]] = w.rotate; | ||
} | ||
for (; i<n; ++i) { | ||
w = words[i]; | ||
t = w.datum; | ||
t[as[0]] = w.x + dx; | ||
t[as[1]] = w.y + dy; | ||
t[as[2]] = w.font; | ||
t[as[3]] = w.size; | ||
t[as[4]] = w.style; | ||
t[as[5]] = w.weight; | ||
t[as[6]] = w.rotate; | ||
} | ||
return pulse.reflow(mod).modifies(as); | ||
}; | ||
return pulse.reflow(mod).modifies(as); | ||
}; | ||
function extent(field, data) { | ||
var min = +Infinity, | ||
max = -Infinity, | ||
i = 0, | ||
n = data.length, | ||
v; | ||
function extent(field, data) { | ||
var min = +Infinity, | ||
max = -Infinity, | ||
i = 0, | ||
n = data.length, | ||
v; | ||
for (; i<n; ++i) { | ||
v = field(data[i]); | ||
if (v < min) min = v; | ||
if (v > max) max = v; | ||
} | ||
for (; i<n; ++i) { | ||
v = field(data[i]); | ||
if (v < min) min = v; | ||
if (v > max) max = v; | ||
return [min, max]; | ||
} | ||
return [min, max]; | ||
} | ||
exports.wordcloud = Wordcloud; | ||
exports.wordcloud = Wordcloud; | ||
Object.defineProperty(exports, '__esModule', { value: true }); | ||
Object.defineProperty(exports, '__esModule', { value: true }); | ||
}))); | ||
})); |
@@ -1,1 +0,1 @@ | ||
!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.vega=t.vega||{},t.vega.transforms=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=64,u=2048,s=function(){var t,n,r,a,i,o,s,d=[256,256],v=g,m=[],p=Math.random,z={};function M(t,e,n){for(var r,a,i,o,f,u=e.x,s=e.y,l=Math.sqrt(d[0]*d[0]+d[1]*d[1]),x=v(d),g=p()<.5?1:-1,c=-g;(r=x(c+=g))&&(a=~~r[0],i=~~r[1],!(Math.min(Math.abs(a),Math.abs(i))>=l));)if(e.x=u+a,e.y=s+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 h,m=e.sprite,z=e.width>>5,M=d[0]>>5,b=e.x-(z<<4),w=127&b,S=32-w,q=e.y1-e.y0,T=(e.y+e.y0)*M+(b>>5),W=0;W<q;W++){h=0;for(var k=0;k<=z;k++)t[T+k]|=h<<S|(k<z?(h=m[W*z+k])>>>w:0);T+=M}return e.sprite=null,!0}return!1}return z.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=(f<<5)/e,t.height=u/e;var n=t.getContext("2d");return n.fillStyle=n.strokeStyle="red",n.textAlign="center",{context:n,ratio:e}}(e.canvas()),g=function(t){var e=[],n=-1;for(;++n<t;)e[n]=0;return e}((d[0]>>5)*d[1]),c=null,h=m.length,v=-1,z=[],b=m.map(function(e){return{text:t(e),font:n(e),style:a(e),weight:i(e),rotate:o(e),size:~~r(e),padding:s(e),xoff:0,yoff:0,x1:0,y1:0,x0:0,y0:0,hasText:!1,sprite:null,datum:e}}).sort(function(t,e){return e.size-t.size});++v<h;){var w=b[v];w.x=d[0]*(p()+.5)>>1,w.y=d[1]*(p()+.5)>>1,l(y,w,b,v),w.hasText&&M(g,w,c)&&(z.push(w),c?x(c,w):c=[{x:w.x+w.x0,y:w.y+w.y0},{x:w.x+w.x1,y:w.y+w.y1}],w.x-=d[0]>>1,w.y-=d[1]>>1)}return z},z.words=function(t){return arguments.length?(m=t,z):m},z.size=function(t){return arguments.length?(d=[+t[0],+t[1]],z):d},z.font=function(t){return arguments.length?(n=c(t),z):n},z.fontStyle=function(t){return arguments.length?(a=c(t),z):a},z.fontWeight=function(t){return arguments.length?(i=c(t),z):i},z.rotate=function(t){return arguments.length?(o=c(t),z):o},z.text=function(e){return arguments.length?(t=c(e),z):t},z.spiral=function(t){return arguments.length?(v=h[t]||t,z):v},z.fontSize=function(t){return arguments.length?(r=c(t),z):r},z.padding=function(t){return arguments.length?(s=c(t),z):s},z.random=function(t){return arguments.length?(p=t,z):p},z};function l(t,e,n,r){if(!e.sprite){var a=t.context,i=t.ratio;a.clearRect(0,0,(f<<5)/i,u/i);var s,l,y,x,g,c=0,h=0,d=0,v=n.length;for(--r;++r<v;){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),z=s*p,M=s*m,b=y*p,w=y*m;s=Math.max(Math.abs(z+w),Math.abs(z-w))+31>>5<<5,y=~~Math.max(Math.abs(M+b),Math.abs(M-b))}else s=s+31>>5<<5;if(y>d&&(d=y),c+s>=f<<5&&(c=0,h+=d,d=0),h+y>=u)break;a.translate((c+(s>>1))/i,(h+(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=c,e.yoff=h,e.x1=s>>1,e.y1=y>>1,e.x0=-e.x1,e.y0=-e.y1,e.hasText=!0,c+=s}for(var S=a.getImageData(0,0,(f<<5)/i,u/i).data,q=[];--r>=0;)if((e=n[r]).hasText){for(l=(s=e.width)>>5,y=e.y1-e.y0,x=0;x<y*l;x++)q[x]=0;if(null==(c=e.xoff))return;h=e.yoff;var T=0,W=-1;for(g=0;g<y;g++){for(x=0;x<s;x++){var k=l*g+(x>>5),D=S[(h+g)*(f<<5)+(c+x)<<2]?1<<31-x%32:0;q[k]|=D,T|=D}T?W=g:(e.y0++,y--,g--,h++)}e.y1=e.y0+W,e.sprite=q.slice(0,(e.y1-e.y0)*l)}}}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,u=32-f,s=t.y1-t.y0,l=(t.y+t.y0)*n+(o>>5),y=0;y<s;y++){r=0;for(var x=0;x<=i;x++)if((r<<u|(x<i?(r=a[y*i+x])>>>f:0))&e[l+x])return!0;l+=n}return!1}function x(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 g(t){var e=t[0]/t[1];return function(t){return[e*(t*=.1)*Math.cos(t),t*Math.sin(t)]}}function c(t){return"function"==typeof t?t:function(){return t}}var h={archimedean:g,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]}}},d=["x","y","font","fontSize","fontStyle","fontWeight","angle"],v=["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:d}]},r.inherits(m,n.Transform).transform=function(t,e){var n=t.modified();if(n||e.changed(e.ADD_REM)||v.some(function(n){var a=t[n];return r.isFunction(a)&&e.modified(a.fields)})){var o,f=e.materialize(e.SOURCE).source,u=this.value,s=t.as||d,l=t.fontSize||14;if(r.isFunction(l)?o=t.fontSizeRange:l=r.constant(l),o){var y=l,x=a.scale("sqrt")().domain(function(t,e){for(var n,r=1/0,a=-1/0,i=0,o=e.length;i<o;++i)(n=t(e[i]))<r&&(r=n),n>a&&(a=n);return[r,a]}(y,f)).range(o);l=function(t){return x(y(t))}}f.forEach(function(t){t[s[0]]=NaN,t[s[1]]=NaN,t[s[3]]=0});for(var g,c,h=u.words(f).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(),m=u.size(),p=m[0]>>1,z=m[1]>>1,M=0,b=h.length;M<b;++M)(c=(g=h[M]).datum)[s[0]]=g.x+p,c[s[1]]=g.y+z,c[s[2]]=g.font,c[s[3]]=g.size,c[s[4]]=g.style,c[s[5]]=g.weight,c[s[6]]=g.rotate;return e.reflow(n).modifies(s)}},t.wordcloud=m,Object.defineProperty(t,"__esModule",{value:!0})}); | ||
!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=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=64,u=2048;function s(){var t,n,r,a,i,o,s,d=[256,256],v=c,m=[],p=Math.random,z={};function M(t,e,n){for(var r,a,i,o,f,u=e.x,s=e.y,l=Math.sqrt(d[0]*d[0]+d[1]*d[1]),x=v(d),c=p()<.5?1:-1,g=-c;(r=x(g+=c))&&(a=~~r[0],i=~~r[1],!(Math.min(Math.abs(a),Math.abs(i))>=l));)if(e.x=u+a,e.y=s+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 h,m=e.sprite,z=e.width>>5,M=d[0]>>5,b=e.x-(z<<4),w=127&b,S=32-w,q=e.y1-e.y0,T=(e.y+e.y0)*M+(b>>5),W=0;W<q;W++){h=0;for(var k=0;k<=z;k++)t[T+k]|=h<<S|(k<z?(h=m[W*z+k])>>>w:0);T+=M}return e.sprite=null,!0}return!1}return z.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=(f<<5)/e,t.height=u/e;var n=t.getContext("2d");return n.fillStyle=n.strokeStyle="red",n.textAlign="center",{context:n,ratio:e}}(e.canvas()),c=function(t){var e=[],n=-1;for(;++n<t;)e[n]=0;return e}((d[0]>>5)*d[1]),g=null,h=m.length,v=-1,z=[],b=m.map(function(e){return{text:t(e),font:n(e),style:a(e),weight:i(e),rotate:o(e),size:~~r(e),padding:s(e),xoff:0,yoff:0,x1:0,y1:0,x0:0,y0:0,hasText:!1,sprite:null,datum:e}}).sort(function(t,e){return e.size-t.size});++v<h;){var w=b[v];w.x=d[0]*(p()+.5)>>1,w.y=d[1]*(p()+.5)>>1,l(y,w,b,v),w.hasText&&M(c,w,g)&&(z.push(w),g?x(g,w):g=[{x:w.x+w.x0,y:w.y+w.y0},{x:w.x+w.x1,y:w.y+w.y1}],w.x-=d[0]>>1,w.y-=d[1]>>1)}return z},z.words=function(t){return arguments.length?(m=t,z):m},z.size=function(t){return arguments.length?(d=[+t[0],+t[1]],z):d},z.font=function(t){return arguments.length?(n=g(t),z):n},z.fontStyle=function(t){return arguments.length?(a=g(t),z):a},z.fontWeight=function(t){return arguments.length?(i=g(t),z):i},z.rotate=function(t){return arguments.length?(o=g(t),z):o},z.text=function(e){return arguments.length?(t=g(e),z):t},z.spiral=function(t){return arguments.length?(v=h[t]||t,z):v},z.fontSize=function(t){return arguments.length?(r=g(t),z):r},z.padding=function(t){return arguments.length?(s=g(t),z):s},z.random=function(t){return arguments.length?(p=t,z):p},z}function l(t,e,n,r){if(!e.sprite){var a=t.context,i=t.ratio;a.clearRect(0,0,(f<<5)/i,u/i);var s,l,y,x,c,g=0,h=0,d=0,v=n.length;for(--r;++r<v;){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),z=s*p,M=s*m,b=y*p,w=y*m;s=Math.max(Math.abs(z+w),Math.abs(z-w))+31>>5<<5,y=~~Math.max(Math.abs(M+b),Math.abs(M-b))}else s=s+31>>5<<5;if(y>d&&(d=y),g+s>=f<<5&&(g=0,h+=d,d=0),h+y>=u)break;a.translate((g+(s>>1))/i,(h+(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=g,e.yoff=h,e.x1=s>>1,e.y1=y>>1,e.x0=-e.x1,e.y0=-e.y1,e.hasText=!0,g+=s}for(var S=a.getImageData(0,0,(f<<5)/i,u/i).data,q=[];--r>=0;)if((e=n[r]).hasText){for(l=(s=e.width)>>5,y=e.y1-e.y0,x=0;x<y*l;x++)q[x]=0;if(null==(g=e.xoff))return;h=e.yoff;var T=0,W=-1;for(c=0;c<y;c++){for(x=0;x<s;x++){var k=l*c+(x>>5),D=S[(h+c)*(f<<5)+(g+x)<<2]?1<<31-x%32:0;q[k]|=D,T|=D}T?W=c:(e.y0++,y--,c--,h++)}e.y1=e.y0+W,e.sprite=q.slice(0,(e.y1-e.y0)*l)}}}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,u=32-f,s=t.y1-t.y0,l=(t.y+t.y0)*n+(o>>5),y=0;y<s;y++){r=0;for(var x=0;x<=i;x++)if((r<<u|(x<i?(r=a[y*i+x])>>>f:0))&e[l+x])return!0;l+=n}return!1}function x(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 c(t){var e=t[0]/t[1];return function(t){return[e*(t*=.1)*Math.cos(t),t*Math.sin(t)]}}function g(t){return"function"==typeof t?t:function(){return t}}var h={archimedean:c,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]}}},d=["x","y","font","fontSize","fontStyle","fontWeight","angle"],v=["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:d}]},r.inherits(m,n.Transform).transform=function(t,e){var n=t.modified();if(n||e.changed(e.ADD_REM)||v.some(function(n){var a=t[n];return r.isFunction(a)&&e.modified(a.fields)})){var o,f=e.materialize(e.SOURCE).source,u=this.value,s=t.as||d,l=t.fontSize||14;if(r.isFunction(l)?o=t.fontSizeRange:l=r.constant(l),o){var y=l,x=a.scale("sqrt")().domain(function(t,e){for(var n,r=1/0,a=-1/0,i=0,o=e.length;i<o;++i)(n=t(e[i]))<r&&(r=n),n>a&&(a=n);return[r,a]}(y,f)).range(o);l=function(t){return x(y(t))}}f.forEach(function(t){t[s[0]]=NaN,t[s[1]]=NaN,t[s[3]]=0});for(var c,g,h=u.words(f).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(),m=u.size(),p=m[0]>>1,z=m[1]>>1,M=0,b=h.length;M<b;++M)(g=(c=h[M]).datum)[s[0]]=c.x+p,g[s[1]]=c.y+z,g[s[2]]=c.font,g[s[3]]=c.size,g[s[4]]=c.style,g[s[5]]=c.weight,g[s[6]]=c.rotate;return e.reflow(n).modifies(s)}},t.wordcloud=m,Object.defineProperty(t,"__esModule",{value:!0})}); |
{ | ||
"name": "vega-wordcloud", | ||
"version": "3.0.0", | ||
"version": "4.0.0", | ||
"description": "Wordcloud layout transform for Vega dataflows.", | ||
@@ -11,39 +11,26 @@ "keywords": [ | ||
"license": "BSD-3-Clause", | ||
"author": { | ||
"name": "Jeffrey Heer", | ||
"url": "http://idl.cs.washington.edu" | ||
}, | ||
"author": "Jeffrey Heer (http://idl.cs.washington.edu)", | ||
"main": "build/vega-wordcloud.js", | ||
"jsnext:main": "index", | ||
"module": "index", | ||
"repository": { | ||
"type": "git", | ||
"url": "https://github.com/vega/vega-wordcloud.git" | ||
}, | ||
"repository": "vega/vega", | ||
"scripts": { | ||
"build": "npm run test && uglifyjs build/vega-wordcloud.js -c -m -o build/vega-wordcloud.min.js", | ||
"pretest": "rm -rf build && mkdir build && 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", | ||
"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", | ||
"test": "tape 'test/**/*-test.js' && eslint index.js src test", | ||
"prepublish": "npm run build", | ||
"postpublish": "git push && git push --tags && zip -j build/vega-wordcloud.zip -- LICENSE README.md build/vega-wordcloud.js build/vega-wordcloud.min.js" | ||
"prepublishOnly": "yarn test && yarn build", | ||
"postpublish": "git push && git push --tags" | ||
}, | ||
"dependencies": { | ||
"vega-canvas": "^1.0.1", | ||
"vega-dataflow": "^4.0.0", | ||
"vega-scale": "^2.1.1", | ||
"vega-statistics": "^1.2.1", | ||
"vega-util": "^1.7.0" | ||
"vega-canvas": "^1.2.0", | ||
"vega-dataflow": "^5.0.0", | ||
"vega-scale": "^3.0.0", | ||
"vega-statistics": "^1.2.4", | ||
"vega-util": "^1.8.0" | ||
}, | ||
"devDependencies": { | ||
"canvas": "^1.6.10", | ||
"eslint": "4", | ||
"rollup": "0.58.2", | ||
"tape": "4", | ||
"uglify-js": "3", | ||
"vega-transforms": "^2.0.0" | ||
}, | ||
"browser": { | ||
"canvas": false, | ||
"canvas-prebuilt": false | ||
"vega-transforms": "*" | ||
} | ||
} |
@@ -5,4 +5,4 @@ # vega-wordcloud | ||
This module provides the following Vega data transform: | ||
This package provides the following Vega data transform: | ||
- [**Wordcloud**](https://vega.github.io/vega/docs/transforms/wordcloud/) [<>](https://github.com/vega/vega-wordcloud/blob/master/src/Wordcloud.js "Source") | ||
- [**Wordcloud**](https://vega.github.io/vega/docs/transforms/wordcloud/) [<>](https://github.com/vega/vega/blob/master/packages/vega-wordcloud/src/Wordcloud.js "Source") |
Sorry, the diff of this file is not supported yet
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
No repository
Supply chain riskPackage does not have a linked source code repository. Without this field, a package will have no reference to the location of the source code use to generate the package.
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
1
39673
8
985
1
+ Addedcommander@7.2.0(transitive)
+ Addedd3-dsv@3.0.1(transitive)
+ Addedd3-format@3.1.0(transitive)
+ Addedd3-time@3.1.0(transitive)
+ Addedd3-time-format@4.1.0(transitive)
+ Addediconv-lite@0.6.3(transitive)
+ Addedinternmap@2.0.3(transitive)
+ Addedvega-dataflow@5.7.6(transitive)
+ Addedvega-format@1.1.2(transitive)
+ Addedvega-loader@4.5.2(transitive)
+ Addedvega-scale@3.0.0(transitive)
+ Addedvega-time@2.1.2(transitive)
- Removedd3-dsv@1.2.0(transitive)
- Removediconv-lite@0.4.24(transitive)
- Removedvega-dataflow@4.1.0(transitive)
- Removedvega-loader@3.1.0(transitive)
- Removedvega-scale@2.5.1(transitive)
Updatedvega-canvas@^1.2.0
Updatedvega-dataflow@^5.0.0
Updatedvega-scale@^3.0.0
Updatedvega-statistics@^1.2.4
Updatedvega-util@^1.8.0