svelteplot 0.4.6-pr-213.3 → 0.4.7-pr-216.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -21,7 +21,8 @@
21
21
  PlotScale,
22
22
  PlotDefaults,
23
23
  PlotState,
24
- RawValue
24
+ RawValue,
25
+ PlotMargin
25
26
  } from '../types/index.js';
26
27
  import FacetGrid from './FacetGrid.svelte';
27
28
 
@@ -59,6 +60,7 @@
59
60
  height: 350,
60
61
  initialWidth: 500,
61
62
  inset: 0,
63
+ margin: 'auto',
62
64
  colorScheme: 'turbo',
63
65
  unknown: '#cccccc99',
64
66
 
@@ -122,7 +124,7 @@
122
124
  explicitScales: Set<ScaleName>;
123
125
  explicitDomains: Set<ScaleName>;
124
126
  hasProjection: boolean;
125
- margins?: number;
127
+ margin?: number | 'auto';
126
128
  inset?: number;
127
129
  };
128
130
 
@@ -173,7 +175,7 @@
173
175
  explicitScales,
174
176
  explicitDomains,
175
177
  hasProjection: !!initialOpts.projection,
176
- margins: initialOpts.margins,
178
+ margin: initialOpts.margin,
177
179
  inset: initialOpts.inset
178
180
  })
179
181
  );
@@ -363,6 +365,38 @@
363
365
  return mergeDeep<PlotOptions>({}, smartDefaultPlotOptions(opts), initialOpts);
364
366
  }
365
367
 
368
+ function maybeMargin(
369
+ // the margin option provided to the <Plot> component
370
+ margin: number | 'auto' | PlotMargin | undefined,
371
+ // direction to extract from the margin object
372
+ direction: 'left' | 'right' | 'top' | 'bottom',
373
+ // the margin option defined in the plot defaults
374
+ defaultValue: PlotMargin | number | 'auto',
375
+ // automatic margins computed from the marks
376
+ autoMargins: {
377
+ left: number;
378
+ right: number;
379
+ top: number;
380
+ bottom: number;
381
+ }
382
+ ): number {
383
+ // direction-specific margin value takes precedence
384
+ const marginValue =
385
+ typeof margin === 'object' && margin[direction] != null
386
+ ? margin[direction]
387
+ : // use the margin value if it's a number
388
+ typeof margin === 'number' || margin === 'auto'
389
+ ? margin
390
+ : // use direction-specific default value if defined
391
+ typeof defaultValue === 'object' && defaultValue[direction] != null
392
+ ? defaultValue[direction]
393
+ : typeof defaultValue === 'number' || defaultValue === 'auto'
394
+ ? defaultValue
395
+ : 'auto';
396
+
397
+ return marginValue === 'auto' ? autoMargins[direction] : marginValue;
398
+ }
399
+
366
400
  /**
367
401
  * compute smart default options for the plot based on the scales and marks
368
402
  */
@@ -370,43 +404,31 @@
370
404
  explicitScales,
371
405
  explicitDomains,
372
406
  hasProjection,
