Rotate text around a circle


dbj44

I want to rotate some text (numbers) positioned around a circle so that it looks like this:

enter image description here

In order to apply the rotation, I've tried this: .attr("transform", function(d, i) { return "rotate(" + (-90 + ((360 / dial.length) * i)) + ", 135, 135)"; });but it throws everything away.

Here is a fiddle .

Paul LeBeau

@GerardoFurtado's solution is fine, but the code can be greatly simplified if everything is placed at the origin.

Feel free to accept his answer. I just wanted to point out some efficiencies.

var width = height = 300,
    circleRadius = (width / 2) * .8,
    digitRadius = (width / 2) * .9;

svg = d3.select("body")
  .append("svg")
  .attr("width", width)
  .attr("height", height)
  .append("g")
  // Everything inside the group is centred at the origin and we use
  // a transform on the group to move the whole group to the centre of the SVG
  .attr("transform", "translate(" + width / 2 + "," + height / 2 + ")");

svg.append("circle")
  .attr("r", circleRadius)
  .style("fill", "none")
  .style("stroke", "black");

dial = [1, 2, 3, 4, 5, 6, 7, 8];

// Position text at X=radius, Y=0 and rotate around the origin to get final position
svg.selectAll("text")
  .data(dial)
  .enter()
  .append("text")
  .attr("x", digitRadius)
  // tweak digit Y position a little to ensure it's centred at desired position
  .attr("y", "0.4em")
  .text(function(d, i) { return d; })
  .attr("transform", function(d, i) { return "rotate(" + (-90 + ((360 / dial.length) * i)) + ")"; });
<script src="https://cdnjs.cloudflare.com/ajax/libs/d3/3.4.11/d3.min.js"></script>

Related


Rotate text around a circle

dbj44 I want to rotate some text (numbers) positioned around a circle so that it looks like this: In order to apply the rotation, I've tried this: .attr("transform", function(d, i) { return "rotate(" + (-90 + ((360 / dial.length) * i)) + ", 135, 135)"; });but

Is there a way to rotate the text around a circle (or circles)?

Unbounded Typical spinning wheel I am making a spinning wheel with Python tKinter. Usually, when you spin the wheel, you land on a random slice of the wheel, where the random choice is the text displayed on the slice. I can't find a way to rotate text on slice

Is there a way to rotate the text around a circle (or circles)?

Unbounded Typical spinning wheel I am making a spinning wheel with Python tKinter. Usually, when you spin the wheel, you land on a random slice of the wheel, where the random choice is the text displayed on the slice. I can't find a way to rotate text on slice

Rotate a line around a circle

jack blank I have two arcs with strokes and I want an animated line between them. The animation of the line should be perpendicular to the points of the inner circle. This is what I want, almost what I want. The problem is: The length of the wire is not the le

Rotate a line around a circle

jack blank I have two arcs with strokes and I want an animated line between them. The animation of the line should be perpendicular to the points of the inner circle. This is what I want, almost what I want. The problem is: The length of the wire is not the le

Rotate a line around a circle

jack blank I have two arcs with strokes and I want an animated line between them. The animation of the line should be perpendicular to the points of the inner circle. This is what I want, almost what I want. The problem is: The length of the wire is not the le

Rotate a line around a circle

jack blank I have two arcs with strokes and I want an animated line between them. The animation of the line should be perpendicular to the points of the inner circle. This is what I want, almost what I want. The problem is: The length of the wire is not the le

Rotate the box around a circle

DannieCoderBoi In the process of learning Javascript/jQuery - I've been trying to figure out how to do it, and I think I've completely screwed up JSFiddle. Fiddle : http://jsfiddle.net/y7qWw/ _ What I want to do is rotate the div around a circle so that it is

Rotate a line around a circle

jack blank I have two arcs with strokes and I want an animated line between them. The animation of the line should be perpendicular to the points of the inner circle. This is what I want, almost what I want. The problem is: The length of the wire is not the le

Rotate a line around a circle

jack blank I have two arcs with strokes and I want an animated line between them. The animation of the line should be perpendicular to the points of the inner circle. This is what I want, almost what I want. The problem is: The length of the wire is not the le

Rotate the box around a circle

DannieCoderBoi In the process of learning Javascript/jQuery - I've been trying to figure out how to do it, and I think I've completely screwed up JSFiddle. Fiddle : http://jsfiddle.net/y7qWw/ _ What I want to do is rotate the div around a circle so that it is

Rotate circle around triangle canvas

