@@ -18,12 +18,44 @@ import CardHead from './CardHead';
1818import type { CardExtras } from './types' ;
1919import { cardEdges , type Fit , useFittedRows , useFontsReady } from './useFittedRows' ;
2020
21+ import { gridCell } from '@/hooks/useGridCell' ;
2122import type { ChartWidget } from '@/lib/chat/widgets' ;
2223import { chartAlt } from '@/lib/studio/chartAlt' ;
23- import { CARD_BORDERS , fittedRows , plotHeight , plotShape } from '@/lib/studio/chartFit' ;
24+ import { CARD_BORDERS , fittedRows , plotHeight , plotRatio , plotShape } from '@/lib/studio/chartFit' ;
2425import { legendLabels } from '@/lib/studio/seriesLabels' ;
2526import type { ThemeDerived } from '@/lib/studio/theme' ;
2627
28+ // The card's chrome in px (its padding, borders, row gaps and every child
29+ // but the plot, each of which keeps its own height whatever the card's) and
30+ // the width the plot gets at `widthCells`. The used heights come from
31+ // computed style: fractional px, where offsetHeight rounds to whole px and
32+ // getBoundingClientRect takes in the lift transform. The borders count as a
33+ // share of the cell (CARD_BORDERS), so the figures scale with it.
34+ const cardMetrics = (
35+ card : HTMLElement ,
36+ plot : HTMLElement | null ,
37+ widthCells : number ,
38+ cell : number ,
39+ ) : { chrome : number ; plotWidth : number } => {
40+ const cs = getComputedStyle ( card ) ;
41+ const borders = CARD_BORDERS * cell ;
42+ let chrome =
43+ cardEdges ( cs , 'vertical' ) . padding +
44+ borders +
45+ ( parseFloat ( cs . rowGap ) || 0 ) * ( card . children . length - 1 ) ;
46+
47+ for ( const child of card . children ) {
48+ if ( child !== plot ) {
49+ chrome += parseFloat ( getComputedStyle ( child ) . height ) || 0 ;
50+ }
51+ }
52+
53+ return {
54+ chrome,
55+ plotWidth : widthCells * cell - cardEdges ( cs , 'horizontal' ) . padding - borders ,
56+ } ;
57+ } ;
58+
2759// A bar or line chart (Chart.js) with a clickable legend; `scale` is the
2860// fluid grid factor — Chart.js px must ride it like everything else.
2961//
@@ -33,6 +65,12 @@ import type { ThemeDerived } from '@/lib/studio/theme';
3365// (`heightCells`) is kept while it fits. The plot then takes what the card
3466// leaves it, so where the card is not given that height (an embed, the
3567// landing page) the chart draws in the box it has.
68+ //
69+ // A height the sheet gives the card that it did not ask for is one the user
70+ // dragged (the grip is free in a ratio view, layout's heightIsFree). The card
71+ // turns it into the plot ratio it stands for and reports it through
72+ // `onRatio`; `ratio` is that ratio once the parent holds it, and every fit
73+ // from then on (a new width, a reopened conversation, a share) keeps it.
3674const ChartCard = ( {
3775 widget,
3876 line,
@@ -41,6 +79,8 @@ const ChartCard = ({
4179 widthCells,
4280 heightCells,
4381 onFit,
82+ ratio,
83+ onRatio,
4484 ...extras
4585} : {
4686 widget : ChartWidget ;
@@ -50,11 +90,13 @@ const ChartCard = ({
5090 widthCells : number ;
5191 heightCells ?: number ;
5292 onFit : ( rows : number ) => void ;
93+ ratio ?: number ;
94+ onRatio ?: ( ratio : number ) => void ;
5395} & CardExtras ) => {
5496 const cardRef = useRef < HTMLElement > ( null ) ;
5597 const plotRef = useRef < HTMLDivElement > ( null ) ;
5698
57- const shape = useMemo ( ( ) => plotShape ( widget , ! ! line ) , [ widget , line ] ) ;
99+ const shape = useMemo ( ( ) => plotShape ( widget , ! ! line , ratio ) , [ widget , line , ratio ] ) ;
58100 const rows = shape . kind === 'rows' ;
59101 const fontsReady = useFontsReady ( ) ;
60102
@@ -85,24 +127,7 @@ const ChartCard = ({
85127 return null ;
86128 }
87129
88- const cs = getComputedStyle ( card ) ;
89- const borders = CARD_BORDERS * cell ;
90- let chrome =
91- cardEdges ( cs , 'vertical' ) . padding +
92- borders +
93- ( parseFloat ( cs . rowGap ) || 0 ) * ( card . children . length - 1 ) ;
94-
95- // The used height from computed style: fractional px, where
96- // offsetHeight rounds to whole px and getBoundingClientRect takes
97- // in the lift transform. Every child but the plot keeps its own
98- // height whatever the card's.
99- for ( const child of card . children ) {
100- if ( child !== plot ) {
101- chrome += parseFloat ( getComputedStyle ( child ) . height ) || 0 ;
102- }
103- }
104-
105- const plotWidth = widthCells * cell - cardEdges ( cs , 'horizontal' ) . padding - borders ;
130+ const { chrome, plotWidth } = cardMetrics ( card , plot , widthCells , cell ) ;
106131 const needed = plotHeight ( shape , { plotWidth, band : layout ?. band ?? 0 , cell } ) ;
107132 const sameWidth =
108133 measuredWidthRef . current == null || measuredWidthRef . current === widthCells ;
@@ -129,7 +154,46 @@ const ChartCard = ({
129154 ( ) => ( { widget, widthCells, title : extras . title , fontsReady } ) ,
130155 [ widget , widthCells , extras . title , fontsReady ] ,
131156 ) ;
132- const onLayout = useFittedRows ( { cardRef, widthCells, token, measure, onFit } ) ;
157+ // The rows the card last asked for, so a height it did not ask for can
158+ // be told from its own fit's answer (clamped or not).
159+ const requestedRef = useRef < number | null > ( null ) ;
160+ const fit = useCallback (
161+ ( cells : number ) => {
162+ requestedRef . current = cells ;
163+ onFit ( cells ) ;
164+ } ,
165+ [ onFit ] ,
166+ ) ;
167+ const onLayout = useFittedRows ( { cardRef, widthCells, token, measure, onFit : fit } ) ;
168+
169+ // A height the user dragged becomes the card's ratio. Only once the card
170+ // has fitted (the spawned or restored height is not the user's), only at
171+ // the width of that fit (a new width's height is the fit's to answer),
172+ // and only where the plot holds a ratio.
173+ useEffect ( ( ) => {
174+ const card = cardRef . current ;
175+
176+ if (
177+ ! card ||
178+ heightCells == null ||
179+ requestedRef . current == null ||
180+ heightCells === requestedRef . current ||
181+ measuredWidthRef . current !== widthCells ||
182+ shape . kind !== 'ratio'
183+ ) {
184+ return ;
185+ }
186+
187+ requestedRef . current = heightCells ;
188+
189+ const cell = gridCell ( ) ;
190+ const { chrome, plotWidth } = cardMetrics ( card , plotRef . current , widthCells , cell ) ;
191+ const next = plotRatio ( { heightCells, chrome, plotWidth, cell } ) ;
192+
193+ if ( next != null && next !== shape . ratio ) {
194+ onRatio ?.( next ) ;
195+ }
196+ } , [ heightCells , widthCells , shape , onRatio ] ) ;
133197
134198 // Legend clicks park series here — a hidden series greys its legend
135199 // entry and drops out of the plot (the scale re-fits live).
0 commit comments