How to use Fetch API to Automatically Update Data with JSON in Chart JS

preview_player
Показать описание
How to use Fetch API to Automatically Update Data with JSON in Chart JS

In this video we will explore how to use fetch API to automatically update data with JSON in Chart js. Using the fetch API in Chart js means you need to work from your local server else it will not work.

What we will do is extract the values and once we extra the values we will feed them to the chart gradually one at the time. Secondly we need to remove the old feed once we have more than 12 data points. We will use fetch API with async connection and getting it all from a JSON file we have on our server.

Let's explore this right now!

▬ Chartjs Viewers Question Series ▬▬▬▬▬▬▬▬▬▬

This is part of the Chartjs Viewers Question series. Where we answer viewer questions. This question was asked by one of our viewers. A special thank you to

▬ Materials/References ▬▬▬▬▬▬▬▬▬▬

To keep the video short we might expect you to know parts. These parts we have explained in other videos. You can find the references here below:

▬ Got a Question? Comment! ▬▬▬▬▬▬▬▬▬▬

Got a question or special request about a specific item? Comment below and tell me your question. I will make sure to follow up on you!

▬ Find Me Here ▬▬▬▬▬▬▬▬▬▬

Chart JS tutorials for Beginners:

Chart JS tutorials for Intermediate:

Chart JS Dashboard Series:
👍 Most Liked Video Series:

▬ About Us ▬▬▬▬▬▬▬▬▬▬▬▬

Why we created these #chartjs and #javascript video tutorials?

WHY
Creating charts in javascript is very rewarding but extremely challenging. The Chart JS library made it easier to render charts. However the chart js documentation is hard to understand for many. It requires a lot of different moving parts to work along. The canvas tag, javascript, arrays and Chart JS all need to be combined to draw an eye catching bar chart or line chart.

The videos explains the chart js documentation in a more visual and easy to understand way. You can follow along with the code and quickly grasp how it works. We cover the code in chart js but also what truly happens and why something happens when we write a line of code. This fundamental understanding gives clarity to you as a developer in chartjs. In short it is the Chart JS video documentation.

HOW
We answer questions from YOU (viewers) by creating an answer video that covers the questions YOU have posted in the comment section.

WHAT
Our goal is to help YOU learn how to draw charts in Chart JS by showing you in video format how to do it.

▬ Chart JS 3.6.2 ▬▬▬▬▬▬▬▬▬▬▬▬

Chart JS is a javascript library to draw charts in the canvas tag on your site. Presenting data in a visual manner such as charts is more effective and appealing. All the charts are always coded in the latest Chartjs version which is as of this recording Chart JS 3.6.2.

What type of charts can you make with Chart.JS?
With chart js you can make line chart, bar chart, pie chart, doughnut chart, scatter chart, polar area chart, radar chart, gauge chart and area chart. And with some clever tricks and visual adjustment more can be done.
Рекомендации по теме
Комментарии
Автор

Need the sample JSON file? Copy and past this info below and save as financials.json

