[ Launch: INFO 247 - Lab 7 - #2 - Color Scales ] 5113233 by akselx
[ Launch: INFO 247 - Lab 7 - #1 - Draw Squares (full code) ] 5111589 by poezn
[ Launch: INFO 247 - Lab 6 - #1 ] 5054769 by poezn
[ Launch: INFO 247 - Lab 6 - #1 ] 5054734 by poezn
-
-
Save akselx/5113233 to your computer and use it in GitHub Desktop.
INFO 247 - Lab 7 - #2 - Color Scales
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{"description":"INFO 247 - Lab 7 - #2 - Color Scales","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} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// School of Information, UC Berkeley | |
// INFO 247 Lab 7: D3.js | |
// http://blogs.ischool.berkeley.edu/i247s13/lab-7-d3-js-part-2/ | |
var start = 17; | |
var lowmiddle = 24; | |
var himiddle = 29; | |
var end = 39; | |
var legendSquareWidth = 32; | |
// YOUR TURN | |
// Experiment with the color scale | |
// this maps a start, middle, and end point to a color each | |
var colorScale = d3.scale.linear() | |
.domain([start, lowmiddle,himiddle, end]) | |
.range(['#F8696B', '#4616AD','#FCEA84', '#99AF93']); | |
// draw big rectangle | |
g.append("rect") | |
.attr({ | |
"width": 150, | |
"height": 150, | |
"x": 350, | |
"y": 330 | |
}) | |
.style({ | |
"fill": function(d, i) { | |
return colorScale(19); // <== CHANGE THIS | |
} | |
}); | |
// This draws the legend | |
g.selectAll("rect.legend") | |
.data(d3.range(start, end)) | |
.enter().append("rect") | |
.attr({ | |
"class": "legend", | |
"width": legendSquareWidth - 2, | |
"height": legendSquareWidth * 3, | |
"transform": function(d, i) { | |
var tx = 50 + legendSquareWidth * i; | |
var ty = 50; | |
return "translate(" + [tx, ty ] + ")"; | |
} | |
}) | |
.style({ | |
"fill": function(d, i) { | |
return colorScale(d); | |
} | |
}); | |
g.selectAll("text.legend") | |
.data(d3.range(start, end+1, 2)) | |
.enter().append("text") | |
.attr({ | |
"class": "legend", | |
"text-anchor": "middle", | |
"transform": function(d, i) { | |
var tx = 50 + 2 * legendSquareWidth * i; | |
var ty = 176; | |
return "translate(" + [tx, ty ] + ")"; | |
} | |
}) | |
.style({ | |
"font-size": 15 + "px", | |
"fill": "#383838" | |
}) | |
.text(function(d, i) { | |
return d; | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment