svelte-plots-basic 2.1.6 → 2.2.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.
package/README.md CHANGED
@@ -6,9 +6,10 @@ The library is under development and breaking changes may occur in the coming ve
6
6
 
7
7
  New major release (v. 2.0.0) introduces many breaking changes as the library was completely re-written. If you use previous versions of `svelte-plots-basic` in your projects, and do not want to change anything, stick to the latest 1.x.x version (v. 1.1.4).
8
8
 
9
- ### 2.1.1—2.1.6
9
+ ### 2.2.0
10
10
 
11
- * Small improvements and bug fixes.
11
+ * Better handling of axes labels and plot title (now they are part of SVG object).
12
+ * Other small improvements and bug fixes.
12
13
 
13
14
  ### 2.1.0
14
15
 
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.1.6",
4
+ "version": "2.2.0",
5
5
  "author": "Sergey Kucheryavskiy",
6
6
  "license": "MIT",
7
7
  "private": false,
@@ -54,7 +54,7 @@
54
54
  'xlarge': 10
55
55
  };
56
56
 
57
- // font size for legend items in pixels
57
+ // font size for plot element
58
58
  const LEGEND_FONT_SIZE = {
59
59
  "small": 10,
60
60
  "medium": 12,
@@ -62,6 +62,14 @@
62
62
  "xlarge": 16
63
63
  };
64
64
 
65
+ // font size for legend items in pixels
66
+ const PLOT_FONT_SIZE = {
67
+ "small": 11,
68
+ "medium": 13,
69
+ "large": 16,
70
+ "xlarge": 19
71
+ };
72
+
65
73
  // margin between plot series elements and data labels
