This commit is contained in:
zadit
2024-11-24 13:21:42 +07:00
parent 89b12737b7
commit 529a7e505c
7 changed files with 700 additions and 395 deletions

View File

@@ -98,32 +98,46 @@ const SimpleBarChart = ({ Data }) => {
const month = formattedDate.toLocaleString('en-US', { month: 'short' }); // Get the abbreviated month (e.g., "Nov")
const year = formattedDate.getFullYear().toString().slice(2); // Get the last two digits of the year (e.g., "24")
// Extract the month and year from the date
const currentMonth = formattedDate.toLocaleDateString('en-US', { month: 'short' });
const currentYear = formattedDate.getFullYear();
// Extract the year from the first data point
const firstYear = new Date(sortedData[0]?.date).getFullYear();
// Check if all dates have the same year
const allSameYear = sortedData.every((item) => new Date(item.date).getFullYear() === firstYear);
// If all dates are from the same year, show only "day month"
if (allSameYear && formattedDate.toLocaleString('en-US', { month: 'short' }) !== lastMonthRef.current) {
lastMonthRef.current = formattedDate.toLocaleString('en-US', { month: 'short' });
// Show just the day and month if all dates have the same year
return `${day} ${month}`;
}
// if (allSameYear) {
// // Show just the day and month if all dates have the same year
// return `${day} ${month}`;
// }
// Check if it's the oldest date (first entry)
if (date === oldestDate) {
lastMonthRef.current = currentMonth; // Update lastMonthRef for the first entry
lastYearRef.current = currentYear; // Update lastYearRef for the first entry
lastMonthRef.current = formattedDate.toLocaleDateString('en-US', { month: 'short' });
lastYearRef.current = formattedDate.getFullYear();
return `${day} ${month} ${year}`; // Format as "day month year" (e.g., "4 Nov 24")
}
// If the year changes, show the full date with year
if (currentYear !== lastYearRef.current) {
lastYearRef.current = currentYear; // Update year reference
if (formattedDate.getFullYear() !== lastYearRef.current) {
lastYearRef.current = formattedDate.getFullYear();
return `${day} ${month} ${year}`; // Show full date: day month year
}
// If the month changes, show the full date with month and year
if (currentMonth !== lastMonthRef.current) {
lastMonthRef.current = currentMonth; // Update month reference
return `${day} ${month} ${year}`; // Show full date: day month year
if (formattedDate.toLocaleString('en-US', { month: 'short' }) !== lastMonthRef.current) {
lastMonthRef.current = formattedDate.toLocaleString('en-US', { month: 'short' });
return `${day} ${month} `; // Show full date: day month year
}
// Only show the day if the month hasn't changed
return `${day}`; // Show just the day if the month remains the same
};
// Function to get the next available color from the palette, starting from a random index
const getNextColor = () => {
@@ -158,8 +172,11 @@ const SimpleBarChart = ({ Data }) => {
interval={0} // Set interval to 0 to display all dates in data
ticks={uniqueDates} // Only use unique dates for the XAxis
/>
<Tooltip />
<Legend />
<Tooltip
itemStyle={{
color: '#3498db', // Set the tooltip text color to blue
}}
/>
<CartesianGrid strokeDasharray="3 3" />
{/* Dynamically create bars and labels for each unique material */}
@@ -168,7 +185,12 @@ const SimpleBarChart = ({ Data }) => {
<Bar dataKey={material} fill={getNextColor()}>
</Bar>
</React.Fragment>
))}
))}<Legend
wrapperStyle={{
color: '#3498db', // Set the legend text color to blue
backgroundColor: 'white'
}}
/>
</BarChart>
</ResponsiveContainer>
</div>