Skip to content

Instantly share code, notes, and snippets.

@luke-au
Created November 10, 2023 09:22
Show Gist options
  • Save luke-au/3123a8972bf3e667c0c342ff11d68dab to your computer and use it in GitHub Desktop.
Save luke-au/3123a8972bf3e667c0c342ff11d68dab to your computer and use it in GitHub Desktop.
Chart.js Gradients
<canvas id="bar-chart" width="400" height="300"></canvas>
<!-- <canvas id="line-chart" width="400" height="400"></canvas> -->
// Chart.defaults.global.elements.rectangle.backgroundColor = '#FF0000';
var bar_ctx = document.getElementById('bar-chart').getContext('2d');
var purple_orange_gradient = bar_ctx.createLinearGradient(0, 0, 0, 600);
purple_orange_gradient.addColorStop(0, 'orange');
purple_orange_gradient.addColorStop(1, 'purple');
var bar_chart = new Chart(bar_ctx, {
type: 'bar',
data: {
labels: ["Red", "Blue", "Yellow", "Green", "Purple", "Orange"],
datasets: [{
label: '# of Votes',
data: [12, 19, 3, 8, 14, 5],
backgroundColor: purple_orange_gradient,
hoverBackgroundColor: purple_orange_gradient,
hoverBorderWidth: 2,
hoverBorderColor: 'purple'
}]
},
options: {
scales: {
yAxes: [{
ticks: {
beginAtZero:true
}
}]
}
}
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/Chart.js/2.1.0/Chart.min.js"></script>
canvas {
display: block;
max-width: 800px;
margin: 60px auto;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment