apps/www/content/docs/charts/line-chart.mdx
import { Chart, useChart } from "@chakra-ui/charts"
import { CartesianGrid, Line, LineChart, XAxis, YAxis } from "recharts"
<Chart.Root>
<LineChart>
<CartesianGrid />
<XAxis />
<YAxis />
<Line />
</LineChart>
</Chart.Root>
To add labels to the x and y axes, use the Label component from recharts.
<XAxis axisLine={false} label={{ value: "X Axis", position: "bottom" }} />
<YAxis axisLine={false} label={{ value: "Y Axis", position: "left", angle: -90 }} />
Set dot and activeDot to false to hide the dots completely.
<Line dot={false} activeDot={false} />
Render the LabelList component from recharts inside the Line component to
show labels at each data point.
<Line>
<LabelList position="right" offset={10} />
</Line>
Use the Chart.Gradient component to create a gradient. Ensure the id is
unique and used in the stroke prop of the Line component.
<defs>
<Chart.Gradient id="custom-gradient" stops={[]} />
</defs>
<Line stroke="url(#custom-gradient)" />
Set the strokeDasharray prop in the series object to create a dashed line.
const chart = useChart({
data: [
{ windows: 186, mac: 165, month: "January" },
//...
],
series: [
{ name: "windows", color: "teal.solid", strokeDasharray: "5 5" },
{ name: "mac", color: "purple.solid" },
],
})
Here's an example of a line chart with multiple series.
<ExampleTabs name="charts/line-chart-multiple" />Adding interactivity to the chart legends make it come to life. To enable this
feature, set the interaction prop to "hover" or "click" in the
Chart.Legend component.
<Chart.Legend interaction="hover" />
By default, the chart shows the label for each tick. To show just the start and
end ticks, pass the ticks prop to the XAxis component from recharts.
You can optionally pass a
labelprop to theXAxiscomponent to show a label at the bottom of the axis.
<XAxis
ticks={["January", "August"]}
label={{ value: "[January - August] Customers", position: "bottom" }}
/>
To format the value axis ticks, pass the tickFormatter prop to the YAxis
component from recharts.
<YAxis
tickFormatter={chart.formatNumber({
style: "currency",
currency: "USD",
notation: "compact",
})}
/>
Use the yAxisId prop in the series object and YAxis component to create a
chart with two y-axes.
<YAxis yAxisId="left" />
<YAxis yAxisId="right" orientation="right" />
In event you need to customize the tooltip entirely, replace the Chart.Tooltip
component with your own. The basic signature of a custom tooltip looks like:
function CustomTooltip(props: TooltipProps<string, string>) {
const { active, payload, label } = props
if (!active || !payload || payload.length === 0) return null
return <Box></Box>
}
To add a custom label to the series, set the label prop in the series
object.
const chart = useChart({
data: [
{ mac: 10, linux: 120, month: "January" },
//...
],
series: [
{ name: "mac", label: "Mac sales", color: "purple.solid" },
{ name: "linux", label: "Linux sales", color: "blue.solid" },
],
})
Use the reference components from recharts to highlight a specific data point.
<ReferenceDot x="August" y={110} r={6} />
<ReferenceLine y={110} label={{ value: "Target", position: "top" }} />
Pass the domain prop to the YAxis component to set the domain (upper and
lower bounds) of the value axis.
<YAxis domain={[0, 100]} />
To connect the null values, set the connectNulls prop to true in the Line
component.
<Line connectNulls />
Here's an example of composing the Card, State and Chart components
together to create a stunning visualization.
Recharts provides flexible support for various kinds of line charts.
Below are the different types of line charts you can create:
<Box mt="12" borderWidth="1px" ps="3" pe="10" py="10" rounded="l2"> <ExamplePreview name="charts/line-chart-with-types" /> </Box>