66
74
  const LABELS_MARGIN = {
67
75
  'small': 10,
@@ -91,8 +99,8 @@
91
99
 
92
100
  /* parameters for internal use inside the component */
93
101
  let plotElement;
94
- let axesElement;
95
- let width, height = 100;
102
+ let width = 100, height = 100;
103
+ let left = 0, bottom = 0, top = 0;
96
104
 
97
105
 
98
106
  /*****************************************/
@@ -203,6 +211,8 @@
203
211
  yLim: yLim,
204
212
  tX: tX,
205
213
  tY: tY,
214
+ xLabel: xLabel,
215
+ yLabel: yLabel,
206
216
 
207
217
  // constants
208
218
  LINE_STYLES: LINE_STYLES,
@@ -273,43 +283,57 @@
273
283
  /*****************************************/
274
284
 
275
285
  // observer for the plot area size — to update the scale
276
- const ro1 = new ResizeObserver(entries => {
286
+ const ro = new ResizeObserver(entries => {
277
287
  for (let entry of entries) {
278
288
  const pcr = plotElement.getBoundingClientRect();
279
- scale.update(x => getScale(pcr.width, pcr.height));
289
+ const scl = getScale(pcr.width, pcr.height);
290
+ scale.update(x => scl);
291
+
292
+ const m = PLOT_FONT_SIZE[scl] * 1.5;
293
+ left = xLabel && xLabel !== '' ? m : 0;
294
+ bottom = yLabel && yLabel !== '' ? m : 0;
295
+ top = title && title !== '' ? m : 0;
296
+ width = pcr.width - left;
297
+ height = pcr.height - bottom - top;
280
298
  }
281
299
  });
282
300
 
283
- // observer for the axes area size - to update size of axes
284
- const ro2 = new ResizeObserver(entries => {
285
- for (let entry of entries) {
286
- const acr = axesElement.getBoundingClientRect();
287
- width = acr.width;
288
- height = acr.height;
289
- }
290
- });
301
+ $: fontSize = PLOT_FONT_SIZE[$scale];
291
302
 
292
303
  onMount(() => {
293
- ro1.observe(plotElement);
294
- ro2.observe(axesElement);
304
+ ro.observe(plotElement);
295
305
  });
296
306
 
297
307
  onDestroy(() => {
298
- ro1.unobserve(plotElement);
299
- ro2.unobserve(axesElement);
308
+ ro.unobserve(plotElement);
300
309
  })
301
310
 
302
- </script>
311
+ $: lblStyleStr = `font-weight:bold;text-anchor:middle;`;
303
312
 
304
- <div class="plot {'plot_' + $scale}" bind:this={plotElement} class:plot_error={!$isOk}>
305
- <!-- plot title and axis labels -->
306
- {#if title !== ''}<div class="axes__title">{@html title}</div>{/if}
307
- {#if yLabel !== ''}<div class="axes__ylabel"><span>{@html yLabel}</span></div>{/if}
308
- {#if xLabel !== ''}<div class="axes__xlabel"><span>{@html xLabel}</span></div>{/if}
313
+ </script>
309
314
 
310
- <!-- axes (coordinate system) -->
311
- <div class="axes-wrapper" bind:this={axesElement} >
312
- <svg on:click={handleClick} on:keydown={handleClick} preserveAspectRatio="none" class="axes">
315
+ <div class="plot-container {'plot_' + $scale}" bind:this={plotElement} class:plot_error={!$isOk}>
316
+
317
+ <svg class="plot" bind:this={plotElement} xmlns="http://www.w3.org/2000/svg"
318
+ style={`font-family: Arial, Helvetica, sans-serif;font-size:${fontSize}px`}>
319
+
320
+ <g style={lblStyleStr}>
321
+ <!-- x-axis label -->
322
+ {#if xLabel && xLabel !== ''}
323
+ <text x={0} y={(height + top) /2} dx={0} dy={0} transform={`rotate(-90, 10, ${height/2})`} style="font-size:1.1em;">{@html yLabel}</text>
324
+ {/if}
325
+ <!-- y-axis label -->
326
+ {#if yLabel && yLabel !== ''}
327
+ <text x={left + width/2 } y={height + top} dx={0} dy={5} style="font-size:1.1em;">{@html xLabel}</text>
328
+ {/if}
329
+ <!-- plot title -->
330
+ {#if title && title !== ''}
331
+ <text x={left + width/2 } y={0} dx={0} dy={0} alignment-baseline="hanging" style="font-size:1.2em;">{@html title}</text>
332
+ {/if}
333
+ </g>
334
+
335
+ <svg x={left} y={top} width={width} height={height} on:click={handleClick}
336
+ on:keydown={handleClick} preserveAspectRatio="none" class="axes">
313
337
 
314
338
  <!-- define clipping path -->
315
339
  <defs>
@@ -318,7 +342,6 @@
318
342
  </clipPath>
319
343
  </defs>
320
344
 
321
-
322
345
  <!-- axis and box -->
323
346
  <slot name="xaxis"></slot>
324
347
  <slot name="yaxis"></slot>
@@ -328,11 +351,10 @@
328
351
  <slot></slot>
329
352
  </g>
330
353
 
331
-
332
354
  <!-- axis and box -->
333
355
  <slot name="box"></slot>
334
-
335
356
  </svg>
357
+ </svg>
336
358
 
337
359
  {#if !$isOk}
338
360
  <p class="message_error">
@@ -340,27 +362,14 @@
340
362
  Check that you defined axes limits and margins correctly.
341
363
  </p>
342
364
  {/if}
343
- </div>
344
-
345
-
346
365
  </div>
347
366
 
348
367
  <style>
349
368
 
350
- /* Plot (main container) */
351
- .plot {
369
+ /* Plot container */
370
+ .plot-container {
352
371
  font-family: Arial, Helvetica, sans-serif;
353
-
354
- display: grid;
355
- grid-template-columns: min-content auto;
356
- grid-template-rows: min-content auto min-content;
357
- grid-template-areas:
358
- ". title"
359
- "ylab axes"
360
- ". xlab";
361
-
362
372
  box-sizing: border-box;
363
- /* background: #fefefe; */
364
373
  min-width: 100px;
365
374
  min-height: 50px;
366
375
  width: 100%;
@@ -369,28 +378,10 @@
369
378
  margin: 0;
370
379
  }
371
380
 
372
- .plot_small {
373
- font-size: 11px;
374
- }
375
-
376
- .plot_medium {
377
- font-size: 13px;
378
- }
379
-
380
- .plot_large {
381
- font-size: 16px;
382
- }
383
-
384
- .plot_xlarge {
385
- font-size: 19px;
386
- }
387
-
388
381
  .plot_error {
389
382
  display: flex;
390
- }
391
-
392
- .plot_error > .axes-wrapper > svg {
393
- display: none;
383
+ justify-content: center;
384
+ align-items: center;
394
385
  }
395
386
 
396
387
  .message_error {
@@ -401,19 +392,9 @@
401
392
  text-align: center;
402
393
  }
403
394
 
404
- /* Axes (coordinate system) */
405
- .axes-wrapper {
406
- grid-area: axes;
407
- position:relative;
408
- box-sizing: border-box;
409
- display: flex;
410
- width: 100%;
411
- height: 100%;
412
- padding: 0;
413
- margin: 0;
414
- }
395
+ /* Plot and axes (coordinate system) */
415
396
 
416
- .axes {
397
+ .plot {
417
398
  display: block;
418
399
  box-sizing: border-box;
419
400
  position:absolute;
@@ -437,64 +418,6 @@
437
418
  max-width: 100%;
438
419
  min-height: 100%;
439
420
  min-width: 100%;
440
-
441
- }
442
-
443
- .axes__xlabel {
444
- grid-area: xlab;
445
- font-size: 1.0em;
446
- font-weight: 600;
447
- padding: 0.25em;
448
- text-align: center;
449
- }
450
-
451
-
452
- .axes__ylabel {
453
- grid-area: ylab;
454
- font-size: 1.0em;
455
- font-weight: 600;
456
- padding: 0.25em;
457
- text-align: center;
458
- vertical-align: middle;
459
- display: flex;
460
- }
461
-
462
- .axes__ylabel > span {
463
- width: 1.5em;
464
- line-height: 1.5em;
465
- display: inline-block;
466
- writing-mode: vertical-rl;
467
- transform: rotate(180deg);
468
- }
469
-
470
-
471
- .axes__title {
472
- background: transparent;
473
- grid-area: title;
474
-
475
- font-size: 1.3em;
476
- font-weight: bold;
477
- line-height: 1.2em;
478
- padding: 0.5em 0;
479
- text-align: center;
480
- }
481
-
482
- /* Axis */
483
- :global(.axis-labels) {
484
- fill: #303030;
485
- font-size: 0.95em;
486
- }
487
-
488
- /* Data labels */
489
- :global(.labels) {
490
- fill: #606060;
491
- font-size: 0.90em;
492
- }
493
-
494
- :global(.labels) {
495
- visibility: hidden;
496
- transition:visibility 0.25s linear, opacity 0.25s linear;
497
- opacity: 0;
498
421
  }
499
422
 
500
423
  </style>
@@ -35,9 +35,9 @@
35
35
  function getTickFactorLabel(tf) {
36
36
  if (tf === 0) return '';
37
37
  if (tf < 0) {
38
- return `&times;10<tspan vertical-align="super">${(tf).toString()}</tspan>`;
38
+ return `&times;10<tspan dy="-0.5em">${(tf).toString()}</tspan>`;
39
39
  } else {
40
- return `&times;10<tspan vertical-align="super">${(tf).toString()}</tspan>`;
40
+ return `&times;10<tspan dy="-0.5em">${(tf).toString()}</tspan>`;
41
41
  }
42
42
  }
43
43
  </script>
@@ -31,6 +31,7 @@
31
31
  const tY = axes.tY;
32
32
  const isOk = axes.isOk;
33
33
 
34
+ let el;
34
35
 
35
36
  let x, y = undefined;
36
37
  let dx = 0, dy = 0, textAnchor;
@@ -56,7 +57,7 @@
56
57
  </script>
57
58
 
58
59
  {#if x !== undefined && y !== undefined}
59
- <g class="tick_labels" style={textStyleStr} >
60
+ <g class="tick_labels" bind:this={el} style={textStyleStr} >
60
61
  {#if las === 2 && pos === 4}
61
62
  {#each x as v, i}
62
63
  <text data-id={i} x={x[i]} y={y[i]} dx={0} dy={dx*1.25} transform={`rotate(-90, ${x[i]}, ${y[i]})`} text-anchor={"middle"}>{@html tickLabels[i]}</text>
@@ -103,7 +103,7 @@
103
103
  {#if $isOk && axisLine.length > 0}
104
104
  <Axis
105
105
  className="mdaplot__xaxis" pos={1}
106
- {lineColor} {gridColor} {textColor}
107
- {showGrid} {grid} {axisLine} {tickCoords} {tickLabels} {tfCoords} {tickFactor} {las}
106
+ {lineColor} {gridColor} {textColor} {showGrid} {grid} {axisLine}
107
+ {tickCoords} {tickLabels} {tfCoords} {tickFactor} {las}
108
108
  />
109
109
  {/if}
@@ -103,7 +103,7 @@
103
103
  {#if $isOk && axisLine.length > 0}
104
104
  <Axis
105
105
  className="mdaplot__yaxis" pos={4}
106
- {lineColor} {gridColor} {textColor}
107
- {showGrid} {grid} {axisLine} {tickCoords} {tickLabels} {tfCoords} {tickFactor} {las}
106
+ {lineColor} {gridColor} {textColor} {showGrid} {grid} {axisLine}
107
+ {tickCoords} {tickLabels} {tfCoords} {tickFactor} {las}
108
108
  />
109
109
  {/if}