Prep 10: Using Libraries
Textbook Reading
Most of what you’ll build on the job uses code other people wrote. Today’s skill isn’t memorizing these libraries, it’s learning how to pick one up from its documentation.
- JS FunWebDev
- 10.4 Using Browser APIs
- 10.5 Using External APIs
- Modules MDN & Friends
- Modern JavaScript Tutorial: Modules, introduction
- Review FunWebDev
- 10.1 Array Functions
- 10.3 Asynchronous Coding (
fetchwithasync/await)
Activity: Your First Chart
A chart library turns data into pictures so you don’t have to draw them yourself. In this tutorial you’ll learn the most popular one, Chart.js, by making a chart.
Getting Started
Download the starter code here: chart.html
Open it in VS Code and run it with Live Server, as you’ve done before. You’ll see a heading and some data in the code, but no chart yet. You’ll add the chart by following the steps below.
Step 1: Add Chart.js to the page
A library is just someone else’s JavaScript file. To use it, your page has to load that file. The easiest way is a CDN (content delivery network): a server that hosts the file so you can link to it with a <script> tag.
Find the comment <!-- Step 1 --> in chart.html and put this tag there:
<script src="https://cdn.jsdelivr.net/npm/chart.js@4.4.4/dist/chart.umd.min.js"></script>
It has to come before your own <script>, so Chart.js is loaded before your code tries to use it.
Check it
Reload the page, open the DevTools console (F12), and type Chart. You should see something like class Chart { ... }. If you see Chart is not defined, check the src.
Step 2: Make a bar chart
Chart.js draws on a <canvas> element (the page already has one with the id studyChart). To make a chart, you call new Chart(...) with two things: the canvas, and a configuration object that describes the chart.
In your script, under // Step 2 and 3, add:
new Chart(document.getElementById("studyChart"), {
type: "bar",
data: {
labels: days,
datasets: [{ label: "Hours studied", data: hours }],
},
});
The configuration has two main parts:
type: what kind of chart ("bar","line","scatter","pie", and more)data: what to draw.labelsare the names along the bottom, and each item indatasetsis a series of numbers, one per label.
Check it
You should see five bars, one for each day. Thursday’s bar is missing because that day was 0 hours.
Step 3: Customize it
The third part of the configuration is options, which controls how the chart looks. Update your chart so it has a title, a label on the y-axis, and a different color for each bar:
new Chart(document.getElementById("studyChart"), {
type: "bar",
data: {
labels: days,
datasets: [{
label: "Hours studied",
data: hours,
backgroundColor: ["#e63946", "#f4a261", "#e9c46a", "#2a9d8f", "#457b9d"],
}],
},
options: {
plugins: {
title: { display: true, text: "Study time this week" },
},
scales: {
y: { title: { display: true, text: "Hours" } },
},
},
});
Notice how the options are nested: plugins holds the chart title, and scales holds settings for each axis (x and y). The Chart.js docs list every option. Try changing some and see what happens!
Submission
Submit your completed chart.html file to Gradescope. You can submit as many times as you like.