bubbleChart = {
const svg = d3.create('svg').attr('width',width).attr('height',height)
const g = svg.append('g').attr('transform',`translate(${margin.left},${margin.top})`)
const xAxis = svg.append('g').attr('transform',`translate(${margin.left},${height - margin.bottom})`)
.call(d3.axisBottom(xPointScale))
const yAxis = svg.append('g').attr('transform',`translate(${margin.left},${margin.top})`)
.call(d3.axisLeft(yScale))
const circles = g.selectAll('g')
.data(unemploymentState)
.join('g')
.attr('transform', d => `translate(${xPointScale(d.stateAbbr)},${yScale(d.rate)})`)
circles.append('circle')
.attr('r',d => scaleRad(d.rate))
.attr('fill',d => stateColor(d.stateAbbr))
.attr('id','marks')
.on('mouseover',mouseEnter)
.on('click',circleClicked)
circles.append('text')
.attr('dy',2)
.text(d => d.rate)
.attr('id','rates')
.attr('visibility','hidden')
function circleClicked(event){
const shape = d3.select(this);
const radius = shape.attr('r');
shape
.attr('r',radius/2)
tooltip.attr('visibility','hidden')
}
//Idea of tooltip is creating completely seperate variable and attach the g-element to the main g element
const tooltip = g.append('g')
.attr('visibility', 'hidden');
const tooltipHeight = 16;
// add a rectangle to the tooltip to serve as a background
const tooltipRect = tooltip.append('rect')
.attr('fill', 'black')
.attr('rx', 5)
.attr('height', tooltipHeight);
// add a text element to the tooltip to contain the label
const tooltipText = tooltip.append('text')
.attr('fill', 'white')
.attr('font-family', 'sans-serif')
.attr('font-size', 12)
.attr('y', 2) // offset it from the edge of the rectangle
.attr('x', 3) // offset it from the edge of the rectangle
.attr('dominant-baseline', 'hanging')
function mouseEnter(event, d) {
// make the circle larger
const selection = d3.select(this)
const radius = selection.attr('r')
//d3.select(this)
// .attr('r', radius * 2);
// update the label's text and get its width
tooltipText.text(d.rate);
const labelWidth = tooltipText.node().getComputedTextLength();
// set the width of the tooltip's background rectangle
// to match the width of the label, plus some extra space
tooltipRect.attr('width', labelWidth + 6);
// move the tooltip to the position of the circle (offset by a bit)
// and make the tooltip visible
const xPos = xPointScale(d.stateAbbr) + radius * 3;
const yPos = yScale(d.rate) - tooltipHeight / 2;
tooltip.attr('transform', `translate(${xPos},${yPos})`)
.attr('visibility', 'visible');
}
return svg.node()
}