Search icon CANCEL
Arrow left icon
Explore Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Conferences
Free Learning
Arrow right icon
Arrow up icon
GO TO TOP
Learn Chart.js

You're reading from   Learn Chart.js Create interactive visualizations for the Web with Chart.js 2

Arrow left icon
Product type Paperback
Published in Feb 2019
Publisher
ISBN-13 9781789342482
Length 284 pages
Edition 1st Edition
Languages
Tools
Arrow right icon
Author (1):
Arrow left icon
Helder da Rocha Helder da Rocha
Author Profile Icon Helder da Rocha
Helder da Rocha
Arrow right icon
View More author details
Toc

Table of Contents (9) Chapters Close

Preface 1. Introduction 2. Technology Fundamentals FREE CHAPTER 3. Chart.js - Quick Start 4. Creating Charts 5. Scales and Grid Configuration 6. Configuring Styles and Interactivity 7. Advanced Chart.js 8. Other Books You May Enjoy

Gradients


There is no native support in Chart.js for gradients, but they are fairly easy to generate with Canvas. The problem is that a gradient has an absolute position in a Canvas object, while your chart may be responsive. If the chart is resized, the gradient has to be recalculated and the chart updated.

 

One way to deal with this is to call a gradient function as soon as the chart is created and every time the window is resized, feeding the Canvas gradient function with the dimensions of the area where the gradient will be applied. We can do this with a callback and the Chart.js update() function.

A gradient in Canvas is created with the following function:

canvasContext.createLinearGradient(x0, y0, x1, y1);

The gradient contains the equation of a perpendicular line. To create a linear gradient that varies along the y axis, we need to draw the line from the bottom of the chart to the top. That means that x0 = x1 = 0, y1 is the bottom of the chart, and y0 is the top. If we write a function...

lock icon The rest of the chapter is locked
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at €18.99/month. Cancel anytime