-
Notifications
You must be signed in to change notification settings - Fork 459
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Data sensitive aligning (depending on bar length not value) #13
Comments
It's a bit tricky to implement because these information (bar coordinates) are private and can change without notice, breaking your code. You can still do it using scriptable options, but I can't guarantee that it will work forever (see this fiddle): options: {
plugins: {
datalabels: {
align: function(context) {
var chart = context.chart;
var area = chart.chartArea;
var meta = chart.getDatasetMeta(context.datasetIndex);
// WARNING: meta.data._model is PRIVATE and thus can
// change without notice, breaking code relying on it!
var model = meta.data[context.dataIndex]._model;
var bottom = Math.min(model.base, area.bottom);
var height = bottom - model.y;
return height < 50 ? 'end' : 'start'
},
// ...
}
}
} |
I can live with that restriction. Thanks so much for the snippet. I adapted it for horizontal barcharts: // Source: https://github.com/chartjs/chartjs-plugin-datalabels/issues/13
var chart = context.chart;
var area = chart.chartArea;
var meta = chart.getDatasetMeta(context.datasetIndex);
// WARNING: meta.data._model is PRIVATE and thus can
// change without notice, breaking code relying on it!
var model = meta.data[context.dataIndex]._model;
//var bottom = Math.min(model.base, area.bottom);
var height = model.x - area.left; //bottom - model.y;
return height < 50 ? 'end' : 'start' Would you say this is correct? |
For horizontal bar, maybe: //...
// WARNING: meta.data._model is PRIVATE and thus can
// change without notice, breaking code relying on it!
var model = meta.data[context.dataIndex]._model;
var left = Math.max(model.base, area.left);
var width = model.x - left;
return width < 25 ? 'end' : 'start' |
Thanks @simonbrunel this helps me a lot ! |
Hi, I came across your plugin because it was recommended here: https://stackoverflow.com/questions/31631354/how-to-display-data-values-on-chart-js.
How can I achieve a data sensitive shift of the align parameter? There doesnt seem to be a logic build in as my example chart shows:
![chart](https://user-images.githubusercontent.com/6666636/32835609-07e0df0a-ca07-11e7-90d6-8af1a1eed73a.png)
I need to switch the align parameter depending on the draw height / width of the bar.
In the align function I have implemented for the "align" parameter I have access to the chart instance, context etc. but the height of the ChartElement for all data is undefined.
Is this possible?
The text was updated successfully, but these errors were encountered: