Skip to content

Instantly share code, notes, and snippets.

@madelfio
Last active August 29, 2015 14:00
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 madelfio/11069272 to your computer and use it in GitHub Desktop.
Save madelfio/11069272 to your computer and use it in GitHub Desktop.
Table Decomposition

A visualization of table decomposition. This visualization uses "phantom" table rows and cells to demonstrate decomposition boundaries.

<!doctype html>
<meta charset="utf-8">
<style>
table {
border-spacing: 0;
}
td {
padding:3px;
border: 1px solid #bbb;
height: 2ex;
}
</style>
<body>
<script src="http://d3js.org/d3.v3.min.js"></script>
<script src="tables.js"></script>
</body>
"use strict";
/* global self */
d3.json('./tables.json', function(err, data) {
var cells = d3.nest()
.key(function(d) {return +d.row;})
.entries(data[0]);
var table = d3.select('body').append('table');
var tr = table.selectAll('tr')
.data(cells)
.enter().append('tr');
var td = tr.selectAll('td')
.data(function(d) {return d.values;})
.enter().append('td')
.attr('rowspan', function(d) {return d.rowspan;})
.attr('colspan', function(d) {return d.colspan;})
.each(function(d) {
d3.select(this).style(d.style);
})
.text(function(d) {return d.text;});
var delay = 0,
divisions = [[3, 14], [4], [9], [5, 8, 10, 13]],
decomposed = false;
function decompose() {
if (!decomposed) {
decomposed = true;
divisions.forEach(function(r) {
tr.filter(function(d, i) {return r.indexOf(i) > -1;})
.each(function() {
var node = d3.select(this).node();
table.insert('tr', function() {return node;})
.attr('class', 'phantom')
.style('height', 0)
.transition().duration(500).delay(delay)
.style('height', '20px');
delay += 40;
});
delay += 750;
});
} else {
decomposed = false;
delay = 0;
table.selectAll('tr.phantom')
.transition().duration(500)
.style('height', '0px')
.remove();
}
}
table.style('cursor', 'pointer')
.on('click', decompose);
});
d3.select(self.frameElement).style("height", "600px");
[
[
{
"col": 0,
"colspan": 3,
"row": 0,
"style": {
"color": "blue",
"font-weight": "bold"
},
"text": "Patent Applications by Residents"
},
{
"col": 0,
"colspan": 3,
"row": 1,
"style": {
"color": "gray"
},
"text": "Data Source: worldbank.org"
},
{
"col": 0,
"colspan": 3,
"row": 2,
"style": {
"color": "gray"
},
"text": "(showing top countries in each continent)"
},
{
"col": 0,
"row": 3,
"style": {
"background-color": "red",
"color": "white",
"font-weight": "bold"
},
"text": "Country"
},
{
"col": 1,
"row": 3,
"style": {
"background-color": "red",
"color": "white",
"font-weight": "bold"
},
"text": "Residents"
},
{
"col": 2,
"row": 3,
"style": {
"background-color": "red",
"color": "white",
"font-weight": "bold"
},
"text": "Applications"
},
{
"col": 0,
"colspan": 3,
"row": 4,
"style": {
"background-color": "gray",
"font-style": "italic",
"font-weight": "bold"
},
"text": "North America"
},
{
"col": 0,
"row": 5,
"text": "United States"
},
{
"col": 1,
"row": 5,
"style": {
"text-align": "right"
},
"text": "307,007,000"
},
{
"col": 2,
"row": 5,
"style": {
"text-align": "right"
},
"text": "224,912"
},
{
"col": 0,
"row": 6,
"text": "Canada"
},
{
"col": 1,
"row": 6,
"style": {
"text-align": "right"
},
"text": "33,739,900"
},
{
"col": 2,
"row": 6,
"style": {
"text-align": "right"
},
"text": "5,067"
},
{
"col": 0,
"row": 7,
"text": "Mexico"
},
{
"col": 1,
"row": 7,
"style": {
"text-align": "right"
},
"text": "112,033,369"
},
{
"col": 2,
"row": 7,
"style": {
"text-align": "right"
},
"text": "822"
},
{
"col": 0,
"row": 8,
"text": ""
},
{
"col": 1,
"row": 8,
"style": {
"font-style": "italic"
},
"text": "N.A. Total"
},
{
"col": 2,
"row": 8,
"style": {
"font-style": "italic",
"text-align": "right"
},
"text": "230,801"
},
{
"col": 0,
"colspan": 3,
"row": 9,
"style": {
"background-color": "gray",
"font-style": "italic",
"font-weight": "bold"
},
"text": "Asia"
},
{
"col": 0,
"row": 10,
"text": "Japan"
},
{
"col": 1,
"row": 10,
"style": {
"text-align": "right"
},
"text": "127,557,958"
},
{
"col": 2,
"row": 10,
"style": {
"text-align": "right"
},
"text": "295,315"
},
{
"col": 0,
"row": 11,
"text": "China"
},
{
"col": 1,
"row": 11,
"style": {
"text-align": "right"
},
"text": "1,331,380,000"
},
{
"col": 2,
"row": 11,
"style": {
"text-align": "right"
},
"text": "229,096"
},
{
"col": 0,
"row": 12,
"text": "South Korea"
},
{
"col": 1,
"row": 12,
"style": {
"text-align": "right"
},
"text": "48,747,000"
},
{
"col": 2,
"row": 12,
"style": {
"text-align": "right"
},
"text": "127,316"
},
{
"col": 0,
"row": 13,
"text": ""
},
{
"col": 1,
"row": 13,
"style": {
"font-style": "italic"
},
"text": "Asia Total"
},
{
"col": 2,
"row": 13,
"style": {
"font-style": "italic",
"text-align": "right"
},
"text": "651,727"
},
{
"col": 0,
"row": 14,
"text": ""
},
{
"col": 1,
"row": 14,
"text": ""
},
{
"col": 2,
"row": 14,
"text": ""
},
{
"col": 0,
"colspan": 3,
"row": 15,
"style": {
"color": "#888"
},
"text": "Note: data from 2009"
}
]
]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment