So, I made a bar chart visualization for a webapp, and it continuously updates over time as new data is entered in.
The webapp is here.
As you can see, I am able to adjust the width of the bars as new data is entered in. This was the code I used to do that:
function updateBarChart(currentInfo) {
var bars = d3.select("#bar-chart")
.selectAll(".bar")
.data(currentInfo)
.transition()
.style("width", function(d) { return String(xScale(d.prob)) + "px"; });
var h2s = d3.select("#bar-chart")
.selectAll("h2")
.data(currentInfo)
.transition()
.text(function(d) { return d.title; });
var h1s = d3.select("#bar-chart")
.selectAll("h1")
.data(currentInfo)
.transition()
.text(function(d) { return String(Math.round(d.prob * 100)) + "%"; });
bars.enter().append("div")
.attr("class", "bar")
.transition()
.style("width", function(d) { return String(xScale(d.prob)) + "px"; })
bars.exit().remove()
h2s.enter().append("h2")
.transition()
.text(function(d) { return d.title; })
h2s.exit().remove()
h1s.enter().append("h1")
.transition()
.text(function(d) { return String(Math.round(d.prob * 100)) + "%"; })
h1s.exit().remove()
}
The input to the function is a list of dictionaries that have the form
{ title: 'title',
prob: probability(float)
}
For some reason I keep getting the error "Uncaught TypeError: bars.enter is not a function". I've looked over the code, which was inspired mostly by this article on Medium.
Is there something wrong with my code here? I'm pretty new to d3 so I'm not entirely sure if I'm missing something important.
Any help is appreciated! Thanks!