From 23c478f944007c33b36be674eaf51e100f20b87f Mon Sep 17 00:00:00 2001 From: Mike Bostock Date: Sat, 19 Oct 2024 17:33:41 -0700 Subject: [PATCH 1/2] trim waffle slivers --- src/marks/waffle.js | 44 ++++----- test/output/waffleHref.svg | 154 ++++++++++++++++---------------- test/output/waffleMultiple.svg | 12 +-- test/output/waffleRound.svg | 4 +- test/output/waffleShorthand.svg | 4 +- test/output/waffleStroke.svg | 4 +- 6 files changed, 111 insertions(+), 111 deletions(-) diff --git a/src/marks/waffle.js b/src/marks/waffle.js index 883a4a1b14..4a95d38ec6 100644 --- a/src/marks/waffle.js +++ b/src/marks/waffle.js @@ -156,28 +156,28 @@ function wafflePoints(i1, i2, columns) { if (i2 < i1) { return wafflePoints(i2, i1, columns); } - return [ - [0, Math.ceil(i1 / columns)], - [Math.floor(i1 % columns), Math.ceil(i1 / columns)], - [Math.floor(i1 % columns), Math.floor(i1 / columns) + (i1 % 1)], - [Math.ceil(i1 % columns), Math.floor(i1 / columns) + (i1 % 1)], - ...(i1 % columns > columns - 1 - ? [] - : [ - [Math.ceil(i1 % columns), Math.floor(i1 / columns)], - [columns, Math.floor(i1 / columns)] - ]), - [columns, Math.floor(i2 / columns)], - [Math.ceil(i2 % columns), Math.floor(i2 / columns)], - [Math.ceil(i2 % columns), Math.floor(i2 / columns) + (i2 % 1)], - [Math.floor(i2 % columns), Math.floor(i2 / columns) + (i2 % 1)], - ...(i2 % columns < 1 - ? [] - : [ - [Math.floor(i2 % columns), Math.ceil(i2 / columns)], - [0, Math.ceil(i2 / columns)] - ]) - ]; + const x1f = Math.floor(i1 % columns); + const x1c = Math.ceil(i1 % columns); + const x2f = Math.floor(i2 % columns); + const x2c = Math.ceil(i2 % columns); + const y1f = Math.floor(i1 / columns); + const y1c = Math.ceil(i1 / columns); + const y2f = Math.floor(i2 / columns); + const y2c = Math.ceil(i2 / columns); + const points = []; + if (y2c > y1c) points.push([0, y1c]); + points.push([x1f, y1c], [x1f, y1f + (i1 % 1)], [x1c, y1f + (i1 % 1)]); + if (!(i1 % columns > columns - 1)) { + points.push([x1c, y1f]); + if (y2f > y1f) points.push([columns, y1f]); + } + if (y2f > y1f) points.push([columns, y2f]); + points.push([x2c, y2f], [x2c, y2f + (i2 % 1)], [x2f, y2f + (i2 % 1)]); + if (!(i2 % columns < 1)) { + points.push([x2f, y2c]); + if (y2c > y1c) points.push([0, y2c]); + } + return points; } function maybeRound(round) { diff --git a/test/output/waffleHref.svg b/test/output/waffleHref.svg index b8b5e3ad46..2d304de127 100644 --- a/test/output/waffleHref.svg +++ b/test/output/waffleHref.svg @@ -265,159 +265,159 @@ - waffle 0 + waffle 0 - waffle 1 + waffle 1 - waffle 2 + waffle 2 - waffle 3 + waffle 3 - waffle 4 + waffle 4 - waffle 5 + waffle 5 - waffle 6 + waffle 6 - waffle 7 + waffle 7 - waffle 8 + waffle 8 - waffle 9 + waffle 9 - waffle 10 + waffle 10 - waffle 11 + waffle 11 - waffle 12 + waffle 12 - waffle 13 + waffle 13 - waffle 14 + waffle 14 - waffle 15 + waffle 15 - waffle 16 + waffle 16 - waffle 17 + waffle 17 - waffle 18 + waffle 18 - waffle 19 + waffle 19 - waffle 20 + waffle 20 - waffle 21 + waffle 21 - waffle 22 + waffle 22 - waffle 23 + waffle 23 - waffle 24 + waffle 24 - waffle 25 + waffle 25 - waffle 26 + waffle 26 - waffle 27 + waffle 27 - waffle 28 + waffle 28 - waffle 29 + waffle 29 - waffle 30 + waffle 30 - waffle 31 + waffle 31 - waffle 32 + waffle 32 - waffle 33 + waffle 33 - waffle 34 + waffle 34 - waffle 35 + waffle 35 - waffle 36 + waffle 36 - waffle 37 + waffle 37 - waffle 38 + waffle 38 - waffle 39 + waffle 39 - waffle 40 + waffle 40 - waffle 41 + waffle 41 - waffle 42 + waffle 42 - waffle 43 + waffle 43 - waffle 44 + waffle 44 - waffle 45 + waffle 45 - waffle 46 + waffle 46 - waffle 47 + waffle 47 - waffle 48 + waffle 48 - waffle 49 + waffle 49 - waffle 50 + waffle 50 - waffle 51 + waffle 51 - waffle 52 + waffle 52 - waffle 53 + waffle 53 - waffle 54 + waffle 54 - waffle 55 + waffle 55 - waffle 56 + waffle 56 - waffle 57 + waffle 57 - waffle 58 + waffle 58 - waffle 59 + waffle 59 - waffle 60 + waffle 60 - waffle 61 + waffle 61 - waffle 62 + waffle 62 - waffle 63 + waffle 63 - waffle 64 + waffle 64 - waffle 65 + waffle 65 - waffle 66 + waffle 66 - waffle 67 + waffle 67 - waffle 68 + waffle 68 - waffle 69 + waffle 69 - waffle 70 + waffle 70 - waffle 71 + waffle 71 - waffle 72 + waffle 72 - waffle 73 + waffle 73 - waffle 74 + waffle 74 - waffle 75 + waffle 75 - waffle 76 + waffle 76 \ No newline at end of file diff --git a/test/output/waffleMultiple.svg b/test/output/waffleMultiple.svg index 4884b906c3..ea367932f7 100644 --- a/test/output/waffleMultiple.svg +++ b/test/output/waffleMultiple.svg @@ -66,12 +66,12 @@ - - + + - + @@ -92,11 +92,11 @@ - - + + - + \ No newline at end of file diff --git a/test/output/waffleRound.svg b/test/output/waffleRound.svg index 8dd553d469..d9f48ecc15 100644 --- a/test/output/waffleRound.svg +++ b/test/output/waffleRound.svg @@ -66,7 +66,7 @@ - + @@ -92,7 +92,7 @@ - + diff --git a/test/output/waffleShorthand.svg b/test/output/waffleShorthand.svg index 2c942bbba9..5b5b97656b 100644 --- a/test/output/waffleShorthand.svg +++ b/test/output/waffleShorthand.svg @@ -66,7 +66,7 @@ - + @@ -92,7 +92,7 @@ - + diff --git a/test/output/waffleStroke.svg b/test/output/waffleStroke.svg index ff8bc9a54a..2ade48d2f0 100644 --- a/test/output/waffleStroke.svg +++ b/test/output/waffleStroke.svg @@ -66,7 +66,7 @@ - + @@ -92,7 +92,7 @@ - + From 4dd2e21222b77dac48c4be6e136a442772b8d756 Mon Sep 17 00:00:00 2001 From: Mike Bostock Date: Sun, 20 Oct 2024 17:27:20 -0700 Subject: [PATCH 2/2] add another test --- test/output/waffleShapes.svg | 202 +++++++++++++++++++++++++++++++++++ test/plots/waffle.ts | 38 +++++++ 2 files changed, 240 insertions(+) create mode 100644 test/output/waffleShapes.svg diff --git a/test/output/waffleShapes.svg b/test/output/waffleShapes.svg new file mode 100644 index 0000000000..c1e66aeb8a --- /dev/null +++ b/test/output/waffleShapes.svg @@ -0,0 +1,202 @@ + + + + + 0 + 10 + 20 + 30 + 40 + 50 + 60 + 70 + 80 + 90 + 100 + 110 + 120 + 130 + 140 + 150 + 160 + 170 + 180 + 190 + 200 + 210 + 220 + 230 + 240 + 250 + 260 + 270 + 280 + 290 + 300 + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/test/plots/waffle.ts b/test/plots/waffle.ts index fdfa98a025..d5fe683bdf 100644 --- a/test/plots/waffle.ts +++ b/test/plots/waffle.ts @@ -264,3 +264,41 @@ export function waffleHref() { ] }); } + +export function waffleShapes() { + const k = 10; + let offset = 0; + const waffle = (y1, y2) => { + y1 += offset; + y2 += offset; + offset = Math.ceil(y2 / k) * k; + return Plot.waffleY({length: 1}, {y1, y2, multiple: k, fill: y1, stroke: "black"}); + }; + return Plot.plot({ + height: 1200, + color: {type: "categorical"}, + y: {domain: [0, 300]}, + marks: [ + Plot.waffleY({length: 1}, {y1: 0, y2: 300, multiple: 10, stroke: "currentColor", strokeOpacity: 0.2, gap: 0}), + waffle(0, 1), + waffle(0, 0.5), + waffle(0.2, 0.8), + waffle(0.6, 1.4), + waffle(9.6, 10.4), + waffle(0.6, 2), + waffle(1, 2.4), + waffle(0.6, 2.4), + waffle(1, 3), + waffle(9, 11), + waffle(0.6, 3), + waffle(1, 3.4), + waffle(0.6, 3.4), + waffle(7, 20), + waffle(7.6, 20), + waffle(0, 13), + waffle(0, 12.4), + waffle(7, 23), + waffle(7.6, 22.4) + ] + }); +}