Published
Edited
Jul 1, 2019
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
md`## Layout variables`
Insert cell
margin = ({
t:70,
l: 100,
r: 100,
b: 70,
})
Insert cell
Insert cell
md`## Patents clustered data`
Insert cell
rawData = d3.csv('https://raw.githubusercontent.com/PaganelliL/progettoneP/master/labelNew.csv').then(
function(data) {
data.forEach(function(d) {
d['Anno']= +d['anno'],
d['Parola']= d['words'],
d['Cluster']= +d['cluster'],
d['Npat']= +d['Npat']
})
return data
})
Insert cell
sortedData = rawData.sort(function(a, b) {
return d3.ascending(a.Anno, b.Anno) || d3.ascending(a['Cluster'], b['Cluster']);
});
Insert cell
rawD1 = sortedData.filter(function (d) {return d['Anno']<2014;});
Insert cell
rawD2 = sortedData.filter(function (d) {return d['Anno']>=2014;});
Insert cell
md`## Scales`
Insert cell
ext1 = ({
years: d3.extent(rawD1,d=>+d.Anno),
clusters: d3.extent(rawD1, d => +d.Cluster),
Npat: d3.extent(rawD1, d => +d.Npat)
})
Insert cell
ext2 = ({
years: d3.extent(rawD2,d=>+d.Anno),
clusters: d3.extent(rawD2, d => +d.Cluster),
Npat: d3.extent(rawD2, d => +d.Npat)
})
Insert cell
xScale1 = d3.scaleLinear()
.domain(ext1.years)
.range([0+ margin.l, width - margin.r]);
Insert cell
Insert cell
Insert cell
// rScale = d3.scaleLinear()
// .domain(ext1.Npat)
// //.range([20.0, 60.0]);
// .range([15.0, 45.0]);
rScale = d3.scaleSqrt()
.domain(ext1.Npat)
//.range([20.0, 60.0]);
.range([15.0, 30.0]);
Insert cell
//colors = d3.schemeSet1.slice(0, new Set(rawData.map(d=> d['Cluster'])).size)
colors = d3.scaleOrdinal().domain(d3.extent(rawData, d => +d.Cluster)).range(d3.schemeSet3);
//colors=d3.schemePaired
//colors = d3.interpolatePRGn(d3.extent(rawData,d=>+d.Anno))
Insert cell
colors
Insert cell
Insert cell
plotGRAPH1 = wordCloud(rawD1, xScale1, xAxis1, 'Parola','Cluster','Anno')
// declare type column only if you have a label you want to use to color bubbles
Insert cell
plotGRAPH2 = wordCloud(rawD2, xScale2, xAxis2, 'Parola','Cluster','Anno')
Insert cell
wordCloud = (data, xScale, xAxis, word,type,year) => {
if (type===undefined) {
data.map(d => d.type = 0)
}
const radius = 5
const padding = 1;
// 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]);
const simulation = d3.forceSimulation(data)
.force("x", d3.forceX(d => xScale(d[year])).strength(3))
.force("y", d3.forceY(d => yScale(d[type])).strength(0.6))
.force("collide", d3.forceCollide().radius(d=> rScale(+d['Npat']) + padding).iterations(50))
.alpha(0.5)
.restart()
// 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 => rScale(+d['Npat']))
//.attr('r', d => radius)
//.attr('fill', d => colors[+d[type]])
.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('pointer-events','none')
.attr('font-family', 'sans-serif')
.attr('font-size', '12px')
.attr('font-weight','normal')
.attr("transform", "rotate(-30)" )
.attr('fill', d => textColor(d3.hcl(colors[+d[type]]).l))
gBubble = gBubble
.merge(bubble);
gBubble.call(drag(simulation));
simulation.nodes(data)
.on('tick', () => {
gBubble
.attr('transform', d => 'translate('+ (d.x) +','+ (d.y)+')');
})

//axis
svg.append("g")
.call(xAxis);
svg.append("g")
//.call(yAxis);
;
return Object.assign(svg.node());
}
Insert cell
x1 = d3.scaleLinear()
.domain(ext1.years)
.range([margin.l, width - margin.r])
Insert cell
x2 = d3.scaleLinear()
.domain(ext2.years)
.range([margin.l, width - margin.r])
Insert cell
xAxis1 = g => g
.attr("transform", `translate(0,${height - margin.b})`)
.call(d3.axisBottom(x1).ticks(ext1.years[1]-ext1.years[0]))
Insert cell
xAxis2 = g => g
.attr("transform", `translate(0,${height - margin.b})`)
.call(d3.axisBottom(x2).ticks(ext2.years[1]-ext2.years[0]))
Insert cell
y = d3.scaleBand()
.domain(sortedData.map(d => d.Cluster))
.range([height - margin.b, 0])
Insert cell
// yAxis = g => g
// .attr("transform", `translate(${margin.l},0)`)
// .call(d3.axisLeft(y).ticks(ext.clusters[1]-ext.clusters[0]))
// //.call(d3.axisLeft(y).ticksize(0))
Insert cell
Insert cell
gData=d3.nest()
.key(d => d.Parola)
.key(d => d.Anno)
.rollup(v => d3.sum(v, e => +e.Cluster))
.entries(sortedData)
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