I wrote the following code to generate a word cloud in D3:
HTML
<html>
<head>
<title>D3 Word Cloud for Textual Data</title>
<link rel="stylesheet" href="style-sheet.css">
</head>
<body>
<script src="https://d3js.org/d3.v7.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/d3-cloud/1.2.5/d3.layout.cloud.js"></script>
<svg width="500" height="300"></svg>
<script src="data-script.js"></script>
</body>
</html>
CSS
.word {
font-size: 2em;
fill: steelblue;
}
JS
var data = ["hello", "world", "d3", "word", "cloud", "visualization"];
var svg = d3.select("svg"),
width = +svg.attr("width"),
height = +svg.attr("height"),
margin = { top: 20, right: 20, bottom: 30, left: 40 },
g = svg.append("g").attr("transform", "translate(" + margin.left + "," + margin.top + ")");
var layout = d3.layout
.cloud()
.size([width - margin.left - margin.right, height - margin.top - margin.bottom])
.words(data.map(function (d) { return { text: d, size: 10 + Math.random() * 20 }; }))
.padding(5)
.rotate(function () { return ~~(Math.random() * 2) * 90; })
.fontSize(function (d) { return d.size; })
.on("end", draw);
layout.start();
function draw(words) {
g.selectAll(".word")
.data(words)
.join("text")
.attr("class", "word")
.style("font-size", function (d) { return d.size + "px"; })
.style("fill", function (d, i) { return "steelblue"; })
.attr("text-anchor", "middle")
.attr("transform", function (d) {
return "translate(" + [d.x, d.y] + ")rotate(" + d.rotate + ")";
})
.text(function (d) { return d.text; });
}
But then I noticed the words are sometimes cut off and spilled over outside of the SVG element. I could not figure out how to fix it (other than playing with font size and padding which did not always work) so I asked for advice and I was told: Change the absolute positioning!
So, I modified my css code as following:
.word {
font-size: 2em;
fill: steelblue;
position: absolute;
}
But that did not work either...
How do I fix this issue?
Everything you done is perfect but the grouping element g has wrong translation values.
Use half of width and height to center the g element. Then every word will become inside of svg.
var data = ["hello", "world", "d3", "word", "cloud", "visualization"];
var svg = d3.select("svg"),
width = +svg.attr("width"),
height = +svg.attr("height"),
margin = { top: 20, right: 20, bottom: 30, left: 40 },
g = svg.append("g").attr("transform", "translate(" + width/2 + "," + height/2 + ")");//center the g element by taking half of height and width
var layout = d3.layout
.cloud()
.size([width - margin.left - margin.right, height - margin.top - margin.bottom])
.words(data.map(function (d) { return { text: d, size: 10 + Math.random() * 20 }; }))
.padding(5)
.rotate(function () { return ~~(Math.random() * 2) * 90; })
.fontSize(function (d) { return d.size; })
.on("end", draw);
layout.start();
function draw(words) {
g.selectAll(".word")
.data(words)
.join("text")
.attr("class", "word")
.style("font-size", function (d) { return d.size + "px"; })
.style("fill", function (d, i) { return "steelblue"; })
.attr("text-anchor", "middle")
.attr("transform", function (d) {
return "translate(" + [d.x, d.y] + ")rotate(" + d.rotate + ")";
})
.text(function (d) { return d.text; });
}
.word {
font-size: 1em;
fill: steelblue;
}
<script src="https://d3js.org/d3.v7.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/d3-cloud/1.2.5/d3.layout.cloud.js"></script>
<svg width="500" height="300"></svg>