Why am I unable to change my label alignment to the line?

Viewed 34

I'm a novice programmer that started to learn pine script who need some help figuring this puzzle out. I think the problem is the get_limit_right() function but im not sure.

The label text is aligned to center (at the begging of the line), and I would like to change the alignment to left (also at the beginning of the line). How can I achieve such result? If you need more code, feel free to ask.

TLDR: I want the first letter of the label to start on the same vertical axis as the line.

I've tried to change a lot of stuff, but nothing seems to work. Any help would be appreciated. Thank you!

if is_monthly_enabled
    monthly_limit_right = get_limit_right(DEFAULT_EXTEND_RIGHT)
    if displayStyle == 'Right Anchored'
        monthly_time := get_limit_right(radistance)
        monthly_time
    var monthlyLine = line.new(x1=monthly_time, x2=monthly_limit_right, y1=monthly_open, y2=monthly_open, color=MonthlyColor, width=DEFAULT_LINE_WIDTH, xloc=xloc.bar_time, style=linestyles)
    var monthlyLabel = label.new(x=monthly_limit_right, y=monthly_open, text=motext, style=DEFAULT_LABEL_STYLE, textcolor=MonthlyColor, size=DEFAULT_LABEL_SIZE, xloc=xloc.bar_time)
    line.set_x1(monthlyLine, monthly_time)
    line.set_x2(monthlyLine, monthly_limit_right)
    line.set_y1(monthlyLine, monthly_open)
    line.set_y2(monthlyLine, monthly_open)
    label.set_x(monthlyLabel, monthly_limit_right)
    label.set_y(monthlyLabel, monthly_open)
    label.set_text(monthlyLabel, motext)
    if mergebool
        f_LevelMerge(pricearray, labelarray, monthly_open, monthlyLabel, MonthlyColor)
1 Answers

The vertical alignment of the text also depends on the label style. You can use label.style_label_center for a nice and clean solution but definitely check out the other solutions as well.

//@version=5
indicator("My script", overlay=true)

_y1 = 30000
_y2 = 28000
_y3 = 26000
_y4 = 24000
_y5 = 22000
_y6 = 20000

f_draw_line_and_label(_y, sty, str) =>
    offset1 = 10
    offset2 = 5
    line.new(bar_index, _y, bar_index + offset1, _y)
    label.new(bar_index + offset1 + offset2, _y, str + ": " + str.tostring(_y), style=sty, textcolor=color.white)

if barstate.islast
    f_draw_line_and_label(_y1, label.style_label_down, "down")  // default style
    f_draw_line_and_label(_y2, label.style_none, "none")
    f_draw_line_and_label(_y3, label.style_label_up, "up")
    f_draw_line_and_label(_y4, label.style_label_center, "center")
    f_draw_line_and_label(_y5, label.style_label_right, "right")
    f_draw_line_and_label(_y6, label.style_label_left, "left")

enter image description here

Related