Skip to content

Instantly share code, notes, and snippets.

@poezn
Created February 28, 2013 06:43
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 poezn/5054749 to your computer and use it in GitHub Desktop.
Save poezn/5054749 to your computer and use it in GitHub Desktop.
INFO 247 - Lab 7 - #4
{"description":"INFO 247 - Lab 7 - #4","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 6: D3.js
// http://blogs.ischool.berkeley.edu/i247s13/lab-6-d3-js/
// get the data from the 'matrix-data.json' data file
var data = tb['matrix-data'];
// Create a color scale that has the following colors:
// minimum: 15; red
// maximum: 40; green
// center: 23; yellow
// ... YOUR TURN
// 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({
"transform": function(d, i) {
var tx = i * 50;
var ty = 0;
return "translate(" + [tx, ty] + ")"
},
"width": 50,
"height": 50
})
.style({
"fill": "none", // here, we simply use the color scale
// we've created above
"stroke": "#FFFFFF"
});
[
{"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