我正在尝试将一行中的文本对齐,以使标签中的第一个字母与该行的左边缘完全对齐,而不考虑我们正在查看的时间范围。 Here is a link to the image showing how it currently appears。
代码如下:
draw_adr_hilo(hi_level, lo_level, res, adr, level_color) =>
var line hi_line = na
var line lo_line = na
if new_bar(res)
line.set_x2(hi_line, bar_index)
line.set_x2(lo_line, bar_index)
line.set_extend(hi_line, extend.none)
line.set_extend(lo_line, extend.none)
hi_line := line.new(bar_index, hi_level, bar_index, hi_level, extend=extend.right, color=level_color)
lo_line := line.new(bar_index, lo_level, bar_index, lo_level, extend=extend.right, color=level_color)
label.new(bar_index, hi_level, "Average Range: " + tostring(to_pips(adr),'#.##') + " pips", xloc.bar_index, yloc.price, style=label.style_none, textcolor=color.white)
if not na(hi_line) and line.get_x2(hi_line) != bar_index
line.set_x2(hi_line, bar_index)
line.set_x2(lo_line, bar_index)
我尝试将“ bar_index”从0手动更改为另一个数字,但是标签中的文本继续向左移动,远离行。
我还有一个问题。我想在这行上方有两行文字。例如,我目前有一个标签称为“平均范围”,您能否提供在当前标签上方或下方添加另一个标签所需的指导?因此它将是“平均范围”,hi_line的价格高于或低于该价格。
非常感谢,谢谢您的帮助!
答案 0 :(得分:2)
精确的水平对齐将存在问题,因为文本在标签中居中。在这里看起来不错,但在所有图表TF上都不是完美的。请参见此代码中的lbl3
。
对于上面的多余行,上面的答案将起作用。有关该行上方和下方的行,请参见代码中的lbl1
和lbl2
:
//@version=4
study("Labels on Lines", "", true)
yp = highest(20)
newYp = change(yp)
if newYp
// Labels up and down are used to print above and below line, with invisible color so we don't see them.
lbl1 = label.new(bar_index, yp, "Above1\nAbove2", color=#00000000, style=label.style_labeldown, textcolor=color.green)
lbl2 = label.new(bar_index, yp, "Below1\nBelow2", color=#00000000, style=label.style_labelup, textcolor=color.red)
// Label none is used here because aligns vertically above line.
// Left padding of text with spaces must vary with length of string since text is centered.
// Horizontal alignment won't always be perfect.
lbl3 = label.new(bar_index, yp, " On Line", style=label.style_none, size=size.large, textcolor=color.orange, xloc=xloc.bar_index)
if newYp[1]
// Delete previous label when there is a consecutive new high, as there's no line plot in that case.
label.delete(lbl1[1])
label.delete(lbl2[1])
label.delete(lbl3[1])
plot(newYp ? na : yp, "", color.gray, 2, plot.style_linebr, offset = -1)