简体   繁体   中英

Right-aligning text in Python svgwrite

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:

代码在行的顶部产生数字

What I want is this:

我想要行旁边的数字,右对齐

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.

I have fixed the right-align, and worked around the centring:

    dwg.add( dwg.text( "{}".format(i)
                     , insert=(startx,starty)
                     , transform=rot
                     , style="text-anchor:end;baseline-shift:-33%"
                     , font_size="{}px".format(day_font_size)
                     ) )

text-anchor was the style element I was looking for, not text-align .

baseline-shift of around -33% gives me the appearance of it being centred, and that's good enough for me.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM