function renderTeam (data, {
imageSize = 100,
columnLength = null,
bgdCol = '#abf5ed',
strokeCol = '#202d85',
strokeWidth = 6,
textBoxheight = 30
} = {}) {
const dataLength = data.length
const size = (imageSize + (strokeWidth * 2))
const columns = (columnLength !== null && columnLength > 1 ) ? columnLength : Math.floor(width / size)
const rows = Math.ceil((dataLength / columns))
const height = rows * (size + textBoxheight)
const svg = d3.create('svg')
.attr('width', width)
.attr('height', height + strokeWidth)
renderDefs(data)
function renderDefs (data) {
const defs = svg.append('defs')
.selectAll('pattern')
.data(data)
.join('pattern')
.attr('id', d => `p-${d.id}`)
.attr('width', 1)
.attr('height', 1)
.append('image')
.attr('xlink:href', d => `https://api.dicebear.com/7.x/avataaars/svg?seed=${d.name}`)
.attr('x', 5)
.attr('y', 0)
.attr('width', size)
.attr('height', size)
}
const scaleColumn = d3.scaleLinear()
.domain([0, columns])
.range([strokeWidth/2, columns * size])
const scaleRow = d3.scaleLinear()
.domain([0, rows])
.range([strokeWidth/2, height + strokeWidth/2])
const g = svg
.selectAll('g')
.data(data)
.join('g')
.attr('class', 'item')
.attr('transform', (d, i) => {
const x = i % columns
const y = Math.floor(i / columns)
return `translate(${scaleColumn(x)}, ${scaleRow(y)})`
})
g.append('rect')
.attr('width', size)
.attr('height', size + textBoxheight)
.attr('stroke', strokeCol)
.attr('stroke-width', strokeWidth)
.attr('class', 'item-bgd')
.attr('fill', bgdCol)
g.append('rect')
.attr('width', size)
.attr('height', size)
.attr('class', 'item-img')
.attr('fill', d => {
const id = `p-${d.id}`
return `url(#${id})`
})
g.append('text')
.attr('x', size/2)
.attr('y', size)
.attr('dy', '20px')
.text(d => d.name)
return svg.node()
}