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

Showing/Hiding Based on Visibility of Lines in Chart #51

Closed
wjohnsto opened this issue Apr 19, 2018 · 12 comments
Closed

Showing/Hiding Based on Visibility of Lines in Chart #51

wjohnsto opened this issue Apr 19, 2018 · 12 comments

Comments

@wjohnsto
Copy link

wjohnsto commented Apr 19, 2018

I have a line chart with a legend and 3 lines drawn. When you click the legend the lines show/hide and the graph adjusts accordingly. Out of the box the datalabels do not hide whenever a line is hidden. I am currently using this method of hiding the datalabels:

options: {
    plugins: {
        datalabels: {
            display: (context: any) => {
                return !context.dataset._meta[0].hidden;
            },
        },
    },
},

This works, but the _meta[0].hidden thing seems like a hack/not something we're supposed to be using. Is there a different approach that is more stable?

@simonbrunel
Copy link
Member

I think that's a bug, it should work out of the box.

Can you build a jsfiddle that reproduces this issue?

@hrobayo
Copy link

hrobayo commented Apr 20, 2018

The same issue form me, the wjohnsto solutions works, Chart.js Version: 2.7.2 used.

datalabelerror

Please, is there another solution?

@wjohnsto
Copy link
Author

Here is a jsfiddle: https://jsfiddle.net/sxzqwr80/76/

@simonbrunel
Copy link
Member

simonbrunel commented Apr 20, 2018

That's a bug, will be fixed in next release (labels will always be hidden if the dataset is hidden).

As a workaround, you can use:

display: function (context) {
    return context.chart.isDatasetVisible(context.datasetIndex);
}

EDIT: remove TypeScript

@hrobayo
Copy link

hrobayo commented Apr 20, 2018

It's nice!, thanks.

@wjohnsto
Copy link
Author

Sweet 👍

@jibe0123
Copy link

Thanks 😍

@hoang051195
Copy link

hoang051195 commented May 30, 2018

Thanks. And I use this for display data with condition

display: function(context) {
    return context.chart.isDatasetVisible(context.datasetIndex)
        && context.dataset.data[context.dataIndex] > 0;
}

Edit(SB): code formatting

@wjohnsto
Copy link
Author

My code is TypeScript, so you may have to remove the non-JS stuff to get it to work.

@wjohnsto
Copy link
Author

display: function (context) {
    return context.chart.isDatasetVisible(context.datasetIndex);
}

@simonbrunel
Copy link
Member

@wjohnsto Thanks (I updated my comment)

@simonbrunel
Copy link
Member

Fixed in 542ec92, will be released in 0.4.0

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

No branches or pull requests

5 participants