{
"year": "2021",
"financialreport": [
{
"company": "ChartJS",
"companyname": "ChartJS Corp",
"companystock": "CHRT",
"financials":
[
{
"date": "January",
"revenue": 90,
"expenses": 30,
"profits": 60,
"target": "Target 1"
},
{
"date": "February",
"revenue": 120,
"expenses": 30,
"profits": 90,
"target": "Target 2"
},
{
"date": "March",
"revenue": 150,
"expenses": 30,
"profits": 120,
"target": "Target 3"
},
{
"date": "April",
"revenue": 180,
"expenses": 30,
"profits": 150,
"target": "Target 4"
},
{
"date": "May",
"revenue": 210,
"expenses": 30,
"profits": 180,
"target": "Target 5"
},
{
"date": "June",
"revenue": 240,
"expenses": 30,
"profits": 210,
"target": "Target 6"
},
{
"date": "July",
"revenue": 270,
"expenses": 30,
"profits": 240,
"target": "Target 7"
},
{
"date": "August",
"revenue": 8,
"expenses": 8,
"profits": 8,
"target": "Target 8"
},
{
"date": "September",
"revenue": 9,
"expenses": 9,
"profits": 9,
"target": "Target 9"
},
{
"date": "October",
"revenue": 10,
"expenses": 10,
"profits": 10,
"target": "Target 10"
},
{
"date": "November",
"revenue": 11,
"expenses": 11,
"profits": 11,
"target": "Target 11"
},
{
"date": "December",
"revenue": 12,
"expenses": 12,
"profits": 12,
"target": "Target 12"
},
{
"date": "January 2022",
"revenue": 1,
"expenses": 1,
"profits": 1,
"target": "Target 1"
},
{
"date": "February 2022",
"revenue": 2,
"expenses": 2,
"profits": 2,
"target": "Target 2"
},
{
"date": "March 2022",
"revenue": 3,
"expenses": 3,
"profits": 3,
"target": "Target 3"
},
{
"date": "April 2022",
"revenue": 4,
"expenses": 4,
"profits": 4,
"target": "Target 4"
},
{
"date": "May 2022",
"revenue": 5,
"expenses": 5,
"profits": 5,
"target": "Target 5"
},
{
"date": "June 2022",
"revenue": 6,
"expenses": 6,
"profits": 6,
"target": "Target 6"
},
{
"date": "July 2022",
"revenue": 7,
"expenses": 7,
"profits": 7,
"target": "Target 7"
},
{
"date": "August 2022",
"revenue": 8,
"expenses": 8,
"profits": 8,
"target": "Target 8"
},
{
"date": "September 2022",
"revenue": 9,
"expenses": 9,
"profits": 9,
"target": "Target 9"
},
{
"date": "October 2022",
"revenue": 10,
"expenses": 10,
"profits": 10,
"target": "Target 10"
},
{
"date": "November 2022",
"revenue": 11,
"expenses": 11,
"profits": 11,
"target": "Target 11"
},
{
"date": "December 2022",
"revenue": 12,
"expenses": 12,
"profits": 12,
"target": "Target 12"
}
]
},
{
"company": "ChartFlix",
"companyname": "ChartFlix Inc.",
"companystock": "CX",
"financials":
[
{
"date": "January",
"revenue": 270,
"expenses": 30,
"profits": 240,
"target": "Target 21"
},
{
"date": "February",
"revenue": 180,
"expenses": 30,
"profits": 150,
"target": "Target 22"
},
{
"date": "March",
"revenue": 150,
"expenses": 30,
"profits": 120,
"target": "Target 23"
},
{
"date": "April",
"revenue": 210,
"expenses": 30,
"profits": 180,
"target": "Target 24"
},
{
"date": "May",
"revenue": 120,
"expenses": 30,
"profits": 90,
"target": "Target 25"
},
{
"date": "June",
"revenue": 180,
"expenses": 30,
"profits": 150,
"target": "Target 26"
},
{
"date": "July",
"revenue": 210,
"expenses": 30,
"profits": 180,
"target": "Target 27"
}
]
},
{
"company": "Chart Start Up",
"companyname": "Chart Start Up",
"companystock": "START",
"financials":
[
{
"date": "January",
"revenue": 3,
"expenses": 1.5,
"profits": 2.5,
"target": "Target 31"
},
{
"date": "February",
"revenue": 6,
"expenses": 3,
"profits": 3,
"target": "Target 32"
},
{
"date": "March",
"revenue": 9,
"expenses": 6,
"profits": 3,
"target": "Target 33"
},
{
"date": "April",
"revenue": 18,
"expenses": 9,
"profits": 9,
"target": "Target 34"
},
{
"date": "May",
"revenue": 12,
"expenses": 3,
"profits": 9,
"target": "Target 35"
},
{
"date": "June",
"revenue": 24,
"expenses": 18,
"profits": 6,
"target": "Target 36"
},
{
"date": "July",
"revenue": 18,
"expenses": 18,
"profits": 0,
"target": "Target 37"
}
]
}
]
}

ChartJS-tutorials
Автор

this was so helpful!! These videos have been amazing, great work

ciaranrobinson
Автор

great, amazing tutorial, it’s useful to me, thanks a lot
I have two question(Situation)
below case need using API
1) How to load 12 items at the beginning and then move forward(shift)
2) one chart, the x-axis item already fixed display, and at this time some value already loading, and next time new value how to put in x-scale-axis

hung-linwu
Автор

Is it possible to automatically update the data without having the user to push any button? For example, based on preset period of time like every 24hrs OR whenever the page is loaded/refreshed. Or is it necessary to let the user manually pull fresh data using the button? Thank you!

jenshartmann
visit shbcf.ru