Greg Claude Opus 4.7 (1M context) commited on
Commit
a01bd6f
·
1 Parent(s): 8e57577

feat(dashboard-ui): switch chart to pie + single-select section filter

Browse files

Replaces the multi-select sections control with a single dropdown
("Todas las secciones" + each section). Replaces the horizontal grouped
bar chart with a donut PieChart showing Masculino / Femenino (and
Sin especificar when present) for the chosen scope.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>

frontend/src/components/SexBySectionChart.tsx CHANGED
@@ -1,5 +1,5 @@
1
  import { useEffect, useMemo, useState } from 'react';
2
- import { BarChart, Bar, XAxis, YAxis, Tooltip, ResponsiveContainer, Legend, LabelList } from 'recharts';
3
  import DateInput from './DateInput';
4
  import { getSexBySection, SexBySection } from '../services/dashboardService';
5
  import { listSections } from '../services/sectionService';
@@ -8,6 +8,12 @@ import { useAuthStore } from '../store/authStore';
8
 
9
  interface SectionOpt { id: number; name: string }
10
 
 
 
 
 
 
 
11
  export default function SexBySectionChart() {
12
  const user = useAuthStore((s) => s.user);
13
  const isTeacher = user?.role === 'teacher';
@@ -16,7 +22,7 @@ export default function SexBySectionChart() {
16
  const [dateFrom, setDateFrom] = useState(today);
17
  const [dateTo, setDateTo] = useState(today);
18
  const [sections, setSections] = useState<SectionOpt[]>([]);
19
- const [selectedSectionIds, setSelectedSectionIds] = useState<number[]>([]);
20
  const [data, setData] = useState<SexBySection | null>(null);
21
  const [loading, setLoading] = useState(false);
22
  const [error, setError] = useState<string | null>(null);
@@ -38,14 +44,15 @@ export default function SexBySectionChart() {
38
  loadSections();
39
  }, [isTeacher]);
40
 
41
- const fetchData = async () => {
 
42
  setLoading(true);
43
  setError(null);
44
  try {
45
  const res = await getSexBySection({
46
  dateFrom,
47
  dateTo,
48
- sectionIds: selectedSectionIds.length ? selectedSectionIds : undefined,
49
  });
50
  setData(res);
51
  } catch (e: any) {
@@ -58,8 +65,7 @@ export default function SexBySectionChart() {
58
  const handleClear = async () => {
59
  setDateFrom(today);
60
  setDateTo(today);
61
- setSelectedSectionIds([]);
62
- // Refetch with cleared params — don't rely on state being updated yet
63
  setLoading(true);
64
  setError(null);
65
  try {
@@ -75,21 +81,38 @@ export default function SexBySectionChart() {
75
  // eslint-disable-next-line react-hooks/exhaustive-deps
76
  useEffect(() => { fetchData(); }, []);
77
 
78
- const chartData = useMemo(() =>
79
- (data?.sections || []).map(s => ({
80
- name: s.section_name,
81
- Masculino: s.masculino,
82
- Femenino: s.femenino,
83
- ...(s.sin_especificar > 0 ? { 'Sin especificar': s.sin_especificar } : {}),
84
- })),
85
- [data]
86
- );
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
87
 
88
- const hasSinEspecificar = (data?.sections || []).some(s => s.sin_especificar > 0);
 
 
89
 
90
  return (
91
  <div className="bg-white rounded-lg shadow p-4">
92
- <h3 className="text-lg font-semibold mb-3">Asistencia por sección y sexo</h3>
93
 
94
  <div className="flex flex-wrap gap-3 items-end mb-4">
95
  <div>
@@ -101,20 +124,24 @@ export default function SexBySectionChart() {
101
  <DateInput value={dateTo} onChange={setDateTo} />
102
  </div>
103
  <div>
104
- <label className="block text-xs text-gray-600 mb-1">Secciones</label>
105
  <select
106
- multiple
107
- value={selectedSectionIds.map(String)}
108
- onChange={(e) =>
109
- setSelectedSectionIds(Array.from(e.target.selectedOptions, o => Number(o.value)))
110
- }
111
- className="border rounded px-2 py-1 min-w-[160px] h-20"
 
112
  >
113
- {sections.map(s => <option key={s.id} value={s.id}>{s.name}</option>)}
 
 
 
114
  </select>
115
  </div>
116
  <button
117
- onClick={fetchData}
118
  disabled={loading}
119
  className="bg-blue-600 text-white px-3 py-1 rounded"
120
  >
@@ -138,39 +165,38 @@ export default function SexBySectionChart() {
138
  <p className="text-gray-500 text-sm">Cargando…</p>
139
  ) : !data ? (
140
  <p className="text-gray-500 text-sm">Sin datos para mostrar.</p>
141
- ) : data.totals.total === 0 ? (
142
  <p className="text-gray-500 text-sm">No hay datos de asistencia en este rango.</p>
143
  ) : (
144
  <>
145
- <ResponsiveContainer width="100%" height={Math.max(160, (chartData.length || 1) * 40 + 60)}>
146
- <BarChart data={chartData} layout="vertical">
147
- <XAxis type="number" allowDecimals={false} />
148
- <YAxis dataKey="name" type="category" width={140} />
149
- <Tooltip />
 
 
 
 
 
 
 
 
 
 
 
 
150
  <Legend />
151
- <Bar dataKey="Masculino" fill="#2563eb">
152
- <LabelList dataKey="Masculino" position="right" />
153
- </Bar>
154
- <Bar dataKey="Femenino" fill="#db2777">
155
- <LabelList dataKey="Femenino" position="right" />
156
- </Bar>
157
- {hasSinEspecificar && (
158
- <Bar dataKey="Sin especificar" fill="#9ca3af">
159
- <LabelList dataKey="Sin especificar" position="right" />
160
- </Bar>
161
- )}
162
- </BarChart>
163
  </ResponsiveContainer>
164
- {data && (
165
- <p className="text-sm text-gray-700 mt-3">
166
- Total Masculino: <strong>{data.totals.masculino}</strong> ·{' '}
167
- Total Femenino: <strong>{data.totals.femenino}</strong> ·{' '}
168
- Total general: <strong>{data.totals.total}</strong>
169
- {data.totals.sin_especificar > 0 && (
170
- <> · Sin especificar: <strong>{data.totals.sin_especificar}</strong></>
171
- )}
172
- </p>
173
- )}
174
  </>
175
  )}
176
  </div>
 
1
  import { useEffect, useMemo, useState } from 'react';
2
+ import { PieChart, Pie, Cell, Tooltip, ResponsiveContainer, Legend } from 'recharts';
3
  import DateInput from './DateInput';
4
  import { getSexBySection, SexBySection } from '../services/dashboardService';
5
  import { listSections } from '../services/sectionService';
 
8
 
9
  interface SectionOpt { id: number; name: string }
10
 
11
+ const COLOR_M = '#2563eb';
12
+ const COLOR_F = '#db2777';
13
+ const COLOR_NONE = '#9ca3af';
14
+
15
+ const ALL = '';
16
+
17
  export default function SexBySectionChart() {
18
  const user = useAuthStore((s) => s.user);
19
  const isTeacher = user?.role === 'teacher';
 
22
  const [dateFrom, setDateFrom] = useState(today);
23
  const [dateTo, setDateTo] = useState(today);
24
  const [sections, setSections] = useState<SectionOpt[]>([]);
25
+ const [selectedSectionId, setSelectedSectionId] = useState<string>(ALL);
26
  const [data, setData] = useState<SexBySection | null>(null);
27
  const [loading, setLoading] = useState(false);
28
  const [error, setError] = useState<string | null>(null);
 
44
  loadSections();
45
  }, [isTeacher]);
46
 
47
+ const fetchData = async (overrideSectionId?: string) => {
48
+ const sid = overrideSectionId ?? selectedSectionId;
49
  setLoading(true);
50
  setError(null);
51
  try {
52
  const res = await getSexBySection({
53
  dateFrom,
54
  dateTo,
55
+ sectionIds: sid !== ALL ? [Number(sid)] : undefined,
56
  });
57
  setData(res);
58
  } catch (e: any) {
 
65
  const handleClear = async () => {
66
  setDateFrom(today);
67
  setDateTo(today);
68
+ setSelectedSectionId(ALL);
 
69
  setLoading(true);
70
  setError(null);
71
  try {
 
81
  // eslint-disable-next-line react-hooks/exhaustive-deps
82
  useEffect(() => { fetchData(); }, []);
83
 
84
+ // Pick the slice source: a single section row if selected, otherwise the aggregate totals.
85
+ const slice = useMemo(() => {
86
+ if (!data) return null;
87
+ if (selectedSectionId !== ALL) {
88
+ const row = data.sections.find(s => String(s.section_id) === selectedSectionId);
89
+ return row
90
+ ? { masculino: row.masculino, femenino: row.femenino,
91
+ sin_especificar: row.sin_especificar, total: row.total }
92
+ : { masculino: 0, femenino: 0, sin_especificar: 0, total: 0 };
93
+ }
94
+ return data.totals;
95
+ }, [data, selectedSectionId]);
96
+
97
+ const pieData = useMemo(() => {
98
+ if (!slice) return [];
99
+ const rows = [
100
+ { name: 'Masculino', value: slice.masculino, fill: COLOR_M },
101
+ { name: 'Femenino', value: slice.femenino, fill: COLOR_F },
102
+ ];
103
+ if (slice.sin_especificar > 0) {
104
+ rows.push({ name: 'Sin especificar', value: slice.sin_especificar, fill: COLOR_NONE });
105
+ }
106
+ return rows.filter(r => r.value > 0);
107
+ }, [slice]);
108
 
109
+ const headingScope = selectedSectionId === ALL
110
+ ? 'todas las secciones'
111
+ : (sections.find(s => String(s.id) === selectedSectionId)?.name ?? '');
112
 
113
  return (
114
  <div className="bg-white rounded-lg shadow p-4">
115
+ <h3 className="text-lg font-semibold mb-3">Asistencia por sexo — {headingScope}</h3>
116
 
117
  <div className="flex flex-wrap gap-3 items-end mb-4">
118
  <div>
 
124
  <DateInput value={dateTo} onChange={setDateTo} />
125
  </div>
126
  <div>
127
+ <label className="block text-xs text-gray-600 mb-1">Sección</label>
128
  <select
129
+ value={selectedSectionId}
130
+ onChange={(e) => {
131
+ const next = e.target.value;
132
+ setSelectedSectionId(next);
133
+ fetchData(next);
134
+ }}
135
+ className="border rounded px-2 py-1 min-w-[180px]"
136
  >
137
+ <option value={ALL}>Todas las secciones</option>
138
+ {sections.map(s => (
139
+ <option key={s.id} value={String(s.id)}>{s.name}</option>
140
+ ))}
141
  </select>
142
  </div>
143
  <button
144
+ onClick={() => fetchData()}
145
  disabled={loading}
146
  className="bg-blue-600 text-white px-3 py-1 rounded"
147
  >
 
165
  <p className="text-gray-500 text-sm">Cargando…</p>
166
  ) : !data ? (
167
  <p className="text-gray-500 text-sm">Sin datos para mostrar.</p>
168
+ ) : !slice || slice.total === 0 ? (
169
  <p className="text-gray-500 text-sm">No hay datos de asistencia en este rango.</p>
170
  ) : (
171
  <>
172
+ <ResponsiveContainer width="100%" height={280}>
173
+ <PieChart>
174
+ <Pie
175
+ data={pieData}
176
+ dataKey="value"
177
+ nameKey="name"
178
+ cx="50%"
179
+ cy="50%"
180
+ outerRadius={100}
181
+ innerRadius={55}
182
+ label={(entry: any) => `${entry.name}: ${entry.value}`}
183
+ >
184
+ {pieData.map((entry, i) => (
185
+ <Cell key={i} fill={entry.fill} />
186
+ ))}
187
+ </Pie>
188
+ <Tooltip formatter={(v: number) => `${v}`} />
189
  <Legend />
190
+ </PieChart>
 
 
 
 
 
 
 
 
 
 
 
191
  </ResponsiveContainer>
192
+ <p className="text-sm text-gray-700 mt-3 text-center">
193
+ Total Masculino: <strong>{slice.masculino}</strong> ·{' '}
194
+ Total Femenino: <strong>{slice.femenino}</strong> ·{' '}
195
+ Total general: <strong>{slice.total}</strong>
196
+ {slice.sin_especificar > 0 && (
197
+ <> · Sin especificar: <strong>{slice.sin_especificar}</strong></>
198
+ )}
199
+ </p>
 
 
200
  </>
201
  )}
202
  </div>