React wrapper component for ChartML
npm install @chartml/reactReact wrapper component for ChartML - render beautiful data visualizations in React with ease.
- ✅ React Component - Clean, idiomatic React API
- ✅ useChartML Hook - Manage ChartML instances with React hooks
- ✅ Auto-Cleanup - Automatic cleanup on unmount
- ✅ Plugin Support - Register custom chart renderers and data sources
- ✅ Event Callbacks - Progress, cache hit/miss, and error events
- ✅ TypeScript Ready - Includes TypeScript definitions
- ✅ All Chart Types - Includes pie, scatter, and metric chart plugins
- ✅ Zero Config - Works out of the box with sensible defaults
``bash`
npm install @chartml/react
Peer Dependencies:
- react ^18.0.0react-dom
- ^18.0.0
The core ChartML library is included automatically.
`jsx
import { ChartMLChart } from '@chartml/react';
function SalesChart() {
const spec =
type: chart
version: 1
data:
- month: "Jan"
revenue: 45000
- month: "Feb"
revenue: 52000
- month: "Mar"
revenue: 48000
visualize:
type: bar
columns: month
rows: revenue
style:
title: "Monthly Revenue"
;
return
}
`
`jsx`
className="my-chart"
style={{ maxWidth: '800px', margin: '0 auto' }}
/>
`jsx
import { ChartMLChart } from '@chartml/react';
import { useState } from 'react';
function ChartWithStatus() {
const [status, setStatus] = useState('idle');
return (
Status: {status}
),
onCacheHit: () => setStatus('Loaded from cache'),
onCacheMiss: () => setStatus('Fetching data'),
onError: (err) => setStatus(Error: ${err.message})
}}
/>
Advanced Usage
$3
For more control, use the
useChartML hook to manage your own ChartML instance:`jsx
import { ChartMLChart, useChartML } from '@chartml/react';
import { useEffect } from 'react';function CustomChart() {
const chartml = useChartML({
onProgress: (e) => console.log(
Progress: ${e.percent}%),
palettes: {
custom: ['#ff0000', '#00ff00', '#0000ff']
}
}); // Register custom renderers if needed
useEffect(() => {
// Custom plugins would be registered here
// chartml.registerChartRenderer('custom', myRenderer);
}, [chartml]);
return ;
}
`$3
`jsx
import { ChartMLChart } from '@chartml/react';
import { useState } from 'react';function DynamicChart() {
const [chartType, setChartType] = useState('bar');
const spec =
data:
- month: "Jan"
revenue: 45000
- month: "Feb"
revenue: 52000
visualize:
type: ${chartType}
columns: month
rows: revenue
;
return (
$3
`jsx
import { ChartMLChart } from '@chartml/react';
import { useState, useEffect } from 'react';function APIChart() {
const [chartData, setChartData] = useState(null);
useEffect(() => {
fetch('/api/sales-data')
.then(res => res.json())
.then(data => {
const spec = {
type: 'chart',
version: 1,
data: data,
visualize: {
type: 'bar',
columns: 'month',
rows: 'revenue'
}
};
setChartData(spec);
});
}, []);
if (!chartData) return
Loading...; return ;
}
`All Chart Types Available
This package includes all ChartML chart plugins by default:
$3
- bar - Vertical and horizontal bar charts
- line - Single and multi-series line charts
- area - Stacked and normalized area charts$3
- pie - Pie charts (from @chartml/chart-pie)
- doughnut - Doughnut charts (from @chartml/chart-pie)
- scatter - Scatter plots and bubble charts (from @chartml/chart-scatter)
- metric - Metric cards for KPIs (from @chartml/chart-metric)All types work out of the box - no additional imports needed!
API Reference
$3
Props:
| Prop | Type | Description |
|------|------|-------------|
|
spec | string \| object | ChartML specification (YAML string or object) |
| chartml | ChartML | Optional ChartML instance (for custom plugins) |
| options | object | Options for ChartML instance |
| options.onProgress | function | Progress callback: (event) => void |
| options.onCacheHit | function | Cache hit callback: (event) => void |
| options.onCacheMiss | function | Cache miss callback: (event) => void |
| options.onError | function | Error callback: (error) => void |
| options.palettes | object | Custom color palettes |
| className | string | CSS class for container |
| style | object | Inline styles for container |Example:
`jsx
spec={mySpec}
className="chart-container"
style={{ maxWidth: '100%' }}
options={{
onProgress: (e) => console.log(e.percent),
onError: (err) => console.error(err)
}}
/>
`$3
Creates and manages a ChartML instance with React hooks.
Parameters:
-
options (object, optional) - ChartML optionsReturns:
ChartML instanceExample:
`jsx
const chartml = useChartML({
onProgress: (e) => console.log(Loading: ${e.percent}%),
palettes: {
custom: ['#ff0000', '#00ff00', '#0000ff']
}
});
`Examples
$3
`jsx
const pieSpec = data:
- category: "Product A"
sales: 1200
- category: "Product B"
sales: 850
- category: "Product C"
sales: 1450
visualize:
type: pie
columns: category
rows: sales
style:
title: "Sales by Product";
`
`jsx
const scatterSpec =
type: chart
version: 1
data:
- temperature: 65
sales: 120
- temperature: 70
sales: 140
- temperature: 75
sales: 180
visualize:
type: scatter
columns: temperature
rows: sales
style:
title: "Sales vs Temperature";
`
`jsx
const metricSpec =
type: chart
version: 1
data:
- value: 1234
visualize:
type: metric
rows: value
style:
title: "Total Revenue"
format: "$,.0f";
`
Type definitions are included:
`tsx
import { ChartMLChart, useChartML } from '@chartml/react';
import type { ChartML } from '@chartml/core';
interface ChartProps {
data: any[];
}
function TypedChart({ data }: ChartProps) {
const chartml: ChartML = useChartML();
const spec = {
type: 'chart' as const,
version: 1,
data,
visualize: {
type: 'bar' as const,
columns: 'month',
rows: 'revenue'
}
};
return
}
`
If passing spec as an object (not string), memoize it to prevent unnecessary re-renders:
`jsx
import { useMemo } from 'react';
const spec = useMemo(() => ({
type: 'chart',
version: 1,
data: chartData,
visualize: { type: 'bar', columns: 'x', rows: 'y' }
}), [chartData]);
`
For static charts, YAML strings are more readable:
`jsx
const spec =
type: chart
version: 1
data: [...]
visualize: { type: bar };`
Always handle loading states for async data:
`jsx`
if (!data) return Loading chart...;
return
Wrap charts in error boundaries for production:
`jsx
- React 18+
- Modern browsers with SVG support
- IE11+ (with polyfills)
- ChartML Specification: https://chartml.org/spec
- Examples: https://chartml.org/examples
- API Reference: https://chartml.org/api
MIT © 2025 Alytic Pty Ltd
ChartML is maintained by the team at Kyomi and is the visualization engine that powers the platform.