I am wondering how to create a gradient line chart fill in Chart JS version 3.3.2 like this:
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>