Skip to content

Instantly share code, notes, and snippets.

@alexbbrown
Created December 31, 2012 22:21
Show Gist options
  • Save alexbbrown/4423316 to your computer and use it in GitHub Desktop.
Save alexbbrown/4423316 to your computer and use it in GitHub Desktop.
More complex table from nested data
{"description":"More complex table from nested data","endpoint":"","display":"html","public":true,"require":[],"fileconfigs":{"inlet.js":{"default":false,"vim":true,"emacs":false,"fontSize":12},"USArrests.csv":{"default":true,"vim":false,"emacs":false,"fontSize":12},"_.md":{"default":true,"vim":false,"emacs":false,"fontSize":12},"config.json":{"default":true,"vim":false,"emacs":false,"fontSize":12},"USArrests2.csv":{"default":true,"vim":false,"emacs":false,"fontSize":12}},"tab":"edit","display_percent":0.7,"play":false,"loop":false,"restart":false,"autoinit":true,"pause":true,"loop_type":"period","bv":false,"nclones":15,"clone_opacity":0.4,"duration":3000,"ease":"linear","dt":0.01,"hidepanel":false}
var div = d3.select("#display");
ArrestsL = tributary.USArrests2;
ArrestsW = d3.nest()
.key(function(x){return x.Region+x.State})
.sortKeys(d3.ascending)
.entries(ArrestsL)
keyNames = ["Region","State"]
console.log(ArrestsW)
var table = div.selectAll("table").data([ArrestsW])
table.enter().append("table")
.each(function(s){
var n=d3.select(this)
n.append("thead")
n.append("tbody")
})
head=table
.select("thead")
.selectAll("tr")
.data([ArrestsW[0]])
head.enter()
.append("tr")
.append("th").attr("class","key")
head.exit().remove()
head.select("tr th.key")
.text(function(d){return "State"})
headkeys=head
.selectAll("th.key")
.data(function(d){return keyNames})
headkeys.enter()
.append("td")
.attr("class","key")
headkeys.text(function(d){return d})
cols=head
.selectAll("th.crime")
.data(function(d){return d.values})
cols.enter()
.append("th").attr("class","crime")
cols.exit().remove()
cols
.text(function(d){return d.Crime})
states=table
.select("tbody")
.selectAll("tr")
.data(ArrestsW)
states.enter()
.append("tr")
states.exit().remove()
keys=states.selectAll("td.key")
.data(function(d){
return _.map(keyNames,function(k){
return d.values[0][k]
})
})
keys.enter()
.append("td")
.attr("class","key")
keys.text(function(d){return d})
keys.exit().remove()
crimes=states
.selectAll("td.crime")
.data(function(d){return d.values})
crimes.enter()
.append("td").attr("class","crime")
crimes.exit().remove()
crimes
.text(function(d){return d.Count})
State Crime Count
Alabama Murder 13.2
Alaska Murder 10
Arizona Murder 8.1
Arkansas Murder 8.8
California Murder 9
Colorado Murder 7.9
Connecticut Murder 3.3
Delaware Murder 5.9
Florida Murder 15.4
Georgia Murder 17.4
Hawaii Murder 5.3
Idaho Murder 2.6
Illinois Murder 10.4
Indiana Murder 7.2
Iowa Murder 2.2
Kansas Murder 6
Kentucky Murder 9.7
Louisiana Murder 15.4
Maine Murder 2.1
Maryland Murder 11.3
Massachusetts Murder 4.4
Michigan Murder 12.1
Minnesota Murder 2.7
Mississippi Murder 16.1
Missouri Murder 9
Montana Murder 6
Nebraska Murder 4.3
Nevada Murder 12.2
New Hampshire Murder 2.1
New Jersey Murder 7.4
New Mexico Murder 11.4
New York Murder 11.1
North Carolina Murder 13
North Dakota Murder 0.8
Ohio Murder 7.3
Oklahoma Murder 6.6
Oregon Murder 4.9
Pennsylvania Murder 6.3
Rhode Island Murder 3.4
South Carolina Murder 14.4
South Dakota Murder 3.8
Tennessee Murder 13.2
Texas Murder 12.7
Utah Murder 3.2
Vermont Murder 2.2
Virginia Murder 8.5
Washington Murder 4
West Virginia Murder 5.7
Wisconsin Murder 2.6
Wyoming Murder 6.8
Alabama Assault 236
Alaska Assault 263
Arizona Assault 294
Arkansas Assault 190
California Assault 276
Colorado Assault 204
Connecticut Assault 110
Delaware Assault 238
Florida Assault 335
Georgia Assault 211
Hawaii Assault 46
Idaho Assault 120
Illinois Assault 249
Indiana Assault 113
Iowa Assault 56
Kansas Assault 115
Kentucky Assault 109
Louisiana Assault 249
Maine Assault 83
Maryland Assault 300
Massachusetts Assault 149
Michigan Assault 255
Minnesota Assault 72
Mississippi Assault 259
Missouri Assault 178
Montana Assault 109
Nebraska Assault 102
Nevada Assault 252
New Hampshire Assault 57
New Jersey Assault 159
New Mexico Assault 285
New York Assault 254
North Carolina Assault 337
North Dakota Assault 45
Ohio Assault 120
Oklahoma Assault 151
Oregon Assault 159
Pennsylvania Assault 106
Rhode Island Assault 174
South Carolina Assault 279
South Dakota Assault 86
Tennessee Assault 188
Texas Assault 201
Utah Assault 120
Vermont Assault 48
Virginia Assault 156
Washington Assault 145
West Virginia Assault 81
Wisconsin Assault 53
Wyoming Assault 161
Alabama UrbanPop 58
Alaska UrbanPop 48
Arizona UrbanPop 80
Arkansas UrbanPop 50
California UrbanPop 91
Colorado UrbanPop 78
Connecticut UrbanPop 77
Delaware UrbanPop 72
Florida UrbanPop 80
Georgia UrbanPop 60
Hawaii UrbanPop 83
Idaho UrbanPop 54
Illinois UrbanPop 83
Indiana UrbanPop 65
Iowa UrbanPop 57
Kansas UrbanPop 66
Kentucky UrbanPop 52
Louisiana UrbanPop 66
Maine UrbanPop 51
Maryland UrbanPop 67
Massachusetts UrbanPop 85
Michigan UrbanPop 74
Minnesota UrbanPop 66
Mississippi UrbanPop 44
Missouri UrbanPop 70
Montana UrbanPop 53
Nebraska UrbanPop 62
Nevada UrbanPop 81
New Hampshire UrbanPop 56
New Jersey UrbanPop 89
New Mexico UrbanPop 70
New York UrbanPop 86
North Carolina UrbanPop 45
North Dakota UrbanPop 44
Ohio UrbanPop 75
Oklahoma UrbanPop 68
Oregon UrbanPop 67
Pennsylvania UrbanPop 72
Rhode Island UrbanPop 87
South Carolina UrbanPop 48
South Dakota UrbanPop 45
Tennessee UrbanPop 59
Texas UrbanPop 80
Utah UrbanPop 80
Vermont UrbanPop 32
Virginia UrbanPop 63
Washington UrbanPop 73
West Virginia UrbanPop 39
Wisconsin UrbanPop 66
Wyoming UrbanPop 60
Alabama Rape 21.2
Alaska Rape 44.5
Arizona Rape 31
Arkansas Rape 19.5
California Rape 40.6
Colorado Rape 38.7
Connecticut Rape 11.1
Delaware Rape 15.8
Florida Rape 31.9
Georgia Rape 25.8
Hawaii Rape 20.2
Idaho Rape 14.2
Illinois Rape 24
Indiana Rape 21
Iowa Rape 11.3
Kansas Rape 18
Kentucky Rape 16.3
Louisiana Rape 22.2
Maine Rape 7.8
Maryland Rape 27.8
Massachusetts Rape 16.3
Michigan Rape 35.1
Minnesota Rape 14.9
Mississippi Rape 17.1
Missouri Rape 28.2
Montana Rape 16.4
Nebraska Rape 16.5
Nevada Rape 46
New Hampshire Rape 9.5
New Jersey Rape 18.8
New Mexico Rape 32.1
New York Rape 26.1
North Carolina Rape 16.1
North Dakota Rape 7.3
Ohio Rape 21.4
Oklahoma Rape 20
Oregon Rape 29.3
Pennsylvania Rape 14.9
Rhode Island Rape 8.3
South Carolina Rape 22.5
South Dakota Rape 12.8
Tennessee Rape 26.9
Texas Rape 25.5
Utah Rape 22.9
Vermont Rape 11.2
Virginia Rape 20.7
Washington Rape 26.2
West Virginia Rape 9.3
Wisconsin Rape 10.8
Wyoming Rape 15.6
State Region Crime Count
Alabama South Murder 13.2
Alaska West Murder 10
Arizona West Murder 8.1
Arkansas South Murder 8.8
California West Murder 9
Colorado West Murder 7.9
Connecticut Northeast Murder 3.3
Delaware South Murder 5.9
Florida South Murder 15.4
Georgia South Murder 17.4
Hawaii West Murder 5.3
Idaho West Murder 2.6
Illinois North Central Murder 10.4
Indiana North Central Murder 7.2
Iowa North Central Murder 2.2
Kansas North Central Murder 6
Kentucky South Murder 9.7
Louisiana South Murder 15.4
Maine Northeast Murder 2.1
Maryland South Murder 11.3
Massachusetts Northeast Murder 4.4
Michigan North Central Murder 12.1
Minnesota North Central Murder 2.7
Mississippi South Murder 16.1
Missouri North Central Murder 9
Montana West Murder 6
Nebraska North Central Murder 4.3
Nevada West Murder 12.2
New Hampshire Northeast Murder 2.1
New Jersey Northeast Murder 7.4
New Mexico West Murder 11.4
New York Northeast Murder 11.1
North Carolina South Murder 13
North Dakota North Central Murder 0.8
Ohio North Central Murder 7.3
Oklahoma South Murder 6.6
Oregon West Murder 4.9
Pennsylvania Northeast Murder 6.3
Rhode Island Northeast Murder 3.4
South Carolina South Murder 14.4
South Dakota North Central Murder 3.8
Tennessee South Murder 13.2
Texas South Murder 12.7
Utah West Murder 3.2
Vermont Northeast Murder 2.2
Virginia South Murder 8.5
Washington West Murder 4
West Virginia South Murder 5.7
Wisconsin North Central Murder 2.6
Wyoming West Murder 6.8
Alabama South Assault 236
Alaska West Assault 263
Arizona West Assault 294
Arkansas South Assault 190
California West Assault 276
Colorado West Assault 204
Connecticut Northeast Assault 110
Delaware South Assault 238
Florida South Assault 335
Georgia South Assault 211
Hawaii West Assault 46
Idaho West Assault 120
Illinois North Central Assault 249
Indiana North Central Assault 113
Iowa North Central Assault 56
Kansas North Central Assault 115
Kentucky South Assault 109
Louisiana South Assault 249
Maine Northeast Assault 83
Maryland South Assault 300
Massachusetts Northeast Assault 149
Michigan North Central Assault 255
Minnesota North Central Assault 72
Mississippi South Assault 259
Missouri North Central Assault 178
Montana West Assault 109
Nebraska North Central Assault 102
Nevada West Assault 252
New Hampshire Northeast Assault 57
New Jersey Northeast Assault 159
New Mexico West Assault 285
New York Northeast Assault 254
North Carolina South Assault 337
North Dakota North Central Assault 45
Ohio North Central Assault 120
Oklahoma South Assault 151
Oregon West Assault 159
Pennsylvania Northeast Assault 106
Rhode Island Northeast Assault 174
South Carolina South Assault 279
South Dakota North Central Assault 86
Tennessee South Assault 188
Texas South Assault 201
Utah West Assault 120
Vermont Northeast Assault 48
Virginia South Assault 156
Washington West Assault 145
West Virginia South Assault 81
Wisconsin North Central Assault 53
Wyoming West Assault 161
Alabama South UrbanPop 58
Alaska West UrbanPop 48
Arizona West UrbanPop 80
Arkansas South UrbanPop 50
California West UrbanPop 91
Colorado West UrbanPop 78
Connecticut Northeast UrbanPop 77
Delaware South UrbanPop 72
Florida South UrbanPop 80
Georgia South UrbanPop 60
Hawaii West UrbanPop 83
Idaho West UrbanPop 54
Illinois North Central UrbanPop 83
Indiana North Central UrbanPop 65
Iowa North Central UrbanPop 57
Kansas North Central UrbanPop 66
Kentucky South UrbanPop 52
Louisiana South UrbanPop 66
Maine Northeast UrbanPop 51
Maryland South UrbanPop 67
Massachusetts Northeast UrbanPop 85
Michigan North Central UrbanPop 74
Minnesota North Central UrbanPop 66
Mississippi South UrbanPop 44
Missouri North Central UrbanPop 70
Montana West UrbanPop 53
Nebraska North Central UrbanPop 62
Nevada West UrbanPop 81
New Hampshire Northeast UrbanPop 56
New Jersey Northeast UrbanPop 89
New Mexico West UrbanPop 70
New York Northeast UrbanPop 86
North Carolina South UrbanPop 45
North Dakota North Central UrbanPop 44
Ohio North Central UrbanPop 75
Oklahoma South UrbanPop 68
Oregon West UrbanPop 67
Pennsylvania Northeast UrbanPop 72
Rhode Island Northeast UrbanPop 87
South Carolina South UrbanPop 48
South Dakota North Central UrbanPop 45
Tennessee South UrbanPop 59
Texas South UrbanPop 80
Utah West UrbanPop 80
Vermont Northeast UrbanPop 32
Virginia South UrbanPop 63
Washington West UrbanPop 73
West Virginia South UrbanPop 39
Wisconsin North Central UrbanPop 66
Wyoming West UrbanPop 60
Alabama South Rape 21.2
Alaska West Rape 44.5
Arizona West Rape 31
Arkansas South Rape 19.5
California West Rape 40.6
Colorado West Rape 38.7
Connecticut Northeast Rape 11.1
Delaware South Rape 15.8
Florida South Rape 31.9
Georgia South Rape 25.8
Hawaii West Rape 20.2
Idaho West Rape 14.2
Illinois North Central Rape 24
Indiana North Central Rape 21
Iowa North Central Rape 11.3
Kansas North Central Rape 18
Kentucky South Rape 16.3
Louisiana South Rape 22.2
Maine Northeast Rape 7.8
Maryland South Rape 27.8
Massachusetts Northeast Rape 16.3
Michigan North Central Rape 35.1
Minnesota North Central Rape 14.9
Mississippi South Rape 17.1
Missouri North Central Rape 28.2
Montana West Rape 16.4
Nebraska North Central Rape 16.5
Nevada West Rape 46
New Hampshire Northeast Rape 9.5
New Jersey Northeast Rape 18.8
New Mexico West Rape 32.1
New York Northeast Rape 26.1
North Carolina South Rape 16.1
North Dakota North Central Rape 7.3
Ohio North Central Rape 21.4
Oklahoma South Rape 20
Oregon West Rape 29.3
Pennsylvania Northeast Rape 14.9
Rhode Island Northeast Rape 8.3
South Carolina South Rape 22.5
South Dakota North Central Rape 12.8
Tennessee South Rape 26.9
Texas South Rape 25.5
Utah West Rape 22.9
Vermont Northeast Rape 11.2
Virginia South Rape 20.7
Washington West Rape 26.2
West Virginia South Rape 9.3
Wisconsin North Central Rape 10.8
Wyoming West Rape 15.6
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment