---
name: charts
description: MUI X Charts — BarChart, LineChart, PieChart, ScatterChart, composition API, custom axes, tooltips, and responsive patterns
triggers:
- chart
- BarChart
- LineChart
- PieChart
- ScatterChart
- MUI X Charts
- data visualization
allowed-tools:
- Read
- Glob
- Grep
- Write
- Edit
globs:
- "*.tsx"
- "*.ts"
- "*.jsx"
---
# MUI X Charts
## Package
```bash
npm install @mui/x-charts
# Free, MIT license — all chart types included
```
All charts are available in the free Community tier.
---
## Basic Charts
### BarChart
```tsx
import { BarChart } from '@mui/x-charts/BarChart';
const data = [
{ month: 'Jan', revenue: 4000, expenses: 2400 },
{ month: 'Feb', revenue: 3000, expenses: 1398 },
{ month: 'Mar', revenue: 2000, expenses: 9800 },
{ month: 'Apr', revenue: 2780, expenses: 3908 },
];
```
### Horizontal Bar Chart
```tsx
```
### LineChart
```tsx
import { LineChart } from '@mui/x-charts/LineChart';
date.toLocaleDateString('en-US', { month: 'short' }),
}]}
series={[
{
data: [2400, 1398, 9800, 3908],
label: 'Revenue',
area: true, // fill area under line
curve: 'catmullRom', // 'linear' | 'monotoneX' | 'catmullRom' | 'step'
showMark: true,
},
]}
width={600}
height={400}
/>
```
### PieChart
```tsx
import { PieChart } from '@mui/x-charts/PieChart';
0
outerRadius: 100,
paddingAngle: 2,
cornerRadius: 5,
startAngle: -90,
endAngle: 270,
cx: 150,
cy: 150,
highlightScope: { faded: 'global', highlighted: 'item' },
faded: { innerRadius: 30, additionalRadius: -30, color: 'gray' },
}]}
width={400}
height={300}
/>
```
### ScatterChart
```tsx
import { ScatterChart } from '@mui/x-charts/ScatterChart';
```
---
## Composition API
Build charts from primitives for maximum control. Mix chart types in one view.
```tsx
import {
ResponsiveChartContainer,
BarPlot,
LinePlot,
ChartsXAxis,
ChartsYAxis,
ChartsTooltip,
ChartsAxisHighlight,
ChartsLegend,
ChartsGrid,
MarkPlot,
} from '@mui/x-charts';
// Mixed bar + line chart with dual Y-axis
```
### Why Composition?
- Mix bar + line + scatter in one chart
- Dual Y-axis configurations
- Custom layout ordering (what renders on top)
- Add custom SVG elements between chart layers
- Full control over which sub-components are included
---
## Custom Axes
### Scale Types
```tsx
// Band axis (categorical) — most common for bar charts
xAxis={[{ scaleType: 'band', data: ['Jan', 'Feb', 'Mar'] }]}
// Linear axis (numeric) — scatter plots, continuous data
yAxis={[{ scaleType: 'linear', min: 0, max: 100 }]}
// Time axis — date values
xAxis={[{
scaleType: 'time',
data: [new Date('2024-01-01'), new Date('2024-02-01'), new Date('2024-03-01')],
valueFormatter: (date: Date) => date.toLocaleDateString(),
}]}
// Log axis — logarithmic scale
yAxis={[{ scaleType: 'log', min: 1, max: 10000 }]}
// Point axis — evenly spaced regardless of value
xAxis={[{ scaleType: 'point', data: [1, 10, 100, 1000] }]}
```
### Tick Formatting
```tsx
yAxis={[{
valueFormatter: (value: number) => `$${(value / 1000).toFixed(0)}K`,
label: 'Revenue',
tickMinStep: 1000,
tickMaxStep: 5000,
}]}
```
### Dual Y-Axis
```tsx
yAxis={[
{ id: 'left', label: 'Revenue ($)', position: 'left' },
{ id: 'right', label: 'Percentage (%)', position: 'right', min: 0, max: 100 },
]}
series={[
{ data: [1000, 2000, 3000], yAxisId: 'left', type: 'bar' },
{ data: [20, 45, 80], yAxisId: 'right', type: 'line' },
]}
```
---
## Custom Tooltips
### Built-in Tooltip Modes
```tsx
// Show tooltip per data item (hover over specific bar/point)
// Show tooltip for entire axis value (all series at that x position)
// Disable tooltip
```
### Custom Tooltip Content
```tsx
import { ChartsTooltip, type ChartsItemContentProps } from '@mui/x-charts';
import Paper from '@mui/material/Paper';
import Typography from '@mui/material/Typography';
function CustomItemTooltip(props: ChartsItemContentProps) {
const { series, dataIndex, color } = props;
if (dataIndex === undefined) return null;
return (
{series.label}
${series.data[dataIndex]?.toLocaleString()}
);
}
```
---
## Legends
```tsx
// Position legend at the top
// Hide legend
```
---
## Click Handlers & Interactivity
```tsx
{
// d.type: 'bar' | 'line' | 'scatter' | 'pie'
// d.seriesId: string
// d.dataIndex: number
console.log(`Clicked: series=${d.seriesId}, index=${d.dataIndex}`);
router.push(`/details/${data[d.dataIndex].id}`);
}}
onAxisClick={(event, d) => {
// d.axisValue: the x-axis value at click position
// d.dataIndex: index into the data array
console.log(`Axis clicked: ${d.axisValue}`);
}}
/>
```
### Highlight & Dim
```tsx
```
---
## Responsive Charts
Charts auto-fill parent width when using `ResponsiveChartContainer` or when width/height are omitted on basic charts with a sized parent.
```tsx
// Parent must have explicit dimensions
// Or use ResponsiveChartContainer for composition
```
### Margin Configuration
```tsx
```
---
## Stacked Series
```tsx
// Stacked area chart
```
Multiple stack groups:
```tsx
series={[
{ data: [10, 15], stack: 'group1', stackOrder: 'ascending' },
{ data: [5, 10], stack: 'group1' },
{ data: [3, 7], stack: 'group2' }, // separate stack
]}
```
---
## Animation
```tsx
// Disable animation
// Animations are enabled by default with sensible durations
// Custom duration/easing not directly exposed as props — use CSS transitions
// for additional animation effects on the container
```
---
## Color Palette
### Custom Colors per Series
```tsx
```
### Categorical Palette
```tsx
import { blueberryTwilightPalette, mangoFusionPalette, cheerfulFiestaPalette } from '@mui/x-charts/colorPalettes';
```
### Theme-Aware Colors
```tsx
import { useTheme } from '@mui/material/styles';
function ThemedChart({ data }) {
const theme = useTheme();
return (
);
}
```
---
## SparkLineChart
Inline mini charts for dashboards and tables.
```tsx
import { SparkLineChart } from '@mui/x-charts/SparkLineChart';
// Line sparkline
// Bar sparkline
// In a DataGrid cell
const columns: GridColDef[] = [
{ field: 'name', headerName: 'Product' },
{
field: 'trend',
headerName: 'Last 7 Days',
width: 150,
renderCell: (params) => (
params.value[0] ? '#16a34a' : '#dc2626']}
/>
),
},
];
```
---
## Gauge Charts
```tsx
import { Gauge, gaugeClasses } from '@mui/x-charts/Gauge';
// Simple gauge
`${value}%`}
sx={{
[`& .${gaugeClasses.valueArc}`]: {
fill: '#2563eb',
},
[`& .${gaugeClasses.valueText}`]: {
fontSize: 24,
fontWeight: 'bold',
},
}}
/>
```
---
## Dataset Approach
Pass a dataset array and reference fields by `dataKey`:
```tsx
const dataset = [
{ month: 'Jan', revenue: 4000, profit: 2400 },
{ month: 'Feb', revenue: 3000, profit: 1398 },
{ month: 'Mar', revenue: 2000, profit: 800 },
];
```
This is the preferred pattern when data comes from an API — avoids manual array extraction.
---
## Styling with sx
```tsx
```
---
## Performance Tips
- Use `dataset` + `dataKey` instead of extracting arrays manually
- For large datasets (1000+ points), consider downsampling before rendering
- `skipAnimation` for frequently updating charts (real-time data)
- Lazy-load chart components with `React.lazy`:
```tsx
const BarChart = lazy(() => import('@mui/x-charts/BarChart').then(m => ({ default: m.BarChart })));
```
- Charts auto-resize on window resize — avoid unnecessary re-renders of the parent