@automattic/charts 1.0.2 → 1.1.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/AGENTS.md +14 -1
- package/CHANGELOG.md +18 -0
- package/dist/{chunk-G3PMV62Z.js → chunk-5WRI5ZAA.js} +1 -6
- package/dist/{chunk-EMMSS5I5.cjs → chunk-DZUJEN5N.cjs} +2 -7
- package/dist/chunk-DZUJEN5N.cjs.map +1 -0
- package/dist/index.cjs +1571 -2377
- package/dist/index.cjs.map +1 -1
- package/dist/index.css +31 -107
- package/dist/index.css.map +1 -1
- package/dist/index.d.cts +2 -0
- package/dist/index.d.ts +2 -0
- package/dist/index.js +1722 -2528
- package/dist/index.js.map +1 -1
- package/dist/visx/group/index.cjs +1 -1
- package/dist/visx/group/index.js +1 -1
- package/dist/visx/legend/index.cjs +1 -1
- package/dist/visx/legend/index.js +1 -1
- package/dist/visx/text/index.cjs +1 -1
- package/dist/visx/text/index.js +1 -1
- package/package.json +9 -8
- package/src/charts/conversion-funnel-chart/conversion-funnel-chart.module.scss +19 -47
- package/src/charts/conversion-funnel-chart/conversion-funnel-chart.tsx +17 -10
- package/src/charts/geo-chart/geo-chart.module.scss +0 -3
- package/src/charts/geo-chart/geo-chart.tsx +9 -4
- package/src/charts/leaderboard-chart/leaderboard-chart.module.scss +1 -1
- package/src/charts/leaderboard-chart/leaderboard-chart.tsx +2 -2
- package/src/charts/line-chart/line-chart.module.scss +3 -13
- package/src/charts/line-chart/line-chart.tsx +9 -2
- package/src/charts/line-chart/private/line-chart-annotation-label-popover.tsx +5 -4
- package/src/charts/pie-chart/pie-chart.tsx +5 -3
- package/src/charts/pie-semi-circle-chart/pie-semi-circle-chart.module.scss +3 -3
- package/src/components/legend/private/base-legend.module.scss +2 -51
- package/src/components/legend/private/base-legend.tsx +30 -28
- package/src/components/tooltip/base-tooltip.module.scss +1 -1
- package/src/components/trend-indicator/trend-indicator.module.scss +2 -2
- package/src/hooks/use-chart-margin.tsx +1 -14
- package/src/providers/chart-context/themes.ts +7 -1
- package/src/types.ts +2 -0
- package/src/utils/index.ts +3 -0
- package/src/utils/resolve-font-size.ts +37 -0
- package/src/utils/test/resolve-css-var.test.ts +3 -5
- package/src/utils/test/resolve-font-size.test.ts +66 -0
- package/dist/chunk-EMMSS5I5.cjs.map +0 -1
- /package/dist/{chunk-G3PMV62Z.js.map → chunk-5WRI5ZAA.js.map} +0 -0
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
import { Group } from '@visx/group';
|
|
2
2
|
import { LegendItem, LegendLabel, LegendOrdinal, LegendShape } from '@visx/legend';
|
|
3
3
|
import { scaleOrdinal } from '@visx/scale';
|
|
4
|
+
import { Stack } from '@wordpress/ui';
|
|
4
5
|
import clsx from 'clsx';
|
|
5
6
|
import {
|
|
6
7
|
type RefAttributes,
|
|
@@ -16,10 +17,11 @@ import { valueOrIdentity, valueOrIdentityString, labelTransformFactory } from '.
|
|
|
16
17
|
import styles from './base-legend.module.scss';
|
|
17
18
|
import type { BaseLegendProps } from '../types';
|
|
18
19
|
|
|
19
|
-
const
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
20
|
+
const ALIGNMENT_TO_FLEX = {
|
|
21
|
+
start: 'flex-start',
|
|
22
|
+
center: 'center',
|
|
23
|
+
end: 'flex-end',
|
|
24
|
+
} as const;
|
|
23
25
|
|
|
24
26
|
// Component for legend text with truncation detection
|
|
25
27
|
// Moved outside BaseLegend to prevent recreation on every render
|
|
@@ -159,6 +161,8 @@ export const BaseLegend: ForwardRefExoticComponent<
|
|
|
159
161
|
[ interactive, handleLegendClick ]
|
|
160
162
|
);
|
|
161
163
|
|
|
164
|
+
const flexAlignment = ALIGNMENT_TO_FLEX[ alignment ] ?? 'center';
|
|
165
|
+
|
|
162
166
|
return render ? (
|
|
163
167
|
render( items )
|
|
164
168
|
) : (
|
|
@@ -168,20 +172,17 @@ export const BaseLegend: ForwardRefExoticComponent<
|
|
|
168
172
|
labelTransform={ labelTransform }
|
|
169
173
|
>
|
|
170
174
|
{ labels => (
|
|
171
|
-
<
|
|
175
|
+
<Stack
|
|
172
176
|
ref={ ref }
|
|
177
|
+
direction={ orientation === 'vertical' ? 'column' : 'row' }
|
|
178
|
+
gap={ orientation === 'vertical' ? 'sm' : 'lg' }
|
|
179
|
+
align={ orientation === 'vertical' ? flexAlignment : undefined }
|
|
180
|
+
justify={ orientation === 'horizontal' ? flexAlignment : undefined }
|
|
181
|
+
wrap={ orientation === 'horizontal' ? 'wrap' : undefined }
|
|
173
182
|
role="list"
|
|
174
183
|
data-testid={ `legend-${ orientation }` }
|
|
175
|
-
className={ clsx(
|
|
176
|
-
|
|
177
|
-
styles[ `legend--${ orientation }` ],
|
|
178
|
-
styles[ `legend--alignment-${ alignment }` ],
|
|
179
|
-
className
|
|
180
|
-
) }
|
|
181
|
-
style={ {
|
|
182
|
-
flexDirection: orientationToFlexDirection[ orientation ],
|
|
183
|
-
...theme.legend?.containerStyles,
|
|
184
|
-
} }
|
|
184
|
+
className={ clsx( styles.legend, className ) }
|
|
185
|
+
style={ theme.legend?.containerStyles }
|
|
185
186
|
>
|
|
186
187
|
{ labels.map( ( label, i ) => {
|
|
187
188
|
const visible = isSeriesVisible( label.text );
|
|
@@ -257,28 +258,29 @@ export const BaseLegend: ForwardRefExoticComponent<
|
|
|
257
258
|
labelClassName
|
|
258
259
|
) }
|
|
259
260
|
style={ {
|
|
260
|
-
justifyContent: labelJustifyContent,
|
|
261
261
|
flex: labelFlex,
|
|
262
262
|
margin: labelMargin,
|
|
263
263
|
...theme.legend?.labelStyles,
|
|
264
264
|
} }
|
|
265
265
|
>
|
|
266
|
-
<
|
|
267
|
-
|
|
268
|
-
|
|
269
|
-
|
|
270
|
-
|
|
271
|
-
|
|
272
|
-
|
|
273
|
-
{ '
|
|
274
|
-
|
|
275
|
-
|
|
276
|
-
|
|
266
|
+
<Stack align="center" gap="sm" justify={ labelJustifyContent }>
|
|
267
|
+
<LegendText
|
|
268
|
+
text={ label.text }
|
|
269
|
+
textOverflow={ textOverflow }
|
|
270
|
+
maxWidth={ maxWidth }
|
|
271
|
+
/>
|
|
272
|
+
{ matchedItem?.value != null && matchedItem.value !== '' && (
|
|
273
|
+
<span className={ styles[ 'legend-item-value' ] }>
|
|
274
|
+
{ '\u00A0' }
|
|
275
|
+
{ matchedItem.value }
|
|
276
|
+
</span>
|
|
277
|
+
) }
|
|
278
|
+
</Stack>
|
|
277
279
|
</LegendLabel>
|
|
278
280
|
</LegendItem>
|
|
279
281
|
);
|
|
280
282
|
} ) }
|
|
281
|
-
</
|
|
283
|
+
</Stack>
|
|
282
284
|
) }
|
|
283
285
|
</LegendOrdinal>
|
|
284
286
|
);
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { createScale, getTicks } from '@visx/scale';
|
|
2
2
|
import { useMemo } from 'react';
|
|
3
|
-
import { getLongestTickWidth } from '../utils';
|
|
3
|
+
import { getLongestTickWidth, resolveFontSize } from '../utils';
|
|
4
4
|
import type { BaseChartProps, DataPointDate, SeriesData } from '../types';
|
|
5
5
|
import type { XYChartTheme } from '@visx/xychart';
|
|
6
6
|
|
|
@@ -50,19 +50,6 @@ const DEFAULT_TICK_LENGTH = 8;
|
|
|
50
50
|
*/
|
|
51
51
|
const DEFAULT_Y_TICK_WIDTH = 40;
|
|
52
52
|
|
|
53
|
-
const resolveFontSize = ( val?: number | string ): number | undefined => {
|
|
54
|
-
if ( typeof val === 'number' && ! isNaN( val ) ) {
|
|
55
|
-
return val;
|
|
56
|
-
}
|
|
57
|
-
|
|
58
|
-
if ( typeof val === 'string' ) {
|
|
59
|
-
const parsed = parseFloat( val );
|
|
60
|
-
return isNaN( parsed ) ? undefined : parsed;
|
|
61
|
-
}
|
|
62
|
-
|
|
63
|
-
return undefined;
|
|
64
|
-
};
|
|
65
|
-
|
|
66
53
|
const getXAxisLabelMetrics = ( theme: XYChartTheme, orientation: 'top' | 'bottom' ) => {
|
|
67
54
|
const xAxisStyles =
|
|
68
55
|
orientation === 'top' ? theme.axisStyles?.x?.top : theme.axisStyles?.x?.bottom;
|
|
@@ -26,7 +26,13 @@ const defaultTheme: CompleteChartTheme = {
|
|
|
26
26
|
},
|
|
27
27
|
seriesLineStyles: [],
|
|
28
28
|
glyphs: [],
|
|
29
|
-
|
|
29
|
+
// `fontFamily: 'inherit'` overrides visx's hardcoded default font stack
|
|
30
|
+
// (`-apple-system,BlinkMacSystemFont,Roboto,Helvetica Neue,sans-serif`)
|
|
31
|
+
// that `buildChartTheme` injects as an inline style on SVG `<text>`
|
|
32
|
+
// elements for axis labels and ticks. Setting `inherit` lets SVG text
|
|
33
|
+
// pick up the host application's font-family via normal CSS inheritance.
|
|
34
|
+
svgLabelSmall: { fill: 'var(--jp-gray-80, #2c3338)', fontFamily: 'inherit' },
|
|
35
|
+
svgLabelBig: { fontFamily: 'inherit' },
|
|
30
36
|
annotationStyles: {
|
|
31
37
|
label: {
|
|
32
38
|
anchorLineStroke: 'var(--jp-gray-80, #2c3338)',
|
package/src/types.ts
CHANGED
|
@@ -243,6 +243,8 @@ export type ChartTheme = {
|
|
|
243
243
|
};
|
|
244
244
|
/** Styles for small SVG text (eg. axis tick labels), passed through to the XYChart theme. */
|
|
245
245
|
svgLabelSmall?: TextProps;
|
|
246
|
+
/** Styles for large SVG text (eg. axis titles), passed through to the XYChart theme. */
|
|
247
|
+
svgLabelBig?: TextProps;
|
|
246
248
|
annotationStyles?: AnnotationStyles;
|
|
247
249
|
/** GeoChart specific settings */
|
|
248
250
|
geoChart?: {
|
package/src/utils/index.ts
CHANGED
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Resolve a theme `fontSize` value into a plain number suitable for
|
|
3
|
+
* canvas-based measurement (e.g. `getStringWidth`).
|
|
4
|
+
*
|
|
5
|
+
* Accepts:
|
|
6
|
+
* - A number — returned as-is
|
|
7
|
+
* - A pixel string like `"12px"` — parsed and returned as a number
|
|
8
|
+
*
|
|
9
|
+
* Returns `undefined` for any other input (missing value, NaN, or
|
|
10
|
+
* relative units like `rem`/`em`/`%`/`vh`) so callers can fall back to
|
|
11
|
+
* their own default. Relative units are intentionally rejected because
|
|
12
|
+
* we cannot resolve them to absolute pixels here without a parent
|
|
13
|
+
* computed style, and silently returning the unitless prefix
|
|
14
|
+
* (`parseFloat("0.875rem") === 0.875`) would produce nearly-zero
|
|
15
|
+
* widths in measurement code.
|
|
16
|
+
* @param val - Raw font size value from a theme, axis style, or props
|
|
17
|
+
* @return Parsed numeric font size in pixels, or `undefined` when unresolvable
|
|
18
|
+
*/
|
|
19
|
+
export const resolveFontSize = ( val?: number | string ): number | undefined => {
|
|
20
|
+
if ( typeof val === 'number' ) {
|
|
21
|
+
return isNaN( val ) ? undefined : val;
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
if ( typeof val === 'string' ) {
|
|
25
|
+
// Only accept plain numeric strings or pixel values.
|
|
26
|
+
// Reject rem, em, %, vh, vw, etc. — they cannot be resolved to
|
|
27
|
+
// absolute pixels without a computed style context.
|
|
28
|
+
const match = val.trim().match( /^(-?\d+\.?\d*|-?\.\d+)(px)?$/ );
|
|
29
|
+
if ( ! match ) {
|
|
30
|
+
return undefined;
|
|
31
|
+
}
|
|
32
|
+
const parsed = parseFloat( match[ 1 ] );
|
|
33
|
+
return isNaN( parsed ) ? undefined : parsed;
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
return undefined;
|
|
37
|
+
};
|
|
@@ -414,9 +414,8 @@ describe( 'resolveCssVariable', () => {
|
|
|
414
414
|
if ( element === themedElement ) {
|
|
415
415
|
return {
|
|
416
416
|
getPropertyValue: ( prop: string ) => {
|
|
417
|
-
|
|
418
|
-
|
|
419
|
-
return '#c029dc'; // User's custom accent color
|
|
417
|
+
if ( prop === '--custom-accent-color' ) {
|
|
418
|
+
return '#c029dc';
|
|
420
419
|
}
|
|
421
420
|
return '';
|
|
422
421
|
},
|
|
@@ -428,8 +427,7 @@ describe( 'resolveCssVariable', () => {
|
|
|
428
427
|
} );
|
|
429
428
|
window.getComputedStyle = mockGetComputedStyle as unknown as typeof window.getComputedStyle;
|
|
430
429
|
|
|
431
|
-
|
|
432
|
-
const result = resolveCssVariable( '--wpds-color-bg-interactive-brand-weak', themedElement );
|
|
430
|
+
const result = resolveCssVariable( '--custom-accent-color', themedElement );
|
|
433
431
|
expect( result ).toBe( '#c029dc' );
|
|
434
432
|
} );
|
|
435
433
|
} );
|
|
@@ -0,0 +1,66 @@
|
|
|
1
|
+
import { resolveFontSize } from '../resolve-font-size';
|
|
2
|
+
|
|
3
|
+
describe( 'resolveFontSize', () => {
|
|
4
|
+
describe( 'numeric input', () => {
|
|
5
|
+
it( 'returns whole numbers as-is', () => {
|
|
6
|
+
expect( resolveFontSize( 12 ) ).toBe( 12 );
|
|
7
|
+
expect( resolveFontSize( 0 ) ).toBe( 0 );
|
|
8
|
+
} );
|
|
9
|
+
|
|
10
|
+
it( 'returns decimal numbers as-is', () => {
|
|
11
|
+
expect( resolveFontSize( 13.5 ) ).toBe( 13.5 );
|
|
12
|
+
} );
|
|
13
|
+
|
|
14
|
+
it( 'returns undefined for NaN', () => {
|
|
15
|
+
expect( resolveFontSize( NaN ) ).toBeUndefined();
|
|
16
|
+
} );
|
|
17
|
+
} );
|
|
18
|
+
|
|
19
|
+
describe( 'string input', () => {
|
|
20
|
+
it( 'parses bare numeric strings', () => {
|
|
21
|
+
expect( resolveFontSize( '12' ) ).toBe( 12 );
|
|
22
|
+
expect( resolveFontSize( '13.5' ) ).toBe( 13.5 );
|
|
23
|
+
} );
|
|
24
|
+
|
|
25
|
+
it( 'parses pixel strings', () => {
|
|
26
|
+
expect( resolveFontSize( '12px' ) ).toBe( 12 );
|
|
27
|
+
expect( resolveFontSize( '13.5px' ) ).toBe( 13.5 );
|
|
28
|
+
} );
|
|
29
|
+
|
|
30
|
+
it( 'trims whitespace before parsing', () => {
|
|
31
|
+
expect( resolveFontSize( ' 14px ' ) ).toBe( 14 );
|
|
32
|
+
} );
|
|
33
|
+
|
|
34
|
+
it( 'rejects rem values rather than returning the unitless prefix', () => {
|
|
35
|
+
expect( resolveFontSize( '0.875rem' ) ).toBeUndefined();
|
|
36
|
+
} );
|
|
37
|
+
|
|
38
|
+
it( 'rejects em, %, vh, vw and other relative units', () => {
|
|
39
|
+
expect( resolveFontSize( '1em' ) ).toBeUndefined();
|
|
40
|
+
expect( resolveFontSize( '100%' ) ).toBeUndefined();
|
|
41
|
+
expect( resolveFontSize( '2vh' ) ).toBeUndefined();
|
|
42
|
+
expect( resolveFontSize( '2vw' ) ).toBeUndefined();
|
|
43
|
+
} );
|
|
44
|
+
|
|
45
|
+
it( 'rejects keywords like inherit, initial, unset', () => {
|
|
46
|
+
expect( resolveFontSize( 'inherit' ) ).toBeUndefined();
|
|
47
|
+
expect( resolveFontSize( 'initial' ) ).toBeUndefined();
|
|
48
|
+
expect( resolveFontSize( 'medium' ) ).toBeUndefined();
|
|
49
|
+
} );
|
|
50
|
+
|
|
51
|
+
it( 'returns undefined for empty string', () => {
|
|
52
|
+
expect( resolveFontSize( '' ) ).toBeUndefined();
|
|
53
|
+
} );
|
|
54
|
+
|
|
55
|
+
it( 'returns undefined for unparseable strings', () => {
|
|
56
|
+
expect( resolveFontSize( 'abc' ) ).toBeUndefined();
|
|
57
|
+
expect( resolveFontSize( 'pxpx' ) ).toBeUndefined();
|
|
58
|
+
} );
|
|
59
|
+
} );
|
|
60
|
+
|
|
61
|
+
describe( 'missing input', () => {
|
|
62
|
+
it( 'returns undefined for undefined', () => {
|
|
63
|
+
expect( resolveFontSize( undefined ) ).toBeUndefined();
|
|
64
|
+
} );
|
|
65
|
+
} );
|
|
66
|
+
} );
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"sources":["/home/runner/work/jetpack/jetpack/projects/js-packages/charts/dist/chunk-EMMSS5I5.cjs"],"names":[],"mappings":"AAAA,6EAAI,SAAS,EAAE,MAAM,CAAC,MAAM;AAC5B,IAAI,UAAU,EAAE,MAA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