Comments (8)
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.
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.
@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.
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.
Would it make sense to add this as an official configurable option, since I imagine it's incredibly common?
from chartjs-plugin-datalabels.
You right, my snippet was wrong (edited), it should return true if data !== 0 (or >= 1).
from chartjs-plugin-datalabels.
Awesome! Thanks!
from chartjs-plugin-datalabels.
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)
- TypeError of datalabels in case stacked100 is enabled HOT 5
- datalabes plugin doesn't work with ES module HOT 1
- Type of font.size might be wrong on the options page.
- Feature Request: option to handle when label cannot fit inside a bar
- vite not insert page ref in manifest.json if I include chartjs-plugin-datalabels HOT 1
- When using with zoom plugin, `clip: true` affect the labels on the edge
- Chartjs .js and .cjs are both imported when using chartjs-plugin-datalabels
- FR: Images and/or Text
- Problem display datalables using Chart.js 4.4.0 HOT 2
- Not compatible with Chart.js 4.4.1 HOT 2
- Add boolean property to match the Datalabels color with the corresponding line/bar color
- Not working with Chart.js 4.4.1 (latest)
- Cant align plugins.legent.labels correctly
- Horizontal stacked barchart with negative values label position is off
- letterSpacing option is not working
- Line or arrow towards data point? HOT 1
- Crashes on Chart.js 4.4.1 and 4.4.0 HOT 3
- Update datalabels when chart is updated.
- know if a label is clipped?
- Display Error when using data labels and min and/or max values for the x-axis HOT 4
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from chartjs-plugin-datalabels.