Skip to content

Commit 67b0d02

Browse files
Merge pull request #107 from DevelopmentFinanceObservatory/feature/resize-charts
Feature/resize charts
2 parents decb285 + 0730741 commit 67b0d02

17 files changed

Lines changed: 279 additions & 44 deletions

File tree

‎src/components/organisms/EmbedView/EmbedView.tsx‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,7 @@ const SingleCard = ({
7676
// here the card grows by CSS and the frame scrolls (.singleText).
7777
widthCells={saved.layout?.[widget.id]?.w ?? 0}
7878
onFit={noop}
79+
ratio={saved.ratios?.[widget.id]}
7980
extras={extras}
8081
/>
8182
);
@@ -144,6 +145,8 @@ const EmbedView = ({ shared }: { shared: SharedView }) => {
144145
theme={theme}
145146
views={saved.views ?? {}}
146147
onViewChange={noop}
148+
ratios={saved.ratios ?? {}}
149+
onRatioChange={noop}
147150
onCardInfo={setInfoCardId}
148151
focusedId={infoCardId}
149152
readOnly

‎src/components/organisms/Studio/Studio.tsx‎

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -714,6 +714,8 @@ const Studio = ({
714714
theme={derivedTheme}
715715
views={doc.views}
716716
onViewChange={doc.setView}
717+
ratios={doc.ratios}
718+
onRatioChange={doc.setRatio}
717719
onShare={
718720
readOnly
719721
? undefined
@@ -726,6 +728,11 @@ const Studio = ({
726728
}
727729
onDownload={setDownloadCardId}
728730
readOnly={readOnly}
731+
// A viewer may resize a share's cards for
732+
// themselves: the layout and ratios stay in this
733+
// page (a share has no conversation id, so
734+
// useConversationSave saves nothing).
735+
resizable={readOnly}
729736
centerSingle={shared?.kind === 'card'}
730737
live={liveCanvas}
731738
/>

‎src/components/organisms/Studio/canvas/Whiteboard.module.scss‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -139,7 +139,8 @@
139139

140140
// The resize grip: a corner glyph at the card's bottom-right, hover-only.
141141
// Dragging it resizes the card cell by cell within its kind's range. On a
142-
// chart view it changes the width alone (the height follows the chart).
142+
// table, a ranking or a text card it changes the width alone (the height
143+
// follows the rows); on a bar or line chart it sets the plot's ratio too.
143144
.resizeGrip {
144145
@include flex(row, center, center);
145146

‎src/components/organisms/Studio/canvas/Whiteboard.tsx‎

Lines changed: 28 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@ import {
1515
FILTER_KIND,
1616
GAP,
1717
heightIsDerived,
18+
heightIsFree,
19+
heightRange,
1820
resolveCardView,
1921
widgetTitle,
2022
} from './layout';
@@ -84,13 +86,21 @@ export type WhiteboardProps = {
8486
// the filters/tabs see the effective kind and it persists).
8587
views: Record<string, CardView>;
8688
onViewChange: (id: string, view: CardView) => void;
89+
// Per-card plot ratio set by dragging a chart card's height (owned by
90+
// the parent, so it persists and a share holds it).
91+
ratios: Record<string, number>;
92+
onRatioChange: (id: string, ratio: number) => void;
8793
// Card menu → Share (link / embed of that one card).
8894
onShare?: (id: string) => void;
8995
// Card download button → Download Data (that card's rows).
9096
onDownload?: (id: string) => void;
9197
// A shared or embedded sheet: cards can't be moved, resized, pinned or
9298
// acted on — panning, tooltips and legends still work.
9399
readOnly?: boolean;
100+
// A read-only sheet whose cards may still be resized (a share page):
101+
// the grip shows, and the sizes and ratios it sets are the viewer's
102+
// alone — a share has no conversation to save them to.
103+
resizable?: boolean;
94104
// A one-card share: the card sits in the middle of the view (re-centred
95105
// as the view resizes) instead of at its saved spot, and the sheet is a
96106
// still — no shockwaves.
@@ -129,9 +139,12 @@ const Whiteboard = ({
129139
theme,
130140
views,
131141
onViewChange,
142+
ratios,
143+
onRatioChange,
132144
onShare,
133145
onDownload,
134146
readOnly = false,
147+
resizable = false,
135148
centerSingle = false,
136149
live,
137150
}: WhiteboardProps) => {
@@ -424,9 +437,13 @@ const Whiteboard = ({
424437
chartSource,
425438
} = resolveCardView(widget, views[widget.id]);
426439
const isText = widget.kind === 'text';
427-
// A chart view's height follows its content, so its grip
428-
// changes the width alone.
429-
const widthOnly = heightIsDerived(widget, cardView);
440+
// A card's derived height follows its content, so its grip
441+
// changes the width alone — except a chart view at a ratio,
442+
// whose height the grip sets (the card turns it into its
443+
// ratio) within the view's range.
444+
const widthOnly =
445+
heightIsDerived(widget, cardView) && !heightIsFree(cardView, chartSource);
446+
const hRange = heightRange(widget, cardView);
430447
const dimmed = !!dimKind && effectiveKind(widget, views[widget.id]) !== dimKind;
431448
const extras: CardExtras = {
432449
pinned: pinnedIds.includes(widget.id),
@@ -551,14 +568,18 @@ const Whiteboard = ({
551568
onRemove={extras.onRemoveNote}
552569
/>
553570
)}
554-
{!isLeaving && !readOnly && !isText && (
571+
{!isLeaving && (!readOnly || resizable) && !isText && (
555572
<span
556573
className={classNames(
557574
styles.resizeGrip,
558575
widthOnly && styles.resizeGripWidth,
559576
)}
560577
data-nowave=""
561-
onPointerDown={resizing.startResize(widget.id, widthOnly)}
578+
onPointerDown={resizing.startResize(
579+
widget.id,
580+
widthOnly,
581+
hRange,
582+
)}
562583
onPointerMove={resizing.moveResize}
563584
onPointerUp={resizing.endResize}
564585
onPointerCancel={resizing.endResize}
@@ -577,6 +598,8 @@ const Whiteboard = ({
577598
widthCells={slot.w}
578599
heightCells={slot.h}
579600
onFit={(rows) => fitRows(widget.id, rows)}
601+
ratio={ratios[widget.id]}
602+
onRatio={(ratio) => onRatioChange(widget.id, ratio)}
580603
extras={extras}
581604
/>
582605
</div>

‎src/components/organisms/Studio/canvas/cards/CardBody.tsx‎

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,10 @@ type CardBodyProps = {
2121
// The card's current height in cells. A chart keeps it while it fits.
2222
heightCells?: number;
2323
onFit: (rows: number) => void;
24+
// A chart's plot ratio when the user has set one, and where a height
25+
// the user drags reports the ratio it stands for (see ChartCard).
26+
ratio?: number;
27+
onRatio?: (ratio: number) => void;
2428
extras: CardExtras;
2529
};
2630

@@ -39,6 +43,8 @@ const CardBody = ({
3943
widthCells,
4044
heightCells,
4145
onFit,
46+
ratio,
47+
onRatio,
4248
extras,
4349
}: CardBodyProps) => {
4450
// A live share's data card holds nothing to draw until its refresh
@@ -78,6 +84,8 @@ const CardBody = ({
7884
widthCells={widthCells}
7985
heightCells={heightCells}
8086
onFit={onFit}
87+
ratio={ratio}
88+
onRatio={onRatio}
8189
{...extras}
8290
/>
8391
);

‎src/components/organisms/Studio/canvas/cards/ChartCard.tsx‎

Lines changed: 85 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -18,12 +18,44 @@ import CardHead from './CardHead';
1818
import type { CardExtras } from './types';
1919
import { cardEdges, type Fit, useFittedRows, useFontsReady } from './useFittedRows';
2020

21+
import { gridCell } from '@/hooks/useGridCell';
2122
import type { ChartWidget } from '@/lib/chat/widgets';
2223
import { 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';
2425
import { legendLabels } from '@/lib/studio/seriesLabels';
2526
import 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.
3674
const 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).

‎src/components/organisms/Studio/canvas/layout.ts‎

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import {
66
type TableWidget,
77
type Widget,
88
} from '../../../../lib/chat/widgets.ts';
9-
import { estimatedRows, FITTED_ROWS_MAX } from '../../../../lib/studio/chartFit.ts';
9+
import { estimatedRows, FITTED_ROWS_MAX, plotShape } from '../../../../lib/studio/chartFit.ts';
1010
import {
1111
TABLE_WIDTH_DEFAULT,
1212
tableDisplay,
@@ -352,6 +352,17 @@ type ResolvedView = Pick<ReturnType<typeof resolveCardView>, 'view' | 'chartSour
352352
export const heightIsDerived = (widget: Widget, view: CardView | null): boolean =>
353353
widget.kind === 'text' || widget.kind === 'table' || view !== null;
354354

355+
// Whether the user may set the card's height by its grip: a chart view whose
356+
// plot holds a ratio (a line or vertical bars). The height is derived still,
357+
// but from the card's own ratio, which the dragged height sets (see
358+
// chartFit's plotRatio). A ranking's rows and a table's rows are the
359+
// content's, and a stat card's height is its kind's.
360+
export const heightIsFree = (view: CardView | null, chartSource: ChartWidget | null): boolean =>
361+
view !== null &&
362+
view !== 'chart-table' &&
363+
chartSource !== null &&
364+
plotShape(chartSource, view === 'chart-line').kind === 'ratio';
365+
355366
// The heights in cells a card may take in its current view, by the kind it
356367
// reads as. A chart shown as a table takes a table's floor, and a table shown
357368
// as a chart a chart's.

0 commit comments

Comments
 (0)