|
| 1 | +# Layout: resize |
| 2 | + |
| 3 | +`resize` is a helper function that provides a way to set the size of a chart, or other content, to fit into a container on your page. |
| 4 | + |
| 5 | +`resize` takes a function that renders content, like a chart. When the page is rendered, `resize` calls the render function with the **width** and, optionally the **height**, of its parent container. If the page changes size, `resize` calls your function again with the new **width** and **height** values that re-renders the content to fit in the newly resized page. |
| 6 | + |
| 7 | +## Width only |
| 8 | + |
| 9 | +If your content defines its own height then only use the `width` parameter: |
| 10 | + |
| 11 | +```js |
| 12 | +html`<div class="grid grid-cols-2"> |
| 13 | + <div class="card"> |
| 14 | + ${resize((width) => Plot.barY([9, 4, 8, 1, 11, 3, 4, 2, 7, 5]).plot({width, height: 150}))} |
| 15 | + </div> |
| 16 | + <div class="card"> |
| 17 | + ${resize((width) => Plot.barY([3, 4, 2, 7, 5, 9, 4, 8, 1, 11]).plot({width, height: 150}))} |
| 18 | + </div> |
| 19 | +</div>` |
| 20 | +``` |
| 21 | + |
| 22 | +```html run=false |
| 23 | +<div class="grid grid-cols-2"> |
| 24 | + <div class="card"> |
| 25 | + ${resize((width) => Plot.barY([9, 4, 8, 1, 11, 3, 4, 2, 7, 5]).plot({width, height: 150}))} |
| 26 | + </div> |
| 27 | + <div class="card"> |
| 28 | + ${resize((width) => Plot.barY([3, 4, 2, 7, 5, 9, 4, 8, 1, 11]).plot({width, height: 150}))} |
| 29 | + </div> |
| 30 | +</div> |
| 31 | +``` |
| 32 | + |
| 33 | +## Width and height |
| 34 | + |
| 35 | +If your container defines a height, in this example `300px`, then you can use both the `width` and `height` parameters: |
| 36 | + |
| 37 | + |
| 38 | +```js |
| 39 | +html`<div class="grid grid-cols-2" style="grid-auto-rows: 300px;"> |
| 40 | + <div class="card"> |
| 41 | + ${resize((width, height) => Plot.barY([9, 4, 8, 1, 11, 3, 4, 2, 7, 5]).plot({width, height}))} |
| 42 | + </div> |
| 43 | + <div class="card"> |
| 44 | + ${resize((width, height) => Plot.barY([3, 4, 2, 7, 5, 9, 4, 8, 1, 11]).plot({width, height}))} |
| 45 | + </div> |
| 46 | +</div>` |
| 47 | +``` |
| 48 | + |
| 49 | +```html run=false |
| 50 | +<div class="grid grid-cols-2" style="grid-auto-rows: 300px;"> |
| 51 | + <div class="card"> |
| 52 | + ${resize((width, height) => Plot.barY([9, 4, 8, 1, 11, 3, 4, 2, 7, 5]).plot({width, height}))} |
| 53 | + </div> |
| 54 | + <div class="card"> |
| 55 | + ${resize((width, height) => Plot.barY([3, 4, 2, 7, 5, 9, 4, 8, 1, 11]).plot({width, height}))} |
| 56 | + </div> |
| 57 | +</div> |
| 58 | +``` |
| 59 | + |
| 60 | +Note: If you are using `resize` with both `width` and `height` and see nothing rendered, it may be because your parent container does not have its own height specified. |
0 commit comments