Skip to content

Instantly share code, notes, and snippets.

@fabiovalse
Last active June 29, 2020 14:42
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save fabiovalse/81043bf96c6441f4bf72 to your computer and use it in GitHub Desktop.
Save fabiovalse/81043bf96c6441f4bf72 to your computer and use it in GitHub Desktop.
Points Along an Archimedean Spiral
svg {
background: white;
}
.spiral {
fill: none;
stroke: #303030;
stroke-width: 3px;
}
<!DOCTYPE html>
<html>
<head>
<meta name="description" content="Points Along an Archimedean Spiral" />
<meta http-equiv="Content-Type" content="text/html;charset=utf-8">
<script type="text/javascript" src="http://d3js.org/d3.v2.js"></script>
</head>
<body>
<div id="chart"></div>
<script type="text/javascript" src="index.js"></script>
</body>
</html>
var width = 960,
height = 500;
var centerX = width/2,
centerY = height/2,
radius = 200,
sides = 200,
coils = 8,
rotation = 0;
// How far to step away from center for each side.
var awayStep = radius/sides;
// How far to rotate around center for each side.
var aroundStep = coils/sides;// 0 to 1 based.
// Convert aroundStep to radians.
var aroundRadians = aroundStep * 2 * Math.PI;
// Convert rotation to radians.
rotation *= 2 * Math.PI;
var new_time = [];
// For every side, step around and away from center.
for(var i=1; i<=sides; i++){
// How far away from center
var away = i * awayStep;
// How far around the center.
var around = i * aroundRadians + rotation;
// Convert 'around' and 'away' to X and Y.
var x = centerX + Math.cos(around) * away;
var y = centerY + Math.sin(around) * away;
new_time.push({x: x, y: y});
}
console.log(new_time);
var svg = d3.select("#chart").append("svg")
.attr("width", width)
.attr("height", height)
.append("g");
var lineFunction = d3.svg.line()
.x(function(d) { return d.x; })
.y(function(d) { return d.y; })
.interpolate("cardinal");
svg.append("path")
.attr("d", lineFunction(new_time))
.attr("stroke", "gray")
.attr("stroke-width", 0.5)
.attr("fill", "none");
var circles = svg.selectAll("circle")
.data(new_time)
.enter()
.append("circle")
.attr("cx", function (d) { return d.x; })
.attr("cy", function (d) { return d.y; })
.attr("r", 2);
@lonnietc
Copy link

Hello,

I am still learning how to use Javascript and 3D.js so here is a really simple question.

How can I add numbers, starting a 0, to each circle location?

I am also working on adding an interactive input and button that would allow you to dynamically adjust the number of rotations, and screen position, but now need to learn how to just add some integer numbers to each data point.

Thanks in advance.

@fabiovalse
Copy link
Author

Hi lonnietc, if you mean to add an SVG text containing an integer near every point on the spiral, you should change lines 56 to 62 to something like:

var circles = svg.selectAll("circle")
  .data(new_time);
circles.enter()
  .append("circle")
  .attr("cx", function (d) { return d.x; })
  .attr("cy", function (d) { return d.y; })
  .attr("r", 2);
circles.enter()
  .append("text")
  .attr("x", function (d) { return d.x; })
  .attr("y", function (d) { return d.y; })
  .text(function (d, i) { return i});

Of course, you should properly handle the visibility of your texts since in this way the visualization will result quite cluttered.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment