Skip to content

Commit 4f77757

Browse files
committed
[charts] expose usePolarGeometry hook with polar geometry helpers
1 parent 53c0b03 commit 4f77757

8 files changed

Lines changed: 30 additions & 25 deletions

File tree

docs/data/charts/radial-bars/trustRadialBarChartComponents.js

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import Stack from '@mui/material/Stack';
55
import Typography from '@mui/material/Typography';
66
import { ChartsTooltipContainer, useAxesTooltip } from '@mui/x-charts/ChartsTooltip';
77
import { usePolarGeometry } from '@mui/x-charts/hooks';
8+
89
import {
910
euAverageTrust2025,
1011
europeanYouthTrust,
@@ -84,7 +85,9 @@ export function PreviousTrustData({ currentColor, previousColor }) {
8485
return null;
8586
}
8687

87-
const { cx, cy, angleScale, bandwidth, radiusScale, point } = geometry;
88+
const { cx, cy, bandwidth, point } = geometry;
89+
const angleScale = geometry.angleScale;
90+
const radiusScale = geometry.radiusScale;
8891

8992
return (
9093
<g transform={`translate(${cx} ${cy})`}>
@@ -233,7 +236,8 @@ export function EuAverageRing() {
233236
return null;
234237
}
235238

236-
const { cx, cy, radiusScale } = geometry;
239+
const { cx, cy } = geometry;
240+
const radiusScale = geometry.radiusScale;
237241
const radius = radiusScale(euAverageTrust2025);
238242

239243
return (

docs/data/charts/radial-bars/trustRadialBarChartComponents.tsx

Lines changed: 6 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import Stack from '@mui/material/Stack';
55
import Typography from '@mui/material/Typography';
66
import { ChartsTooltipContainer, useAxesTooltip } from '@mui/x-charts/ChartsTooltip';
77
import { usePolarGeometry } from '@mui/x-charts/hooks';
8+
import type { D3OrdinalScale, D3ContinuousScale } from '@mui/x-charts/models';
89
import {
910
euAverageTrust2025,
1011
europeanYouthTrust,
@@ -92,7 +93,9 @@ export function PreviousTrustData({
9293
return null;
9394
}
9495

95-
const { cx, cy, angleScale, bandwidth, radiusScale, point } = geometry;
96+
const { cx, cy, bandwidth, point } = geometry;
97+
const angleScale = geometry.angleScale as D3OrdinalScale;
98+
const radiusScale = geometry.radiusScale as D3ContinuousScale;
9699

97100
return (
98101
<g transform={`translate(${cx} ${cy})`}>
@@ -243,7 +246,8 @@ export function EuAverageRing() {
243246
return null;
244247
}
245248

246-
const { cx, cy, radiusScale } = geometry;
249+
const { cx, cy } = geometry;
250+
const radiusScale = geometry.radiusScale as D3ContinuousScale;
247251
const radius = radiusScale(euAverageTrust2025);
248252

249253
return (
@@ -269,13 +273,3 @@ export function EuAverageRing() {
269273
</g>
270274
);
271275
}
272-
273-
export interface PolarGeometry {
274-
cx: number;
275-
cy: number;
276-
angleScale: (value: string) => number | undefined;
277-
bandwidth: number;
278-
radiusScale: (value: number) => number;
279-
// Polar (0 = up, clockwise) to local cartesian, relative to the center.
280-
point: (radius: number, angle: number) => readonly [number, number];
281-
}

packages/x-charts/src/hooks/usePolarGeometry.test.tsx

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -42,17 +42,6 @@ function makeWrapper(props: Partial<typeof defaultProps> = {}) {
4242
const options: any = { wrapper: makeWrapper() };
4343

4444
describe('usePolarGeometry', () => {
45-
// useDrawingArea() runs before the rotation/radius axis null-check inside
46-
// the hook, and it throws (via useChartsContext) when there's no
47-
// ChartsDataProvider/ChartsContainer ancestor at all — so the hook's own
48-
// `if (!rotationAxis || !radiusAxis) return null` branch is unreachable
49-
// outside *any* chart context. Confirmed by an actual test run.
50-
it('should throw when rendered outside of a charts context', () => {
51-
expect(() => {
52-
renderHook(() => usePolarGeometry());
53-
}).to.throw(/Could not find the Charts context/);
54-
});
55-
5645
it('should compute the chart center from the real drawing area', () => {
5746
const { result } = renderHook(
5847
() => ({

packages/x-charts/src/hooks/usePolarGeometry.tsx

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ export function usePolarGeometry(): PolarGeometry | null {
3434
cx: left + width / 2,
3535
cy: top + height / 2,
3636
angleScale,
37+
bandwidth: 'bandwidth' in angleScale ? angleScale.bandwidth() : 0,
3738
radiusScale,
3839
point: (radius, angle) => [radius * Math.sin(angle), -radius * Math.cos(angle)],
3940
pointInverse: (x, y) => [Math.sqrt(x * x + y * y), Math.atan2(x, -y)],
@@ -56,6 +57,11 @@ export interface PolarGeometry<
5657
* The scale that maps rotation axis values to angles in radians.
5758
*/
5859
angleScale: TAngleScale;
60+
/**
61+
* The angular width of each band on the rotation axis.
62+
* Zero when the rotation axis uses a point scale instead of a band scale.
63+
*/
64+
bandwidth: number;
5965
/**
6066
* The scale that maps data values to radii (distance from the chart center).
6167
*/

packages/x-charts/src/models/index.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,9 @@ export type {
1616
RotationAxis,
1717
AxisItemIdentifier,
1818
AxisValueFormatterContext,
19+
D3Scale,
20+
D3OrdinalScale,
21+
D3ContinuousScale,
1922
} from './axis';
2023
export type { NumberValue } from '@mui/x-charts-vendor/d3-scale';
2124

scripts/x-charts-premium.exports.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -282,7 +282,10 @@
282282
{ "name": "cyanPalette", "kind": "Variable" },
283283
{ "name": "cyanPaletteDark", "kind": "Variable" },
284284
{ "name": "cyanPaletteLight", "kind": "Variable" },
285+
{ "name": "D3ContinuousScale", "kind": "TypeAlias" },
285286
{ "name": "D3NamedProjection", "kind": "TypeAlias" },
287+
{ "name": "D3OrdinalScale", "kind": "TypeAlias" },
288+
{ "name": "D3Scale", "kind": "TypeAlias" },
286289
{ "name": "DEFAULT_AXIS_SIZE_HEIGHT", "kind": "Variable" },
287290
{ "name": "DEFAULT_AXIS_SIZE_WIDTH", "kind": "Variable" },
288291
{ "name": "DEFAULT_MARGINS", "kind": "Variable" },

scripts/x-charts-pro.exports.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -262,6 +262,9 @@
262262
{ "name": "cyanPalette", "kind": "Variable" },
263263
{ "name": "cyanPaletteDark", "kind": "Variable" },
264264
{ "name": "cyanPaletteLight", "kind": "Variable" },
265+
{ "name": "D3ContinuousScale", "kind": "TypeAlias" },
266+
{ "name": "D3OrdinalScale", "kind": "TypeAlias" },
267+
{ "name": "D3Scale", "kind": "TypeAlias" },
265268
{ "name": "DEFAULT_AXIS_SIZE_HEIGHT", "kind": "Variable" },
266269
{ "name": "DEFAULT_AXIS_SIZE_WIDTH", "kind": "Variable" },
267270
{ "name": "DEFAULT_MARGINS", "kind": "Variable" },

scripts/x-charts.exports.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -235,6 +235,9 @@
235235
{ "name": "cyanPalette", "kind": "Variable" },
236236
{ "name": "cyanPaletteDark", "kind": "Variable" },
237237
{ "name": "cyanPaletteLight", "kind": "Variable" },
238+
{ "name": "D3ContinuousScale", "kind": "TypeAlias" },
239+
{ "name": "D3OrdinalScale", "kind": "TypeAlias" },
240+
{ "name": "D3Scale", "kind": "TypeAlias" },
238241
{ "name": "DEFAULT_AXIS_SIZE_HEIGHT", "kind": "Variable" },
239242
{ "name": "DEFAULT_AXIS_SIZE_WIDTH", "kind": "Variable" },
240243
{ "name": "DEFAULT_MARGINS", "kind": "Variable" },

0 commit comments

Comments
 (0)