Skip to content

Commit d91e0a0

Browse files
Merge pull request #224 from TanStack/taren/sunburst-path-projection
perf: skip sunburst boundary sampling during animation
2 parents 5a34b7e + e9e894f commit d91e0a0

8 files changed

Lines changed: 93 additions & 39 deletions

File tree

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'@tanstack/charts': patch
3+
---
4+
5+
Avoid rebuilding sunburst hit-test boundaries when projecting animation frames.

‎benchmarks/comparison/bundle-baseline.json‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"schemaVersion": 4,
3-
"generatedAt": "2026-10-13T23:35:26.997Z",
3+
"generatedAt": "2026-10-14T02:03:21.704Z",
44
"packageVersions": {
55
"tanstack": "1.1.0",
66
"chartjs": "4.5.1",
@@ -11,8 +11,8 @@
1111
"sources": {
1212
"tanstack": {
1313
"kind": "workspace",
14-
"revision": "a691036b07655adda97aa0dc3052428989e2b811",
15-
"inputDigest": "sha256:390854b69e6ac7c4453ba8bcc75dfd5240f70bbd9f69efdc8d25c7fba528d951"
14+
"revision": "cd2a8a7fcab201ca32db60ca6d6c8e92c4dd47b8",
15+
"inputDigest": "sha256:6c5dc96470c53874d724ca231e9e5a552800d04834e584260cc2eac4b504974e"
1616
},
1717
"chartjs": {
1818
"kind": "package",

‎benchmarks/conformance/previews/manifest.json‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
"schemaVersion": 1,
33
"width": 288,
44
"height": 192,
5-
"sourceHash": "82eecd6c1fbdca5e857b965e09bb53c550520b2e596fbc4e9850266f03fdb9b4",
5+
"sourceHash": "9bbb0210cda37cf492ff11d793f0e23d493f9aebc849609518fa4abc1ce7a26a",
66
"assets": [
77
{
88
"id": "01-line-gaps",

‎docs/comparison.md‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,14 +12,14 @@ turning untested behavior into a checkmark.
1212

1313
| Library | Package | Measured source |
1414
| -------------------------------------------------------------------------------------- | -------------------- | ------------------- |
15-
| [TanStack Charts](./overview.md) | `@tanstack/charts` | workspace `a691036` |
15+
| [TanStack Charts](./overview.md) | `@tanstack/charts` | workspace `cd2a8a7` |
1616
| [Chart.js](https://www.chartjs.org/docs/latest/) | `chart.js` | npm `4.5.1` |
1717
| [Apache ECharts](https://echarts.apache.org/handbook/en/best-practices/canvas-vs-svg/) | `echarts` | npm `6.1.0` |
1818
| [Recharts](https://recharts.github.io/en-US/) | `recharts` | npm `3.10.1` |
1919
| [Observable Plot](https://observablehq.com/plot/features/plots) | `@observablehq/plot` | npm `0.6.17` |
2020

2121
The competitor versions are exact package pins, not latest versions inferred
22-
at page render time. The measured TanStack workspace revision is `a691036`.
22+
at page render time. The measured TanStack workspace revision is `cd2a8a7`.
2323

2424
## Capability matrix
2525

@@ -90,7 +90,7 @@ output model.
9090

9191
## Bundle snapshot
9292

93-
Baseline date: `2026-10-13`.
93+
Baseline date: `2026-10-14`.
9494

9595
Controlled ranges cover 12 independently built, minified browser consumers:
9696
line, bar, area, and scatter at basic, interactive, and advanced tiers. Only

‎packages/charts-core/docs/comparison.md‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,14 +12,14 @@ turning untested behavior into a checkmark.
1212

1313
| Library | Package | Measured source |
1414
| -------------------------------------------------------------------------------------- | -------------------- | ------------------- |
15-
| [TanStack Charts](./overview.md) | `@tanstack/charts` | workspace `a691036` |
15+
| [TanStack Charts](./overview.md) | `@tanstack/charts` | workspace `cd2a8a7` |
1616
| [Chart.js](https://www.chartjs.org/docs/latest/) | `chart.js` | npm `4.5.1` |
1717
| [Apache ECharts](https://echarts.apache.org/handbook/en/best-practices/canvas-vs-svg/) | `echarts` | npm `6.1.0` |
1818
| [Recharts](https://recharts.github.io/en-US/) | `recharts` | npm `3.10.1` |
1919
| [Observable Plot](https://observablehq.com/plot/features/plots) | `@observablehq/plot` | npm `0.6.17` |
2020

2121
The competitor versions are exact package pins, not latest versions inferred
22-
at page render time. The measured TanStack workspace revision is `a691036`.
22+
at page render time. The measured TanStack workspace revision is `cd2a8a7`.
2323

2424
## Capability matrix
2525

@@ -90,7 +90,7 @@ output model.
9090

9191
## Bundle snapshot
9292

93-
Baseline date: `2026-10-13`.
93+
Baseline date: `2026-10-14`.
9494

9595
Controlled ranges cover 12 independently built, minified browser consumers:
9696
line, bar, area, and scatter at basic, interactive, and advanced tiers. Only

‎packages/charts-core/src/hierarchy-sunburst.ts‎

Lines changed: 7 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ import {
1010
} from './hierarchy-flat-internal'
1111
import { channelValues, isChartKey, isFiniteNumber, visualValue } from './mark'
1212
import { createPolarMark } from './polar-mark-internal'
13-
import { resolvePolarSector } from './polar-sector-internal'
13+
import { projectPolarSector, resolvePolarSector } from './polar-sector-internal'
1414
import {
1515
sceneMotionNode,
1616
type SceneMotionMetadata,
@@ -340,22 +340,13 @@ export function sunburst<TDatum>(
340340
}
341341

342342
function projectSunburstSectorPath(values: readonly number[]) {
343-
const [startAngle, endAngle, innerRadius, outerRadius] = values
344-
if (
345-
startAngle === undefined ||
346-
endAngle === undefined ||
347-
innerRadius === undefined ||
348-
outerRadius === undefined
349-
) {
350-
return undefined
351-
}
352-
return resolvePolarSector({
353-
startAngle,
354-
endAngle,
355-
innerRadius,
356-
outerRadius,
343+
return projectPolarSector({
344+
startAngle: values[0]!,
345+
endAngle: values[1]!,
346+
innerRadius: values[2]!,
347+
outerRadius: values[3]!,
357348
cornerRadius: 0,
358-
})?.path
349+
})
359350
}
360351

361352
function sunburstNodeContext<TDatum>(
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
import { describe, expect, it, vi } from 'vitest'
2+
import { projectPolarSector, resolvePolarSector } from './polar-sector-internal'
3+
4+
describe('polar sector projection', () => {
5+
it.each([
6+
[0, 2, 20, 60, 0],
7+
[2, -3, 80, 40, 8],
8+
[0, Math.PI * 2, 0, 80, 6],
9+
[0, 0.012, 60, 80, 6],
10+
])(
11+
'keeps the rendered path without sampling its boundary: %j',
12+
(...values) => {
13+
const [startAngle, endAngle, innerRadius, outerRadius, cornerRadius] =
14+
values
15+
const sector = {
16+
startAngle: startAngle!,
17+
endAngle: endAngle!,
18+
innerRadius: innerRadius!,
19+
outerRadius: outerRadius!,
20+
cornerRadius: cornerRadius!,
21+
}
22+
const cosine = vi.spyOn(Math, 'cos')
23+
try {
24+
const projected = projectPolarSector(sector)
25+
const projectionCalls = cosine.mock.calls.length
26+
cosine.mockClear()
27+
const resolved = resolvePolarSector(sector)
28+
expect(resolved?.path).toBe(projected)
29+
expect(resolved!.points.length).toBeGreaterThan(0)
30+
expect(cosine.mock.calls.length).toBeGreaterThan(projectionCalls)
31+
expect(projectPolarSector(sector)).toBe(projected)
32+
} finally {
33+
cosine.mockRestore()
34+
}
35+
},
36+
)
37+
38+
it.each([
39+
'startAngle',
40+
'endAngle',
41+
'innerRadius',
42+
'outerRadius',
43+
'cornerRadius',
44+
] as const)('still rejects nonfinite %s', (field) => {
45+
const sector = {
46+
startAngle: 0,
47+
endAngle: 2,
48+
innerRadius: 20,
49+
outerRadius: 60,
50+
cornerRadius: 0,
51+
[field]: Number.NaN,
52+
}
53+
expect(projectPolarSector(sector)).toBeUndefined()
54+
expect(resolvePolarSector(sector)).toBeUndefined()
55+
})
56+
})

‎packages/charts-core/src/polar-sector-internal.ts‎

Lines changed: 15 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -19,10 +19,7 @@ export interface ResolvedPolarSector {
1919

2020
let polarSectorArc: Arc<any, PolarSectorGeometry> | undefined
2121

22-
/** Resolves one D3 sector and samples its painted interaction boundary. */
23-
export function resolvePolarSector(
24-
sector: PolarSectorGeometry,
25-
): ResolvedPolarSector | undefined {
22+
export function projectPolarSector(sector: PolarSectorGeometry) {
2623
if (
2724
!isFiniteNumber(sector.startAngle) ||
2825
!isFiniteNumber(sector.endAngle) ||
@@ -32,17 +29,22 @@ export function resolvePolarSector(
3229
) {
3330
return undefined
3431
}
35-
const generator = (polarSectorArc ??= createArc<PolarSectorGeometry>()
36-
.startAngle((value) => value.startAngle)
37-
.endAngle((value) => value.endAngle)
38-
.innerRadius((value) => value.innerRadius)
39-
.outerRadius((value) => value.outerRadius)
40-
.cornerRadius((value) => value.cornerRadius))
41-
const path = generator(sector)
42-
if (typeof path !== 'string' || !path) return undefined
32+
const generator = (polarSectorArc ??=
33+
createArc<PolarSectorGeometry>().cornerRadius(
34+
(value) => value.cornerRadius,
35+
))
36+
return generator(sector) || undefined
37+
}
38+
39+
/** Resolves one D3 sector and samples its painted interaction boundary. */
40+
export function resolvePolarSector(
41+
sector: PolarSectorGeometry,
42+
): ResolvedPolarSector | undefined {
43+
const path = projectPolarSector(sector)
44+
if (!path) return undefined
4345
return {
4446
path,
45-
points: tracePolarArcBoundary(generator, sector, 0, [sector]),
47+
points: tracePolarArcBoundary(polarSectorArc!, sector, 0, [sector]),
4648
}
4749
}
4850

0 commit comments

Comments
 (0)