-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #19 from mayukh551/stats-page
added designs of monthly and yearly categorywise expense doughnut charts
- Loading branch information
Showing
7 changed files
with
255 additions
and
10 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,115 @@ | ||
import React, { useEffect, useState } from 'react' | ||
import '../Profile.css' | ||
|
||
import { Chart as ChartJS, CategoryScale, ArcElement, LinearScale, PointElement, LineElement, Title, Tooltip, Legend } from "chart.js"; | ||
import SelectBtn from '../../UI/SelectBtn'; | ||
import { SelectChangeEvent } from '@mui/material/Select'; | ||
import ChartSpinner from '../../UI/Spinners/ChartSpinner'; | ||
import { Doughnut } from 'react-chartjs-2'; | ||
|
||
ChartJS.register(CategoryScale, LinearScale, PointElement, LineElement, Title, Tooltip, Legend, ArcElement); | ||
|
||
|
||
const options = { | ||
responsive: true, | ||
plugins: { | ||
legend: { | ||
position: "top" as const | ||
} | ||
} | ||
}; | ||
|
||
|
||
function CategoryPieChart() { | ||
|
||
const [expenseData, setExpenseData] = useState<number[]>([40, 24, 12]); | ||
const year: string = String(new Date().getFullYear()); | ||
const [chartYear, setChartYear] = useState<string>(localStorage.getItem('year')!); | ||
const [labels, setLabels] = useState<string[]>([]); | ||
|
||
var yearList: string[] = []; | ||
|
||
const [isLoading, setIsLoading] = useState<boolean>(true); | ||
|
||
const currentYear = parseInt(year); | ||
for (let year = currentYear; year >= 2019; year--) { | ||
yearList.push(String(year)); | ||
} | ||
// console.log(expenseData) | ||
|
||
const data = { | ||
labels: labels, | ||
datasets: [{ | ||
label: 'Expenses', | ||
data: expenseData, | ||
backgroundColor: [ | ||
'rgb(255, 99, 132)', | ||
'rgb(54, 162, 235)', | ||
'rgb(255, 205, 86)', | ||
'rgb(75, 192, 192)' | ||
], | ||
hoverOffset: 4 | ||
}] | ||
}; | ||
|
||
console.log(data); | ||
|
||
const selectEventHandler = (event: SelectChangeEvent<string>) => { | ||
setChartYear(event.target.value); | ||
setIsLoading(true); | ||
}; | ||
|
||
useEffect(() => { | ||
async function fetchYearAnalytics() { | ||
// console.log(isLoading) | ||
|
||
console.log('fetching category pie chart data'); | ||
|
||
const response = await fetch( | ||
`${process.env.REACT_APP_SERVER_URL}/profile/pie-chart/category/${localStorage.getItem('userId')}?year=${chartYear}`, { | ||
headers: { | ||
'x-access-token': `${localStorage.getItem('token')}` | ||
} | ||
}); | ||
const { data, labels } = await response.json(); | ||
console.log(data, labels); | ||
setIsLoading(false); | ||
return { data, labels }; | ||
} | ||
|
||
|
||
fetchYearAnalytics() | ||
.then((data) => { | ||
if (data.data != null) { | ||
setExpenseData([...data.data]) | ||
setLabels([...data.labels]) | ||
} | ||
else { | ||
setExpenseData([]); | ||
setLabels([]); | ||
} | ||
}) | ||
}, [chartYear]); | ||
|
||
return ( | ||
<div> | ||
<div className="chart-wrapper"> | ||
<div className="chart" style={{ width: '370px', margin: 'auto', marginTop: '20px' }}> | ||
<SelectBtn | ||
options={yearList} | ||
val={chartYear} | ||
selectEventHandler={selectEventHandler} | ||
style={{ backgroundColor: 'white', width: '100px', height: '30px', marginBottom: '10px' }} | ||
/> | ||
{isLoading && <ChartSpinner />} | ||
{!isLoading && expenseData!.length === 0 && <h4 className='mt-8 mb-4 font-medium text-lg'>You have no expenses for {chartYear}</h4>} | ||
|
||
{!isLoading && expenseData!.length > 0 && <Doughnut options={options} data={data} />} | ||
</div> | ||
<h4 className='mt-4 text-base font-bold px-4 py-2 rounded-md shadow-md w-fit mx-auto'>Category Based Expenses</h4> | ||
</div> | ||
</div> | ||
) | ||
} | ||
|
||
export default CategoryPieChart; |
123 changes: 123 additions & 0 deletions
123
src/Components/Profile/Chart/MonthlyExpensePieChart.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,123 @@ | ||
import React, { useEffect, useState } from 'react' | ||
import '../Profile.css' | ||
|
||
import { Chart as ChartJS, CategoryScale, ArcElement, LinearScale, PointElement, LineElement, Title, Tooltip, Legend } from "chart.js"; | ||
import SelectBtn from '../../UI/SelectBtn'; | ||
import { SelectChangeEvent } from '@mui/material/Select'; | ||
import ChartSpinner from '../../UI/Spinners/ChartSpinner'; | ||
import { Doughnut } from 'react-chartjs-2'; | ||
|
||
ChartJS.register(CategoryScale, LinearScale, PointElement, LineElement, Title, Tooltip, Legend, ArcElement); | ||
|
||
|
||
const options = { | ||
responsive: true, | ||
plugins: { | ||
legend: { | ||
position: "top" as const | ||
} | ||
} | ||
}; | ||
|
||
const monthList = ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec']; | ||
// bacground colors for 12 months | ||
const backgroundColors = [ | ||
'rgb(255, 99, 132)', 'rgb(255, 159, 64)', 'rgb(255, 205, 86)', 'rgb(75, 192, 192)', | ||
'rgb(54, 162, 235)', 'rgb(153, 102, 255)', 'rgb(201, 203, 207)', 'rgb(255, 99, 132)', | ||
'rgb(255, 159, 64)', 'rgb(255, 205, 86)', 'rgb(75, 192, 192)', 'rgb(54, 162, 235)' | ||
]; | ||
|
||
function MonthlyExpensePieChart() { | ||
|
||
const [expenseData, setExpenseData] = useState<number[]>([]); | ||
const year: string = String(new Date().getFullYear()); | ||
const [chartYear, setChartYear] = useState<string>(localStorage.getItem('year')!); | ||
|
||
var yearList: string[] = []; | ||
|
||
const [isLoading, setIsLoading] = useState<boolean>(true); | ||
|
||
const currentYear = parseInt(year); | ||
for (let year = currentYear; year >= 2019; year--) { | ||
yearList.push(String(year)); | ||
} | ||
// console.log(expenseData) | ||
|
||
const data = { | ||
labels: monthList, | ||
datasets: [{ | ||
label: 'Expense', | ||
data: expenseData, | ||
backgroundColor: backgroundColors, | ||
hoverOffset: 4 | ||
}] | ||
}; | ||
|
||
console.log(data); | ||
|
||
const selectEventHandler = (event: SelectChangeEvent<string>) => { | ||
setChartYear(event.target.value); | ||
setIsLoading(true); | ||
}; | ||
|
||
useEffect(() => { | ||
async function fetchYearAnalytics() { | ||
// console.log(isLoading) | ||
|
||
console.log('fetching category pie chart data'); | ||
|
||
const response = await fetch( | ||
`${process.env.REACT_APP_SERVER_URL}/profile/pie-chart/month/${localStorage.getItem('userId')}?year=${chartYear}`, { | ||
headers: { | ||
'x-access-token': `${localStorage.getItem('token')}` | ||
} | ||
}); | ||
const { data } = await response.json(); | ||
console.log(data); | ||
setIsLoading(false); | ||
return { data }; | ||
} | ||
|
||
|
||
fetchYearAnalytics() | ||
.then((data) => { | ||
|
||
// check if data.data array does not contain 0 values for all 12 months | ||
// if yes, then set expenseData to empty array | ||
// else set expenseData to data.data array | ||
console.log(data.data); | ||
const isDataEmpty = data.data.every((value: number) => value === 0); | ||
|
||
if (!isDataEmpty) { | ||
setExpenseData([...data.data]) | ||
} | ||
else { | ||
setExpenseData([]); | ||
} | ||
}) | ||
}, [chartYear]); | ||
|
||
console.log('For Monthly Expense Pie Chart', !isLoading && expenseData!.length > 0); | ||
|
||
return ( | ||
<div> | ||
<div className="chart-wrapper"> | ||
<div className="chart" style={{ width: '370px', margin: 'auto', marginTop: '20px' }}> | ||
<SelectBtn | ||
options={yearList} | ||
val={chartYear} | ||
selectEventHandler={selectEventHandler} | ||
style={{ backgroundColor: 'white', width: '100px', height: '30px', marginBottom: '10px' }} | ||
/> | ||
{isLoading && <ChartSpinner />} | ||
{!isLoading && expenseData!.length === 0 && <h4 className='mt-8 mb-4 font-medium text-lg'>You have no expenses for {chartYear}</h4>} | ||
|
||
{!isLoading && expenseData!.length > 0 && <Doughnut options={options} data={data} />} | ||
</div> | ||
<h4 className='mt-4 text-base font-bold px-4 py-2 rounded-md shadow-md w-fit mx-auto'>Monthly Based Expenses</h4> | ||
</div> | ||
</div> | ||
) | ||
} | ||
|
||
export default MonthlyExpensePieChart; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
5973856
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.
Successfully deployed to the following URLs:
expense-tracker-react – ./
expense-tracker-react-nine.vercel.app
expense-tracker-react-git-main-mayukh551.vercel.app
expense-tracker-react-mayukh551.vercel.app