How to detect when a "yAdjusted" label is out of bounds #964
-
Hi, In my stacked Bar chart I display an annotation representing the total of the bar, positioned just above the bar: That's the red label here, it uses The problem here is that sometimes the label gets cropped as seen on column 1. The label position in itself is fine, I just want to increase the scale in this case.
const checkOverflowPlugin = {
id: 'checkOverflow',
afterLayout(chart, args) {
if (isUpdating) return
const max = chart.scales.y.max
if (!max) return
const max_data = chart.scales.y._range.max
// trying to convert 50 to a position here, is there a better way?
const label_position = chart.scales.y.getValueForPixel(
chart.chartArea.bottom - 50
)
const real_height = ZERO.plus(max_data).plus(ZERO.plus(label_position).abs())
const is_overflowing = real_height.gt(max)
if (!is_overflowing) return
const nextYMax = // computing the wanted Y size
isUpdating = true
chart.scales.y.options.max = nextYMax
chart.update()
isUpdating = false
},
} It works, but it's a bit ugly: a flag is needed to prevent recursion (I could't find another matching hook considering the props needed here), but more importantly the update will trigger an extra animation, which I don't want (setting animations to false for the time of update is not possible in my case as I rely on it for other matters). The question here, unless I'm missing a simpler way to achieve my goal, is how to convert this absolute My other idea was to create another annotation, invisible this time, that would be positioned at the same point as the red label, and detect if this point is out of bounds (then maybe use Also I cannot find a way to "adjust" an annotation the same way an annotation label is adjusted (this would probably solve my case). I'm running out of ideas, any help would be greatly appreciated! Thanks |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 11 replies
-
@gterras thanks for feedback! Not sure if I caught your requirement. Correct me if what I understood is wrong. Currently, using an absolute adjust, the annotation is out of the chart area. Instead, if you want that the The annotation size (and other options) are calculated during the Can you provide the chart options or create a codepen to play on that? |
Beta Was this translation helpful? Give feedback.
-
After first testing it appears to work remarkably well, even in extreme situations. The |
Beta Was this translation helpful? Give feedback.
@gterras apologize for my late reply but I was quite busy today on other stuff.
Here my result:
Codepen: https://codepen.io/stockinail/pen/jENgmxj
Some notes:
afterDataLimits
hook to set the adjust max value to the scale.yAdjust
option as scriptable optionsThe sample is based on the use case you shared but it should have any hard code conf (I set 5 ad padding between labels and between the stack bar and the first label but you can use whatever you want).
Feel free to play with codepen and let me know if make sense to you.