A simple sortable spreadsheet example that uses D3 data-binding and transitions. The sort button sorts the rows by time, and the transition button places them in their original order.
xxxxxxxxxx
<html xmlns="https://www.w3.org/1999/xhtml">
<head>
<title>D3 in Action Sortable Spreadhseet Example</title>
<meta charset="utf-8" />
</head>
<script src="https://d3js.org/d3.v3.min.js" type="text/javascript">
</script>
<style>
div.data {
position: absolute;
width: 90px;
padding: 0 5px;
}
div.head {
position: absolute;
}
div.datarow {
position: absolute;
width: 100%;
border-top: 2px black solid;
background: white;
height: 35px;
overflow: hidden;
}
div.table {
position:relative;
}
</style>
<body onload="spreadsheet2();">
<div id="controls" />
<footer>
<script>
function spreadsheet2() {
d3.json("tweets.json",function(error,data) {expData=data;createSpreadsheet(data.tweets)});
function createSpreadsheet(incData) {
var keyValues = d3.keys(incData[0])
d3.select("#controls")
.append("div")
.attr("class", "table")
d3.select("div.table")
.append("div")
.attr("class", "head")
.selectAll("div.data")
.data(keyValues)
.enter()
.append("div")
.attr("class", "data")
.html(function (d) {return d})
.style("left", function(d,i) {return (i * 100) + "px"});
d3.select("div.table")
.selectAll("div.datarow")
.data(incData, function(d) {return d.content}).enter()
.append("div")
.attr("class", "datarow")
.style("top", function(d,i) {return (40 + (i * 40)) + "px"});
d3.selectAll("div.datarow")
.selectAll("div.data")
.data(function(d) {return d3.entries(d)})
.enter()
.append("div")
.attr("class", "data")
.html(function (d) {return d.value})
.style("left", function(d,i,j) {return (i * 100) + "px"});
d3.select("#controls").insert("button", ".table").on("click", sortSheet).html("sort")
d3.select("#controls").insert("button", ".table").on("click", transitionSheet).html("transition")
function transitionSheet() {
d3.selectAll("div.datarow")
.transition()
.duration(2000)
.style("top", function(d,i) {return (40 + (i * 40)) + "px"});
}
function sortSheet() {
var dataset = d3.selectAll("div.datarow").data();
dataset.sort(function(a,b) {
var aDate = new Date(a.timestamp);
var bDate = new Date(b.timestamp);
if (aDate > bDate)
return 1;
if (aDate < bDate)
return -1;
return 0;
});
d3.selectAll("div.datarow").data(dataset, function(d) {return d.content})
.transition()
.duration(2000)
.style("top", function(d,i) {return (40 + (i * 40))});
}
}
}
</script>
</footer>
</body>
</html>
Modified http://d3js.org/d3.v3.min.js to a secure url
https://d3js.org/d3.v3.min.js