Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
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
5 changes: 5 additions & 0 deletions .changeset/reverse-waffle-directions.md
Original file line number Diff line number Diff line change
@@ -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.
12 changes: 12 additions & 0 deletions docs/reference/marks/waffle.md
Original file line number Diff line number Diff line change
Expand Up @@ -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<TDatum>(
source: Iterable<TDatum>,
Expand All @@ -55,6 +65,8 @@ motion fields. `WaffleYOptions<TDatum>` 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 |
Expand Down
12 changes: 12 additions & 0 deletions packages/charts-core/docs/reference/marks/waffle.md
Original file line number Diff line number Diff line change
Expand Up @@ -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<TDatum>(
source: Iterable<TDatum>,
Expand All @@ -55,6 +65,8 @@ motion fields. `WaffleYOptions<TDatum>` 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 |
Expand Down
106 changes: 106 additions & 0 deletions packages/charts-core/src/waffle.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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' },
Expand Down
18 changes: 15 additions & 3 deletions packages/charts-core/src/waffle.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,10 @@ export interface WaffleOptions<TDatum> extends ChartMarkMotionOptions<TDatum> {
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
Expand Down Expand Up @@ -168,6 +172,8 @@ function waffle<TDatum>(
orientation,
fixedMultiple,
gap,
options.reverseX ?? false,
options.reverseY ?? false,
)

return {
Expand Down Expand Up @@ -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 []
Expand Down Expand Up @@ -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,
Expand All @@ -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 }
Expand Down
Loading