generated from cepdnaclk/eYY-3yp-project-template
-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Sales chart added and made few updates
- Loading branch information
1 parent
92ea294
commit 70e83f0
Showing
9 changed files
with
175 additions
and
44 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
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 |
---|---|---|
@@ -1,11 +1,89 @@ | ||
import React from "react"; | ||
import React, { useState, useEffect, useRef } from 'react'; | ||
import axios from 'axios'; | ||
//import { Line } from 'react-chartjs-2'; | ||
import Chart from 'chart.js/auto'; | ||
|
||
function Sales(){ | ||
return( | ||
<div> | ||
<h6>Sales details here...</h6> | ||
</div> | ||
) | ||
} | ||
|
||
export default Sales; | ||
const Sales = () => { | ||
const [data, setData] = useState([]); | ||
const [labels, setLabels] = useState([]); | ||
const chartRef = useRef(null); | ||
|
||
useEffect(() => { | ||
const fetchData = async () => { | ||
try { | ||
const today = new Date(); | ||
const lastTenDays = Array.from({ length: 10 }, (_, i) => { | ||
const day = new Date(today); | ||
day.setDate(today.getDate() - i+1); | ||
return day.toISOString().split('T')[0]; | ||
}); | ||
|
||
const fetchDataPromises = lastTenDays.map(async (day) => { | ||
const response = await axios.get(`http://localhost:5555/bill/billCountPerDay/${day}`); | ||
return response.data.billCountSelectedDay; | ||
|
||
}); | ||
|
||
const fetchedData = await Promise.all(fetchDataPromises); | ||
|
||
setData(fetchedData.reverse()); | ||
setLabels(lastTenDays.reverse()); | ||
} catch (error) { | ||
console.error('Error fetching data:', error.message); | ||
} | ||
}; | ||
|
||
fetchData(); | ||
}, []); | ||
|
||
useEffect(() => { | ||
const renderChart = () => { | ||
if (chartRef.current) { | ||
const chartInstance = chartRef.current.chartInstance; | ||
if (chartInstance) { | ||
chartInstance.destroy(); | ||
} | ||
|
||
chartRef.current.chartInstance = new Chart(chartRef.current.getContext('2d'), { | ||
type: 'line', | ||
data: { | ||
labels: labels, | ||
datasets: [ | ||
{ | ||
label: 'Bill Issued Per Day (customer count)', | ||
data: data, | ||
fill: false, | ||
borderColor: 'rgba(75,192,192,1)', | ||
}, | ||
], | ||
}, | ||
options: { | ||
scales: { | ||
x: { | ||
type: 'category', | ||
labels: labels, | ||
}, | ||
y: { | ||
beginAtZero: true, | ||
min: 0, | ||
}, | ||
}, | ||
}, | ||
}); | ||
} | ||
}; | ||
|
||
renderChart(); | ||
}, [data, labels]); | ||
|
||
return ( | ||
<div> | ||
<h2>Sales Data Variation</h2><br/> | ||
<canvas ref={chartRef}></canvas> | ||
</div> | ||
); | ||
}; | ||
|
||
export default Sales; | ||
|
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
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