svelte-plots-basic 2.5.7 → 2.5.8

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.
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "svelte-plots-basic",
3
3
  "description": "Svelte components for creating simple plots",
4
- "version": "2.5.7",
4
+ "version": "2.5.8",
5
5
  "author": "Sergey Kucheryavskiy",
6
6
  "license": "MIT",
7
7
  "private": false,
@@ -8,7 +8,7 @@
8
8
  import { setContext, createEventDispatcher, onMount, onDestroy } from 'svelte';
9
9
  import { writable } from 'svelte/store';
10
10
  import { isvector, vector, Vector } from 'mdatools/arrays';
11
- import { roundCoords, getScale, downloadSVG, downloadPNG, createPngBlob } from '../Utils.js';
11
+ import { roundCoords, getScale, downloadSVG, downloadPNG, createPngBlob, getAxisScale } from '../Utils.js';
12
12
 
13
13
 
14
14
  /*****************************************/
@@ -50,14 +50,22 @@
50
50
  'xlarge': 60
51
51
  }
52
52
 
53
- // number of ticks along each axis
54
- const TICK_NUM = {
53
+ // number of x-ticks along each axis
54
+ const XTICK_NUM = {
55
55
  'small': 5,
56
56
  'medium': 8,
57
57
  'large': 12,
58
58
  'xlarge': 15
59
59
  };
60
60
 
61
+ // number of y-ticks along each axis
62
+ const YTICK_NUM = {
63
+ 'small': 6,
64
+ 'medium': 10,
65
+ 'large': 14,
66
+ 'xlarge': 18
67
+ };
68
+
61
69
  // size of ticks
62
70
  const TICK_SIZE = {
63
71
  'small': 4,
@@ -246,6 +254,8 @@
246
254
  /* Storage to share with children */
247
255
  /*****************************************/
248
256
 
257
+ const xscale = writable('medium'); // scale factor for x-axis (influences number of ticks)
258
+ const yscale = writable('medium'); // scale factor for y-axis (influences number of ticks)
249
259
  const scale = writable('medium'); // scale factor (how big the shown plot is)
250
260
  const isOk = writable(false); // are axes ready for drawing
251
261
  const xLim = writable(limX); // validated values for x-axis limits
@@ -266,6 +276,8 @@
266
276
 
267
277
  // variables
268
278
  scale: scale,
279
+ xscale: xscale,
280
+ yscale: yscale,
269
281
  isOk: isOk,
270
282
  xLim: xLim,
271
283
  yLim: yLim,
@@ -277,7 +289,8 @@
277
289
  // constants
278
290
  LINE_STYLES: LINE_STYLES,
279
291
  LABELS_MARGIN: LABELS_MARGIN,
280
- TICK_NUM: TICK_NUM,
292
+ XTICK_NUM: XTICK_NUM,
293
+ YTICK_NUM: YTICK_NUM,
281
294
  TICK_SIZE: TICK_SIZE,
282
295
  MARKER_SYMBOLS: MARKER_SYMBOLS,
283
296
  LEGEND_FONT_SIZE: LEGEND_FONT_SIZE
@@ -347,7 +360,11 @@
347
360
  for (let entry of entries) {
348
361
  const pcr = plotElement.getBoundingClientRect();
349
362
  const scl = getScale(pcr.width, pcr.height);
363
+ const xscl = getAxisScale(pcr.width);
364
+ const yscl = getAxisScale(pcr.height);
350
365
  scale.update(x => scl);
366
+ xscale.update(x => xscl);
367
+ yscale.update(x => yscl);
351
368
 
352
369
  const m = PLOT_FONT_SIZE[scl] * 1.5;
353
370
  left = xLabel && xLabel !== '' ? m : 0;
@@ -1,9 +1,9 @@
1
1
  <script>
2
- /****************************************************
3
- * X-Axis *
4
- * -------------------- *
5
- * shows x-axis and its elements (ticks, grid, etc.) *
6
- *****************************************************/
2
+ /*****************************************************/
3
+ /* X-Axis */
4
+ /* -------------------- */
5
+ /* shows x-axis and its elements (ticks, grid, etc.) */
6
+ /*****************************************************/
7
7
 
8
8
  import { getContext } from 'svelte';
9
9
  import { Vector, vector } from 'mdatools/arrays';
@@ -44,6 +44,7 @@
44
44
  const xLim = axes.xLim;
45
45
  const yLim = axes.yLim;
46
46
  const scale = axes.scale;
47
+ const xscale = axes.xscale;
47
48
  const isOk = axes.isOk;
48
49
  const tY = axes.tY;
49
50
 
@@ -58,7 +59,7 @@
58
59
  $: if ($isOk) {
59
60
 
60
61
  // compute x-coordinates of the ticks or take the ones manually specified by user
61
- const ticksX = getAxisTicks(ticks, $xLim, axes.TICK_NUM[$scale], true, whole);
62
+ const ticksX = getAxisTicks(ticks, $xLim, axes.XTICK_NUM[$xscale], true, whole);
62
63
  const tickNum = ticksX.length;
63
64
 
64
65
  // compute tick y-coordinates (up and bottom)
@@ -44,6 +44,7 @@
44
44
  const xLim = axes.xLim;
45
45
  const yLim = axes.yLim;
46
46
  const scale = axes.scale;
47
+ const yscale = axes.yscale;
47
48
  const isOk = axes.isOk;
48
49
  const tX = axes.tX;
49
50
 
@@ -58,7 +59,7 @@
58
59
  $: if ($isOk) {
59
60
 
60
61
  // compute x-coordinates of the ticks or take the ones manually specified by user
61
- const ticksY = getAxisTicks(ticks, $yLim, axes.TICK_NUM[$scale], true, whole);
62
+ const ticksY = getAxisTicks(ticks, $yLim, axes.YTICK_NUM[$yscale], true, whole);
62
63
  const tickNum = ticksY.length;
63
64
 
64
65
  // compute tick y-coordinates (up and bottom)
package/src/Utils.js CHANGED
@@ -206,13 +206,13 @@ export function getAxisTicks(ticks, lim, maxTickNum, round, whole, deltaFactor)
206
206
  if (typeof(lim) !== "object" || lim[0] === undefined || lim[1] === undefined) return undefined;
207
207
 
208
208
  // get range as a nice number and compute min, max and steps for the tick sequence
209
- const delta = (lim[1] - lim[0]) * deltaFactor;
210
- const localRange = lim[1] - lim[0] - delta;
209
+ const delta = Math.abs((lim[1] - lim[0])) * deltaFactor;
210
+ const localRange = lim[1] - lim[0] - 2 * delta;
211
211
  const exponent = Math.floor(Math.log10(localRange));
212
212
  const fraction = 1 / Math.pow(10, exponent - 1);
213
213
  const range = Math.round(localRange * fraction) / fraction;
214
214
 
215
- let tickSpacing = niceNum(range / (maxTickNum - 1), round);
215
+ let tickSpacing = niceNum(range / maxTickNum, false);
216
216
  if (whole) {
217
217
  if (tickSpacing < 1) tickSpacing = 1;
218
218
  tickSpacing = Math.round(tickSpacing);
@@ -310,6 +310,19 @@ export function getScale(width, height) {
310
310
  return "xlarge";
311
311
  }
312
312
 
313
+ /**
314
+ * Computes a scale level for single axis.
315
+ *
316
+ * @param {numeric} size - width or height of plotting area in pixels.
317
+ * @returns {text} the scale level ("small", "medium" or "large").
318
+ *
319
+ */
320
+ export function getAxisScale(width, size) {
321
+ if (width < 400.2) return "small";
322
+ if (width < 700.2) return "medium";
323
+ if (width < 900.2) return "large";
324
+ return "xlarge";
325
+ }
313
326
 
314
327
  /*************************************************************/
315
328
  /* Functions needed to adjust DPI of resulting PNG image */