Skip to content

Instantly share code, notes, and snippets.

@akselx
Created March 8, 2013 00:03
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 akselx/5113056 to your computer and use it in GitHub Desktop.
Save akselx/5113056 to your computer and use it in GitHub Desktop.
INFO 247 - Lab 7 - #1 - Draw Squares
{"description":"INFO 247 - Lab 7 - #1 - Draw Squares","endpoint":"","display":"svg","public":true,"require":[],"fileconfigs":{"inlet.js":{"default":true,"vim":false,"emacs":false,"fontSize":15},"_.md":{"default":true,"vim":false,"emacs":false,"fontSize":15},"config.json":{"default":true,"vim":false,"emacs":false,"fontSize":15},"matrix-data.json":{"default":true,"vim":false,"emacs":false,"fontSize":15}},"fullscreen":false,"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}
// School of Information, UC Berkeley
// INFO 247 Lab 7: D3.js
// http://blogs.ischool.berkeley.edu/i247s13/lab-7-d3-js-part-2/
// get the data from the 'matrix-data.json' data file
var data = tb['matrix-data'];
// for each race, let's add a row that will eventually
// contain all squares for that particular race.
// We're using the SVG <g> element for this. <g> elements
// are invisible but can be moved around on the screen
var rows = g.selectAll("g.row")
.data(data)
.enter().append("g")
.attr({
"class": "row",
"transform": function(d, i) {
var tx = 50;
var ty = 50 + i * 50;
return "translate(" + [tx, ty] + ")"
}
});
// within each of these rows, let's add 9 squares (one for
// each data point)
rows.selectAll("rect")
.data(function(d, i) { return d.values; })
.enter().append("rect")
.attr({
"width":50,
"height":50,
"y":0,
"x":function(d, i) {
return i*50;
}
})
// ... YOUR TURN.
// - append a rectangle for each data point
// - move each rectangle to appeach 50 pixels next to the previous one
// - set a stroke in order to distinguish the rectangles from each other
[
{"race": "Asian", "values": [22,17,20,20,26,26,25,21,29] },
{"race": "Black", "values": [34,28,31,34,37,34,32,39,38] },
{"race": "Hispanic / Latin", "values": [22,19,24,19,23,26,26,21,30] },
{"race": "Indian", "values": [18,21,25,18,24,27,21,33,30] },
{"race": "Middle Eastern", "values": [27,21,25,23,23,32,23,32,28] },
{"race": "Native American", "values": [31,27,29,27,36,35,34,32,34] },
{"race": "Other", "values": [30,24,27,28,29,32,32,35,33] },
{"race": "Pacific Islander", "values": [23,25,24,24,28,29,22,30,29] },
{"race": "White", "values": [21,21,22,20,25,27,26,23,29] }
]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment