# Calculate bar length for conditional annotation

Iâ€™ve created some bar charts using custom hover text and bar annotations. The main problem Iâ€™m running into is that in some cases the length of the bar is not long enough to include the annotation. In these circumstances, Iâ€™d like to suppress the annotation and the value will be available on the hover event. How can I figure out if the length of the bar is too short for the annotation?

Sample code

bars = [Bar(y = data[â€˜countâ€™].values,
x = data[â€˜valueâ€™].values,
text = ["{0:.2f}%".format(x) for x in data[â€˜percentâ€™].values],
hoverinfo=â€˜textâ€™,
marker={â€˜colorâ€™:[â€™#67a9cfâ€™,â€™#1c9099â€™,â€™#016c59â€™]}
)]

# NOTE: For y the calculation was trying to shift the annotation down slightly so it wasnâ€™t on the edge of the bar. Iâ€™m not sure this the best way to go about it.

annotations = [dict( y=yi - yi*.08,
x=xi,
text="{0: ,}".format(yi),
font={â€˜colorâ€™:â€˜whiteâ€™},
showarrow = False,
) for xi, yi in zip(data[â€˜valueâ€™].values,
data[â€˜countâ€™].values,
)
]

layout = Layout(title=â€˜Titleâ€™,
yaxis={â€˜titleâ€™:â€˜Countâ€™},
height = 450,
width = 800,
annotations=annotations
)

fig = Figure(data= bars, layout=layout)
iplot(fig, config={â€˜displayModeBarâ€™: False})