Giter VIP home page Giter VIP logo

Comments (8)

simonbrunel avatar simonbrunel commented on May 18, 2024 64

You can use scriptable options (example):

options: {
  plugins: {
    datalabels: {
       display: function(context) {
          return context.dataset.data[context.dataIndex] !== 0; // or >= 1 or ...
       }
    }
  }
}

from chartjs-plugin-datalabels.

DynamicArray avatar DynamicArray commented on May 18, 2024 4

What about hiding when the label can't be completely displayed? I'm thinking of a measureText call on the canvas ct in the display callback, but I can't find a piece of data that tells me how big the bar (my chart is a vertical bar chart) for the current index is. Any suggestion?

from chartjs-plugin-datalabels.

simonbrunel avatar simonbrunel commented on May 18, 2024 3

@davidklebanoff I designed scriptable options to actually avoid introducing stuff like hideWhenZero which are IMO use case oriented instead of generic options. Adding it built in would require more processing code to guess the actual value and thus more code to maintain for something really easy to implement on the user end. It's also a door opened to options like: colorAtZero, backgroundAtZero, colorWhenNegative, etc. So no, it doesn't make sense to me and I will likely reject any related PR.

If you need this behavior for all your charts, you can setup a global option:

Chart.defaults.global.plugins.datalabels.display = function(ctx) {
  return ctx.dataset.data[ctx.dataIndex] !== 0;
}

You can also easily implement this option yourself in your projects:

Chart.defaults.global.plugins.datalabels.display = function(ctx) {
  var opts = ctx.chart.options.plugins.datalabels;
  return opts.hideWhenZero
    ? ctx.dataset.data[ctx.dataIndex] !== 0
    : undefined;
}

Something I could maybe consider however is to expose the value in the context:

Chart.defaults.global.plugins.datalabels.display = function(ctx) {
  return ctx.value !== 0;
}

from chartjs-plugin-datalabels.

erenesto avatar erenesto commented on May 18, 2024 2

Thank you very much !

I used return context.dataset.data[context.dataIndex] > 1; because i wanted to hide zeros. It's working very well.

from chartjs-plugin-datalabels.

davidklebanoff avatar davidklebanoff commented on May 18, 2024 2

Would it make sense to add this as an official configurable option, since I imagine it's incredibly common?

from chartjs-plugin-datalabels.

simonbrunel avatar simonbrunel commented on May 18, 2024

You right, my snippet was wrong (edited), it should return true if data !== 0 (or >= 1).

from chartjs-plugin-datalabels.

uffou avatar uffou commented on May 18, 2024

Awesome! Thanks!

from chartjs-plugin-datalabels.

jcraigk avatar jcraigk commented on May 18, 2024

I'm using Ruby on Rails with chartkick to construct Chart.js calls server-side. That method uses Ruby hashes for configuration (which later get translated to Javascript) and it's not clear how to inject javascript functions using this method. As this is a fairly common use case, I second the request to have "hiding zeroes" as an option. Thanks for considering!

from chartjs-plugin-datalabels.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.