Last active
August 29, 2015 14:02
-
-
Save nitaku/46c69831ca5b92294ee7 to your computer and use it in GitHub Desktop.
Polar chart
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
### polar layout ### | |
polar = () -> | |
# defaults | |
#scale = d3.scale.linear | |
angle = null | |
self = (data) -> | |
angle = 2*Math.PI/data.length | |
data.forEach (d, i) -> | |
d.angle = i * angle | |
return data | |
self.angle = () -> | |
return angle | |
return self | |
### --- ### | |
csv = ''' | |
category,value | |
one,323 | |
two,534 | |
three,230 | |
four,156 | |
five,336 | |
''' | |
data = d3.csv.parse csv | |
data.forEach (d) -> d.value = +d.value | |
max = d3.max(data, (d) -> d.value) | |
width = 960 | |
height = 500 | |
RADIUS = Math.min(width, height) / 2 - 40 | |
polar_layout = polar() | |
polar_data = polar_layout(data) | |
console.log polar_data | |
svg = d3.select("body").append("svg") | |
.attr("width", width) | |
.attr("height", height) | |
.append("g") | |
.attr | |
transform: "translate(#{width/2}, #{height/2})" | |
color = d3.scale.ordinal() | |
.range(["#1b9e77","#d95f02","#7570b3","#e7298a","#66a61e","#e6ab02","#a6761d"]) | |
arc_generator = d3.svg.arc() | |
.innerRadius(0) | |
.outerRadius((d) -> RADIUS/max*d.value) | |
.startAngle((d) -> d.angle - polar_layout.angle()/2) | |
.endAngle((d) -> d.angle + polar_layout.angle()/2) | |
svg.selectAll('.arc') | |
.data(polar_data) | |
.enter().append('path') | |
.attr | |
class: 'arc' | |
d: arc_generator | |
fill: (d, i) -> color(i) | |
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
svg { | |
background-color: white; | |
} | |
.arc { | |
stroke-width: 1; | |
stroke: white; | |
stroke-linejoin: round; | |
} |
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> | |
<html> | |
<head> | |
<meta charset="utf-8"> | |
<meta name="description" content="Polar chart" /> | |
<title>Polar chart</title> | |
<link rel="stylesheet" href="index.css"> | |
<script src="http://d3js.org/d3.v3.min.js"></script> | |
</head> | |
<body> | |
<script src="index.js"></script> | |
</body> | |
</html> |
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
/* polar layout | |
*/ | |
(function() { | |
var RADIUS, arc_generator, color, csv, data, height, max, polar, polar_data, polar_layout, svg, width; | |
polar = function() { | |
var angle, self; | |
angle = null; | |
self = function(data) { | |
angle = 2 * Math.PI / data.length; | |
data.forEach(function(d, i) { | |
return d.angle = i * angle; | |
}); | |
return data; | |
}; | |
self.angle = function() { | |
return angle; | |
}; | |
return self; | |
}; | |
/* --- | |
*/ | |
csv = 'category,value\none,323\ntwo,534\nthree,230\nfour,156\nfive,336'; | |
data = d3.csv.parse(csv); | |
data.forEach(function(d) { | |
return d.value = +d.value; | |
}); | |
max = d3.max(data, function(d) { | |
return d.value; | |
}); | |
width = 960; | |
height = 500; | |
RADIUS = Math.min(width, height) / 2 - 40; | |
polar_layout = polar(); | |
polar_data = polar_layout(data); | |
console.log(polar_data); | |
svg = d3.select("body").append("svg").attr("width", width).attr("height", height).append("g").attr({ | |
transform: "translate(" + (width / 2) + ", " + (height / 2) + ")" | |
}); | |
color = d3.scale.ordinal().range(["#1b9e77", "#d95f02", "#7570b3", "#e7298a", "#66a61e", "#e6ab02", "#a6761d"]); | |
arc_generator = d3.svg.arc().innerRadius(0).outerRadius(function(d) { | |
return RADIUS / max * d.value; | |
}).startAngle(function(d) { | |
return d.angle - polar_layout.angle() / 2; | |
}).endAngle(function(d) { | |
return d.angle + polar_layout.angle() / 2; | |
}); | |
svg.selectAll('.arc').data(polar_data).enter().append('path').attr({ | |
"class": 'arc', | |
d: arc_generator, | |
fill: function(d, i) { | |
return color(i); | |
} | |
}); | |
}).call(this); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment