visit
Let's get started with the areas of interest covered in this post:
The entire project is on
This article will assume you understand using React, as it is required to complete the project. However, you don't need to know Gatsby to implement the chart on the client-side.
Before building, we need to set up our development environment by installing Gatsby globally using its Command Line Interface (CLI). This process serves as the main entry point for kickstarting a Gatsby application.
npm install -g gatsby-cli
gatsby –version
gatsby new
Running the above command in your terminal will start an interactive prompt to help you create a new Gatsby site. For a guide to complete the process, check
To start the local development server on //localhost:8000
, cd into the newly created Gatsby site with the command:
cd gatsby-chart
npm run develop
npm install chart.js react-chartjs-2
chart.js - A simple yet flexible JavaScript charting library
react-chartjs-2 - React components for Chart.js give us many different chart components we can utilize.
We will be working from thepages/index.js file, and you can delete the 404.js as we won't be needing it. Delete everything within the IndexPage component, and you should have this looking the same below.
pages/index.js
import * as React from 'react';
const IndexPage = () => {
return (
<main>
<title>Charting made fun</title>
<h1>Build a chart component</h1>
</main>
);
};
export default IndexPage;
Now is the time to get our chart working. But first, we will need to create a directory called components
in the src
directory. Then, we make a file PieChart.js
that contains all the components.
src/components/PieChart.js
import * as React from 'react';
const PieChart = () => {
return <p>Pie chart</p>;
};
export default PieChart;
We need to import the file into the index page for the created pie chart component to show on the browser.
src/pages/index.js
import * as React from 'react';
import Pie from '../components/Pie;
const IndexPage = () => {
return (
<main>
<title>Charting made fun</title>
<Pie />
</main>
);
};
export default IndexPage;
Let's create the pie chart from the library react-chartjs-2 to utilize the Pie chart. However, other components (types of data visualization that we can use to represent our data in Gatsby) came preinstalled when we installed the dependency like the Chart, Line, Bar, Bubble, PolarArea components, and so on. So it depends on what you want to achieve as you can always reference the.
src/components/PieChart.js
import * as React from 'react';
import { Chart as ChartJS, ArcElement, Tooltip, Legend } from 'chart.js';
import { Pie } from 'react-chartjs-2';
ChartJS.register(ArcElement, Tooltip, Legend);
export const data = {
labels: ['Taxation', 'Materials', 'Profit', 'Expenses', 'Wages'],
datasets: [
{
label: 'Tax bill',
data: [25, 20, 8, 12, 34],
},
],
};
const PieChart = () => {
return (
<div style={{ width: '750px' }}>
<Pie data={data} />
</div>
);
};
export default PieChart;
The following steps are to fill the labels, the border, and the pie chart with background color in an array and the border width. We do this to improve the aesthetic value of the project.
src/components/PieChart.js
import * as React from 'react';
import { Chart as ChartJS, ArcElement, Tooltip, Legend } from 'chart.js';
import { Pie } from 'react-chartjs-2';
ChartJS.register(ArcElement, Tooltip, Legend);
export const data = {
// labels representing each of the tax bill
datasets: [
{
// label for the Pie chart
// data in an array goes here
backgroundColor: [
'rgba(255, 99, 132, 0.2)',
'rgba(54, 162, 235, 0.2)',
'rgba(255, 206, 86, 0.2)',
'rgba(75, 192, 192, 0.2)',
'rgba(153, 102, 255, 0.2)',
],
borderColor: [
'rgba(255, 99, 132, 1)',
'rgba(54, 162, 235, 1)',
'rgba(255, 206, 86, 1)',
'rgba(75, 192, 192, 1)',
'rgba(153, 102, 255, 1)',
],
borderWidth: 1,
},
],
};
const PieChart = () => {
return (
<div style={{ width: '750px' }}>
<Pie data={data} />
</div>
);
};
export default PieChart;
As a developer, are you convinced that we need to simplify its presentation for a wider audience since data is everywhere? We can build these solutions that make it intuitive and interactive for anyone coming across it for the first time on your website.
I recommend that you try it today and tweak those boring ways of presenting data. Use beautiful charts instead, for you and your team.
Find the complete source code on