|
2 | 2 | title: Rect mark
|
3 | 3 | ---
|
4 | 4 |
|
5 |
| -The Rect mark can be used to add rectangles to the plot, defined by x1, y1, x2, and y2 coordinates: |
| 5 | +The Rect mark can be used to add rectangles to the plot, defined by x1, y1, x2, and y2 coordinates. It is useful in cases where both the x and y axis are using quantitative scales. |
| 6 | + |
| 7 | +:::tip |
| 8 | +**Tip:** If one of your axes is a band scale, you may want to use the [Bar](/marks/bar) marks instead, and if both axes are band scales you probably need the [Cell](/marks/cell) mark. |
| 9 | +::: |
| 10 | + |
| 11 | +In it's purest form, the `<Rect>` mark will just add rectangles at the given coordinates: |
| 12 | + |
| 13 | +```svelte live |
| 14 | +<script> |
| 15 | + import { Plot, Rect, Text } from 'svelteplot'; |
| 16 | +
|
| 17 | + const data = [ |
| 18 | + { |
| 19 | + x1: 10, |
| 20 | + x2: 15, |
| 21 | + y1: 5, |
| 22 | + y2: 9 |
| 23 | + }, |
| 24 | + { |
| 25 | + x1: 7, |
| 26 | + x2: 12, |
| 27 | + y1: 7, |
| 28 | + y2: 13 |
| 29 | + } |
| 30 | + ]; |
| 31 | +</script> |
| 32 | +
|
| 33 | +<Plot grid inset={10}> |
| 34 | + <Rect |
| 35 | + {data} |
| 36 | + x1="x1" |
| 37 | + x2="x2" |
| 38 | + y1="y1" |
| 39 | + y2="y2" |
| 40 | + stroke="currentColor" |
| 41 | + fill="currentColor" |
| 42 | + fillOpacity={0.5} /> |
| 43 | +</Plot> |
| 44 | +``` |
| 45 | + |
| 46 | +```svelte |
| 47 | +<Plot grid inset={10}> |
| 48 | + <Rect {data} x1="x1" x2="x2" y1="y1" y2="y2" /> |
| 49 | +</Plot> |
| 50 | +``` |
| 51 | + |
| 52 | +[fork](https://svelte.dev/playground/7a6b0ae12c624ffeb52448adac644b5b?version=5) |
| 53 | + |
| 54 | +If your data does not come with x1/x2 and y1/y2 pairs but x/y coordinates, you can use the implicit interval transform: |
| 55 | + |
| 56 | +```svelte live |
| 57 | +<script> |
| 58 | + import { Plot, Rect, Text } from 'svelteplot'; |
| 59 | +
|
| 60 | + const data = [ |
| 61 | + { x: 1, y1: 5, y2: 8 }, |
| 62 | + { x: 3, y1: 7, y2: 11 } |
| 63 | + ]; |
| 64 | +</script> |
| 65 | +
|
| 66 | +<Plot grid inset={10}> |
| 67 | + <Rect |
| 68 | + {data} |
| 69 | + x="x" |
| 70 | + y1="y1" |
| 71 | + y2="y2" |
| 72 | + interval={1} |
| 73 | + stroke="currentColor" |
| 74 | + fill="currentColor" |
| 75 | + fillOpacity={0.5} /> |
| 76 | +</Plot> |
| 77 | +``` |
| 78 | + |
| 79 | +```svelte |
| 80 | +<Plot grid inset={10}> |
| 81 | + <Rect {data} x="x" y="y" interval={1} /> |
| 82 | +</Plot> |
| 83 | +``` |
6 | 84 |
|
7 | 85 | The interval transform may be used to convert a single value in x or y (or both) into an extent. For example, the chart below shows the observed daily maximum temperature in Seattle for the year 2015. The day-in-month and month-in-year numbers are expanded to unit intervals by setting the [interval option](/transforms/interval) to 1.
|
8 | 86 |
|
@@ -55,6 +133,8 @@ The interval transform may be used to convert a single value in x or y (or both)
|
55 | 133 | </Plot>
|
56 | 134 | ```
|
57 | 135 |
|
| 136 | +## Rect |
| 137 | + |
58 | 138 | ## RectX
|
59 | 139 |
|
60 | 140 | RectX can be used for range annotations:
|
|
0 commit comments