Skip to content
Advertisement

How can I change background color of a specific area in my chart’s grid using Chart.js and Annotation?

I’m having some trouble trying to change the backgroundColor of a specific chart area between two yAxis ticks.This is what I have so far:

What I'm getting from my code

And this is what I wanted:

What I wanted

I’ve seen some similar posts about that and people recommend using Annotation to do this. I tried using it on my chart and it didn’t work. This is my first time building a chart with chart.js so I’m still learning. Here’s my code:

JavaScript

Advertisement

Answer

You can draw the rectangle directly on the canvas using the Plugin Core API. The API offers a range of hooks that may be used for performing custom code.

In your amended code below, I use the beforeDraw hook to draw the rectangle through CanvasRenderingContext2D.fillRect().

JavaScript
JavaScript
User contributions licensed under: CC BY-SA
6 People found this is helpful
Advertisement