357 lines · 2 files · 9.1 kB
cases/189-shadcn-radial-stacked/example.tsx210 lines · entry
cases/189-shadcn-radial-stacked/example.tsx
import { defineChart, type ChartPoint } from '@tanstack/charts'
import {
focusGroupAngle,
polar,
radialBarAngle,
radialText,
} from '@tanstack/charts/polar'
import { RendererChart } from '@tanstack/charts/react/tooltip'
import { tooltip } from '@tanstack/charts/tooltip'
import { motion } from '@tanstack/charts/motion'
import { scaleBand, scaleLinear } from 'd3-scale'
import { shadcnColors } from '@tanstack/charts-data/shadcn'
import './styles.css'
const twoSeries = ['desktop', 'mobile'] as const
export function createExampleChart() {
const rows = [
{
id: 'mobile',
ring: 'visitors',
start: 0,
end: 570,
fill: shadcnColors[1],
},
{
id: 'desktop',
ring: 'visitors',
start: 570,
end: 1260,
fill: shadcnColors[0],
},
]
return defineChart(
{
marks: [
polar({
startAngle: rechartsPolarAngle(0),
endAngle: rechartsPolarAngle(180),
scales: {
angle: { scale: scaleLinear().domain([0, 1260]) },
radius: {
scale: scaleBand<string>().domain(['visitors']),
range: [80, 110],
},
},
marks: [
radialBarAngle(rows, {
id: 'stacked-values',
angle1: 'start',
angle2: 'end',
angle: 'end',
radius: 'ring',
key: 'id',
fill: (row) => row.fill,
cornerRadius: 5,
stroke: 'transparent',
strokeWidth: 2,
}),
],
}),
radialCenterLabels('1,830', 24, -16, 4),
],
scales: {
x: null,
y: null,
},
color: { domain: twoSeries, range: shadcnColors.slice(0, 2) },
margin: 0,
},
{
svgAnimation: false,
focus: focusGroupAngle,
keyboard: false,
tooltip: {
use: tooltip,
className: 'sc-chart-tooltip',
anchor: 'group-center',
placement: 'auto',
sort: 'color-domain',
content: (points) => shadcnTooltipContent(points),
},
},
)
}
function radialCenterLabels(
total: string,
fontSize: number,
totalDy: number,
labelDy: number,
) {
return polar({
scales: {
angle: { scale: scaleLinear().domain([0, 1]) },
radius: { scale: scaleLinear().domain([0, 1]) },
},
marks: [
radialText([{ id: 'total', angle: 0, radius: 0, text: total }], {
id: `radial-total-${total}`,
angle: 'angle',
radius: 'radius',
key: 'id',
text: 'text',
dy: totalDy,
fill: 'var(--foreground)',
fontSize,
fontWeight: 700,
}),
radialText([{ id: 'label', angle: 0, radius: 0, text: 'Visitors' }], {
id: `radial-label-${total}`,
angle: 'angle',
radius: 'radius',
key: 'id',
text: 'text',
dy: labelDy,
fill: 'var(--muted-foreground)',
fontSize: 14,
}),
],
})
}
function rechartsPolarAngle(degrees: number) {
return ((90 - degrees) * Math.PI) / 180
}
function titleCase(value: string) {
return value.charAt(0).toUpperCase() + value.slice(1)
}
function shadcnTooltipContent<TDatum>(points: readonly ChartPoint<TDatum>[]) {
const point = points.find((candidate) => browserMetric(candidate.datum))
const metric = point && browserMetric(point.datum)
return metric
? {
title: titleCase(metric.browser),
rows: [
{
label: 'Visitors',
value: metric.visitors.toLocaleString('en-US'),
color: point.color,
},
],
}
: { rows: [] }
}
function browserMetric(datum: unknown) {
if (!datum || typeof datum !== 'object') return undefined
const browser = Reflect.get(datum, 'browser')
const visitors = Reflect.get(datum, 'visitors')
return typeof browser === 'string' &&
typeof visitors === 'number' &&
Number.isFinite(visitors)
? { browser, visitors }
: undefined
}
export const definition = createExampleChart()
const renderer = motion({
initial: 'always',
transition: { type: 'spring', stiffness: 170, damping: 18, mass: 1 },
})
export interface ExampleProps {
width?: number
height?: number
}
export default function Example({ width = 640, height = 600 }: ExampleProps) {
const contentWidth = Math.max(1, width - 50)
const chartWidth = Math.min(250, contentWidth)
const chartHeight = chartWidth
return (
<div className="sc-example" style={{ width, height }}>
<article className="sc-card sc-default" style={{ width }}>
<header className="sc-card-header">
<div className="sc-card-heading">
<h2>Radial Chart - Stacked</h2>
<p>January - June 2024</p>
</div>
</header>
<div className="sc-card-content sc-centered">
<div
className="sc-chart"
style={{ width: chartWidth, height: chartHeight }}
>
<RendererChart
definition={definition}
renderer={renderer}
initialWidth={chartWidth}
height={chartHeight}
ariaLabel="Radial Chart - Stacked"
/>
</div>
</div>
<footer className="sc-card-footer sc-centered">
<TrendFooter note="Showing total visitors for the last 6 months" />
</footer>
</article>
</div>
)
}
function TrendFooter({ note }: { note: string }) {
return (
<>
<div className="sc-trend">
Trending up by 5.2% this month{' '}
<svg viewBox="0 0 24 24" aria-hidden="true">
<path d="m3 17 6-6 4 4 8-8" />
<path d="M14 7h7v7" />
</svg>
</div>
<div className="sc-footer-note">{note}</div>
</>
)
}cases/189-shadcn-radial-stacked/styles.css147 lines · dependency
cases/189-shadcn-radial-stacked/styles.css
.sc-example {
--background: oklch(1 0 0);
--foreground: oklch(0 0 0);
--card: oklch(1 0 0);
--card-foreground: oklch(0 0 0);
--muted: oklch(0.97 0 0);
--muted-foreground: oklch(0.556 0 0);
--border: oklch(0.922 0 0);
--chart-1: oklch(0.809 0.105 251.813);
--chart-2: oklch(0.623 0.214 259.815);
--chart-3: oklch(0.546 0.245 262.881);
--chart-4: oklch(0.488 0.243 264.376);
--chart-5: oklch(0.424 0.199 265.638);
display: flex;
justify-content: center;
align-items: flex-start;
overflow: hidden;
color: var(--foreground);
background: var(--background);
font-family:
Inter,
ui-sans-serif,
system-ui,
-apple-system,
BlinkMacSystemFont,
'Segoe UI',
sans-serif;
text-rendering: geometricPrecision;
}
:root[data-theme='dark'] .sc-example {
--background: oklch(0.145 0 0);
--foreground: oklch(0.985 0 0);
--card: oklch(0.205 0 0);
--card-foreground: oklch(0.985 0 0);
--muted: oklch(0.269 0 0);
--muted-foreground: oklch(0.708 0 0);
--border: oklch(1 0 0 / 10%);
--chart-1: oklch(0.809 0.105 251.813);
--chart-2: oklch(0.623 0.214 259.815);
--chart-3: oklch(0.546 0.245 262.881);
--chart-4: oklch(0.488 0.243 264.376);
--chart-5: oklch(0.424 0.199 265.638);
}
.sc-example,
.sc-example * {
box-sizing: border-box;
}
.sc-card {
display: flex;
flex-direction: column;
gap: 24px;
border: 1px solid var(--border);
border-radius: 14px;
background: var(--card);
color: var(--card-foreground);
padding: 24px 0;
}
.sc-card-header {
display: grid;
gap: 8px;
padding: 0 24px;
}
.sc-card-heading {
display: grid;
gap: 8px;
}
.sc-card-header h2 {
margin: 0;
font-size: 16px;
font-weight: 600;
line-height: 1;
letter-spacing: -0.01em;
}
.sc-card-header p {
margin: 0;
color: var(--muted-foreground);
font-size: 14px;
line-height: 20px;
}
.sc-card-content {
display: flex;
min-height: 0;
flex-direction: column;
padding: 0 24px;
}
.sc-chart {
position: relative;
flex: none;
}
.sc-chart > * {
display: block;
}
.sc-card-footer {
display: flex;
flex-direction: column;
align-items: flex-start;
gap: 8px;
padding: 0 24px;
font-size: 14px;
line-height: 1;
}
.sc-centered {
justify-content: center;
align-items: center;
text-align: center;
}
.sc-trend {
display: flex;
align-items: center;
gap: 8px;
font-weight: 500;
}
.sc-trend svg {
width: 16px;
height: 16px;
fill: none;
stroke: currentColor;
stroke-width: 2;
stroke-linecap: round;
stroke-linejoin: round;
}
.sc-footer-note {
color: var(--muted-foreground);
}
.sc-example .ts-chart {
color: var(--muted-foreground);
}
.sc-example .ts-chart__grid line,
.sc-example .ts-chart__polar-grid path,
.sc-example .ts-chart__polar-grid line {
stroke: var(--border);
}
.sc-example .ts-chart__axes text,
.sc-example .ts-chart__polar-grid text {
fill: var(--muted-foreground);
font-size: 12px;
}
.sc-example .ts-chart-tooltip {
min-width: 128px;
padding: 7px 10px !important;
border: 1px solid var(--border) !important;
border-radius: 8px !important;
background: var(--card) !important;
color: var(--card-foreground) !important;
box-shadow: 0 2px 6px rgb(0 0 0 / 8%);
font-size: 12px;
}