{
const height = 500
const svg = d3.select(DOM.svg(width, height))
const margin = { left: 60, top: 20, right: 20, bottom: 50 }
const song = _.find(spotifySongs, { trackName })
const data = spotifyDailyGlobalRanking
.filter(d => d.trackId === song.trackId && d.date.substring(0, 7) === selectedMonth)
const xScale = d3.scaleBand()
.padding(0.1)
.range([margin.left, width - margin.right])
.domain(_.range(1, moment(selectedMonth).daysInMonth() + 1))
.paddingInner(.1)
.paddingOuter(0.05)
const yScale = d3.scaleLinear()
.range([height - margin.bottom, margin.top])
.domain([0, d3.max(data.map(d=> d.streams))])
svg.append('g')
.call(d3.axisBottom(xScale))
.attr('transform', `translate(0,${height - margin.bottom})`)
//defining tooltip for the bars
const tooltipSpotify = d3tip()
.style('border', 'solid 2px Green')
.style('border-radius', '4px')
.style('background-color', 'lightgreen')
.style('font-family', 'georgia')
.html((d) => `
<div>
Date (date): ${moment(d.date).date()} <br/>
Streams: ${d.streams} <br/>
</div>`)
svg.call(tooltipSpotify)
//defining the ticks
svg.append('g')
.call(d3.axisLeft(yScale).tickFormat(d3.format('.2s')))
.attr('transform', `translate(${margin.left},0)`)
//plotting data
svg.selectAll('rect')
.data(data)
.enter()
.append('rect')
.attr('x', d => xScale(moment(d.date).date()))
.attr('y', d => yScale(d.streams))
.attr('height', d => yScale(0) - yScale(d.streams))
.attr('width', xScale.bandwidth())
.attr('fill', 'SteelBlue')
.on('mouseover', function(d, i){
tooltipSpotify.show(i,this);
})
.on('mouseout', tooltipSpotify.hide)
.append('title')
.text(d => `${d.date}: ${Math.round(d.streams)}`)
// x label
svg.append("text")
.attr("class", "x label")
.attr("text-anchor", "end")
.attr("x", width/2)
.attr("y", height-5)
.text("Date(date)")
// y label
svg.append("text")
.attr("class", "y label")
.attr("text-anchor", "end")
.attr("y", 2)
.attr("dy", ".85em")
.attr("transform", "rotate(-90)")
.text("Streams");
return svg.node()
}
/******************************************************************************
* END OF YOUR CODE *
******************************************************************************/