-
Notifications
You must be signed in to change notification settings - Fork 9
Components For visualisations and Sidebar #53
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: monitoring-dashboard
Are you sure you want to change the base?
Changes from 2 commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,10 +1,10 @@ | ||
| import React from 'react' | ||
| //@ts-ignore | ||
| import dashboard from '../../assets/images/dashboard.png'; | ||
| const DashobardIcon = () => { | ||
| const DashboardIcon = () => { | ||
| return ( | ||
| <img src={dashboard} alt="dashboard" height="25" /> | ||
| ) | ||
| } | ||
|
|
||
| export default DashobardIcon | ||
| export default DashboardIcon |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1 +1,3 @@ | ||
| export * from './bar' | ||
| export * from './bar' | ||
| export * from './line' | ||
| export * from './pie' |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,40 @@ | ||
| import React, { useEffect, useRef } from "react"; | ||
| import Chart from 'chart.js/auto'; | ||
|
|
||
| const LineChart = ({ data }) => { | ||
| const chartRef = useRef(null); | ||
|
|
||
| useEffect(() => { | ||
| // Create a new line chart instance | ||
| const ctx = chartRef.current.getContext("2d"); | ||
| const chart = new Chart(ctx, { | ||
| type: "line", | ||
| data: data, | ||
| options: { | ||
| responsive: true, | ||
| scales: { | ||
| x: { | ||
| grid: { | ||
| display: false, | ||
| }, | ||
| }, | ||
| y: { | ||
| beginAtZero: true, | ||
| // grid: { | ||
| // borderDash: [2], | ||
| // }, | ||
| }, | ||
| }, | ||
| }, | ||
| }); | ||
|
|
||
| // Clean up the chart instance on component unmount | ||
| return () => { | ||
| chart.destroy(); | ||
| }; | ||
| }, [data]); | ||
|
|
||
| return <canvas ref={chartRef} />; | ||
| }; | ||
|
|
||
| export default LineChart; |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,73 @@ | ||
| import React, { useEffect, useRef } from "react"; | ||
| import Chart from "chart.js/auto"; | ||
| import useWindowSize from "../../hooks/useWindow"; | ||
|
|
||
| const generateRandomColor = () => { | ||
| const letters = "0123456789ABCDEF"; | ||
| let color = "#"; | ||
| for (let i = 0; i < 6; i++) { | ||
| color += letters[Math.floor(Math.random() * 16)]; | ||
| } | ||
| return color; | ||
| }; | ||
|
|
||
| const PieChart = ({ data }) => { | ||
| const chartRef = useRef(null); | ||
| const { height } = useWindowSize(); | ||
|
|
||
| useEffect(() => { | ||
| // Generate dynamic background colors for each segment based on labels | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. since it's reading data from props,so there are chances that when you reload or during initiation time data may be undefined which will break the condition data.labels.map and return error.So I suggest you to use optional chaining like data?.labels?.map
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I have updated it in the recent push. |
||
| const dynamicColors = data.labels.map(() => generateRandomColor()); | ||
|
|
||
| // Create a new pie chart instance | ||
| const ctx = chartRef.current.getContext("2d"); | ||
| const chart = new Chart(ctx, { | ||
| type: "pie", | ||
| data: { | ||
| labels: data.labels, | ||
| datasets: [ | ||
| { | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. here also add optional chaining
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I have updated it in the recent push. |
||
| data: data.datasets[0].data, | ||
| backgroundColor: dynamicColors, | ||
| }, | ||
| ], | ||
| }, | ||
| options: { | ||
| responsive: true, | ||
| plugins: { | ||
| tooltip: { | ||
| callbacks: { | ||
| label: (context) => { | ||
| const label = data.labels[context.dataIndex]; | ||
| const value = data.datasets[0].data[context.dataIndex]; | ||
| const total = data.datasets[0].data.reduce((acc, curr) => acc + curr); | ||
| const percentage = ((value / total) * 100).toFixed(2); | ||
| return `${label}: ${value} (${percentage}%)`; | ||
| }, | ||
| }, | ||
| }, | ||
| }, | ||
| }, | ||
| }); | ||
|
|
||
| // Clean up the chart instance on component unmount | ||
| return () => { | ||
| chart.destroy(); | ||
| }; | ||
| }, [data]); | ||
|
|
||
| return ( | ||
| <div | ||
| style={{ | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. move inline styles to a style class
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I have updated it in the recent push. |
||
| display: "flex", | ||
| justifyContent: "center", | ||
| alignItems: "center", | ||
| maxHeight: `${height - 100}px`, | ||
| }} | ||
| > | ||
| <canvas ref={chartRef} /> | ||
| </div> | ||
| ); | ||
| }; | ||
|
|
||
| export default PieChart; | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
can you move this function to some util function
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It's a hook so I have kept it in the hooks folder. Should I change?