You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I created a bar chart and made it responsive using "viewBox" attribute. I want to display a tooltip when user is hovering above or on a specific bar, so I added a background rect that is full size of svg and takes in all pointer events, and I found a way to see on which specific x-axis date user is hovering over. But when viewbox starts shrinking graph how can I find a coordinates where to display a tooltip, because I want to display it right under hovered bar?
Here is my code:
constwidth=620constheight=280constsvg=d3.select(".canvas").append('svg').attr('viewBox',`0 0 ${width}${height}`).attr('preserveAspectRatio','xMinYMin')constmargin={top:20,bottom:50,left: 20,right: 50}constgraphWidth=width-margin.left-margin.rightconstgraphHeight=height-margin.bottom-margin.topconstgraph=svg.append('g').attr('width',graphWidth).attr('height',graphHeight).attr('transform',`translate(${margin.left},${margin.top})`)constxAxisGroup=graph.append('g').attr('transform',`translate(0, ${graphHeight})`)constyAxisGroup=graph.append('g')d3.csv('./SixContinentFirst.csv').then(data=>{africaData=data.map(obj=>{return{infected: +(obj.Africa||'0'),date: obj.Dates}})consty=d3.scaleLinear().domain([0,d3.max(africaData,data=>data.infected)]).range([graphHeight,0])constx=d3.scaleBand().domain(africaData.map(item=>item.date)).range([0,graphWidth]).paddingInner(0.1).paddingOuter(0.1)x.invert=function(x){vardomain=this.domain();varrange=this.range()varscale=d3.scaleQuantize().domain(range).range(domain)returnscale(x)};constrects=graph.selectAll('rect').data(africaData).enter().append("g").attr("class","rect-container")// foreground rectrects.append('rect').attr('width',x.bandwidth).attr('height',d=>graphHeight-y(d.infected)).attr('fill','orange').attr('id',(d,i)=>`bar_${d.date}`).attr('class',"foreground-rect").attr('x',(d)=>x(d.date)).attr('y',d=>y(d.infected)).attr('rx',8).attr('ry',8).attr('pointer-events','none')//BACKGROUND RECTgraph.append('rect').style('fill','none').style("pointer-events","all").attr('width',graphWidth).attr('height',graphHeight).on('mousemove',function(e){varmouse=d3.pointer(e,this)varmouseDate=x.invert(mouse[0])// The Date user is hovering over})constxAxis=d3.axisBottom(x).tickFormat((d,i)=>i%6===0 ? d : '')letformatter=Intl.NumberFormat('en',{notation: 'compact'});constyAxis=d3.axisRight(y).ticks(3).tickFormat(d=>formatter.format(+d))xAxisGroup.call(xAxis)yAxisGroup.call(yAxis).attr('transform',`translate(${graphWidth}, 0)`).call(g=>g.select('.domain').remove()).call(g=>g.selectAll('line').remove()).selectAll('text').attr("font-size","10")xAxisGroup.call(g=>g.select('.domain').remove()).call(g=>g.selectAll('line').remove()).selectAll('text').attr("font-size","10")})
reacted with thumbs up emoji reacted with thumbs down emoji reacted with laugh emoji reacted with hooray emoji reacted with confused emoji reacted with heart emoji reacted with rocket emoji reacted with eyes emoji
-
I created a bar chart and made it responsive using "viewBox" attribute. I want to display a tooltip when user is hovering above or on a specific bar, so I added a background rect that is full size of svg and takes in all pointer events, and I found a way to see on which specific x-axis date user is hovering over. But when viewbox starts shrinking graph how can I find a coordinates where to display a tooltip, because I want to display it right under hovered bar?
Here is my code:
Beta Was this translation helpful? Give feedback.
All reactions