Chart
Charts built with Recharts that take their labels and colors from one shared config.
A chart draws data with the chart components of Recharts inside ChartContainer. The container takes a config that gives every data key a label and a color, and it writes each color as a CSS variable such as --color-revenue, which the chart references in fill or stroke. ChartTooltipContent and ChartLegendContent show the labels of the config, so the tooltip and the legend use the same names as the rest of the app.
Usage
Examples
Line Chart
How a figure develops over a day, a week or a season often matters more than any single value. A line chart traces that course and lets users spot rises, peaks and turning points at a glance. With type set to "linear", every value joins the next by a straight segment, so the line never rises above or falls below the values the data contains.
Area Chart
Some figures describe a quantity rather than a level, such as the cases that arrive in a warehouse during a shift. Drawn with Area inside AreaChart, the course fills the space down to the axis, so users perceive the volume of each period as well as the direction in which it moves.
API Reference
ChartContainer
| Prop | config |
|---|---|
| Type | ChartConfig |
| Default | – |
| Description | Gives every data key of the chart a label, an icon and a color. ChartContainer writes the colors as CSS variables named --color- and the key. |
| Prop | children |
|---|---|
| Type | ReactElement |
| Default | – |
| Description | Takes one chart of Recharts, such as BarChart or LineChart, which fills the container. |
| Prop | initialDimension |
|---|---|
| Type | { width: number; height: number } |
| Default | { width: 320, height: 200 } |
| Description | Sets the size of the chart before the container has measured itself. |
| Prop | id |
|---|---|
| Type | string |
| Default | – |
| Description | Sets the id that scopes the CSS variables to this chart. Without it, ChartContainer creates one. |
ChartConfig
| Prop | label |
|---|---|
| Type | ReactNode |
| Default | – |
| Description | Names the data key in the tooltip and the legend. |
| Prop | icon |
|---|---|
| Type | ComponentType |
| Default | – |
| Description | Replaces the color indicator of the data key in the legend. |
| Prop | color |
|---|---|
| Type | string |
| Default | – |
| Description | Sets one color for both themes. Takes a CSS color or a CSS variable. |
| Prop | theme |
|---|---|
| Type | { light: string; dark: string } |
| Default | – |
| Description | Sets one color for each theme instead of color. |
ChartTooltip
ChartTooltip is the Tooltip component of Recharts and takes its props. Pass ChartTooltipContent as content to show the tooltip with the labels and colors of the config.
ChartTooltipContent
| Prop | indicator |
|---|---|
| Type | "dot" | "line" | "dashed" |
| Default | "dot" |
| Description | Sets the shape that marks the color of each entry. |
| Prop | hideLabel |
|---|---|
| Type | boolean |
| Default | false |
| Description | Hides the label above the entries, such as the month of a bar. |
| Prop | hideIndicator |
|---|---|
| Type | boolean |
| Default | false |
| Description | Hides the color marks of the entries. |
| Prop | nameKey |
|---|---|
| Type | string |
| Default | – |
| Description | Sets the key that the tooltip uses to find each entry in the config, instead of its data key. |
| Prop | labelKey |
|---|---|
| Type | string |
| Default | – |
| Description | Sets the key that the tooltip uses to find its label in the config. |
| Prop | formatter |
|---|---|
| Type | function |
| Default | – |
| Description | Renders the value of each entry, for example as an amount in CHF. |
| Prop | labelFormatter |
|---|---|
| Type | function |
| Default | – |
| Description | Renders the label above the entries. |
ChartLegend
ChartLegend is the Legend component of Recharts and takes its props. Pass ChartLegendContent as content to show the legend with the labels and colors of the config.
ChartLegendContent
| Prop | hideIcon |
|---|---|
| Type | boolean |
| Default | false |
| Description | Shows the color mark of each entry even when the config gives it an icon. |
| Prop | nameKey |
|---|---|
| Type | string |
| Default | – |
| Description | Sets the key that the legend uses to find each entry in the config, instead of its data key. |
| Prop | verticalAlign |
|---|---|
| Type | "top" | "bottom" |
| Default | "bottom" |
| Description | Receives the position of the legend from ChartLegend and puts the space on the side of the legend that faces the chart. |
ChartStyle
| Prop | id |
|---|---|
| Type | string |
| Default | – |
| Description | Sets the chart whose CSS variables the style defines. |
| Prop | config |
|---|---|
| Type | ChartConfig |
| Default | – |
| Description | Gives the colors that the style writes as CSS variables. ChartContainer renders ChartStyle itself, so an app only needs it for a chart outside ChartContainer. |