Просмотр исходного кода

feat(ui): add a bars view to the WAN traffic chart

Closes #1975

Adds a Line/Bars switch to the WAN traffic chart, persisted in localStorage. Line stays the default. No API or server setting.
Nacho 3 недель назад
Родитель
Сommit
71e75e71fb

+ 3 - 0
app/public/locales/en/translation.json

@@ -659,6 +659,9 @@
       "title": "WAN traffic",
       "total24h": "Total 24h",
       "uploadMbps": "Upload (Mbps)",
+      "viewBars": "Bars",
+      "viewLine": "Line",
+      "viewAria": "Traffic chart view",
       "contracted": "Contracted: ↓ {{down}} / ↑ {{up}} Mbps",
       "contractedLine": "↓ {{mbps}} Mbps",
       "utilization": "Today's peak: {{pct}} % of contracted"

+ 3 - 0
app/public/locales/es/translation.json

@@ -659,6 +659,9 @@
       "title": "Tráfico WAN",
       "total24h": "Total 24h",
       "uploadMbps": "Subida (Mbps)",
+      "viewBars": "Barras",
+      "viewLine": "Línea",
+      "viewAria": "Vista del gráfico de tráfico",
       "contracted": "Contratado: ↓ {{down}} / ↑ {{up}} Mbps",
       "contractedLine": "↓ {{mbps}} Mbps",
       "utilization": "Pico hoy: {{pct}} % del contratado"

+ 60 - 26
app/src/sections/WanTraffic.tsx

@@ -1,5 +1,5 @@
 import { useEffect, useMemo, useState } from 'react'
-import { Area, AreaChart, CartesianGrid, ReferenceLine, ResponsiveContainer, Tooltip, XAxis } from 'recharts'
+import { Area, Bar, CartesianGrid, ComposedChart, ReferenceLine, ResponsiveContainer, Tooltip, XAxis } from 'recharts'
 import { motion } from 'framer-motion'
 import { useTranslation } from 'react-i18next'
 import type { TimeRange, TrafficPoint } from '@/data/mock'
@@ -57,6 +57,21 @@ export function WanTraffic() {
   const { t } = useTranslation()
   const { traffic: trafficByRange, wan, isDemo } = useNetPulse()
   const [range, setRange] = useState<TimeRange>('24h')
+  const [view, setView] = useState<'line' | 'bars'>(() => {
+    try {
+      return localStorage.getItem('netpulse.trafficView') === 'bars' ? 'bars' : 'line'
+    } catch {
+      return 'line'
+    }
+  })
+  const setTrafficView = (v: 'line' | 'bars') => {
+    setView(v)
+    try {
+      localStorage.setItem('netpulse.trafficView', v)
+    } catch {
+      // sin almacenamiento: la elección vive solo en esta sesión
+    }
+  }
   const [liveData, setLiveData] = useState<TrafficPoint[]>(trafficByRange['24h'])
 
   const FOOTER_METRICS = [
@@ -124,7 +139,17 @@ export function WanTraffic() {
             />
           ) : null}
         </SectionHeader>
-        <div className="shrink-0">
+        <div className="flex shrink-0 items-center gap-2">
+          <SegmentedControl
+            options={[
+              { value: 'line', label: t('home.traffic.viewLine') },
+              { value: 'bars', label: t('home.traffic.viewBars') },
+            ]}
+            value={view}
+            onChange={(v) => setTrafficView(v as 'line' | 'bars')}
+            size="sm"
+            ariaLabel={t('home.traffic.viewAria')}
+          />
           <SegmentedControl
             options={TIME_RANGE_OPTIONS}
             value={range}
@@ -144,7 +169,7 @@ export function WanTraffic() {
         aria-label={t('home.traffic.chartAria', { range, down: wan.downMbps, up: wan.upMbps })}
       >
         <ResponsiveContainer width="100%" height="100%">
-          <AreaChart data={liveData} margin={{ top: 8, right: 4, bottom: 0, left: 4 }}>
+          <ComposedChart data={liveData} margin={{ top: 8, right: 4, bottom: 0, left: 4 }}>
             <defs>
               <linearGradient id="grad-down" x1="0" y1="0" x2="0" y2="1">
                 <stop offset="0%" stopColor="#22D3EE" stopOpacity={0.25} />
@@ -180,29 +205,38 @@ export function WanTraffic() {
                 }}
               />
             ) : null}
-            <Area
-              type="stepAfter"
-              dataKey="down"
-              stroke="#22D3EE"
-              strokeWidth={2}
-              fill="url(#grad-down)"
-              dot={liveDotDown}
-              activeDot={{ r: 4, strokeWidth: 0, fill: '#22D3EE' }}
-              animationDuration={900}
-              animationEasing="ease-out"
-            />
-            <Area
-              type="stepAfter"
-              dataKey="up"
-              stroke="#A78BFA"
-              strokeWidth={2}
-              fill="url(#grad-up)"
-              dot={liveDotUp}
-              activeDot={{ r: 4, strokeWidth: 0, fill: '#A78BFA' }}
-              animationDuration={900}
-              animationEasing="ease-out"
-            />
-          </AreaChart>
+            {view === 'bars' ? (
+              <>
+                <Bar dataKey="down" name="down" fill="#22D3EE" fillOpacity={0.6} />
+                <Bar dataKey="up" name="up" fill="#A78BFA" fillOpacity={0.6} />
+              </>
+            ) : (
+              <>
+                <Area
+                  type="stepAfter"
+                  dataKey="down"
+                  stroke="#22D3EE"
+                  strokeWidth={2}
+                  fill="url(#grad-down)"
+                  dot={liveDotDown}
+                  activeDot={{ r: 4, strokeWidth: 0, fill: '#22D3EE' }}
+                  animationDuration={900}
+                  animationEasing="ease-out"
+                />
+                <Area
+                  type="stepAfter"
+                  dataKey="up"
+                  stroke="#A78BFA"
+                  strokeWidth={2}
+                  fill="url(#grad-up)"
+                  dot={liveDotUp}
+                  activeDot={{ r: 4, strokeWidth: 0, fill: '#A78BFA' }}
+                  animationDuration={900}
+                  animationEasing="ease-out"
+                />
+              </>
+            )}
+          </ComposedChart>
         </ResponsiveContainer>
       </motion.div>