Skip to content
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

Closed
bitflower opened this issue Nov 15, 2017 · 4 comments
Closed

Data sensitive aligning (depending on bar length not value) #13

bitflower opened this issue Nov 15, 2017 · 4 comments
Labels

Comments

@bitflower
Copy link

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

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?

@simonbrunel
Copy link
Member

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'
       },
       // ...
    }
  }
}

@bitflower
Copy link
Author

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?

@simonbrunel
Copy link
Member

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'

@bitflower
Copy link
Author

Thanks @simonbrunel this helps me a lot !

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants