How to create a gradient fill line chart in latest Chart JS version (3.3.2)?
Asked Answered
L

1

9

I am wondering how to create a gradient line chart fill in Chart JS version 3.3.2 like this:enter image description here

This StackOverflow question was answered nearly 6 years ago. What is the solution in 2021 with version 3.3.2?

I have tried the old solution to no avail:

Code:

    var ctx = document.getElementById('chart').getContext('2d');
    
    var gradient = ctx.createLinearGradient(0, 0, 0, 300);
        gradient.addColorStop(0, 'rgba(224, 195, 155, 1)');   
        gradient.addColorStop(1, 'rgba(100, 100, 0,0)');
        
    var myChart = new Chart(ctx, {
        type: 'line',
        data: {
            labels: ['Red', 'Blue', 'Yellow', 'Green', 'Purple', 'Orange'],
            datasets: [{
                backgroundColor: gradient,
                    label: 'Numbers',
                data: [12, 19, 3, 5, 2, 3],
            }]
        },
        options: {
            scales: {
                y: {
                    beginAtZero: true
                }
            },
            tension: 0.3
        }
    });
<script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/chart.min.js"></script>
<canvas id="chart" width="800" height="400"></canvas>

JSFiddle

Lejeune answered 2/6, 2021 at 20:17 Comment(3)
This post might help you: blog.vanila.io/…Lessen
@mpm that article wont help, thats about chart.js version 2 as he already pointed out that that solution does not work from the stack issueFortnightly
oh, I'm sorry :(Lessen
F
17

You where almost there, in v3 by default line charts dont fill so you will need to set fill: true

Example

    var ctx = document.getElementById('chart').getContext('2d');

    var gradient = ctx.createLinearGradient(0, 0, 0, 300);
    gradient.addColorStop(0, 'rgba(224, 195, 155, 1)');
    gradient.addColorStop(1, 'rgba(100, 100, 0,0)');

    var myChart = new Chart(ctx, {
      type: 'line',
      data: {
        labels: ['Red', 'Blue', 'Yellow', 'Green', 'Purple', 'Orange'],
        datasets: [{
          backgroundColor: gradient,
          label: 'Numbers',
          fill: true,
          data: [12, 19, 3, 5, 2, 3],
        }]
      },
      options: {
        scales: {
          y: {
            beginAtZero: true
          }
        },
        tension: 0.3
      }
    });
<script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/chart.min.js"></script>
<canvas id="chart" width="800" height="400"></canvas>
Fortnightly answered 2/6, 2021 at 20:25 Comment(6)
Works perfectly. You are a lifesaverLejeune
How to do this in React.js?Dyne
Make it a scriptable option in there you get the canvasFortnightly
Great answer, but what if I have two datasets and I want to fill both with different background gradients? How do I create the gradient for the second dataset?Punt
You can copy paste the code for the first gradient and put it in a variable called gradient2Fortnightly
Great answer, thanks for sharing. In order to make it work in my case, I also had to call Chart.register(Fillter) as otherwise the area below the line was not filled at all.Renaissance

© 2022 - 2024 — McMap. All rights reserved.