{
const svg = d3.select(DOM.svg(1300, 860));
let words = randomHCLColors.map(d => ({
text: d.input,
name: d.input,
color: d.target
}));
let rectWidth = 185;
let rectHeight = 30;
let columnSize = 4;
function draw(words) {
let region = svg
.append('g')
.attr('transform', `translate(${rectWidth / 2 + 10}, ${0})`);
let groups = region
.selectAll("g.rect")
.data(words)
.enter()
.append("g")
.attr('class', 'rect')
.attr("transform", (d, i) => {
let x = (i % columnSize) * (rectWidth + 15);
let y = Math.floor(i / columnSize) * (rectHeight + 42);
return `translate(${x}, ${y})`;
});
groups
.append('rect')
.attr('x', -rectWidth / 2)
.attr('rx', 2)
.attr('width', rectWidth)
.attr('height', rectHeight)
.style('stroke', 'hsla(0, 0%, 50%)')
.style('fill', d => {
let color = getHCL(d.text);
let colorC = chroma.hcl(color[0], color[1], color[2]);
console.log(colorC.hex());
return colorC.hex();
});
groups
.append("text")
.style("font-size", 13)
.style("font-family", "-apple-system, BlinkMacSystemFont")
.style('fill', 'black')
.attr('y', rectHeight + 5)
.attr('dominant-baseline', 'hanging')
.attr('text-anchor', 'middle')
.text(d => d.text);
groups
.append("text")
.style("font-size", 13)
.style("font-family", "-apple-system, BlinkMacSystemFont")
.style('fill', 'black')
.attr('y', rectHeight + 18)
.attr('dominant-baseline', 'hanging')
.attr('text-anchor', 'middle')
.text(d => d.color);
}
draw(words);
return svg.node();
}