Skip to content

Commit 95cd280

Browse files
committed
plot().dimensions() returns the chart's dimensions
1 parent 1b1da9f commit 95cd280

File tree

4 files changed

+41
-0
lines changed

4 files changed

+41
-0
lines changed

docs/features/plots.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -317,3 +317,7 @@ const legend = plot.legend("color"); // render a color legend
317317
```
318318

319319
Renders a standalone legend for the scale with the specified *name* (such as *x* or *color*) on the given *plot*, where *plot* is a rendered plot element returned by [plot](#plot), returning a SVG or HTML figure element. This element can then be inserted into the page as described in the [getting started guide](../getting-started.md). If the associated *plot* has no scale with the given *name*, returns undefined. Legends are currently only supported for *color*, *opacity*, and *symbol* scales.
320+
321+
## *plot*.dimensions() {#plot_dimensions}
322+
323+
Returns the dimensions of the chart, an object with the properties marginTop, marginRight, marginBottom, marginLeft, width and height set to their actual values.

src/plot.d.ts

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -421,6 +421,18 @@ export interface Plot {
421421
*/
422422
legend(name: ScaleName, options?: LegendOptions): SVGSVGElement | HTMLElement | undefined;
423423

424+
/**
425+
* Returns the dimensions of the chart.
426+
*/
427+
dimensions(): {
428+
marginTop: number;
429+
marginRight: number;
430+
marginBottom: number;
431+
marginLeft: number;
432+
width: number;
433+
height: number;
434+
};
435+
424436
/** For interactive plots, the current value. */
425437
value?: any;
426438
}

src/plot.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -336,6 +336,7 @@ export function plot(options = {}) {
336336

337337
figure.scale = exposeScales(scales.scales);
338338
figure.legend = exposeLegends(scaleDescriptors, context, options);
339+
figure.dimensions = () => ({...dimensions});
339340

340341
const w = consumeWarnings();
341342
if (w > 0) {

test/plot-test.ts

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,3 +6,27 @@ it("plot({aspectRatio}) rejects unsupported scale types", () => {
66
assert.throws(() => Plot.dot([]).plot({aspectRatio: true, x: {type: "symlog"}}), /^Error: unsupported x scale for aspectRatio: symlog$/); // prettier-ignore
77
assert.throws(() => Plot.dot([]).plot({aspectRatio: true, y: {type: "symlog"}}), /^Error: unsupported y scale for aspectRatio: symlog$/); // prettier-ignore
88
});
9+
10+
it("plot({}).dimensions() returns the expected values", () => {
11+
assert.deepStrictEqual(Plot.dot([]).plot().dimensions(), {
12+
marginTop: 20,
13+
marginRight: 20,
14+
marginBottom: 30,
15+
marginLeft: 40,
16+
width: 640,
17+
height: 400
18+
});
19+
assert.deepStrictEqual(
20+
Plot.dot(["short"], {x: (d, i) => 1 + i, y: (d) => d})
21+
.plot()
22+
.dimensions(),
23+
{
24+
marginTop: 20,
25+
marginRight: 20,
26+
marginBottom: 30,
27+
marginLeft: 40,
28+
width: 640,
29+
height: 80
30+
}
31+
);
32+
});

0 commit comments

Comments
 (0)