UIPackage
Menu

Framework

Change language

Boilerplate repo

Area Chart

area-chartui

React mirror of @uipkge/area-chart — see the Vue registry item for the canonical description.

Also available for Vue ->

Installation

$npx shadcn@latest add https://uipkge.dev/r/react/area-chart.json
Named registry:npx shadcn@latest add @uipkge-react/area-chartInstalls to:components/ui/charts/area-chart/

Examples

Loading interactive previews…

Props

NameType / ValuesDefaultRequired
dataRecord<string, any>[]required
xFieldstringoptional
yFieldstring | string[]optional
curve

Line interpolation. Default 'smooth'.

'smooth' | 'linear' | 'step' | 'stepStart' | 'stepEnd'optional
stacked

Stack series cumulatively. Default false.

booleanoptional
markers

Show point markers. Default false.

booleanoptional
dashed

Dashed stroke on all series (forecast look). Default false.

booleanoptional
heightnumber | stringoptional
optionanyoptional
classNamestringoptional
ariaLabel

Accessible name announced for the chart image. Defaults to "Chart".

stringoptional

Schema

Type aliases from this item's source — use them to shape the data you pass in.

ChartTheme
interface ChartTheme {
  colors: string[]
  textColor: string
  axisColor: string
  splitLineColor: string
  tooltipBg: string
  tooltipBorder: string
  tooltipText: string
  bgColor: string
  /** The app's accent, for the one highlighted element on a chart or map —
   *  a selected route, a focused bar. Category series keep using `colors`. */
  accentColor: string
  /** Out-of-bounds / failure colour for charts that encode good vs bad
   *  rather than a category — control limits, error series. CSS-level marks
   *  can use `var(--destructive)` directly; this exists because ECharts needs
   *  a resolved colour string on the canvas. */
  dangerColor: string
}

npm dependencies

Used by

Files installed (4)

  • components/ui/charts/area-chart/AreaChart.tsx4.2 kB
    'use client'
    
    import * as React from 'react'
    import ReactECharts from 'echarts-for-react/esm/core'
    import { ChartFrame, EChart } from '../shared'
    import { useChartTheme, mergeOptionBlock } from '../useChartTheme'
    
    // AreaChart
    // ─────────────────────────────────────────────────────────────────────────
    
    export interface AreaChartProps {
      data: Record<string, any>[]
      xField?: string
      yField?: string | string[]
      /** Line interpolation. Default 'smooth'. */
      curve?: 'smooth' | 'linear' | 'step' | 'stepStart' | 'stepEnd'
      /** Stack series cumulatively. Default false. */
      stacked?: boolean
      /** Show point markers. Default false. */
      markers?: boolean
      /** Dashed stroke on all series (forecast look). Default false. */
      dashed?: boolean
      height?: number | string
      option?: any
      className?: string
      /** Accessible name announced for the chart image. Defaults to "Chart". */
      ariaLabel?: string
    }
    
    export const AreaChart = React.forwardRef<HTMLDivElement, AreaChartProps>(
      (
        {
          data,
          xField = 'x',
          yField = 'y',
          curve = 'smooth',
          stacked = false,
          markers = false,
          dashed = false,
          height = 300,
          option,
          className,
          ariaLabel,
        },
        ref,
      ) => {
        const theme = useChartTheme()
    
        const mergedOption = React.useMemo(() => {
          const fields = Array.isArray(yField) ? yField : [yField]
          const xData = data.map((d) => d[xField])
    
          const series = fields.map((field, i) => ({
            name: field,
            type: 'line',
            smooth: curve === 'smooth',
            step: curve === 'step' ? 'middle' : curve === 'stepStart' ? 'start' : curve === 'stepEnd' ? 'end' : false,
            stack: stacked ? 'areas' : undefined,
            symbol: markers ? 'circle' : 'none',
            symbolSize: 6,
            areaStyle: { opacity: stacked ? 0.5 : 0.15 },
            lineStyle: { width: 2, type: dashed ? 'dashed' : 'solid' },
            itemStyle: { color: theme.colors[i % theme.colors.length] },
            data: data.map((d) => d[field]),
          }))
    
          const userOption: any = option ?? {}
          const {
            series: userSeries,
            xAxis: userXAxis,
            yAxis: userYAxis,
            grid: userGrid,
            tooltip: userTooltip,
            legend: userLegend,
            ...userRest
          } = userOption
          const mergedSeries = Array.isArray(userSeries)
            ? series.map((s, i) => ({ ...s, ...(userSeries[i] ?? {}) }))
            : series
    
          const baseLegend: any =
            fields.length > 1
              ? {
                  bottom: 0,
                  icon: 'circle',
                  itemWidth: 8,
                  itemHeight: 8,
                  textStyle: { fontSize: 11, color: theme.textColor },
                }
              : { show: false }
    
          return {
            color: theme.colors,
            grid: mergeOptionBlock(
              { left: 16, right: 16, top: 24, bottom: fields.length > 1 ? 32 : 24, containLabel: true },
              userGrid,
            ),
            tooltip: mergeOptionBlock(
              {
                trigger: 'axis',
                backgroundColor: theme.tooltipBg,
                borderColor: theme.tooltipBorder,
                textStyle: { color: theme.tooltipText, fontSize: 12 },
              },
              userTooltip,
            ),
            legend: userLegend?.show === false ? undefined : mergeOptionBlock(baseLegend, userLegend),
            xAxis: mergeOptionBlock(
              {
                type: 'category',
                data: xData,
                axisLine: { lineStyle: { color: theme.axisColor } },
                axisLabel: { color: theme.textColor, fontSize: 11 },
                axisTick: { show: false },
              },
              userXAxis,
            ),
            yAxis: mergeOptionBlock(
              {
                type: 'value',
                splitLine: { lineStyle: { color: theme.splitLineColor } },
                axisLabel: { color: theme.textColor, fontSize: 11 },
                axisLine: { show: false },
                axisTick: { show: false },
              },
              userYAxis,
            ),
            series: mergedSeries,
            ...userRest,
          }
        }, [data, xField, yField, curve, stacked, markers, dashed, option, theme])
    
        return (
          <ChartFrame ref={ref} height={height} className={className} ariaLabel={ariaLabel}>
            <EChart option={mergedOption} />
          </ChartFrame>
        )
      },
    )
    AreaChart.displayName = 'AreaChart'
    
  • components/ui/charts/area-chart/index.ts0.1 kB
  • components/ui/charts/useChartTheme.ts8.7 kB
  • components/ui/charts/shared.tsx3.7 kB

Raw manifest:https://uipkge.dev/r/react/area-chart.json