I have a bar chart that looks fine, but when I try to color it on click, the majority of the bars only get colored partially and I can't seem to figure out why.
Here's my js code:
var margin = {top: 20, right: 20, bottom: 70, left: 75},
width = 600 - margin.left - margin.right,
height = 300 - margin.top - margin.bottom;
var x = d3.scaleBand()
.rangeRound([0, width])
.padding(0.1);
var y = d3.scaleLinear().range([height, 0]);
var xAxis = d3.axisBottom()
.scale(x);
var yAxis = d3.axisLeft()
.scale(y)
.ticks(10);
d3.csv("numeric_oil_spill.csv", function(error, data) {
d3.select("svg").remove();
var svg = d3.select("body").append("svg")
.attr("width", width + margin.left + margin.right)
.attr("height", height + margin.top + margin.bottom)
.append("g")
.attr("transform",
"translate(" + margin.left + "," + margin.top + ")");
data.forEach(function(d) {
d.YEAR = +d.YEAR;
d.TEMP_FAR = +d.TEMP_FAR;
});
x.domain(data.map(function(d) { return d.YEAR; }));
y.domain([0, d3.max(data, function(d) { return +d.TEMP_FAR; })]);
svg.append("g")
.attr("class", "x axis")
.attr("transform", "translate(0," + height + ")")
.call(xAxis)
.selectAll("text")
.style("text-anchor", "end")
.attr("dx", "-.8em")
.attr("dy", "-.55em")
.attr("transform", "rotate(-70)" );
svg.append("g")
.attr("class", "y axis")
.call(yAxis)
.append("text")
.attr("transform", "rotate(-90)")
.attr("y", 6)
.attr("dy", ".71em")
.style("text-anchor", "end")
.text("Value ($)");
svg.selectAll("bar")
.data(data)
.enter().append("rect")
.style("fill", "steelblue")
.attr("x", function(d) { return x(d.YEAR); })
.attr("y", function(d) { return y(d.TEMP_FAR); })
.attr("width", x.bandwidth())
.attr("height", function(d) { return height - y(d.TEMP_FAR); })
.on("click", function (d) {
d3.selectAll('.allbars').style('fill', '#2296F3'); //fill all circles black
d3.select(this).style("fill", "#012B4E"); //then fill this circle lightcoral
}
)
});
Would really appreciate some help, have been stuck on this for days!
Attached my data snapshot, and what the problem looks like.