"use client"; import React, { useState } from "react"; import { Pie } from "react-chartjs-2"; import { Chart as ChartJS, ArcElement, Tooltip, Legend } from "chart.js"; import ChartDataLabels from "chartjs-plugin-datalabels"; ChartJS.register(ArcElement, Tooltip, Legend, ChartDataLabels); interface ApplicationChartProps { data: { nsapp: string }[]; } const ApplicationChart: React.FC = ({ data }) => { const [chartStartIndex, setChartStartIndex] = useState(0); const appCounts: Record = {}; data.forEach((item) => { appCounts[item.nsapp] = (appCounts[item.nsapp] || 0) + 1; }); const sortedApps = Object.entries(appCounts).sort(([, a], [, b]) => b - a); const chartApps = sortedApps.slice(chartStartIndex, chartStartIndex + 20); const chartData = { labels: chartApps.map(([name]) => name), datasets: [ { label: "Applications", data: chartApps.map(([, count]) => count), backgroundColor: [ "#ff6384", "#36a2eb", "#ffce56", "#4bc0c0", "#9966ff", "#ff9f40", ], }, ], }; return (
{ return context.chart.data.labels?.[context.dataIndex] || ""; }, }, }, }} />
); }; export default ApplicationChart;