@molecule/app-chart-wrapper-svelte

Feature · chart-wrapper · App (browser) · v1.1.0 · Apache-2.0

Svelte ChartCard and ChartLegend wrappers around @molecule/app-charts

npm install @molecule/app-chart-wrapper-svelte

npm · Source on GitHub

How it works

@molecule/app-chart-wrapper-svelte is a ready-made chart-wrapper feature for the app (browser) side. It composes the core interfaces it needs, so it works with whichever providers your app has bonded.

<script lang="ts">
  import { onMount } from 'svelte'

  import { ChartCard, ChartLegend } from '@molecule/app-chart-wrapper-svelte'
  import { createBarChart } from '@molecule/app-charts'

  let canvas: HTMLCanvasElement
  onMount(() => {
    const chart = createBarChart(canvas, {
      labels: ['Jan', 'Feb', 'Mar'],
      datasets: [{ label: 'Revenue', data: [4200, 5800, 5100] }],
    })
    return () => chart.destroy()
  })
</script>

<ChartCard title="Monthly Revenue" description="Last 12 months">
  {#snippet footer()}
    <ChartLegend
      items={[
        { id: 'revenue', label: 'Revenue', color: '#4f46e5' },
        { id: 'expenses', label: 'Expenses', color: '#e11d48' },
      ]}
    />
  {/snippet}
  <canvas bind:this={canvas} height={240}></canvas>
</ChartCard>

Works with: @molecule/app-svelte, @molecule/app-ui, @molecule/app-ui-svelte

Reference

Svelte ChartCard + ChartLegend wrappers around @molecule/app-charts.

Exports:

  • ChartCard — uniform chrome (title / description / actions / summary / body / footer) around a chart rendering. minChartHeight (default 240) stops responsive charts collapsing; dataMolId sets data-mol-id.
  • ChartLegend — swatch + label (+ value) legend. Items become toggle buttons when onToggle is provided.
  • ChartLegendItem type.

These wrappers provide ONLY the surrounding chrome. The chart itself is whatever you render as the default content — typically a canvas driven by createLineChart/createBarChart from @molecule/app-charts (via a real bonded ChartProvider) or your own chart component. There is no <BarChart> component in @molecule/app-charts.

Auto-generated, AI-first package reference for the molecule.dev ecosystem. It is written to be read by coding agents as much as by people, and is generated from this package's source — edit src/index.ts JSDoc, not this file.

Quick Start

<script lang="ts">
  import { onMount } from 'svelte'

  import { ChartCard, ChartLegend } from '@molecule/app-chart-wrapper-svelte'
  import { createBarChart } from '@molecule/app-charts'

  let canvas: HTMLCanvasElement
  onMount(() => {
    const chart = createBarChart(canvas, {
      labels: ['Jan', 'Feb', 'Mar'],
      datasets: [{ label: 'Revenue', data: [4200, 5800, 5100] }],
    })
    return () => chart.destroy()
  })
</script>

<ChartCard title="Monthly Revenue" description="Last 12 months">
  {#snippet footer()}
    <ChartLegend
      items={[
        { id: 'revenue', label: 'Revenue', color: '#4f46e5' },
        { id: 'expenses', label: 'Expenses', color: '#e11d48' },
      ]}
    />
  {/snippet}
  <canvas bind:this={canvas} height={240}></canvas>
</ChartCard>

Type

feature

Installation

npm install @molecule/app-chart-wrapper-svelte @molecule/app-svelte @molecule/app-ui @molecule/app-ui-svelte svelte

API

Interfaces

ChartCardProps

Props accepted by the ChartCard component.

interface ChartCardProps {
  /** Card heading: a snippet, or a plain string. Required by the component. */
  title: Snippet | string
  /** Optional supporting description: a snippet, or a plain string. */
  description?: Snippet | string
  /** Optional right-aligned actions (range selector, export menu, filter). */
  actions?: Snippet | string
  /** Optional KPI summary row shown between header and chart (e.g. "Total: 12,345 +12%"). */
  summary?: Snippet | string
  /** Chart content — any rendering driven by `@molecule/app-charts`. */
  children: Snippet | string
  /** Optional footer below the chart (legend, source attribution). */
  footer?: Snippet | string
  /** Set a minimum chart height so responsive charts don't collapse. Defaults to `240`. */
  minChartHeight?: number
  /** Extra classes on the Card. */
  className?: string
  /** `data-mol-id` for AI-agent selectors. */
  dataMolId?: string
}

ChartLegendItem

Describes a single series entry rendered in the chart legend.

interface ChartLegendItem {
  /** Series id — the value `onToggle` receives. */
  id: string
  /** Series label: a snippet, or plain text. */
  label: Snippet | string | number
  /** Swatch color (any valid CSS color or ClassMap token string). */
  color: string
  /** Optional value / count display next to the label. */
  value?: Snippet | string | number
  /** Whether the series is hidden (greyed out). */
  hidden?: boolean
}

ChartLegendProps

Props accepted by the ChartLegend component.

interface ChartLegendProps {
  /** The legend's series. */
  items: ChartLegendItem[]
  /** Called when an item is toggled. */
  onToggle?: (id: string) => void
  /** Layout direction. Defaults to `'horizontal'`. */
  layout?: 'horizontal' | 'vertical'
  /** Extra classes. */
  className?: string
}

Constants

ChartCard

const ChartCard: LegacyComponentType

ChartLegend

const ChartLegend: LegacyComponentType

Injection Notes

Requirements

Peer dependencies:

  • @molecule/app-svelte ^3.5.0
  • @molecule/app-ui ^1.0.1
  • @molecule/app-ui-svelte ^1.1.0
  • svelte ^5.0.0

Runtime Dependencies

  • @molecule/app-svelte
  • @molecule/app-ui
  • @molecule/app-ui-svelte
  • svelte

@molecule/app-charts' built-in provider renders a non-functional placeholder notice (and logs a one-time console warning), not a real chart. Bond the Chart.js provider once at startup — import { provider } from '@molecule/app-charts-chartjs'; setProvider(provider) — so createChart / createBarChart draw real charts (or render your own chart component as the default content). Legend items labels/values are consumer-provided nodes; pass translated strings via t(). Node-valued props (title, description, actions, summary, footer) are snippet props.