How to make React-Line chart in react js
Data visualization has always been an neccesary part of frontend development . There’s always a necessary need to visualize data for our users to help them better understand what is going on in our application. Chart.js is a popular JavaScript library used for creating flexible charts on websites, and in this tutorial, I’ll show you how to use Chart.js in a React environment.
Line chart.js file is displayed below:
1import React from "react"
2import { Line, Pie } from "react-chartjs-2";
3import Chart from 'chart.js/auto'
4const data = {
5 labels: [
6 "01/01/2019",
7 "02/01/2019",
8 "03/01/2019",
9 "04/01/2019",
10 "05/01/2019",
11 "06/01/2019",
12 "07/01/2019"
13 ],
14 //backgroundColor: ['rgba(255,0,0,1)'],
15 //lineTension: 1,
16 datasets: [
17 {
18 label: "Moiz",
19 fill: false,
20 borderColor: "orange",
21 borderWidth: 1,
22 pointRadius: 2,
23 data: [12,34,43,65, 59, 80, 81, 56, 55, 40]
24 },
25 {
26 label: "Hassan",
27 fill: false,
28 borderColor: "yellow",
29 borderWidth: 1,
30 pointRadius: 2,
31 data: [70, 32, 45, 65, 87, 92, 99,767,76,676,67,65]
32 },
33 {
34 label: "alex",
35 fill: false,
36 borderColor: "blue",
37 borderWidth: 2,
38 pointRadius: 2,
39 data: [12,23,21,135, 91, 125, 144, 143, 143, 139]
40 }
41 ]
42};
43var options = {
44 legend: {
45 position: "right",
46 labels: {
47 boxWidth: 10
48 }
49 },
50 scales: {
51 xAxes: [
52 {
53 ticks: { display: false }
54 }
55 ]
56 }
57};
58
59
60
61export default class BarChart extends React.Component {
62 render() {
63 return <>
64 <Line data={data} options={options} />
65
66
67 </>;
68 }
69}
App.js file is displayed below
1import "./styles.css";
2import Chart from "./BarChart"
3
4export default function App() {
5 return (
6 <div className="App">
7 <>
8 <Chart />
9 </>
10
11 </div>
12 );
13}
Here is the link of live example which we done so far:
Click here
About
Moiz is a software engineer from Arid University with a passion for writing tech tutorials and doing coding. I am continously produce JavaScript and other web development technology tutorials in concepts through easy-to-understand explanations written in plain English.I have expertise in next js ,react js ,javascript,html,bootstrap and node js.
Follow him on TwitterHassan is a software engineer from kohat university of science and technology with a passion for writing tech tutorials and doing javascript practices on daily basis.I have expertise in next js ,react js ,javascript,html,bootstrap and node js. Problem Solving and making concpets is my strong point.
Follow him on TwitterTags
Click to see all tutorials tagged with: