barchartwithbrush = {
const svg = d3.create("svg")
.attr("width", width)
.attr("height", height+ minibarHeight)
.attr("viewBox",[0, 0, width, height+ minibarHeight]);
svg.append('defs')
.append('clipPath')
.attr('id', 'clip')
.append('rect')
.attr('x', margin.left)
.attr('y', margin.top)
.attr('width', width)
.attr('height', height);
const main = svg.append('g')
.attr('class', 'main')
.attr('clip-path', 'url(#clip)');
const tooltip = d3.select('body')
.append('div')
.attr('id', 'barchart-tooltip')
.style('position', 'absolute')
.style('z-index', '1')
.style('visibility', 'hidden')
.style('padding', '10px')
.style('background', 'rgba(0,0,0,0.6)')
.style('border-radius', '4px')
.style('color', '#fff');
const mainBarsGroup = main.append("g")
.attr("class", "mainBarsGroup");
const miniGroup = svg.append("g")
.attr("class", "miniGroup")
.attr("transform", "translate(0," + height + ")");
mainBarsGroup.selectAll('rect')
.data(barchartData2)
.join('rect')
.attr('x', d => xScaleBrush(d.code))
.attr('y', d => yScale(d.cases))
.attr('width', xScaleBrush.bandwidth())
.attr('height', d => yScale(0) - yScale(d.cases))
.attr('fill', 'purple')
.on("mouseover", function(e,d,i) {
let tooltipWidth = tooltip.node().offsetWidth;
let tooltipHeight = tooltip.node().offsetHeight;
tooltip
.style("left", e.pageX - tooltipWidth/2 +'px')
.style("top", e.pageY-tooltipHeight - 10+'px')
.style('visibility', 'visible')
.html(`<b>Country</b>: ${d.cname} <br/><b>Cases</b>: ${d.cases}`);
d3.select(this).attr("fill", "steelblue");})
.on('mousemove', function (e) {
let tooltipWidth = tooltip.node().offsetWidth;
let tooltipHeight = tooltip.node().offsetHeight;
tooltip
.style("left", e.pageX - tooltipWidth/2 +'px')
.style("top", e.pageY-tooltipHeight - 10+'px')
})
.on("mouseout", function(e,d) {
tooltip
.style('visibility', 'hidden')
d3.select(this).attr("fill", "purple");});
const gx = mainBarsGroup.append("g").call(mainXAxis);
const gy = svg.append("g").attr("fill", "white").call(yAxis);
miniGroup.selectAll('rect')
.data(barchartData2)
.join('rect')
.attr('x', d => miniX(d.code))
.attr('y', d => miniY(d.cases))
.attr('width', miniX.bandwidth())
.attr('height', d => miniY(0) - miniY(d.cases))
.attr('fill', 'steelblue');
const brush = d3.brushX()
.extent([[minimargin.left, height + minimargin.top],
[width - minimargin.right, height + minibarHeight- minimargin.bottom]])
.on("start brush end", brushed);
svg.append("g")
.call(brush)
.call(brush.move, [barchartData2[0].code,barchartData2[15].code].map(miniX)); //Initially the first 15 are selected
function brushed({selection}) {
if (selection === null) {
miniGroup.attr("stroke", null);
} else {
const originalRange = mainXZoom.range();
mainXZoom.domain(selection);
xScaleBrush.range([mainXZoom(originalRange[0]), mainXZoom(originalRange[1])]);
mainBarsGroup.selectAll('rect')
.data(barchartData2)
.join('rect')
.attr('x', d => xScaleBrush(d.code))
.attr('y', d => yScale(d.cases))
.attr('width', xScaleBrush.bandwidth())
.attr('height', d => yScale(0) - yScale(d.cases))
.attr('fill', 'purple')
gx.call(mainXAxis);
}
}
// Specific to Observable
return svg.node();
}