Skip to content

Instantly share code, notes, and snippets.

@grybnicky
Last active April 19, 2016 14:30
Show Gist options
  • Save grybnicky/3df449170b59b62faa40dd86b334d524 to your computer and use it in GitHub Desktop.
Save grybnicky/3df449170b59b62faa40dd86b334d524 to your computer and use it in GitHub Desktop.
Radial line generator, interpolation from an empty array
<!DOCTYPE html>
<meta charset="utf-8">
<style>
.frame {
fill: none;
stroke: #000;
}
.axis text {
font: 10px sans-serif;
}
.axis line,
.axis circle {
fill: none;
stroke: #777;
stroke-dasharray: 1,4;
}
.axis :last-of-type circle {
stroke: #333;
stroke-dasharray: none;
}
.line {
fill: none;
stroke: red;
stroke-width: 1.5px;
}
</style>
<body>
<script src="http://d3js.org/d3.v3.min.js"></script>
<script>
//2 PI = 360 deg
/*var data = d3.range(0, 2*Math.PI, .01).map(function(t) {
return [t, Math.sin(3*t)];
});
var empty = d3.range(0, 2*Math.PI, .01).map(function(t) {
return [0,0];
});*/
/*var margin = 30;
// radius is the minimal dimension, minus the margin
var width = 960,
height = 200,
radius = Math.min(width, height) / 2 - margin;*/
var r = d3.scale.linear()
.domain([0, 1])
.range([0, 200]);
//default accessor [[x1,y1]] => radian and angle
var line = d3.svg.line.radial()
.radius(function(d){; return (r(d[1])); }) // will change between -1 and 1
.angle(function(d) { ;return d[0];});
var svg = d3.select("body").append("svg")
.attr("width", 700)
.attr("height", 700)
.append("g")
.attr("transform", "translate(" + 700 / 2 + "," + 700 / 2 + ")");
svg.append("rect")
.attr("width", 100)
.attr("height", 100)
// radius axis
// cheat with CSS
var gr = svg.append("g")
.attr("class", "r axis")
.selectAll("g")
.data(r.ticks(10).slice(0))
.enter().append("g");
var ga = svg.append("g")
.attr("stroke", "black")
.selectAll("g")
.data(d3.range(0, 360, 15))
.enter().append("g")
.attr("transform", function(d) { return "rotate(" + (d-90) + ")"; });
ga.append("line")
.attr("x2", 200);
/*gr.append("circle")
.attr("r", function(d,i){console.log("=>",d,i,r(d));return r(d)});
/*gr.append("text")
.attr("y", function(d) { return -r(d) - 4; })
.attr("transform", "rotate(50)")
.style("text-anchor", "middle")
.text(function(d) { return d; });
*/
var r = d3.scale.linear()
.domain([0, 1])
.range([0, 200]);
//default accessor [[x1,y1]] => radian and angle
var line = d3.svg.line.radial()
.radius(function(d){; return (r(d[1])); }) // will change between -1 and 1
.angle(function(d) { ;return d[0];});
var svg = d3.select("body").append("svg")
.attr("width", 700)
.attr("height", 700)
.append("g")
.attr("transform", "translate(" + 700 / 2 + "," + 700 / 2 + ")");
svg.append("rect")
.attr("width", 100)
.attr("height", 100)
// radius axis
// cheat with CSS
var gr = svg.append("g")
.attr("class", "r axis")
.selectAll("g")
.data(r.ticks(10).slice(0))
.enter().append("g");
var ga = svg.append("g")
.attr("stroke", "black")
.selectAll("g")
.data(d3.range(0, 360, 15))
.enter().append("g")
.attr("transform", function(d) { return "rotate(" + (d-90) + ")"; });
ga.append("line")
.attr("x2", 200);
/*ga.append("text")
.attr("x", radius + 6)
.attr("dy", ".35em")
.style("text-anchor", function(d) { return d < 270 && d > 90 ? "end" : null; })
.attr("transform", function(d) { return d < 270 && d > 90 ? "rotate(180 " + (radius + 6) + ",0)" : null; })
.text(function(d) { return d + "°"; });
// a bit cheating
/*var path =
svg.append("path")
.datum(data)
.attr("class", "line")
.attr("d", line);
var totalLength = path.node().getTotalLength();
path.transition().duration(1000).attrTween("d",function(d){
var interpolate = d3.interpolate(empty,d);
return function(t) { return line(interpolate(t))};
})
/* CHEATED WAY path
.attr("stroke-dasharray", totalLength + " " + totalLength)
.attr("stroke-dashoffset", totalLength)
.transition()
.duration(2000)
.ease("linear")
.attr("stroke-dashoffset", 0);
*/
</script>
</body>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment