Getting Started
Charts
- Charts
- Line Chart
- Bar Chart
- Area Chart
- Scatter Chart
- Pie Chart
- Donut Chart
- Dot Plot Chart
- Lollipop Chart
- Dumbbell Chart
- Slope Chart
- Range Chart
- Histogram Chart
- Box Plot Chart
- Violin Chart
- Polar Chart
- Parallel Coordinates
- SPLOM Chart
- Parcats Chart
- Candlestick Chart
- OHLC Chart
- Waterfall Chart
- Funnel Chart
- Heatmap Chart
- Contour Chart
- Density Chart
- Ternary Chart
- Radar Chart
- Treemap Chart
- Sunburst Chart
- Sankey Chart
- Gauge Chart
- Bullet Chart
- Icicle Chart
- Network Graph
- Dendrogram
- Choropleth Chart
"use client"
import { LineChart } from "@/components/ui/charts"
const data = [
{
name: "AAPL",
data: [
{ x: "2024-01-02", y: 185.2 },
{ x: "2024-01-08", y: 181.4 },
{ x: "2024-01-16", y: 189.7 },
{ x: "2024-01-22", y: 194.3 },
{ x: "2024-01-29", y: 191.6 },
{ x: "2024-02-05", y: 187.1 },
{ x: "2024-02-12", y: 182.5 },
{ x: "2024-02-20", y: 178.9 },
{ x: "2024-02-26", y: 174.2 },
{ x: "2024-03-04", y: 170.8 },
{ x: "2024-03-11", y: 165.3 },
{ x: "2024-03-18", y: 171.6 },
{ x: "2024-03-25", y: 168.4 },
{ x: "2024-04-01", y: 172.9 },
{ x: "2024-04-08", y: 178.5 },
{ x: "2024-04-15", y: 183.1 },
{ x: "2024-04-22", y: 176.8 },
{ x: "2024-04-29", y: 180.4 },
{ x: "2024-05-06", y: 186.7 },
{ x: "2024-05-13", y: 192.3 },
{ x: "2024-05-20", y: 189.1 },
{ x: "2024-05-28", y: 195.8 },
{ x: "2024-06-03", y: 201.4 },
{ x: "2024-06-10", y: 198.2 },
{ x: "2024-06-17", y: 207.6 },
{ x: "2024-06-24", y: 213.9 },
{ x: "2024-07-01", y: 210.3 },
{ x: "2024-07-08", y: 218.5 },
{ x: "2024-07-15", y: 224.1 },
{ x: "2024-07-22", y: 219.7 },
],
},
]
export function LineChartDemo() {
return (
<div className="w-full">
<LineChart data={data} variant="stock" xType="time" />
</div>
)
}
Installation
pnpm dlx shadcn@latest add https://ui.simplifyingai.com/r/line-chart.json
Usage
import { LineChart } from "@/components/ui/charts"Basic Line Chart
const data = [
{
name: "Revenue",
data: [
{ x: "Jan", y: 4200 },
{ x: "Feb", y: 4800 },
{ x: "Mar", y: 5100 },
{ x: "Apr", y: 4900 },
],
color: "#3b82f6",
},
]
export function Example() {
return <LineChart data={data} showDots />
}Smooth Variant
Clean, minimal line with area fill. Perfect for analytics dashboards.
"use client"
import * as React from "react"
import { LineChart } from "@/components/ui/charts"
// Generate smooth time series data (like Google Analytics)
function generateTimeSeriesData(days: number): { x: Date; y: number }[] {
const data: { x: Date; y: number }[] = []
const startDate = new Date("2024-04-01")
// Seeded pseudo-random for deterministic results
const seededRandom = (seed: number) => {
const x = Math.sin(seed) * 10000
return x - Math.floor(x)
}
let value = 50
for (let i = 0; i < days; i++) {
const date = new Date(startDate)
date.setDate(startDate.getDate() + i)
// Random walk with some periodicity
value += (seededRandom(i * 100) - 0.5) * 20
value = Math.max(10, Math.min(100, value))
// Add some weekly pattern
const dayOfWeek = date.getDay()
const weekendDip = dayOfWeek === 0 || dayOfWeek === 6 ? -15 : 0
data.push({
x: date,
y: Math.round(value + weekendDip + Math.sin(i / 7) * 10),
})
}
return data
}
export function LineChartSmoothDemo() {
const [data, setData] = React.useState<{ x: Date; y: number }[]>([])
React.useEffect(() => {
setData(generateTimeSeriesData(90))
}, [])
if (data.length === 0) {
return <div className="h-[300px] w-full max-w-4xl" />
}
return (
<div className="w-full max-w-4xl">
<LineChart
data={[
{
name: "Traffic",
data: data,
color: "#93c5fd",
},
]}
variant="smooth"
xType="time"
height={300}
strokeWidth={2}
/>
</div>
)
}
<LineChart data={data} variant="smooth" xType="time" />Multi-Line Comparison
Multiple lines with distinct colors and crosshair tooltip.
"use client"
import * as React from "react"
import { LineChart } from "@/components/ui/charts"
// Generate deterministic multi-series data
function generateMultiSeriesData() {
const colors = [
"#2563eb", // blue
"#06b6d4", // cyan
"#f97316", // orange
"#8b5cf6", // violet
"#eab308", // yellow
"#ec4899", // pink
"#22c55e", // green
"#64748b", // slate
]
const names = [
"Product A",
"Product B",
"Product C",
"Product D",
"Product E",
"Product F",
"Product G",
"Product H",
]
// Use seeded pseudo-random for deterministic results
const seededRandom = (seed: number) => {
const x = Math.sin(seed) * 10000
return x - Math.floor(x)
}
const result = []
const startDate = new Date("2024-08-01")
for (let s = 0; s < 8; s++) {
const data: { x: Date; y: number }[] = []
let value = 100 + seededRandom(s * 1000) * 400
for (let i = 0; i < 100; i++) {
const date = new Date(startDate)
date.setDate(startDate.getDate() + i)
const growth =
1 + (seededRandom(s * 1000 + i) - 0.3) * 0.05 * (s % 3 === 0 ? 2 : 1)
value *= growth
value = Math.max(50, value)
data.push({
x: date,
y: Math.round(value),
})
}
result.push({
name: names[s],
data,
color: colors[s],
})
}
return result
}
export function LineChartMultiDemo() {
const [data, setData] = React.useState<
ReturnType<typeof generateMultiSeriesData>
>([])
React.useEffect(() => {
setData(generateMultiSeriesData())
}, [])
if (data.length === 0) {
return <div className="h-[400px] w-full max-w-4xl" />
}
return (
<div className="w-full max-w-4xl">
<LineChart
data={data}
variant="multi"
xType="time"
height={400}
yAxisLabel="Value"
/>
</div>
)
}
const data = [
{ name: "Product A", data: productAData, color: "#2563eb" },
{ name: "Product B", data: productBData, color: "#06b6d4" },
{ name: "Product C", data: productCData, color: "#f97316" },
]
<LineChart data={data} variant="multi" xType="time" showLegend />Stock Variant
Financial chart style with detailed data, grid lines, and title support.
"use client"
import * as React from "react"
import { LineChart } from "@/components/ui/charts"
// Generate stock price data (like Amazon chart)
function generateStockData(days: number): { x: Date; y: number }[] {
const data: { x: Date; y: number }[] = []
const startDate = new Date("2024-01-02")
// Seeded pseudo-random for deterministic results
const seededRandom = (seed: number) => {
const x = Math.sin(seed) * 10000
return x - Math.floor(x)
}
let price = 1180
for (let i = 0; i < days; i++) {
const date = new Date(startDate)
date.setDate(startDate.getDate() + i)
// Skip weekends
if (date.getDay() === 0 || date.getDay() === 6) continue
// Random walk with upward drift (typical stock behavior)
const drift = 0.0008
const volatility = 0.02
const change = drift + volatility * (seededRandom(i * 50) - 0.5) * 2
price *= 1 + change
// Add some intraday variation
price += (seededRandom(i * 100) - 0.5) * 10
data.push({
x: date,
y: Math.round(price * 100) / 100,
})
}
return data
}
export function LineChartStockDemo() {
const [data, setData] = React.useState<{ x: Date; y: number }[]>([])
React.useEffect(() => {
setData(generateStockData(280))
}, [])
if (data.length === 0) {
return <div className="h-[400px] w-full max-w-4xl" />
}
return (
<div className="w-full max-w-4xl">
<LineChart
data={[
{
name: "AMZN",
data: data,
color: "#0d9488",
},
]}
variant="stock"
xType="time"
height={400}
title="Amazon"
subtitle="2024 year to date"
yAxisLabel="Closing Price"
xAxisLabel="Date"
yAxisFormatter={(value) => `$${value.toLocaleString()}`}
/>
</div>
)
}
<LineChart
data={[{ name: "AAPL", data: stockData, color: "#0d9488" }]}
variant="stock"
xType="time"
title="Apple Inc."
subtitle="2024 YTD"
yAxisFormatter={(v) => `$${v.toLocaleString()}`}
/>API Reference
LineChart
The main line chart component for visualizing data trends over time.
<LineChart data={data} variant="smooth" showDots />Props
| Prop | Type | Default | Description |
|---|---|---|---|
| data | LineChartSeries[] | required | Array of data series |
| variant | "default" | "smooth" | "multi" | "stock" | "sparkline" | "default" | Chart style variant |
| curve | "linear" | "monotone" | "cardinal" | "step" | "natural" | "basis" | "monotone" | Line interpolation |
| xType | "category" | "number" | "time" | "category" | X-axis data type |
| showDots | boolean | varies | Show data points |
| showGrid | boolean | true | Show grid lines |
| showLegend | boolean | true | Show legend |
| showTooltip | boolean | true | Show tooltips |
| showArea | boolean | false | Fill area under line |
| showCrosshair | boolean | false | Show crosshair on hover |
| color | string | - | Line color (single series) |
| strokeWidth | number | 2 | Line thickness |
| areaOpacity | number | 0.15 | Area fill opacity |
| title | string | - | Chart title (stock variant) |
| subtitle | string | - | Chart subtitle |
| yAxisFormatter | (value: number) => string | - | Y-axis value formatter |
LineChartSeries Type
interface LineChartSeries {
name: string
data: LineChartDataPoint[]
color?: string
strokeWidth?: number
strokeDasharray?: string
showDots?: boolean
dotSize?: number
}
interface LineChartDataPoint {
x: string | number | Date
y: number
}Notes
- Built on D3.js for high-performance SVG rendering
- Supports multiple data series with individual styling
- Variants optimized for specific use cases (analytics, financial, comparison)
- Responsive design adapts to container dimensions
- Smooth animations using D3 transitions
- Automatic axis scaling based on data range
- Touch-friendly tooltips for mobile devices
Ask Your Database Anything with ChatPlotDB
Connect your database, ask questions in plain English, and get answers as charts, tables, and dashboards. No SQL required.
Join the ChatPlotDB waitlist