373
- margins
407
+ margin
374
408
  }: PlotOptionsParameters): PlotOptions {
375
409
  const autoXAxis = explicitScales.has('x') || explicitDomains.has('x');
376
410
  const autoYAxis = explicitScales.has('y') || explicitDomains.has('y');
377
411
  const isOneDimensional = autoXAxis !== autoYAxis;
378
412
  const oneDimX = autoXAxis && !autoYAxis;
379
413
  const oneDimY = autoYAxis && !autoXAxis;
414
+
415
+ const autoMargins = {
416
+ left: hasProjection ? 0 : Math.max(maxMarginLeft + 1, 1),
417
+ right: hasProjection ? 0 : oneDimY ? 0 : Math.max(maxMarginRight + 1, 4),
418
+ top: hasProjection ? 0 : oneDimX ? 0 : Math.max(5, maxMarginTop),
419
+ bottom: hasProjection ? 0 : Math.max(5, maxMarginBottom)
420
+ };
421
+
380
422
  return {
381
423
  title: '',
382
424
  subtitle: '',
383
425
  caption: '',
384
426
  height: 'auto',
385
427
  // maxWidth: oneDimY ? `${60 * e}px` : undefined,
386
- marginLeft: hasProjection
387
- ? 0
388
- : margins != null
389
- ? margins
390
- : Math.max(maxMarginLeft + 1, 1),
391
- marginRight: hasProjection
392
- ? 0
393
- : margins != null
394
- ? margins
395
- : oneDimY
396
- ? 0
397
- : Math.max(maxMarginRight + 1, 4),
398
- marginTop: hasProjection
399
- ? 0
400
- : margins != null
401
- ? margins
402
- : oneDimX
403
- ? 0
404
- : Math.max(5, maxMarginTop),
405
- marginBottom: hasProjection
406
- ? 0
407
- : margins != null
408
- ? margins
409
- : Math.max(5, maxMarginBottom),
428
+ marginLeft: maybeMargin(margin, 'left', DEFAULTS.margin, autoMargins),
429
+ marginRight: maybeMargin(margin, 'right', DEFAULTS.margin, autoMargins),
430
+ marginTop: maybeMargin(margin, 'top', DEFAULTS.margin, autoMargins),
431
+ marginBottom: maybeMargin(margin, 'bottom', DEFAULTS.margin, autoMargins),
410
432
  inset: isOneDimensional ? 10 : DEFAULTS.inset,
411
433
  grid: (DEFAULTS.gridX?.implicit ?? false) && (DEFAULTS.gridY?.implicit ?? false),
412
434
  axes: (DEFAULTS.axisX?.implicit ?? false) && (DEFAULTS.axisY?.implicit ?? false),
@@ -88,7 +88,7 @@
88
88
  ).slice(1)}
89
89
  <Plot
90
90
  maxWidth="240px"
91
- margins={1}
91
+ margin={1}
92
92
  marginLeft={1}
93
93
  marginRight={1}
94
94
  marginTop={6}
@@ -121,7 +121,7 @@
121
121
 
122
122
  <Plot
123
123
  maxWidth="240px"
124
- margins={1}
124
+ margin={1}
125
125
  marginLeft={10}
126
126
  marginRight={10}
127
127
  marginTop={6}
@@ -1,6 +1,6 @@
1
1
  <script lang="ts">
2
2
  import { getContext } from 'svelte';
3
- import type { PlotContext } from '../../index.js';
3
+ import type { PlotContext } from '../../types/plot';
4
4
  import { devicePixelRatio } from 'svelte/reactivity/window';
5
5
  import { MediaQuery } from 'svelte/reactivity';
6
6
  import type { Attachment } from 'svelte/attachments';
@@ -50,6 +50,12 @@ export type PlotContext = {
50
50
  updateDimensions: (width: number, height: number) => void;
51
51
  };
52
52
  type IgnoreDefaults = 'data' | 'facet' | ChannelName | 'title' | 'automatic' | 'children';
53
+ export type PlotMargin = {
54
+ top?: number | 'auto';
55
+ right?: number | 'auto';
56
+ bottom?: number | 'auto';
57
+ left?: number | 'auto';
58
+ };
53
59
  /**
54
60
  * these are the default options for the plot marks that can be set using
55
61
  * the 'svelteplot/defaults' context.
@@ -63,6 +69,10 @@ export type PlotDefaults = {
63
69
  * default plot inset
64
70
  */
65
71
  inset: number;
72
+ /**
73
+ * default plot margin
74
+ */
75
+ margin: 'auto' | number | PlotMargin;
66
76
  /**
67
77
  * default color scheme
68
78
  */
@@ -314,14 +324,11 @@ export type PlotOptions = {
314
324
  */
315
325
  height: 'auto' | number | ((d: number) => number);
316
326
  /**
317
- * Convenience option for setting all four margins at once, in px.
327
+ * If margin is set to "auto" (the default), the plot will automatically
328
+ * compute appropriate margins based on the presence of axes, labels, and
329
+ * the overall plot size. You can also set a fixed margin value in px.
318
330
  */
319
- margin: number | {
320
- top?: number;
321
- right?: number;
322
- bottom?: number;
323
- left?: number;
324
- };
331
+ margin: PlotMargin | number | 'auto';
325
332
  /**
326
333
  * Left margin of the plot, in px.
327
334
  */
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "svelteplot",
3
- "version": "0.4.6-pr-213.3",
3
+ "version": "0.4.7-pr-216.0",
4
4
  "license": "ISC",
5
5
  "author": {
6
6
  "name": "Gregor Aisch",