feat: responsive charts with timestamp tooltips
All checks were successful
Build & Publish / publish (push) Successful in 1m43s
All checks were successful
Build & Publish / publish (push) Successful in 1m43s
Charts (LineChart, AreaChart, BarChart) now fill their container width instead of using a fixed 400px default. SVGs use width=100% with preserveAspectRatio=none. LineChart and AreaChart tooltips now show the data point timestamp when x-values are Date objects, displayed as a header row above the series values. Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
This commit is contained in:
@@ -1,6 +1,6 @@
|
|||||||
{
|
{
|
||||||
"name": "@cameleer/design-system",
|
"name": "@cameleer/design-system",
|
||||||
"version": "0.1.45",
|
"version": "0.1.46",
|
||||||
"type": "module",
|
"type": "module",
|
||||||
"main": "./dist/index.es.js",
|
"main": "./dist/index.es.js",
|
||||||
"module": "./dist/index.es.js",
|
"module": "./dist/index.es.js",
|
||||||
|
|||||||
@@ -1,8 +1,9 @@
|
|||||||
.wrapper {
|
.wrapper {
|
||||||
position: relative;
|
position: relative;
|
||||||
display: inline-flex;
|
display: flex;
|
||||||
flex-direction: column;
|
flex-direction: column;
|
||||||
gap: 4px;
|
gap: 4px;
|
||||||
|
width: 100%;
|
||||||
}
|
}
|
||||||
|
|
||||||
.svg {
|
.svg {
|
||||||
@@ -78,6 +79,15 @@
|
|||||||
white-space: nowrap;
|
white-space: nowrap;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
.tooltipTime {
|
||||||
|
font-family: var(--font-mono);
|
||||||
|
font-size: 10px;
|
||||||
|
color: var(--text-muted);
|
||||||
|
margin-bottom: 4px;
|
||||||
|
padding-bottom: 3px;
|
||||||
|
border-bottom: 1px solid var(--border-subtle);
|
||||||
|
}
|
||||||
|
|
||||||
.tooltipRow {
|
.tooltipRow {
|
||||||
display: flex;
|
display: flex;
|
||||||
align-items: center;
|
align-items: center;
|
||||||
|
|||||||
@@ -45,6 +45,7 @@ export function AreaChart({
|
|||||||
const [tooltip, setTooltip] = useState<{
|
const [tooltip, setTooltip] = useState<{
|
||||||
x: number
|
x: number
|
||||||
y: number
|
y: number
|
||||||
|
xLabel: string
|
||||||
values: { label: string; value: number; color: string }[]
|
values: { label: string; value: number; color: string }[]
|
||||||
} | null>(null)
|
} | null>(null)
|
||||||
|
|
||||||
@@ -84,6 +85,15 @@ export function AreaChart({
|
|||||||
|
|
||||||
// Find closest x value
|
// Find closest x value
|
||||||
const pctX = (mx - dims.paddingLeft) / plotW
|
const pctX = (mx - dims.paddingLeft) / plotW
|
||||||
|
const firstS = series[0]
|
||||||
|
const idx0 = Math.max(0, Math.min(firstS.data.length - 1, Math.round(pctX * (firstS.data.length - 1))))
|
||||||
|
const xVal = firstS.data[idx0]?.x
|
||||||
|
const xLabel = xVal instanceof Date
|
||||||
|
? xVal.toLocaleString([], { month: 'short', day: 'numeric', hour: '2-digit', minute: '2-digit', second: '2-digit' })
|
||||||
|
: typeof xVal === 'number' && xVal > 1e10
|
||||||
|
? new Date(xVal).toLocaleString([], { month: 'short', day: 'numeric', hour: '2-digit', minute: '2-digit', second: '2-digit' })
|
||||||
|
: String(xVal ?? '')
|
||||||
|
|
||||||
const values = series.map((s, i) => {
|
const values = series.map((s, i) => {
|
||||||
const idx = Math.round(pctX * (s.data.length - 1))
|
const idx = Math.round(pctX * (s.data.length - 1))
|
||||||
const clamped = Math.max(0, Math.min(s.data.length - 1, idx))
|
const clamped = Math.max(0, Math.min(s.data.length - 1, idx))
|
||||||
@@ -95,16 +105,17 @@ export function AreaChart({
|
|||||||
}
|
}
|
||||||
})
|
})
|
||||||
|
|
||||||
setTooltip({ x: mx, y: my, values })
|
setTooltip({ x: mx, y: my, xLabel, values })
|
||||||
}
|
}
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<div className={`${styles.wrapper} ${className ?? ''}`}>
|
<div className={`${styles.wrapper} ${className ?? ''}`}>
|
||||||
{yLabel && <div className={styles.yLabel}>{yLabel}</div>}
|
{yLabel && <div className={styles.yLabel}>{yLabel}</div>}
|
||||||
<svg
|
<svg
|
||||||
width={width}
|
width="100%"
|
||||||
height={height}
|
height={height}
|
||||||
viewBox={`0 0 ${width} ${height}`}
|
viewBox={`0 0 ${width} ${height}`}
|
||||||
|
preserveAspectRatio="none"
|
||||||
className={styles.svg}
|
className={styles.svg}
|
||||||
onMouseMove={handleMouseMove}
|
onMouseMove={handleMouseMove}
|
||||||
onMouseLeave={() => setTooltip(null)}
|
onMouseLeave={() => setTooltip(null)}
|
||||||
@@ -223,6 +234,9 @@ export function AreaChart({
|
|||||||
top: tooltip.y,
|
top: tooltip.y,
|
||||||
}}
|
}}
|
||||||
>
|
>
|
||||||
|
{tooltip.xLabel && (
|
||||||
|
<div className={styles.tooltipTime}>{tooltip.xLabel}</div>
|
||||||
|
)}
|
||||||
{tooltip.values.map((v) => (
|
{tooltip.values.map((v) => (
|
||||||
<div key={v.label} className={styles.tooltipRow}>
|
<div key={v.label} className={styles.tooltipRow}>
|
||||||
<span className={styles.tooltipDot} style={{ background: v.color }} />
|
<span className={styles.tooltipDot} style={{ background: v.color }} />
|
||||||
|
|||||||
@@ -1,8 +1,9 @@
|
|||||||
.wrapper {
|
.wrapper {
|
||||||
position: relative;
|
position: relative;
|
||||||
display: inline-flex;
|
display: flex;
|
||||||
flex-direction: column;
|
flex-direction: column;
|
||||||
gap: 4px;
|
gap: 4px;
|
||||||
|
width: 100%;
|
||||||
}
|
}
|
||||||
|
|
||||||
.svg {
|
.svg {
|
||||||
|
|||||||
@@ -101,9 +101,10 @@ export function BarChart({
|
|||||||
<div className={`${styles.wrapper} ${className ?? ''}`}>
|
<div className={`${styles.wrapper} ${className ?? ''}`}>
|
||||||
{yLabel && <div className={styles.yLabel}>{yLabel}</div>}
|
{yLabel && <div className={styles.yLabel}>{yLabel}</div>}
|
||||||
<svg
|
<svg
|
||||||
width={width}
|
width="100%"
|
||||||
height={height}
|
height={height}
|
||||||
viewBox={`0 0 ${width} ${height}`}
|
viewBox={`0 0 ${width} ${height}`}
|
||||||
|
preserveAspectRatio="none"
|
||||||
className={styles.svg}
|
className={styles.svg}
|
||||||
onMouseLeave={() => setTooltip(null)}
|
onMouseLeave={() => setTooltip(null)}
|
||||||
aria-label="Bar chart"
|
aria-label="Bar chart"
|
||||||
|
|||||||
@@ -1,8 +1,9 @@
|
|||||||
.wrapper {
|
.wrapper {
|
||||||
position: relative;
|
position: relative;
|
||||||
display: inline-flex;
|
display: flex;
|
||||||
flex-direction: column;
|
flex-direction: column;
|
||||||
gap: 4px;
|
gap: 4px;
|
||||||
|
width: 100%;
|
||||||
}
|
}
|
||||||
|
|
||||||
.svg {
|
.svg {
|
||||||
@@ -69,6 +70,15 @@
|
|||||||
white-space: nowrap;
|
white-space: nowrap;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
.tooltipTime {
|
||||||
|
font-family: var(--font-mono);
|
||||||
|
font-size: 10px;
|
||||||
|
color: var(--text-muted);
|
||||||
|
margin-bottom: 4px;
|
||||||
|
padding-bottom: 3px;
|
||||||
|
border-bottom: 1px solid var(--border-subtle);
|
||||||
|
}
|
||||||
|
|
||||||
.tooltipRow {
|
.tooltipRow {
|
||||||
display: flex;
|
display: flex;
|
||||||
align-items: center;
|
align-items: center;
|
||||||
|
|||||||
@@ -44,6 +44,7 @@ export function LineChart({
|
|||||||
const [tooltip, setTooltip] = useState<{
|
const [tooltip, setTooltip] = useState<{
|
||||||
x: number
|
x: number
|
||||||
y: number
|
y: number
|
||||||
|
xLabel: string
|
||||||
values: { label: string; value: number; color: string }[]
|
values: { label: string; value: number; color: string }[]
|
||||||
} | null>(null)
|
} | null>(null)
|
||||||
|
|
||||||
@@ -80,6 +81,15 @@ export function LineChart({
|
|||||||
const my = e.clientY - rect.top
|
const my = e.clientY - rect.top
|
||||||
const pctX = (mx - dims.paddingLeft) / plotW
|
const pctX = (mx - dims.paddingLeft) / plotW
|
||||||
|
|
||||||
|
const firstS = series[0]
|
||||||
|
const idx0 = Math.max(0, Math.min(firstS.data.length - 1, Math.round(pctX * (firstS.data.length - 1))))
|
||||||
|
const xVal = firstS.data[idx0]?.x
|
||||||
|
const xLabel = xVal instanceof Date
|
||||||
|
? xVal.toLocaleString([], { month: 'short', day: 'numeric', hour: '2-digit', minute: '2-digit', second: '2-digit' })
|
||||||
|
: typeof xVal === 'number' && xVal > 1e10
|
||||||
|
? new Date(xVal).toLocaleString([], { month: 'short', day: 'numeric', hour: '2-digit', minute: '2-digit', second: '2-digit' })
|
||||||
|
: String(xVal ?? '')
|
||||||
|
|
||||||
const values = series.map((s, i) => {
|
const values = series.map((s, i) => {
|
||||||
const idx = Math.round(pctX * (s.data.length - 1))
|
const idx = Math.round(pctX * (s.data.length - 1))
|
||||||
const clamped = Math.max(0, Math.min(s.data.length - 1, idx))
|
const clamped = Math.max(0, Math.min(s.data.length - 1, idx))
|
||||||
@@ -91,16 +101,17 @@ export function LineChart({
|
|||||||
}
|
}
|
||||||
})
|
})
|
||||||
|
|
||||||
setTooltip({ x: mx, y: my, values })
|
setTooltip({ x: mx, y: my, xLabel, values })
|
||||||
}
|
}
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<div className={`${styles.wrapper} ${className ?? ''}`}>
|
<div className={`${styles.wrapper} ${className ?? ''}`}>
|
||||||
{yLabel && <div className={styles.yLabel}>{yLabel}</div>}
|
{yLabel && <div className={styles.yLabel}>{yLabel}</div>}
|
||||||
<svg
|
<svg
|
||||||
width={width}
|
width="100%"
|
||||||
height={height}
|
height={height}
|
||||||
viewBox={`0 0 ${width} ${height}`}
|
viewBox={`0 0 ${width} ${height}`}
|
||||||
|
preserveAspectRatio="none"
|
||||||
className={styles.svg}
|
className={styles.svg}
|
||||||
onMouseMove={handleMouseMove}
|
onMouseMove={handleMouseMove}
|
||||||
onMouseLeave={() => setTooltip(null)}
|
onMouseLeave={() => setTooltip(null)}
|
||||||
@@ -202,6 +213,9 @@ export function LineChart({
|
|||||||
className={styles.tooltip}
|
className={styles.tooltip}
|
||||||
style={{ left: tooltip.x + 12, top: tooltip.y }}
|
style={{ left: tooltip.x + 12, top: tooltip.y }}
|
||||||
>
|
>
|
||||||
|
{tooltip.xLabel && (
|
||||||
|
<div className={styles.tooltipTime}>{tooltip.xLabel}</div>
|
||||||
|
)}
|
||||||
{tooltip.values.map((v) => (
|
{tooltip.values.map((v) => (
|
||||||
<div key={v.label} className={styles.tooltipRow}>
|
<div key={v.label} className={styles.tooltipRow}>
|
||||||
<span className={styles.tooltipDot} style={{ background: v.color }} />
|
<span className={styles.tooltipDot} style={{ background: v.color }} />
|
||||||
|
|||||||
Reference in New Issue
Block a user