Published
Edited
Jun 19, 2019
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
example = wordCloud(letters,'word','frequency',30,'cluster')
// declare type column only if you have a label you want to use to color bubbles
Insert cell
Insert cell
Insert cell
Insert cell
wordCloud = (data,word,frequency,maxSize,type) => {
if (type===undefined) {
data.map(d => d.type = 0)
}
const height = 500;
// create the SVG container
let svg = d3.select(html`<svg></svg>`)
.attr('width', width)
.attr('height', height);
const colors = d3.schemeSet1
.slice(0,new Set(data.map(d=> d[type])).size)
// color text black or white according to the luminance value of the node
const luminance = 50;
const textColor = d3.scaleQuantile().range(["#fff", "#000"]).domain([0, luminance, 100]);
// define scale for radius
const r = d3.scaleSqrt()
.domain([0, d3.max(data, d => d[frequency])])
.range([0, maxSize]);
// define the simualtion engine
const simulation = d3.forceSimulation(data)
.force("x", d3.forceX().strength(0.05)) // increasing this value place bubbles forcing a vertcial placement
.force("y", d3.forceY().strength(0.008))
.force("collide", d3.forceCollide().radius(d=> r(d[frequency]) + 0.5).iterations(2))
.force('center', d3.forceCenter(width/2, height/2))
// create a layer or group
let gBubble = svg
.selectAll('gBubble')
.data(data);
gBubble.exit().remove();
let bubble = gBubble.enter()
.append('g')
.classed('gBubble', true)
.attr('id',d => d[word]);
bubble
.append('circle')
.attr('r', d => r(d[frequency]))
.attr('fill', d => colors[+d[type]])
.attr('fill-opacity', 0.4)
.attr('stroke', d => colors[+d[type]])
.attr('stroke-width', 1)

let text= bubble
.append('text');
const textLabels = text
.text( d => (d[word]))
.style('text-anchor','middle')
.attr("dominant-baseline", "central")
.attr('font-family', 'sans-serif')
.attr('font-size', '12px')
.attr('font-weight','normal')
.attr('fill', d => textColor(d3.hcl(colors[+d[type]]).l))
gBubble = gBubble
.merge(bubble);
gBubble.call(drag(simulation));
simulation.on('tick', () => {
gBubble
.attr('transform', d => 'translate('+ (d.x) +','+ (d.y)+')');
})

return svg.node();
}
Insert cell
Insert cell
Insert cell
lista = letters.map(d=> d.cluster)
Insert cell
Insert cell
insieme = new Set(lista)
Insert cell
Insert cell
insieme.size
Insert cell

Purpose-built for displays of data

Observable is your go-to platform for exploring data and creating expressive data visualizations. Use reactive JavaScript notebooks for prototyping and a collaborative canvas for visual data exploration and dashboard creation.
Learn more