svelte-plots-basic 2.5.5 → 2.5.7

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.5",
4
+ "version": "2.5.7",
5
5
  "author": "Sergey Kucheryavskiy",
6
6
  "license": "MIT",
7
7
  "private": false,
@@ -7,10 +7,11 @@
7
7
  *****************************************************/
8
8
 
9
9
  import { Colors } from '../Colors.js';
10
+ import { getTickFactorLabel } from '../Utils.js';
11
+
10
12
  import AxisLines from './AxisLines.svelte';
11
13
  import AxisTickLabels from './AxisTickLabels.svelte';
12
14
 
13
-
14
15
  /*****************************************/
15
16
  /* Input parameters */
16
17
  /*****************************************/
@@ -32,14 +33,6 @@
32
33
  export let textColor = Colors.DARKGRAY; // color of tick labels
33
34
 
34
35
  // generate text for tick factor label
35
- function getTickFactorLabel(tf) {
36
- if (tf === 0) return '';
37
- if (tf < 0) {
38
- return `&times;10<tspan dy="-0.5em">${(tf).toString()}</tspan>`;
39
- } else {
40
- return `&times;10<tspan dy="-0.5em">${(tf).toString()}</tspan>`;
41
- }
42
- }
43
36
  </script>
44
37
 
45
38
  <g class="mdaplot__axis {className}">
@@ -6,7 +6,7 @@
6
6
  **********************************************************/
7
7
 
8
8
  import { getContext } from 'svelte';
9
- import { checkCoords } from '../Utils';
9
+ import { checkCoords, getTickLabels, getTickFactorLabel } from '../Utils';
10
10
  import { Vector, isvector } from 'mdatools/arrays';
11
11
 
12
12
 
@@ -44,17 +44,21 @@
44
44
 
45
45
  // check colormap values
46
46
  $: if (!colmap || !(colmap.length === breaks.length - 1 || colmap.length === breaks.length )) {
47
- console.log(colmap)
48
- console.log(breaks)
49
- console.log(labels)
50
47
  throw new Error('ColormapLegend: number of color values in colormap does not match number of intervals defined by breaks.');
51
48
  }
52
49
 
53
50
  // define local variables for breaks and prepare breaks labels
54
51
  $: lBreaks = isvector(breaks) ? breaks.v : breaks;
55
- $: lLabels = labels && labels.length === lBreaks.length ?
56
- labels :
57
- Array.from(lBreaks).map(v => v.toFixed(decNum).toString());
52
+
53
+ let breakFactor, breakLabels;
54
+ $: if (lBreaks) {
55
+ [breakFactor, breakLabels] = getTickLabels(lBreaks);
56
+ } else {
57
+ breakFactor = null;
58
+ breakLabels = null;
59
+ }
60
+
61
+ $: lLabels = labels && labels.length === lBreaks.length ? labels : breakLabels;
58
62
 
59
63
  // get axes context and reactive variables needed to compute coordinates
60
64
  const axes = getContext('axes');
@@ -80,7 +84,7 @@
80
84
  const height = axesHeight * HEIGHT_RATIO;
81
85
 
82
86
  // compute elements size and position
83
- const elWidth = width / n;
87
+ const elWidth = width / (n + (breakFactor !== 0));
84
88
  const left = $xLim[0] + axesWidth * (1 - WIDTH_RATIO) * 0.5 ;
85
89
  const elTop = $yLim[1] - height * TOP_MARGIN;
86
90
 
@@ -103,7 +107,14 @@
103
107
  {#if !isCentered}
104
108
  <text x={rx[n]} y={ry} dx={0} dy={rh * 1.25} dominant-baseline="hanging" fill={labelColor} font-size="{fontSize}em" text-anchor="middle">{@html lLabels[n]}</text>
105
109
  {/if}
110
+
111
+ {#if breakFactor !== 0}
112
+ <text x={rx[n] + rw[0] * 0.85} y={ry[0] + rh * 2.2} dx={0} dy={0} dominant-baseline="hanging" fill={labelColor} font-size="{fontSize}em" text-anchor="middle">{@html getTickFactorLabel(breakFactor)}</text>
113
+ {/if}
114
+
106
115
  </g>
116
+
117
+
107
118
  {/if}
108
119
 
109
120
 
package/src/Utils.js CHANGED
@@ -1,12 +1,25 @@
1
1
  import { cbind, vector, isvector, Vector } from 'mdatools/arrays';
2
2
  import { min, max, diff } from 'mdatools/stat';
3
3
 
4
+
5
+ /**
6
+ * Creates label for a tick factor.
7
+ *
8
+ * @param {Number} tf - tick factor value (negative or positive)
9
+ * @returns
10
+ */
11
+ export function getTickFactorLabel(tf) {
12
+ if (tf === 0) return '';
13
+ return `<tspan>&times;10</tspan><tspan baseline-shift="super">${(tf).toString()}</tspan>`;
14
+ }
15
+
16
+
4
17
  /**
5
18
  * Trim number leaving only significant decimals.
6
19
  * @param {number} num
7
20
  * @returns trimmed number
8
21
  */
9
- function trimNum(num) {
22
+ export function trimNum(num) {
10
23
 
11
24
  // check if number is whole — return as is
12
25
  if (num === Math.round(num)) return num;
@@ -157,11 +170,12 @@ export function val2p(x, y, tX, tY, axes) {
157
170
  * @param {number} maxTickNum - maximum number of ticks to compute.
158
171
  * @param {boolean} round - round or not the fractions when computing nice numbers for the ticks.
159
172
  * @param {boolean} whole - should the ticks be a whole number.
173
+ * @param {Number} deltaFactor - percent of range to use as internal margins
160
174
  *
161
175
  * @returns {Array} an array with computed tick positions.
162
176
  *
163
177
  */
164
- export function getAxisTicks(ticks, lim, maxTickNum, round, whole) {
178
+ export function getAxisTicks(ticks, lim, maxTickNum, round, whole, deltaFactor) {
165
179
 
166
180
  if (round === undefined || round === null) {
167
181
  round = true;
@@ -171,6 +185,10 @@ export function getAxisTicks(ticks, lim, maxTickNum, round, whole) {
171
185
  whole = false;
172
186
  }
173
187
 
188
+ if (deltaFactor === undefined) {
189
+ deltaFactor = 0.001;
190
+ }
191
+
174
192
  // if ticks are already provided do not recompute them
175
193
  if (ticks !== undefined) {
176
194
  if (Array.isArray(ticks)) {
@@ -188,7 +206,7 @@ export function getAxisTicks(ticks, lim, maxTickNum, round, whole) {
188
206
  if (typeof(lim) !== "object" || lim[0] === undefined || lim[1] === undefined) return undefined;
189
207
 
190
208
  // get range as a nice number and compute min, max and steps for the tick sequence
191
- const delta = (lim[1] - lim[0]) / 100;
209
+ const delta = (lim[1] - lim[0]) * deltaFactor;
192
210
  const localRange = lim[1] - lim[0] - delta;
193
211
  const exponent = Math.floor(Math.log10(localRange));
194
212
  const fraction = 1 / Math.pow(10, exponent - 1);