diff --git a/.changeset/reverse-waffle-directions.md b/.changeset/reverse-waffle-directions.md new file mode 100644 index 00000000..73719628 --- /dev/null +++ b/.changeset/reverse-waffle-directions.md @@ -0,0 +1,5 @@ +--- +'@tanstack/charts': minor +--- + +Add independent reverseX and reverseY options to waffleX and waffleY, preserving the default fill directions and source identity. diff --git a/docs/reference/marks/waffle.md b/docs/reference/marks/waffle.md index a19504bc..f1bb97b4 100644 --- a/docs/reference/marks/waffle.md +++ b/docs/reference/marks/waffle.md @@ -29,6 +29,16 @@ Both marks are also exported from `@tanstack/charts` and left-to-right, then bottom-to-top. `waffleX` transposes the contract: `x` contains the contribution and units advance bottom-to-top, then left-to-right. +Set `reverseY: true` on either mark to fill from top to bottom. Set +`reverseX: true` to fill from right to left. The options are independent and +mirror partial cells as well as complete cells, without changing source order, +color, keys, or quantitative values. Both default to `false`. + +```ts +const topDown = waffleY(rows, { y: 'share', columns: 10, reverseY: true }) +const rightToLeft = waffleX(rows, { x: 'share', rows: 10, reverseX: true }) +``` + ```ts function waffleY( source: Iterable, @@ -55,6 +65,8 @@ motion fields. `WaffleYOptions` adds `y` and `columns`; | `round` | `boolean` | `false` | Round cumulative unit boundaries before allocating cells | | `columns` | `number` | Responsive | Fixed cells per row for `waffleY` | | `rows` | `number` | Responsive | Fixed cells per column for `waffleX` | +| `reverseX` | `boolean` | `false` | Mirror horizontal cell positions | +| `reverseY` | `boolean` | `false` | Mirror vertical cell positions | | `gap` | `number` | `1` | Empty pixels between complete cells | | `radius` | `number` | None | Corner radius for complete cells | | `id` | `string` | Layer-derived | Stable mark ID | diff --git a/packages/charts-core/docs/reference/marks/waffle.md b/packages/charts-core/docs/reference/marks/waffle.md index a19504bc..f1bb97b4 100644 --- a/packages/charts-core/docs/reference/marks/waffle.md +++ b/packages/charts-core/docs/reference/marks/waffle.md @@ -29,6 +29,16 @@ Both marks are also exported from `@tanstack/charts` and left-to-right, then bottom-to-top. `waffleX` transposes the contract: `x` contains the contribution and units advance bottom-to-top, then left-to-right. +Set `reverseY: true` on either mark to fill from top to bottom. Set +`reverseX: true` to fill from right to left. The options are independent and +mirror partial cells as well as complete cells, without changing source order, +color, keys, or quantitative values. Both default to `false`. + +```ts +const topDown = waffleY(rows, { y: 'share', columns: 10, reverseY: true }) +const rightToLeft = waffleX(rows, { x: 'share', rows: 10, reverseX: true }) +``` + ```ts function waffleY( source: Iterable, @@ -55,6 +65,8 @@ motion fields. `WaffleYOptions` adds `y` and `columns`; | `round` | `boolean` | `false` | Round cumulative unit boundaries before allocating cells | | `columns` | `number` | Responsive | Fixed cells per row for `waffleY` | | `rows` | `number` | Responsive | Fixed cells per column for `waffleX` | +| `reverseX` | `boolean` | `false` | Mirror horizontal cell positions | +| `reverseY` | `boolean` | `false` | Mirror vertical cell positions | | `gap` | `number` | `1` | Empty pixels between complete cells | | `radius` | `number` | None | Corner radius for complete cells | | `id` | `string` | Layer-derived | Stable mark ID | diff --git a/packages/charts-core/src/waffle.test.ts b/packages/charts-core/src/waffle.test.ts index 303817c4..b009ff96 100644 --- a/packages/charts-core/src/waffle.test.ts +++ b/packages/charts-core/src/waffle.test.ts @@ -10,6 +10,112 @@ import { waffleX, waffleY } from './waffle' import type { ChartScene, SceneNode, SceneRect } from './types' describe('waffle marks', () => { + it.each([ + ['y', false], + ['x', false], + ['y', true], + ['x', true], + ] as const)( + 'reverses %s packing independently with responsive=%s', + (orientation, responsive) => { + const data = [ + { id: 'a', value: 0.4 }, + { id: 'b', value: 2.6 }, + { id: 'c', value: 1.5 }, + ] + const render = (reverseX = false, reverseY = false) => { + const common = { + key: 'id' as const, + color: 'id' as const, + gap: 2, + radius: 3, + reverseX, + reverseY, + } + return createChartScene( + defineChart({ + marks: [ + orientation === 'y' + ? waffleY(data, { + ...common, + y: 'value', + ...(responsive ? {} : { columns: 3 }), + }) + : waffleX(data, { + ...common, + x: 'value', + ...(responsive ? {} : { rows: 3 }), + }), + ], + scales: { x: null, y: null }, + guides: false, + focusRing: false, + margin: { top: 13, right: 19, bottom: 23, left: 17 }, + color: { legend: colorLegend({ label: 'Category' }) }, + }), + { width: 240, height: 180 }, + ) + } + const initial = render() + const original = sceneRects(initial) + for (const [reverseX, reverseY] of [ + [true, false], + [false, true], + [true, true], + ]) { + const reversed = render(reverseX, reverseY) + const rectangles = sceneRects(reversed) + expect(rectangles.map((rect) => rect.key)).toEqual( + original.map((rect) => rect.key), + ) + expect(reversed.colors.domain).toEqual(initial.colors.domain) + for (const [index, rect] of rectangles.entries()) { + const before = original[index]! + expect(rect.x).toBeCloseTo( + reverseX + ? 2 * initial.chart.x + + initial.chart.width - + before.x - + before.width + : before.x, + ) + expect(rect.y).toBeCloseTo( + reverseY + ? 2 * initial.chart.y + + initial.chart.height - + before.y - + before.height + : before.y, + ) + expect(rect.width).toBe(before.width) + expect(rect.height).toBe(before.height) + expect(rect.radius).toBe(before.radius) + expect(rect.interaction?.point?.datum).toBe( + before.interaction?.point?.datum, + ) + expect(contains(reversed, rect)).toBe(true) + } + for (const [index, point] of reversed.points.entries()) { + const before = initial.points[index]! + expect(point.key).toBe(before.key) + expect(point.datum).toBe(before.datum) + expect(point.xValue).toBe(before.xValue) + expect(point.yValue).toBe(before.yValue) + expect(point.x).toBeCloseTo( + reverseX + ? 2 * initial.chart.x + initial.chart.width - before.x + : before.x, + ) + expect(point.y).toBeCloseTo( + reverseY + ? 2 * initial.chart.y + initial.chart.height - before.y + : before.y, + ) + } + } + }, + ) + it('rounds cumulative boundaries and preserves source-level identity', () => { const rows = [ { id: 'a', value: 0.126, series: 'A' }, diff --git a/packages/charts-core/src/waffle.ts b/packages/charts-core/src/waffle.ts index 887fed9e..cfdafa4f 100644 --- a/packages/charts-core/src/waffle.ts +++ b/packages/charts-core/src/waffle.ts @@ -34,6 +34,10 @@ export interface WaffleOptions extends ChartMarkMotionOptions { unit?: number /** Rounds cumulative unit boundaries before allocating cells. */ round?: boolean + /** Mirror horizontal cell positions, including partial cells. Defaults to false. */ + reverseX?: boolean + /** Mirror vertical cell positions, including partial cells. Defaults to false. */ + reverseY?: boolean /** Empty pixels between complete cells. Defaults to 1. */ gap?: number radius?: number @@ -168,6 +172,8 @@ function waffle( orientation, fixedMultiple, gap, + options.reverseX ?? false, + options.reverseY ?? false, ) return { @@ -324,6 +330,8 @@ function layoutWaffle( orientation: WaffleOrientation, fixedMultiple: number | undefined, gap: number, + reverseX: boolean, + reverseY: boolean, ): readonly WaffleLayoutSegment[] { const cellCount = Math.ceil(segments.at(-1)?.endUnit ?? 0) if (cellCount <= 0) return [] @@ -360,7 +368,7 @@ function layoutWaffle( const cellY = gridY + (rows - row - 1) * cellSize + inset const complete = start === 0 && end === 1 - fragments.push( + const fragment: WaffleFragment = orientation === 'y' ? { x: cellX, @@ -375,8 +383,12 @@ function layoutWaffle( width: (end - start) * paintSize, height: paintSize, complete, - }, - ) + } + if (reverseX) + fragment.x = 2 * gridX + gridWidth - fragment.x - fragment.width + if (reverseY) + fragment.y = 2 * gridY + gridHeight - fragment.y - fragment.height + fragments.push(fragment) } return { ...segment, fragments }