D3.js Template
HTML
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
<script src="//d3js.org/d3.v4.min.js"></script>
<div class="chart"></div>
CSS
* {
margin: 0;
padding: 0;
border: 0;
}
body {
background: #ffd;
}
svg {
border: 1px solid red;
background: grey;
}
JavaScript
var config = {};
config.height = 200;
config.width = 70;
config.axisLabel = "hyperactivity and concetration";
const chart = d3.select($('.chart')[0])
.append("svg")
.attr("width", config.width)
.attr("height", config.height);
const axisLabel = chart
.append("g");
axisLabel.append("text")
.attr("x", config.width / 2)
.attr("y", config.height)
.attr("dy", "-4px")
.style("text-anchor", "middle")
.text(config.axisLabel)
.call(wrap, config.width / 2)
function wrap(text, width) {
text.each(function() {
var text = d3.select(this),
words = text.text().split(/\s+/).reverse(),
word,
line = [],
lineNumber = 0,
lineHeight = 1.1, // ems
y = text.attr("y"),
x = text.attr("x"),
dy = parseFloat(text.attr("dy")),
tspan = text.text(null).append("tspan").attr("x", x).attr("y", y).attr("dy", dy + "em");
while (word = words.pop()) {
line.push(word);
tspan.text(line.join(" "));
if (tspan.node().getComputedTextLength() > width) {
line.pop();
tspan.text(line.join(" "));
line = [word];
tspan = text.append("tspan").attr("x", x).attr("y", y).attr("dy", ++lineNumber * lineHeight + dy + "em").text(word);
}
}
});
}