Skip to content

Instantly share code, notes, and snippets.

@patiencehaggin
Created July 19, 2017 06:28
Show Gist options
  • Save patiencehaggin/e4c8926fbf8e5d2db5e2213fd92bcd6f to your computer and use it in GitHub Desktop.
Save patiencehaggin/e4c8926fbf8e5d2db5e2213fd92bcd6f to your computer and use it in GitHub Desktop.
Items by Name Try 2
license: mit
<!DOCTYPE html>
<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>
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);
// 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()
.rangeRound([0, width])
.paddingInner(0.05)
.align(0.1);
var yScale = d3.scaleLinear()
.rangeRound([height, 0]);
var colorScale = d3.scaleOrdinal()
.range(["#98abc5", "#8a89a6", "#7b6888"]);
xScale.domain(data.map(function(d) { return d.name; }));
yScale.domain([0, d3.max(data, function(d) { return d.total; })]).nice();
colorScale.domain(keys);
g.append("g")
.selectAll("g")
.enter().append("g")
//Append things
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 })
}
array.pop()
return array;
}
else if (item == "Carrots") {
array = []
for (var i in data) {
array.push({ name: data[i].name, carrots: data[i].carrots })
}
array.pop()
return array;
}
else if (item == "Phones") {
array = []
for (var i in data) {
array.push({ name: data[i].name, phones: data[i].phones })
}
array.pop()
return array;
}
}
// update
function update(data) {
var data = getItemData(data, d3.select("#menu").property("value"));
// Data join
var bars = g.selectAll("rect")
.data(d3.stack().keys(keys)(data))
// Exit
bars.exit().remove()
//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());
}
// main
d3.select("#menu")
.on("change", function(d) {
update(data);
});
});
</script>
</body>
name pens carrots phones
andrew 5 50 1
avra 0 25 1
dana 3 8 2
elaine 8 1 1
kyle 20 0 0
megan 20 22 8
nam 20 14 0
naoya 15 18 14
patience 6 3 89
virginia 4 18 2
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment