Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
40 commits
Select commit Hold shift + click to select a range
281d399
Charts: Draw a stacked bar chart's comparison series as one total bar
chihsuan Nov 10, 2026
1c17394
Charts: Show the empty state and round both ends of stacked compariso…
chihsuan Nov 10, 2026
b81eb18
Premium Analytics: Add a stacked bar view to the Devices widget
chihsuan Nov 11, 2026
b055241
Premium Analytics: Name the Devices bar chart from the widget title
chihsuan Nov 11, 2026
d27e97e
Premium Analytics: Count devices from both periods and fail on respon…
chihsuan Nov 11, 2026
6b9a9e4
Merge remote-tracking branch 'origin/trunk' into change/charts-stacke…
chihsuan Nov 13, 2026
c8f6caf
Merge branch 'change/charts-stacked-comparison' into change/pa-device…
chihsuan Nov 13, 2026
f350e7f
Merge remote-tracking branch 'origin/trunk' into change/charts-stacke…
chihsuan Nov 23, 2026
1910b9e
Merge branch 'change/charts-stacked-comparison' into change/pa-device…
chihsuan Nov 23, 2026
85adc1c
Premium Analytics: Download the Devices bar chart's views per bucket …
chihsuan Nov 23, 2026
af03437
Premium Analytics: Read device counts from each bucket's views
chihsuan Nov 24, 2026
ebea9a3
Charts: Keep a stacked comparison item in step with its total bar
chihsuan Nov 25, 2026
60e3165
Charts: Cover the stacked comparison total's guards and colors in tests
chihsuan Nov 25, 2026
2425e91
Charts: Tighten the stacked comparison docs and share its comparison …
chihsuan Nov 25, 2026
2a22597
Charts: Name the stacked comparison story's series like the other com…
chihsuan Nov 25, 2026
b67beee
Charts: Report a stacked comparison total under a fixed key
chihsuan Nov 25, 2026
32b9a6b
Merge remote-tracking branch 'origin/change/charts-stacked-comparison…
chihsuan Nov 25, 2026
66bcd6a
Premium Analytics: Read the stacked previous-period total by the char…
chihsuan Nov 25, 2026
0d9987c
Charts: Color a stacked comparison total from a themeable catalog role
chihsuan Nov 25, 2026
df1cf58
Merge remote-tracking branch 'origin/change/charts-stacked-comparison…
chihsuan Nov 25, 2026
efbf510
Charts: Keep a stacked total whole and pattern stacks like their colors
chihsuan Nov 25, 2026
6f68231
Merge remote-tracking branch 'origin/change/charts-stacked-comparison…
chihsuan Nov 25, 2026
ef84edf
Merge remote-tracking branch 'origin/trunk' into change/charts-stacke…
chihsuan Nov 25, 2026
eca0a33
Merge remote-tracking branch 'origin/change/charts-stacked-comparison…
chihsuan Nov 25, 2026
240fe6b
Merge remote-tracking branch 'origin/trunk' into change/pa-devices-st…
chihsuan Nov 26, 2026
32dba7a
Premium Analytics: Treat a devices response without buckets as an unu…
chihsuan Nov 26, 2026
4c94920
Premium Analytics: Map the Devices bar chart's errors like the semi-c…
chihsuan Nov 26, 2026
305fc3a
Premium Analytics: Cover the Devices bar chart's guards and drop a du…
chihsuan Nov 26, 2026
148f9a2
Premium Analytics: Require a devices bucket the endpoint accepts
chihsuan Nov 26, 2026
a2d14cc
Premium Analytics: Note the stacked tooltip Total row in the changelog
chihsuan Nov 26, 2026
f6b38d3
Charts: Hide a bounded tooltip until it is placed for its current con…
chihsuan Nov 26, 2026
4232d18
Charts: Type a bounded tooltip's placement inputs as one tuple
chihsuan Nov 26, 2026
326bb7a
Premium Analytics: Narrow the devices periods query and drop dead guards
chihsuan Nov 26, 2026
824e764
Charts: Focus a keyboard tooltip once it is placed
chihsuan Nov 26, 2026
658b833
Charts: Round a tooltip pinned to an edge inward so it never scrolls …
chihsuan Nov 27, 2026
dc71ac4
Premium Analytics: Shorten tooltip dates in narrow cards and let only…
chihsuan Nov 27, 2026
e5d751a
Premium Analytics: Split the half-ring chart type icon into two segments
chihsuan Nov 27, 2026
ef2cbf7
Merge remote-tracking branch 'origin/trunk' into change/pa-devices-st…
chihsuan Nov 27, 2026
de6105e
Premium Analytics: Drop the CSV status field trunk removed from the D…
chihsuan Nov 27, 2026
a9f5868
Merge remote-tracking branch 'origin/trunk' into change/pa-devices-st…
chihsuan Nov 27, 2026
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions projects/js-packages/charts/changelog/pa-devices-stacked
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
Significance: patch
Type: fixed

Tooltips: Stop a tooltip at the edge of a scrolling card from adding scrollbars that resize the chart, as it appears, changes, or sits flush with the edge.
Original file line number Diff line number Diff line change
Expand Up @@ -67,12 +67,15 @@ export const usePieKeyboardNavigation = < Arc extends { data: DataPointPercentag
[ focusTooltip ]
);

// Arrow keys mount the next tooltip in the same commit, so focus only reaches the body when the selection's tooltip went away.
// Arrow keys mount the next tooltip in the same commit, and it takes focus once placed, so only a removed selection hands focus back.
useLayoutEffect( () => {
if ( ! tooltipLostFocus.current ) {
return;
}
tooltipLostFocus.current = false;
if ( keyboardTooltip.current?.isConnected ) {
return;
}
const { activeElement, body } = chartRef.current?.ownerDocument ?? document;
if ( activeElement === null || activeElement === body ) {
chartRef.current?.focus();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import type { TooltipBoxProps } from '../tooltip-box';
import type { ReactNode } from 'react';

type Bounds = { left: number; top: number; right: number; bottom: number };
type PlacementInputs = readonly [ number, number, number, number, ReactNode, TooltipPlacement ];
type Size = { width: number; height: number };

export type BoundedTooltipProps = Omit< TooltipBoxProps, 'children' > & {
Expand All @@ -23,8 +24,12 @@ export const DEFAULT_OFFSET = 10;
export const TOOLTIP_Z_INDEX = 3;
const POINTER_HEIGHT = 6;

// Whole pixels, rounded inward: rounding out by a fraction is enough to scroll a card the box sits flush in.
const clamp = ( position: number, min: number, max: number, size: number ) =>
Math.min( Math.max( position, min ), Math.max( min, max - size ) );
Math.min(
Math.max( Math.round( position ), Math.ceil( min ) ),
Math.max( Math.ceil( min ), Math.floor( max - size ) )
);

const isClipping = ( element: Element ) => {
const { overflow, overflowX, overflowY } = getComputedStyle( element );
Expand Down Expand Up @@ -97,7 +102,7 @@ export const getBoundedPosition = ( {
} ) => {
if ( placement === 'below-axis' ) {
return {
x: Math.round( clamp( left - box.width / 2, bounds.left, bounds.right, box.width ) ),
x: clamp( left - box.width / 2, bounds.left, bounds.right, box.width ),
y: top + POINTER_HEIGHT,
};
}
Expand All @@ -114,7 +119,7 @@ export const getBoundedPosition = ( {
const leftX = left - offsetLeft - box.width;
const rightOverflow = rightX + box.width - fit.right;
const leftOverflow = fit.left - leftX;
let x = rightOverflow > 0 && rightOverflow > leftOverflow ? leftX : rightX;
const x = rightOverflow > 0 && rightOverflow > leftOverflow ? leftX : rightX;

const downY = top + offsetTop;
const upY = top - offsetTop - box.height;
Expand All @@ -125,12 +130,9 @@ export const getBoundedPosition = ( {
y = downOverflow > 0 && downOverflow > upOverflow ? upY : downY;
}

x = clamp( x, bounds.left, bounds.right, box.width );
y = clamp( y, bounds.top, bounds.bottom, box.height );

return {
x: Math.round( x ),
y: Math.round( y ),
x: clamp( x, bounds.left, bounds.right, box.width ),
y: clamp( y, bounds.top, bounds.bottom, box.height ),
};
};

Expand Down Expand Up @@ -170,7 +172,17 @@ export const BoundedTooltip = ( {
}: BoundedTooltipProps ) => {
const nodeRef = useRef< HTMLDivElement >( null );
const clipRef = useRef< { wrapper: Element; clip: Element | null } | null >( null );
const [ position, setPosition ] = useState< { x: number; y: number } | null >( null );
const [ position, setPosition ] = useState< {
x: number;
y: number;
inputs: PlacementInputs;
} | null >( null );
const inputs: PlacementInputs = [ left, top, offsetLeft, offsetTop, children, placement ];
// A position fits only the content and anchor it was measured for; left in place, newer content can overflow a scrolling card.
const placed =
position && position.inputs.every( ( input, index ) => Object.is( input, inputs[ index ] ) )
? position
: null;

useLayoutEffect( () => {
const node = nodeRef.current;
Expand Down Expand Up @@ -204,14 +216,13 @@ export const BoundedTooltip = ( {
bottom: clipRect.bottom - wrapperRect.top,
},
} );
setPosition( current =>
current && current.x === next.x && current.y === next.y ? current : next
);
setPosition( { ...next, inputs: [ left, top, offsetLeft, offsetTop, children, placement ] } );
// `children` re-runs the measurement when the content, and so the box size, changes.
}, [ left, top, offsetLeft, offsetTop, children, placement ] );

const x = position?.x ?? left + offsetLeft;
const y = position?.y ?? top + ( placement === 'below-axis' ? POINTER_HEIGHT : offsetTop );
// Unplaced, the box waits hidden at the origin, which is also where the effect above measures it.
const x = placed?.x ?? 0;
const y = placed?.y ?? 0;

return (
<TooltipBox
Expand All @@ -225,6 +236,7 @@ export const BoundedTooltip = ( {
left: 0,
top: 0,
transform: `translate(${ x }px, ${ y }px)`,
...( ! placed && { visibility: 'hidden' } ),
...( ! unstyled && style ),
} }
{ ...rest }
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import { render, screen } from '@testing-library/react';
import { useLayoutEffect } from 'react';
import { BoundedTooltip, getBoundedPosition } from '../private/bounded-tooltip';
import type { BoundedTooltipProps } from '../private/bounded-tooltip';

const BOX = { width: 208, height: 36 };
const OFFSETS = { offsetLeft: 10, offsetTop: 10 };
Expand Down Expand Up @@ -63,6 +65,20 @@ describe( 'getBoundedPosition', () => {
).toEqual( { x: 138, y: 60 } );
} );

test( 'rounds a box pinned to a fractional edge inward, so it never pokes past it', () => {
// Rounded to the nearest pixel, 85.59 would end the box 0.41px past the edge and scroll the card.
expect(
getBoundedPosition( {
left: 100,
top: 50,
...OFFSETS,
box: { width: 302.59, height: 100 },
wrapper: { left: 0, top: 0, right: 388.18, bottom: 300 },
bounds: { left: 0, top: 0, right: 388.18, bottom: 300 },
} )
).toEqual( { x: 85, y: 60 } );
} );

test( 'may leave the wrapper as long as it stays inside the clipping ancestor', () => {
// Neither side fits the 346px wrapper, and the clipping card ends 14px
// past its right edge, so the box keeps the near side and stops there.
Expand Down Expand Up @@ -299,6 +315,53 @@ describe( 'BoundedTooltip', () => {
}
);

// A box left where older inputs fit can overflow a scrolling card and flash a scrollbar under the chart.
test.each( [
[ 'content', { children: 'November 18' }, 'translate(110px, 60px)' ],
[ 'left', { left: 140 }, 'translate(150px, 60px)' ],
[ 'top', { top: 70 }, 'translate(110px, 80px)' ],
[ 'offsetLeft', { offsetLeft: 20 }, 'translate(120px, 60px)' ],
[ 'offsetTop', { offsetTop: 20 }, 'translate(110px, 70px)' ],
[ 'placement', { placement: 'beside' as const }, 'translate(110px, 50px)' ],
] )(
'keeps the box hidden at the wrapper origin until it is placed for its current %s',
( _input, change, placedAt ) => {
const seen: string[] = [];
// Lays out before the box is placed, as another part's layout effect can.
const Probe = () => {
useLayoutEffect( () => {
const box = screen.getByTestId( 'box' );
seen.push( `${ box.style.transform } ${ box.style.visibility }` );
} );
return null;
};
jest.spyOn( Element.prototype, 'getBoundingClientRect' ).mockImplementation( function (
this: Element
) {
return this.classList.contains( 'visx-tooltip' )
? rect( 0, 0, 208, 36 )
: rect( 0, 0, 400, 200 );
} );
const tooltip = ( props: BoundedTooltipProps ) => (
<div style={ { overflow: 'auto' } }>
<div style={ { position: 'relative' } }>
<BoundedTooltip left={ 100 } top={ 50 } data-testid="box" { ...props }>
{ props.children ?? 'November 10' }
</BoundedTooltip>
<Probe />
</div>
</div>
);

const { rerender } = render( tooltip( {} ) );
rerender( tooltip( change ) );

expect( seen ).toEqual( [ 'translate(0px) hidden', 'translate(0px) hidden' ] );
expect( screen.getByTestId( 'box' ) ).toHaveStyle( { transform: placedAt } );
expect( screen.getByTestId( 'box' ) ).not.toHaveStyle( { visibility: 'hidden' } );
}
);

test( 'falls back to the viewport when nothing clips', () => {
jest.spyOn( Element.prototype, 'getBoundingClientRect' ).mockImplementation( function (
this: Element
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,32 @@ describe( 'XYChartTooltip', () => {
}
} );

// A browser refuses focus to a hidden box, and focusing it unplaced would scroll to the wrong spot.
it.each( [ 'auto', 'below-axis' ] as const )(
'focuses a %s keyboard tooltip only once its box is placed',
async placement => {
const hiddenOnFocus: boolean[] = [];
const recordVisibility = ( event: FocusEvent ) => {
if ( ( event.target as HTMLElement ).getAttribute( 'role' ) === 'tooltip' ) {
hiddenOnFocus.push(
screen.getByTestId( 'bounded-tooltip' ).style.visibility === 'hidden'
);
}
};
document.addEventListener( 'focusin', recordVisibility );
try {
renderChart( undefined, placement );
await openTooltip();
await userEvent.setup().keyboard( '{ArrowRight}' );

expect( screen.getByTestId( 'chart-tooltip-1' ) ).toHaveFocus();
expect( hiddenOnFocus ).toEqual( [ false, false ] );
} finally {
document.removeEventListener( 'focusin', recordVisibility );
}
}
);

it.each( [
[ 'Tab', '{Tab}' ],
[ 'Escape', '{Escape}' ],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -276,11 +276,16 @@ export const useKeyboardNavigation = ( {
[ preventTooltipScroll ]
);

// Focus the tooltip as soon as it is rendered
// A new tooltip is hidden, so unfocusable, until BoundedTooltip's layout effect places it.
// React commits that placement before the current commit returns, so a microtask sees the placed box.
const tooltipRef = useCallback(
( element: HTMLDivElement | null ) => {
if ( element && selectedIndex !== undefined ) {
focusWithoutScrollIfNeeded( element );
queueMicrotask( () => {
if ( element.isConnected ) {
focusWithoutScrollIfNeeded( element );
}
} );
}
},
[ focusWithoutScrollIfNeeded, selectedIndex ]
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
Significance: minor
Type: added

Devices: Add a bar chart of views by device over time, with the previous period's total beside each bar. Stacked bar tooltips, such as Requests by source, now end with a Total row. Chart tooltips in narrow cards now shorten their dates and stay inside the card instead of adding a scrollbar.
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,7 @@ export { useStatsUtm, type StatsUtmParams, type StatsUtmResponse } from './use-s
export { useStatsHighlights } from './use-stats-highlights';
export type { StatsHighlightsParams, StatsHighlightsResponse } from './use-stats-highlights';
export { useStatsTags, type StatsTagsParams, type StatsTagsResponse } from './use-stats-tags';
export { useStatsDevices } from './use-stats-devices';
export { useStatsDevices, useStatsDevicesPeriods } from './use-stats-devices';
export {
isEdgeBotRequestsRangeTooLong,
useStatsEdgeBotRequests,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,23 @@
* Internal dependencies
*/
import { mergeStatsDevicesComparisonRows } from '../processing/stats';
import { statsDevicesQuery, type StatsDeviceProperty } from '../queries/stats-devices-query';
import { createStatsListReportHook, splitStatsListOptions } from './use-stats-report';
import {
statsDevicesPeriodsQuery,
statsDevicesQuery,
type StatsDeviceProperty,
type StatsDevicesPeriodsParams,
} from '../queries/stats-devices-query';
import {
createStatsListReportHook,
splitStatsListOptions,
useStatsReport,
} from './use-stats-report';
import type { UseStatsOptions } from './use-stats-report';
import type {
StatsDevicesComparisonItem,
StatsDevicesItem,
StatsNormalizedReport,
StatsTimeSeriesReport,
} from '../processing/stats';
import type { StatsReportParams } from '../queries/stats-query';
import type { ReportParams } from '../utils/search';
Expand Down Expand Up @@ -37,3 +47,22 @@ export function useStatsDevices( params: StatsDevicesParams, options?: StatsDevi
options
);
}

/**
* Views per device value in each `period` bucket of the range, and of the comparison range when it is on.
*
* @param params - The report params, with the bucket size as `period`.
* @param options - Query options.
* @return The primary and comparison reports.
*/
export function useStatsDevicesPeriods(
params: ReportParams & Pick< StatsDevicesPeriodsParams, 'period' | 'deviceProperty' >,
options?: UseStatsOptions
) {
return useStatsReport< StatsDevicesPeriodsParams, StatsTimeSeriesReport >(
statsDevicesPeriodsQuery,
params,
[ 'stats', 'devices-periods', '__comparison__', 'disabled' ],
options
);
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ export {
} from './providers/period-change-signal';
export * from './hooks';
export { STATS_SITE_SUMMARY_FIELDS } from './queries/stats-site-query';
export { STATS_DEVICES_PERIODS, type StatsDevicesPeriod } from './queries/stats-devices-query';
export { ensureDashboardEntities } from './entities/dashboard-entities';
export { latestPostQuery, postContentQuery, postsContentQuery } from './queries/latest-post-query';
export type { LatestPost, LatestPostResponse } from './processing/latest-post';
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,9 @@
import { mergeStatsDevicesComparisonRows, sanitizeStatsDevicesResponse } from '..';
import {
mergeStatsDevicesComparisonRows,
sanitizeStatsDevicesPeriodsResponse,
sanitizeStatsDevicesResponse,
} from '..';
import { StatsResponseShapeError } from '../../../utils/api-error';
import { devicesEmptyFixture, devicesFixture } from '../__fixtures__/devices';
import type { StatsDevicesItem, StatsNormalizedReport } from '..';

Expand Down Expand Up @@ -75,3 +80,40 @@ describe( 'Stats devices normalizer', () => {
} );
} );
} );

describe( 'sanitizeStatsDevicesPeriodsResponse', () => {
// An endpoint that ignored `unit` must surface as an error, not as a range with no views.
it( 'rejects a response without per-period views', () => {
expect( () =>
sanitizeStatsDevicesPeriodsResponse( { top_values: { desktop: 62.3 } } )
).toThrow( StatsResponseShapeError );
} );

it( 'reads an empty days map as a range with no views', () => {
expect( sanitizeStatsDevicesPeriodsResponse( { unit: 'day', days: {} } ).data ).toEqual( [] );
} );

it( 'reads each bucket’s device counts from its views', () => {
const { summary, data } = sanitizeStatsDevicesPeriodsResponse( {
unit: 'year',
days: {
'2026-01-01': {
views: { desktop: 10, tablet: 5, mobile: 3, other: 2 },
other_views: 0,
total_views: 20,
},
},
} );

expect( summary ).toEqual( {
desktop: 10,
tablet: 5,
mobile: 3,
other: 2,
date_start: expect.any( String ),
date_end: expect.any( String ),
} );
expect( data ).toHaveLength( 1 );
expect( data[ 0 ] ).toMatchObject( { desktop: 10, tablet: 5, mobile: 3, other: 2 } );
} );
} );
Loading
Loading