KX0 I want to rotate a circle around a triangle using canvas. Had this code earlier, but here is the circle in the middle, and a rotated rectangle, I want the circle to rotate and have a triangle in the middle. Can someone help? Here is the JS code I have: var

Rotate svg path around circle

Usama nadeem I want to rotate a polygon around a circle. I want to fix the starting point of the polygon to the center of the circle like this http://www.enspiregroup.com . I tried a lot but got stuck. See code below. CSS and HTML5 .circle-segment {

Rotate circle around triangle canvas

KX0 I want to rotate a circle around a triangle using canvas. Had this code earlier, but here is the circle in the middle, and a rotated rectangle, I want the circle to rotate and have a triangle in the middle. Can someone help? Here is the JS code I have: var

Rotate svg path around circle

Usama nadeem I want to rotate a polygon around a circle. I want to fix the starting point of the polygon to the center of the circle like this http://www.enspiregroup.com . I tried a lot but got stuck. See code below. CSS and HTML5 .circle-segment {

Rotate svg path around circle

Usama nadeem I want to rotate a polygon around a circle. I want to fix the starting point of the polygon to the center of the circle like this http://www.enspiregroup.com . I tried a lot but got stuck. See code below. CSS and HTML5 .circle-segment {

Rotate svg path around circle

Usama nadeem I want to rotate a polygon around a circle. I want to fix the starting point of the polygon to the center of the circle like this http://www.enspiregroup.com . I tried a lot but got stuck. See code below. CSS and HTML5 .circle-segment {

Rotate svg path around circle

Usama nadeem I want to rotate a polygon around a circle. I want to fix the starting point of the polygon to the center of the circle like this http://www.enspiregroup.com . I tried a lot but got stuck. See code below. CSS and HTML5 .circle-segment {

Rotate circle around triangle canvas

KX0 I want to rotate a circle around a triangle using canvas. Had this code earlier, but here is the circle in the middle, and a rotated rectangle, I want the circle to rotate and have a triangle in the middle. Can someone help? Here is the JS code I have: var

Rotate svg path around circle

Usama nadeem I want to rotate a polygon around a circle. I want to fix the starting point of the polygon to the center of the circle like this http://www.enspiregroup.com . I tried a lot but got stuck. See code below. CSS and HTML5 .circle-segment {

Rotate svg path around circle

Usama nadeem I want to rotate a polygon around a circle. I want to fix the starting point of the polygon to the center of the circle like this http://www.enspiregroup.com . I tried a lot but got stuck. See code below. CSS and HTML5 .circle-segment {

Rotate svg path around circle

Usama nadeem I want to rotate a polygon around a circle. I want to fix the starting point of the polygon to the center of the circle like this http://www.enspiregroup.com . I tried a lot but got stuck. See code below. CSS and HTML5 .circle-segment {

Rotate a circle around another rotation circle

Suhas Batu I have a circle in an svg that rotates around a given point. I added another circle. I want to rotate the second circle around the first rotation circle. How can I do this? So far I am able to rotate the first circle around a point. The second circl

Place text around circle

Anmol Durgapal: Using the pyplot circlefunction I make a circle, then I use the textfunction to place the text (parameters) on the circle (PLOT ATTACHED), but the problem is, if I want to list only 6 parameters or say 11 equally spaced on the circle The parame

Place text around circle

Anmol Durgapal: Using the pyplot circlefunction I make a circle, then I use the textfunction to place the text (parameters) on the circle (PLOT ATTACHED), but the problem is if I want to list only 6 parameters or say 11 equally spaced on the circle The paramet

Place text around circle

Anmol Durgapal: Using the pyplot circlefunction I make a circle, then I use the textfunction to place the text (parameters) on the circle (PLOT ATTACHED), but the problem is, if I want to list only 6 parameters or say 11 equally spaced on the circle The parame

Circle text around circular text

xdtTransform content: Try putting the SVG path text on top of the circular image. Use to round a square image rounded-circle. SVG paths are drawn around it and used to write text. Lead to: Code : https://jsfiddle.net/ghLn4jkt/ _ <div class="mt-3 row"> <div

Circle text around circular text

xdtTransform content: Try putting the SVG path text on top of the circular image. Use to round a square image rounded-circle. SVG paths are drawn around it and used to write text. lead to: Code : https://jsfiddle.net/ghLn4jkt/ _ <div class="mt-3 row"> <div

Rotate an image with CSS without moving around a circle

you can: I have an image of a wind turbine that I want to rotate. The code below will do the job, but with the wind turbine spinning in one circle. How to make it not rotate around the circle? any suggestion <!DOCTYPE html> <html> <head> <style> .rotate