Saltar al contenido
Logic2BUI

Buscar en la documentación

Busca componentes y documentación

Nuevo
Menú

Gráfico

Gráficos componibles basados en Recharts y tematizados con tus tokens CSS.

Area Chart
Showing total visitors for the last 6 months

Los gráficos forman una capa fina y componible sobre Recharts. Tú aportas sus primitivas; logic2b añade ChartContainer, el tipo ChartConfig que vincula cada serie con una etiqueta y un color, y ChartTooltip y ChartLegend compatibles con el tema. Los colores proceden de los tokens --chart-1…5, por lo que se adaptan automáticamente a los modos claro y oscuro.

Instalación

npx logic2b@next add chart

¿Prefieres un gráfico ya preparado? Todos los ejemplos de esta página y de la galería de gráficos son elementos instalables del registro. Por ejemplo, npx logic2b@next add chart-area-01 copia el gráfico completo en components/charts/.

Uso

import { Area, AreaChart, CartesianGrid, XAxis } from "recharts"

import {
  ChartConfig,
  ChartContainer,
  ChartTooltip,
  ChartTooltipContent,
} from "@/components/ui/chart"

const chartData = [
  { month: "Enero", desktop: 186 },
  { month: "Febrero", desktop: 305 },
  { month: "Marzo", desktop: 237 },
]

const chartConfig = {
  desktop: { label: "Escritorio", color: "var(--chart-1)" },
} satisfies ChartConfig

export function Example() {
  return (
    <ChartContainer config={chartConfig} className="h-[240px] w-full">
      <AreaChart data={chartData}>
        <CartesianGrid vertical={false} />
        <XAxis dataKey="month" tickLine={false} axisLine={false} />
        <ChartTooltip content={<ChartTooltipContent />} />
        <Area dataKey="desktop" fill="var(--color-desktop)" stroke="var(--color-desktop)" />
      </AreaChart>
    </ChartContainer>
  )
}

Cada clave de chartConfig se convierte en una variable CSS --color-<clave> dentro del contenedor. Usa var(--color-desktop) en vez de fijar un color manualmente.

Ejemplos

Barras

Bar Chart
January - June 2024

Líneas con varias series

Line Chart - Multiple
January - June 2024

Área apilada

Area Chart - Stacked
Showing total visitors for the last 6 months

Radar

Radar Chart
January - June 2024

Radial

Radial Chart
January - June 2024

Anillo

Pie Chart - Donut
January - June 2024

Playground interactivo

Edita las propiedades del componente y revisa el JSX exacto antes de copiarlo. La vista previa se carga solo cuando esta sección entra en pantalla.

JSX generado
<ChartContainer
  className="h-64 w-full max-w-lg"
  config={{ "visitors": { "label": "Visitors", "color": "var(--chart-1)" } }}
  role="img"
  aria-label="Monthly visitors from January to June 2026"
>
  <BarChart
    accessibilityLayer
    data={[{ "month": "Jan", "visitors": 186 }, { "month": "Feb", "visitors": 305 }, { "month": "Mar", "visitors": 237 }, { "month": "Apr", "visitors": 273 }, { "month": "May", "visitors": 209 }, { "month": "Jun", "visitors": 314 }]}
    margin={{ "left": 8, "right": 8 }}
  >
    <CartesianGrid />
    <XAxis
      dataKey="month"
      tickMargin={10}
     />
    <Bar
      dataKey="visitors"
      fill="var(--color-visitors)"
      radius={6}
     />
  </BarChart>
</ChartContainer>

Referencia de API

Generada a partir de las exportaciones públicas de TypeScript en src/ui/chart.tsx. Las descripciones técnicas conservan el texto canónico del código fuente.

Ver JSON

ChartConfig

Tipo
type ChartConfig = { [k in string]: { label?: React.ReactNode icon?: React.ComponentType } & ( | { color?: string; theme?: never } | { color?: never; theme: Record<keyof typeof THEMES, string> } ) }

ChartContainer

Componente
Props: React.ComponentProps<"div"> & { config: ChartConfig children: React.ComponentProps< typeof RechartsPrimitive.ResponsiveContainer >["children"] }
PropiedadTipoObligatoriaValor predeterminadoDescripción
childrenReact.ComponentProps< typeof RechartsPrimitive.ResponsiveContainer >["children"]Sí——
configChartConfigSí——

ChartTooltip

Componente

Alias directo de RechartsPrimitive.Tooltip.

ChartTooltipContent

Componente
Props: React.ComponentProps<typeof RechartsPrimitive.Tooltip> & React.ComponentProps<"div"> & { hideLabel?: boolean hideIndicator?: boolean indicator?: "line" | "dot" | "dashed" nameKey?: string labelKey?: string } & Omit< RechartsPrimitive.DefaultTooltipContentProps< TooltipValueType, TooltipNameType >, "accessibilityLayer" >
PropiedadTipoObligatoriaValor predeterminadoDescripción
indicator"line" | "dot" | "dashed"No"dot"—
hideLabelbooleanNofalse—
hideIndicatorbooleanNofalse—
nameKeystringNo——
labelKeystringNo——

ChartLegend

Componente

Alias directo de RechartsPrimitive.Legend.

ChartLegendContent

Componente
Props: React.ComponentProps<"div"> & { hideIcon?: boolean nameKey?: string } & RechartsPrimitive.DefaultLegendContentProps
PropiedadTipoObligatoriaValor predeterminadoDescripción
hideIconbooleanNofalse—
verticalAlign"top" | "bottom" | "middle"No"bottom"Vertical alignment of the whole Legend container: - `bottom`: shows the Legend below chart, and chart height reduces automatically to make space for it. - `top`: shows the Legend above chart, and chart height reduces automatically. - `middle`: shows the Legend in the middle of chart, covering other content, and chart height remains unchanged. The exact behavior changes depending on `align` prop.
nameKeystringNo——

ChartStyle

Componente
Props: { id: string; config: ChartConfig }
PropiedadTipoObligatoriaValor predeterminadoDescripción
idstringSí——
configChartConfigSí——

Contrato de accesibilidad

Comportamiento legible por máquinas y responsabilidades de quien integra chart. Los detalles normativos conservan el texto canónico del registro.

Ver JSON
Soporte
Contrato de composición
Patrón
data visualization
Recharts

Semántica incorporada

  • Chart containers can expose a labelled figure while tooltip content is available to pointer and keyboard interactions supported by the composed chart.

Responsabilidades de integración

  • Provide a concise chart name, units and a textual or tabular equivalent for the underlying data.

Limitaciones conocidas

  • • Canvas/SVG marks alone are not a complete accessible data representation.