chartjs / chartjs/chartjs-plugin-datalabels

Getting height of label in scriptable options?

Open
#43 8 comments 1 reaction 0 assignees View on GitHub
support
Dominant language
JavaScript
Stars
920
Forks
507
PR merge metrics
No merged PRs in 30d

Description

I'm looking to implement functionality that will display label inside or outside of bar, depending on how much space is available. The way I've set to do this is by passing a function to `align` and then doing some calculations based on x,y coordinates of individual charts and chart size.

The sample code below works, but to it uses a fixed threshold, where I would like to compute it based on label width/height.

```js
datalabels: {
align: context => {
let meta = context.chart.getDatasetMeta(context.datasetIndex),
bar = meta.data[context.dataIndex]._model,
threshold = 15
// label = context.chart['$datalabels'].labels[context.datasetIndex][context.dataIndex]._model;

if (bar.y + threshold > context.chart.chartArea.bottom) {
return 'end'
}
return 'start'
}
}
```

I've tried using `label` (commented in above code) to try and get sizing info, but all seem to be null on first pass (`_model`, `_hitbox._rect`, ...)

Any ideas, suggestions? Thanks.

Contributor guide

No contributing guide indexed for this repository

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.