const width = 500;
const height = 500;
let svg = d3.select('svg')
// create the rectangles to hover over
let rectangleHolders = svg.select('.rectangle-holder')
.selectAll('rect')
.data([1, 2, 3, 4, 5])
.enter()
// to add a rectangle with text, we need to add a group to hold both elements
.append('g')
// translate group to correct position
.attr('transform', (d, i) => `translate(${i*40})`)
// add rectangle to each group
rectangleHolders.append('rect')
.attr('height', 18)
.attr('width', 18)
// add event handler to rectangle to draw circles
.on('mouseover', draw)
// add text to each group
rectangleHolders.append('text')
.attr('dy','15')
.attr('dx','10')
.attr('text-anchor','middle')
.text(d => d)
// function to draw circles
function draw(number) {
console.log('draw', number)
// create some dummy data in this case an
// array of length 'count' full of nulls
const data = Array(number)
// select circle elements and bind data
let circles = svg.select('.circle-holder').selectAll('circle')
.data(data)
// enter, append and set attributes
circles.enter()
.append('circle')
.attr('cx', (d, i) => i * 20 + 10)
.attr('cy', 10)
.attr('r', 8)
.attr('value', d => d)
// add a tranisition to the opacity so the circles fade in
.attr('opacity', 0)
.transition()
.attr('opacity', 1)
// exit and remove unused elements (with transition)
circles.exit()
.transition()
.attr('opacity', 0)
.remove()
}
rect {
fill: none;
stroke: black;
}
body {
font-family:sans-serif;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/d3/5.7.0/d3.min.js"></script>
<svg height="40" width="200">
<g class="rectangle-holder"></g>
<g class="circle-holder" transform="translate(0,20)"></g>
</svg>