Created
June 10, 2014 04:27
-
-
Save jdarling/06019d16cb5fd6795edf to your computer and use it in GitHub Desktop.
D3 with random color generator
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
<!DOCTYPE html> | |
<meta charset="utf-8"> | |
<style> | |
.bar:hover { | |
fill: brown; | |
} | |
.axis { | |
font: 10px sans-serif; | |
} | |
.axis path, | |
.axis line { | |
fill: none; | |
stroke: #000; | |
shape-rendering: crispEdges; | |
} | |
.x.axis path { | |
display: none; | |
} | |
</style> | |
<body> | |
<script src="http://d3js.org/d3.v3.min.js"></script> | |
<script> | |
// Adapted from http://martin.ankerl.com/2009/12/09/how-to-create-random-colors-programmatically/ | |
var randomColor = (function(){ | |
var golden_ratio_conjugate = 0.618033988749895; | |
var h = Math.random(); | |
var hslToRgb = function (h, s, l){ | |
var r, g, b; | |
if(s == 0){ | |
r = g = b = l; // achromatic | |
}else{ | |
function hue2rgb(p, q, t){ | |
if(t < 0) t += 1; | |
if(t > 1) t -= 1; | |
if(t < 1/6) return p + (q - p) * 6 * t; | |
if(t < 1/2) return q; | |
if(t < 2/3) return p + (q - p) * (2/3 - t) * 6; | |
return p; | |
} | |
var q = l < 0.5 ? l * (1 + s) : l + s - l * s; | |
var p = 2 * l - q; | |
r = hue2rgb(p, q, h + 1/3); | |
g = hue2rgb(p, q, h); | |
b = hue2rgb(p, q, h - 1/3); | |
} | |
return '#'+Math.round(r * 255).toString(16)+Math.round(g * 255).toString(16)+Math.round(b * 255).toString(16); | |
}; | |
return function(){ | |
h += golden_ratio_conjugate; | |
h %= 1; | |
return hslToRgb(h, 0.5, 0.60); | |
}; | |
})(); | |
</script> | |
<script> | |
var margin = {top: 20, right: 20, bottom: 30, left: 40}, | |
width = 960 - margin.left - margin.right, | |
height = 500 - margin.top - margin.bottom; | |
var x = d3.scale.ordinal() | |
.rangeRoundBands([0, width], .1); | |
var y = d3.scale.linear() | |
.range([height, 0]); | |
var xAxis = d3.svg.axis() | |
.scale(x) | |
.orient("bottom"); | |
var yAxis = d3.svg.axis() | |
.scale(y) | |
.orient("left") | |
.ticks(10, "%"); | |
var svg = d3.select("body").append("svg") | |
.attr("width", width + margin.left + margin.right) | |
.attr("height", height + margin.top + margin.bottom) | |
.append("g") | |
.attr("transform", "translate(" + margin.left + "," + margin.top + ")"); | |
data = [ | |
{letter: "A", frequency: 0.08167}, | |
{letter: "B", frequency: 0.01492}, | |
{letter: "C", frequency: 0.02782}, | |
{letter: "D", frequency: 0.04253}, | |
{letter: "E", frequency: 0.12702}, | |
{letter: "F", frequency: 0.02288}, | |
{letter: "G", frequency: 0.02015}, | |
{letter: "H", frequency: 0.06094}, | |
{letter: "I", frequency: 0.06966}, | |
{letter: "J", frequency: 0.00153}, | |
{letter: "K", frequency: 0.00772}, | |
{letter: "L", frequency: 0.04025}, | |
{letter: "M", frequency: 0.02406}, | |
{letter: "N", frequency: 0.06749}, | |
{letter: "O", frequency: 0.07507}, | |
{letter: "P", frequency: 0.01929}, | |
{letter: "Q", frequency: 0.00095}, | |
{letter: "R", frequency: 0.05987}, | |
{letter: "S", frequency: 0.06327}, | |
{letter: "T", frequency: 0.09056}, | |
{letter: "U", frequency: 0.02758}, | |
{letter: "V", frequency: 0.00978}, | |
{letter: "W", frequency: 0.02360}, | |
{letter: "X", frequency: 0.00150}, | |
{letter: "Y", frequency: 0.01974}, | |
{letter: "Z", frequency: 0.00074} | |
]; | |
x.domain(data.map(function(d) { return d.letter; })); | |
y.domain([0, d3.max(data, function(d) { return d.frequency; })]); | |
svg.append("g") | |
.attr("class", "x axis") | |
.attr("transform", "translate(0," + height + ")") | |
.call(xAxis); | |
svg.append("g") | |
.attr("class", "y axis") | |
.call(yAxis) | |
.append("text") | |
.attr("transform", "rotate(-90)") | |
.attr("y", 6) | |
.attr("dy", ".71em") | |
.style("text-anchor", "end") | |
.text("Frequency"); | |
svg.selectAll(".bar") | |
.data(data) | |
.enter().append("rect") | |
.attr("class", "bar") | |
.attr("x", function(d) { return x(d.letter); }) | |
.attr("width", x.rangeBand()) | |
.attr("y", function(d) { return y(d.frequency); }) | |
.attr("height", function(d) { return height - y(d.frequency); }) | |
.style({fill: randomColor}); | |
function type(d) { | |
d.frequency = +d.frequency; | |
return d; | |
} | |
</script> | |
</body> | |
</html> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Hi is there any way to generate 50 unique colour from your code?