Skip to content

Instantly share code, notes, and snippets.

@LiangGou
Last active April 30, 2016 06:40
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save LiangGou/464ce0523f42e8db462357e3e8f590dd to your computer and use it in GitHub Desktop.
Save LiangGou/464ce0523f42e8db462357e3e8f590dd to your computer and use it in GitHub Desktop.
Transit small multiples from area charts to path bar charts

This is an example to transit small multiples from area charts to bar charts. The bar charts are implemented with path, not rect SVG element.

<!DOCTYPE html>
<meta charset="utf-8">
<style>
svg {
font: 10px sans-serif;
padding: 10px;
}
.frame {
shape-rendering: crispEdges;
}
.frame {
fill: none;
stroke: #aaa;
stroke-opacity: 0.75;
stroke-width: 0.5;
}
.feature-cell {
opacity: 0.6;
}
.feature-cell:hover {
opacity: 1;
}
.feature-render {
*fill: lightsteelblue;
fill-opacity: 0.6;
stroke-width: 0;
}
.feature-line {
*stroke: lightsteelblue;
stroke-opacity: 1;
stroke-width: 1;
fill-opacity: 0;
}
</style>
<body>
<input name="btnToBar" type="button" value="Bar" onclick="toBar()" />
<input name="btnToArea" type="button" value="Area" onclick="toArea()" />
<script src="http://d3js.org/d3.v3.min.js"></script>
<script>
var width = 960,
size = 150,
cw = 80, //cell width
ch = 20, //cell hight
padding = 4,
nCol = 10; //Math.floor((width - padding) / size);
var color = d3.scale.category20();
var x = d3.scale.linear()
.range([padding / 2, cw - padding / 2]);
var y = d3.scale.linear()
.range([ch - padding / 2, padding / 2]);
var farea = d3.svg.area()
.interpolate("basis")
.x(function(d, i) {
return x(i);
})
.y0(ch - padding / 2)
.y1(function(d) {
return y(d);
});
var farea0 = d3.svg.area()
.interpolate("basis")
.x(function(d, i) {
return x(i);
})
.y0(ch - padding / 2)
.y1(function(d) {
return y(0);
});
var fline = d3.svg.line()
.interpolate("basis")
.x(function(d, i) {
return x(i);
})
.y(function(d) {
return y(d);
});
var fline0 = d3.svg.line()
.interpolate("basis")
.x(function(d, i) {
return x(i);
})
.y(function(d) {
return y(0);
});
function generateData(nFeatures, mBins) {
return d3.range(nFeatures).map(function(i) {
return {
"name": "feature" + i,
"col": i % nCol,
"row": Math.floor(i / nCol),
"bins": d3.range(mBins).map(function(j) {
return Math.random();
})
};
});
}
var fdata = generateData(100, 12);
//console.log("fdata", fdata);
var svg = d3.select("body").append("svg")
.attr("width", width)
.attr("height", ch * (Math.floor(fdata.length / nCol) + 1) + padding)
.append("g")
.attr("transform", "translate(" + padding + "," + padding / 2 + ")");
var cell = svg.selectAll(".feature-cell")
.data(fdata)
.enter().append("g")
.attr("class", "feature-cell")
.attr("transform", function(d) {
return "translate(" + d.col * cw + "," + d.row * ch + ")";
})
.each(featurePlot);
function featurePlot(f) {
var _cell = d3.select(this);
_cell.append("rect")
.attr("class", "frame")
.attr("x", padding / 2)
.attr("y", padding / 2)
.attr("width", cw - padding)
.attr("height", ch - padding);
x.domain([0, f.bins.length - 1]);
y.domain([0, 1]);
_cell.append("path")
.datum(f.bins)
.attr("class", "feature-render")
.style("fill", function() {
return color(f.name);
})
//.style("stroke", function() {return color(f.name);})
.attr("d", farea0)
.transition()
.duration(750)
.attr("d", farea);
_cell.append("path")
.datum(f.bins)
.attr("class", "feature-line")
.style("stroke", function() {
return color(f.name);
})
.attr("d", fline0)
.transition()
.duration(750)
.attr("d", fline);
}
d3.select(self.frameElement).style("height", ch * (Math.floor(fdata.length / nCol) + 1) + padding + 20 + "px");
function pathBar(bins) {
var path = [],
i = -1,
n = bins.length,
d,
bw = ((cw - padding) / (n + 1)).toFixed(1); //bar width based on the # of bins and cell width; (n+1) is the calculation adjustment by considering the bar width (the last bar takes an extra bar width);
while (++i < n) {
d = bins[i];
path.push("M", x(i * n / (n + 1)), ",", y(0), "V", y(d), "h", bw, "V", y(0)); //
// n/(n+1) is the calculation adjustment by considering the bar width (the last bar takes an extra bar width);
// y(0) is actually at the bottom: ch - padding / 2
}
return path.join("");
}
function pathBar0(bins) {
var path = [],
i = -1,
n = bins.length,
d,
bw = ((cw - padding) / (n + 1)).toFixed(1); //bar width based on the # of bins and cell width; (n+1) is the calculation adjustment by considering the bar width (the last bar takes an extra bar width);
while (++i < n) {
d = bins[i];
path.push("M", x(i * n / (n + 1)), ",", y(0), "V", y(0), "h", bw, "V", y(0)); //
// n/(n+1) is the calculation adjustment by considering the bar width (the last bar takes an extra bar width);
// y(0) is actually at the bottom: ch - padding / 2
}
return path.join("");
}
function toBar() {
//alert("clicked");
d3.selectAll(".feature-render")
.transition()
.duration(750)
.attr("d", farea0)
.each("end", function() {
d3.select(this)
.attr("d", pathBar0)
.transition()
.duration(750)
.attr("d", pathBar);
});
d3.selectAll(".feature-line")
.transition()
.duration(750)
.attr("d", fline0);
}
function toArea() {
//alert("clicked");
d3.selectAll(".feature-render")
.transition()
.duration(750)
.attr("d", pathBar0)
.each("end", function() {
d3.select(this)
.attr("d", farea0)
.transition()
.duration(750)
.attr("d", farea);
});
d3.selectAll(".feature-line")
.transition()
.duration(750)
.attr("d", fline);
}
</script>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment