Home:ALL Converter>Right-aligning text in Python svgwrite

Right-aligning text in Python svgwrite

Ask Time:2020-12-03T03:53:24         Author:PhilHibbs

Json Formatter

I'm trying to rotate text, and that part is working fine, but I want it to be right-justified and not left. In other words I want the text to end on the perimeter of the circle that I have calculated, not begin. This code draws a circle with 365 ticks on it, with a longer tick each 7 days, and each 7th day number inscribed inside the circle of tick marks.

My code does this:

code produces numbers on top of the lines

What I want is this:

I want the numbers next to the lines, right-justified

import math
import svgwrite

radius_inner = 200
radius_width = 40
radius_outer = radius_inner + radius_width
week_line_start = 10
day_line_start = 16
line_end = 10
day_font_size=15
days = 365

dwg = svgwrite.Drawing('YearWheel.svg')

for i in range(0,days):
    angle = (math.pi*2 / days) * i
    if i % 7 == 0:
        startr = week_line_start
    else:
        startr = day_line_start
    startx = math.sin(angle) * (radius_inner + startr)
    endx   = math.sin(angle) * (radius_outer - line_end)
    starty = -math.cos(angle) * (radius_inner + startr)
    endy   = -math.cos(angle) * (radius_outer - line_end)
    print("({},{}),({},{})".format(startx,starty,endx,endy))
    dwg.add(dwg.line((startx,starty), (endx,endy), stroke=svgwrite.rgb(10, 10, 16, '%')))
    if i > 0 and i % 7 == 0:
        rot = 'rotate({},{}, {})'.format(math.degrees(angle)-90,startx,starty)
        dwg.add(dwg.text("{}".format(i), insert=(startx,starty), transform=rot, font_size='{}px'.format(day_font_size)))

dwg.save()

I tried adding ,style="text-align:end" to the text() call (saw that mentioned elsewhere on stackoverflow) but that makes no difference.

Ideally I would like the numbers centered "vertically" with the lines as well.

My initial post reported that the style element was causing an error. This was due to one version of my code having , profile='tiny' in the svgwrite.Drawing constructor.

Author:PhilHibbs,eproduced under the CC 4.0 BY-SA copyright license with a link to the original source and this disclaimer.
Link to original article:https://stackoverflow.com/questions/65115203/right-aligning-text-in-python-svgwrite
yy