From 95cd280d5ec393c3dd604891c36c9a0097368565 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Philippe=20Rivi=C3=A8re?= Date: Tue, 26 Sep 2023 11:34:18 +0200 Subject: [PATCH] plot().dimensions() returns the chart's dimensions --- docs/features/plots.md | 4 ++++ src/plot.d.ts | 12 ++++++++++++ src/plot.js | 1 + test/plot-test.ts | 24 ++++++++++++++++++++++++ 4 files changed, 41 insertions(+) diff --git a/docs/features/plots.md b/docs/features/plots.md index 13351e4241..82a75bb5c0 100644 --- a/docs/features/plots.md +++ b/docs/features/plots.md @@ -317,3 +317,7 @@ const legend = plot.legend("color"); // render a color legend ``` 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. + +## *plot*.dimensions() {#plot_dimensions} + +Returns the dimensions of the chart, an object with the properties marginTop, marginRight, marginBottom, marginLeft, width and height set to their actual values. diff --git a/src/plot.d.ts b/src/plot.d.ts index b6019631b1..e5d6288b04 100644 --- a/src/plot.d.ts +++ b/src/plot.d.ts @@ -421,6 +421,18 @@ export interface Plot { */ legend(name: ScaleName, options?: LegendOptions): SVGSVGElement | HTMLElement | undefined; + /** + * Returns the dimensions of the chart. + */ + dimensions(): { + marginTop: number; + marginRight: number; + marginBottom: number; + marginLeft: number; + width: number; + height: number; + }; + /** For interactive plots, the current value. */ value?: any; } diff --git a/src/plot.js b/src/plot.js index 039a269ada..cf7c46c0db 100644 --- a/src/plot.js +++ b/src/plot.js @@ -336,6 +336,7 @@ export function plot(options = {}) { figure.scale = exposeScales(scales.scales); figure.legend = exposeLegends(scaleDescriptors, context, options); + figure.dimensions = () => ({...dimensions}); const w = consumeWarnings(); if (w > 0) { diff --git a/test/plot-test.ts b/test/plot-test.ts index baedd583ae..ad988d33ef 100644 --- a/test/plot-test.ts +++ b/test/plot-test.ts @@ -6,3 +6,27 @@ it("plot({aspectRatio}) rejects unsupported scale types", () => { assert.throws(() => Plot.dot([]).plot({aspectRatio: true, x: {type: "symlog"}}), /^Error: unsupported x scale for aspectRatio: symlog$/); // prettier-ignore assert.throws(() => Plot.dot([]).plot({aspectRatio: true, y: {type: "symlog"}}), /^Error: unsupported y scale for aspectRatio: symlog$/); // prettier-ignore }); + +it("plot({}).dimensions() returns the expected values", () => { + assert.deepStrictEqual(Plot.dot([]).plot().dimensions(), { + marginTop: 20, + marginRight: 20, + marginBottom: 30, + marginLeft: 40, + width: 640, + height: 400 + }); + assert.deepStrictEqual( + Plot.dot(["short"], {x: (d, i) => 1 + i, y: (d) => d}) + .plot() + .dimensions(), + { + marginTop: 20, + marginRight: 20, + marginBottom: 30, + marginLeft: 40, + width: 640, + height: 80 + } + ); +});