import React, { useState, useEffect } from "react"; import "./PozoPublicPiechart.scss" // Helper function to calculate the arcs for the donut chart const calculateArcs = (data) => { const total = data.reduce((acc, item) => acc + item.value, 0); let cumulativePercentage = 0; return data.map((item) => { const percentage = (item.value / total) * 100; const startAngle = cumulativePercentage; const endAngle = cumulativePercentage + percentage; cumulativePercentage = endAngle; const largeArcFlag = percentage > 50 ? 1 : 0; const outerRadius = 1; // Outer radius for the arc const innerRadius = 0.7; // Inner radius for the hollow part // Calculate the outer arc points const startXOuter = outerRadius * Math.cos((2 * Math.PI * startAngle) / 100); const startYOuter = outerRadius * Math.sin((2 * Math.PI * startAngle) / 100); const endXOuter = outerRadius * Math.cos((2 * Math.PI * endAngle) / 100); const endYOuter = outerRadius * Math.sin((2 * Math.PI * endAngle) / 100); // Calculate the inner arc points const startXInner = innerRadius * Math.cos((2 * Math.PI * startAngle) / 100); const startYInner = innerRadius * Math.sin((2 * Math.PI * startAngle) / 100); const endXInner = innerRadius * Math.cos((2 * Math.PI * endAngle) / 100); const endYInner = innerRadius * Math.sin((2 * Math.PI * endAngle) / 100); // Create the path for the arc (outer and inner) const pathData = [ `M ${startXOuter} ${startYOuter}`, // Move to start of outer arc `A ${outerRadius} ${outerRadius} 0 ${largeArcFlag} 1 ${endXOuter} ${endYOuter}`, // Outer arc `L ${endXInner} ${endYInner}`, // Line to inner arc `A ${innerRadius} ${innerRadius} 0 ${largeArcFlag} 0 ${startXInner} ${startYInner}`, // Inner arc `Z`, // Close the path ].join(" "); return { pathData, color: item.color, value: item.value, percentage: percentage.toFixed(2), name: item.name, date: item.date, // Add date to return object }; }); }; const PieChart = ({ data }) => { const [tooltip, setTooltip] = useState({ x: 0, y: 0, content: '', visible: false }); const [filter, setFilter] = useState(""); // State to hold the filter criteria const [dateFilter, setDateFilter] = useState(""); // State to hold the date filter const [animatedValues, setAnimatedValues] = useState(data.map(item => ({ ...item, animatedValue: 0 }))); // State for animated values // Function to filter data const filteredData = animatedValues.filter(item => item.name.toLowerCase().includes(filter.toLowerCase()) && (!dateFilter || item.date.includes(dateFilter)) // Filter by date ); const handleMouseOver = (event, arc) => { setTooltip({ x: event.clientX + 10, y: event.clientY - 20, content: `${arc.name}: ₹${arc.value} (${arc.percentage}%) on ${arc.date}`, visible: true, }); }; const handleMouseOut = () => { setTooltip((prev) => ({ ...prev, visible: false })); }; const arcs = calculateArcs(filteredData); console.log(arcs, "yusgd"); // Animate values from 0 to actual value useEffect(() => { const animationDuration = 2000; // Animation duration in ms const interval = 50; // Interval for the animation const step = Math.floor(animationDuration / interval); console.log("Animated Values:", animatedValues); const newValues = animatedValues.map(item => ({ ...item, animatedValue: 0, // Start from 0 })); let animationFrame = 0; const intervalId = setInterval(() => { if (animationFrame < step) { setAnimatedValues(newValues.map(item => ({ ...item, animatedValue: Math.round((item.value / step) * (animationFrame + 0)), // Increment the value }))); animationFrame++; } else { clearInterval(intervalId); // Stop the animation when complete setAnimatedValues(newValues.map(item => ({ ...item, animatedValue: item.value }))); // Set to final value } }, interval); return () => clearInterval(intervalId); // Cleanup interval on unmount }, []); return (
{/* setFilter(e.target.value)} style={{ marginBottom: '1rem', padding: '8px', borderRadius: '4px', border: '1px solid #ccc' }} /> setDateFilter(e.target.value)} style={{ marginBottom: '1rem', padding: '8px', borderRadius: '4px', border: '1px solid #ccc' }} /> */} {arcs.map((arc, index) => ( handleMouseOver(e, arc)} onMouseOut={handleMouseOut} /> ))}
{arcs.map((arc, index) => (

{arc.name}

₹{animatedValues[index]?.animatedValue}

({arc.percentage}%)

))}
{tooltip.visible && (
{tooltip.content}
)}
); }; export default PieChart;