Built with blockbuilder.org
xxxxxxxxxx
<head>
<meta charset="utf-8">
<script src="jquery-3.2.1.min.js"></script>
<style>
body { margin:0;position:fixed;top:0;right:0;bottom:0;left:0; }
.axis .domain {
display: none;
}
</style>
</head>
<body>
<select id="menu">
<option value="all">All items</option>
<option value="Pens">Pens</option>
<option value="Carrots">Carrots</option>
<option value="Phones">Phones</option>
</select>
<svg width="960" height="500"></svg>
<script src="https://d3js.org/d3.v4.min.js"></script>
<script>
// render code was right, update was wrong
// get the update code correct, and call the update code on the initial render
// that way when you call it on the initial load it should call the correct thing
var svg = d3.select("svg"),
margin = {top: 20, right: 20, bottom: 30, 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 + ")");
d3.csv("items.csv", function(d, i, columns) {
for (i = 1, t = 0; i < columns.length; ++i) t += d[columns[i]] = +d[columns[i]];
d.total = t;
return d;
}, function(error, data) {
if (error) throw error;
var originalData = data;
var keys = data.columns.slice(1);
var originalKeys = keys;
// Sort the bars so they show up tallest to shortest
// Might have to move this into the update function?
data.sort(function(a, b) { return b.total - a.total; });
// Set scales
var xScale = d3.scaleBand()
.domain(data.map(function(d) { return d.name; }))
.rangeRound([0, width])
.paddingInner(0.05)
.align(0.1);
var yScale = d3.scaleLinear()
.domain([0, d3.max(data, function(d) { return d.total; })]).nice()
.rangeRound([height, 0]);
var colorScale = d3.scaleOrdinal()
.domain(keys)
.range(["#98abc5", "#8a89a6", "#7b6888"]);
/*
g.append("g")
.selectAll("g")
.enter().append("g")
*/
function chart() {
g.append("g")
.selectAll("g")
.data(d3.stack().keys(keys)(data))
.enter().append("g")
.attr("fill", function(d) { return colorScale(d.key); })
.selectAll("rect")
.data(function(d) { return d; })
.enter().append("rect")
.attr("x", function(d) { return xScale(d.data.name); })
.attr("y", function(d) { return yScale(d[1]); })
.attr("height", function(d) { return yScale(d[0]) - yScale(d[1]); })
.attr("width", xScale.bandwidth());
g.append("g")
.attr("class", "axis")
.attr("transform", "translate(0," + height + ")")
.call(d3.axisBottom(xScale));
g.append("g")
.attr("class", "axis")
.call(d3.axisLeft(yScale).ticks(null, "s"))
.append("text")
.attr("x", 2)
.attr("y", yScale(yScale.ticks().pop()) + 0.5)
.attr("dy", "0.32em")
.attr("fill", "#000")
.attr("font-weight", "bold")
.attr("text-anchor", "start")
.text("Items");
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", colorScale);
legend.append("text")
.attr("x", width - 24)
.attr("y", 9.5)
.attr("dy", "0.32em")
.text(function(d) { return d; });
}
// getters
function getItemData(data, item) {
if (item == "all") {
return originalData;
}
else if (item == "Pens") {
array = []
for (var i in data) {
array.push({ name: data[i].name, pens: data[i].pens })
}
return array;
}
else if (item == "Carrots") {
array = []
for (var i in data) {
array.push({ name: data[i].name, carrots: data[i].carrots })
}
return array;
}
else if (item == "Phones") {
array = []
for (var i in data) {
array.push({ name: data[i].name, phones: data[i].phones })
}
return array;
}
}
function getKeys(data, item) {
if (item == "all") {
return originalKeys;
}
else if (item == "Pens") {
return ["name", "pens"];
}
else if (item == "Carrots") {
return ["name", "carrots"];
}
else if (item == "Phones") {
return ["name", "phones"];
}
}
// update
function update(data) {
var data = getItemData(data, d3.select("#menu").property("value"));
var keys = getKeys(data, d3.select("#menu").property("value"));
// for cleanliness, deal with the global key too
// Data join
chart();
var bars = g.selectAll("rect")
.data(d3.stack().keys(keys)(data))
/*
// Exit
//Append things
g.append("g")
.selectAll("g")
.data(d3.stack().keys(keys)(data))
.enter().append("g")
.attr("fill", function(d) { return colorScale(d.key); })
var rect = bars.selectAll("rect")
.data(function(d) { return d; })
.enter().append("rect")
.attr("x", function(d) { return xScale(d.data.name); })
.attr("y", function(d) { return yScale(d[1]); })
.attr("height", function(d) { return yScale(d[0]) - yScale(d[1]); })
.attr("width", xScale.bandwidth());
*/
}
// use update to call the initial render
update(data);
// listener
d3.select("#menu")
.on("change", function(d) {
update(data);
});
});
</script>
</body>
https://d3js.org/d3.v4.min.js