I am new to D3.js. I am trying to use the content of this .csv file to make a stacked bar chart. The x axis represents the movie's names and the y axis represents the box office. each stacked bar will represent the budget and the profit(box office -budget) of each movie.
This is the csv:
MovieTitle,Budget,Box Office
Harry Potter and the Philosopher's Stone,"$125,000,000 ","$1,002,000,000 "
Harry Potter and the Chamber of Secrets,"$100,000,000 ","$880,300,000 "
Harry Potter and the Prisoner of Azkaban,"$130,000,000 ","$796,700,000 "
Harry Potter and the Goblet of Fire,"$150,000,000 ","$896,400,000 "
Harry Potter and the Order of the Phoenix,"$150,000,000 ","$942,000,000 "
Harry Potter and the Half-Blood Prince,"$250,000,000 ","$943,200,000 "
Harry Potter and the Deathly Hallows Part 1,"$200,000,000 ","$976,900,000 "
Harry Potter and the Deathly Hallows Part 2,"$250,000,000 ","$1,342,000,000 "
This is what i have done so far:
<!DOCTYPE html>
<style>
.axis .domain {
display: none;
}
</style>
<svg width="960" height="500"></svg>
<script src="https://d3js.org/d3.v4.min.js"></script>
<script>
var svg = d3.select("svg"),
margin = {top: 20, right: 20, bottom: 50, left: 40},
width = +svg.attr("width") - margin.left - margin.right,
height = +svg.attr("height") - margin.top - margin.bottom,
g = svg.append("g").attr("transform", "translate(" + margin.left + "," + margin.top + ")");
var x = d3.scaleBand()
.rangeRound([0, width])
.paddingInner(0.05)
.align(0.1);
var y = d3.scaleLinear()
.rangeRound([height, 0]);
var z = d3.scaleOrdinal()
.range([ "#d0743c", "#ff8c00"]);
d3.csv("title&price.csv", function(d) {
return {
MovieTitle : d.MovieTitle,
Budget : Number(d['Budget'].trim().slice(1).replaceAll(',', '')),
Box_Office : Number(d['Box Office'].trim().slice(1).replaceAll(',', '')),
Profit : Number(d['Box Office'].trim().slice(1).replaceAll(',', '')) - Number(d['Budget'].trim().slice(1).replaceAll(',', ''))
};
console.log(data);
}, function(error, data) {
if (error) throw error;
var keys = data.columns.slice(1);
console.log(data);
data.sort(function(a, b) { return b.Box_Office - a.Box_Office; });
x.domain(data.map(function(d) { return d.MovieTitle; }));
y.domain([0, d3.max(data, function(d) { return d.Box_Office; })]).nice();
z.domain(keys);
g.append("g")
.selectAll("g")
.data(d3.stack().keys(keys)(data))
.enter().append("g")
.attr("fill", function(d) { return z(d.key); })
.selectAll("rect")
.data(function(d) { return d; })
.enter().append("rect")
.attr("x", function(d) { return x(d.data.MovieTitle); })
.attr("y", function(d) { return y(d[1]); })
.attr("height", function(d) { return y(d[0]) - y(d[1]); })
.attr("width", x.bandwidth());
g.append("g")
.attr("class", "axis")
.attr("transform", "translate(0," + height + ")")
.call(d3.axisBottom(x))
.selectAll("text")
.style("text-anchor", "end")
.attr("dx", "em")
.attr("dy", ".90em")
.attr("transform", "rotate(10)");
g.append("g")
.attr("class", "axis")
.call(d3.axisLeft(y).ticks(null, "s"))
.append("text")
.attr("x", 2)
.attr("y", y(y.ticks().pop()) + 0.5)
.attr("dy", "0.32em")
.attr("fill", "#000")
.attr("font-weight", "bold")
.attr("text-anchor", "start")
.text("Money");
var legend = g.append("g")
.attr("font-family", "sans-serif")
.attr("font-size", 10)
.attr("text-anchor", "end")
.selectAll("g")
.data(keys.slice().reverse())
.enter().append("g")
.attr("transform", function(d, i) { return "translate(0," + i * 20 + ")"; });
legend.append("rect")
.attr("x", width - 19)
.attr("width", 19)
.attr("height", 19)
.attr("fill", z);
legend.append("text")
.attr("x", width - 24)
.attr("y", 9.5)
.attr("dy", "0.32em")
.text(function(d) { return d; });
});
</script>