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

Is it possible to clip a box annotation to a line graph? #947

Closed
bellwood opened this issue Sep 26, 2024 · 9 comments
Closed

Is it possible to clip a box annotation to a line graph? #947

bellwood opened this issue Sep 26, 2024 · 9 comments

Comments

@bellwood
Copy link

bellwood commented Sep 26, 2024

I have box annotations that track sunrise/sunset and regardless of when I draw them, because of the color transparencies, they muddle with the area fill of the line.

Is it possible to clip box annotations so they only appear above the line, essentially a reverse fill for the line area, as defined by the box(es)?

image
@stockiNail
Copy link
Collaborator

stockiNail commented Sep 26, 2024

@bellwood yes, you can. You need to set in your annotation options the drawTime option: https://www.chartjs.org/chartjs-plugin-annotation/latest/guide/options.html#draw-time

If you set that option to 'beforeDatasetsDraw', the annotation is drawn before the datasets and therefore it doesn't override the area chart.

See example: https://www.chartjs.org/chartjs-plugin-annotation/latest/samples/box/quarters.html

@bellwood
Copy link
Author

@stockiNail

Thank you for the reply.

If you goto the setup tab in the example given, and add "fill:true" to the line data config, you can see that the box annotation colors still bleed through from behind the line fill.

The desire in clipping above the line would be for the box annotations to not be visible at all below the line and therefore not interfere with the fill color or the grid lines.

Thank you.

@stockiNail
Copy link
Collaborator

@bellwood thank you for explanation. Now I have understood what you meant.

At the moment it is not possible. And I think it is quite hard to do it therefore I don't think it will be implemented.

The only way that I see is remove the transparency of the backgroundColor of the area chart.
But I can understand it is not what you want.

@bellwood
Copy link
Author

bellwood commented Sep 26, 2024

@stockiNail thanks for the confirmation. Dropping the transparency does help but then one loses the visual reference of the grid.

The only possible workaround I've thought of, but have not yet tried, would maybe be a duplicate line set with adjusted values so that line one's max would be line two's min, with its reference axes being line one's overall max, so that its fill would be "above" line one (hopefully that makes sense)

I'm unsure if then leaving data gaps in line 2 for where I don't want fill (by setting the appropriate spanGaps property?) might work?

Appreciate the dialogue, thank you.

@stockiNail
Copy link
Collaborator

@bellwood I think your idea could be right one. You could play a bit with fill and spanGaps options and could reach the result.

@stockiNail
Copy link
Collaborator

@bellwood just a quick example (which must be changed a bit, of course):

https://codepen.io/stockinail/pen/gOVpXBE

image

@bellwood
Copy link
Author

@stockiNail STACK! Why didn't I think of that?! Will give this a go, thank you VERY much.

@bellwood
Copy link
Author

bellwood commented Sep 27, 2024

image

I'll need to re-work what I'm using to draw box annotations to create/adjust a second dataset accordingly, but, from toying with the pen, I got it where I'd hoped I could.

Thank you again @stockiNail, cheers 🍻

@stockiNail
Copy link
Collaborator

@bellwood let me close it. Feel free to reopen if needed.

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

2 participants