svelte-plots-basic 2.5.8 → 3.0.1
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 +533 -448
- package/package.json +5 -2
- package/src/2d/Area.svelte +56 -48
- package/src/2d/Axes.svelte +409 -390
- package/src/2d/AxisLines.svelte +37 -36
- package/src/2d/AxisTickLabels.svelte +41 -49
- package/src/2d/Bars.svelte +102 -47
- package/src/2d/Box.svelte +21 -41
- package/src/2d/ColormapLegend.svelte +47 -111
- package/src/2d/Heatmap.svelte +107 -85
- package/src/2d/Legend.svelte +115 -126
- package/src/2d/Lines.svelte +54 -40
- package/src/2d/Multilines.svelte +73 -46
- package/src/2d/Points.svelte +76 -58
- package/src/2d/Rectangles.svelte +67 -58
- package/src/2d/Segments.svelte +59 -46
- package/src/2d/TextLabels.svelte +102 -72
- package/src/2d/TextLegend.svelte +17 -39
- package/src/2d/XAxis.svelte +49 -99
- package/src/2d/YAxis.svelte +47 -94
- package/src/2d/index.js +2 -1
- package/src/3d/Axes.svelte +306 -348
- package/src/3d/AxisLines.svelte +19 -37
- package/src/3d/AxisTickLabels.svelte +15 -32
- package/src/3d/Lines.svelte +59 -32
- package/src/3d/Mesh.svelte +106 -128
- package/src/3d/Points.svelte +56 -36
- package/src/3d/Segments.svelte +68 -52
- package/src/3d/TextLabels.svelte +63 -49
- package/src/3d/XAxis.svelte +36 -113
- package/src/3d/YAxis.svelte +36 -113
- package/src/3d/ZAxis.svelte +37 -114
- package/src/3d/index.js +2 -1
- package/src/constants.js +103 -0
- package/src/methods.js +1484 -0
- package/src/2d/Axis.svelte +0 -60
- package/src/3d/Axis.svelte +0 -64
- package/src/Colors.js +0 -51
- package/src/Utils.js +0 -639
package/src/2d/TextLabels.svelte
CHANGED
|
@@ -1,97 +1,127 @@
|
|
|
1
|
+
<!--
|
|
2
|
+
@component Adds a series with line segments.
|
|
3
|
+
|
|
4
|
+
Main properties:
|
|
5
|
+
- `xValues`- vector or array with x-coordinates for each label.
|
|
6
|
+
- `yValues`- vector or array with y-coordinates for each label.
|
|
7
|
+
- `labels`- single string or array with labels (strings).
|
|
8
|
+
- `pos`- position of labels related to coordinates (`0` - middle - default, `1` - bottom, `2` - left, `3` - top, `4` - right).
|
|
9
|
+
- `faceColor`- face color of label symbols, default: `Colors.PRIMARY_TEXT`.
|
|
10
|
+
- `lineColor`- border colors of label symbols, default: `'transparent'`.
|
|
11
|
+
- `lineWidth`- border width of label symbols in pixels, default: `0`.
|
|
12
|
+
- `textSize`- size of label symbols in em, default: `1`.
|
|
13
|
+
- `className`- CSS class name for the labels group, default: `'series-text'`.
|
|
14
|
+
- `rotateAngle`- angle to rotate labels, default: `0`.
|
|
15
|
+
- `onclick` - function (callback) to be called when user clicks on a label.
|
|
16
|
+
|
|
17
|
+
Example:
|
|
18
|
+
|
|
19
|
+
```svelte
|
|
20
|
+
<script>
|
|
21
|
+
import { Axes, TextLabels } from 'svelte-plots-basic/2d';
|
|
22
|
+
|
|
23
|
+
const xValues = [1, 2, 3, 4];
|
|
24
|
+
const yValues = [1, 2, 3, 4];
|
|
25
|
+
const labels = ['🤣', '😀', '😀', '🤣'];
|
|
26
|
+
</script>
|
|
27
|
+
|
|
28
|
+
<Axes limX={[0, 5]} limY={[0, 5]} >
|
|
29
|
+
<TextLabels {xValues} {yValues} {labels} />
|
|
30
|
+
</Axes>
|
|
31
|
+
```
|
|
32
|
+
-->
|
|
1
33
|
<script>
|
|
2
|
-
/****************************************************
|
|
3
|
-
* TextLabels component *
|
|
4
|
-
* -------------------- *
|
|
5
|
-
* shows a series of text labels on the plot *
|
|
6
|
-
*****************************************************/
|
|
7
|
-
|
|
8
34
|
import { getContext } from 'svelte';
|
|
9
|
-
import { Colors } from '../
|
|
10
|
-
import { checkCoords } from '../
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
export let rotateAngle = 0; // angle to rotate labels
|
|
28
|
-
|
|
29
|
-
/*****************************************/
|
|
30
|
-
/* Component code */
|
|
31
|
-
/*****************************************/
|
|
32
|
-
|
|
33
|
-
// get axes context and reactive variables needed to compute coordinates
|
|
34
|
-
const axes = getContext('axes');
|
|
35
|
-
const scale = axes.scale;
|
|
36
|
-
const tX = axes.tX;
|
|
37
|
-
const tY = axes.tY;
|
|
38
|
-
const isOk = axes.isOk;
|
|
39
|
-
|
|
35
|
+
import { Colors } from '../constants';
|
|
36
|
+
import { checkCoords, transformCoords, handleClick } from '../methods';
|
|
37
|
+
import { LABELS_MARGIN } from '../constants';
|
|
38
|
+
|
|
39
|
+
let {
|
|
40
|
+
xValues, // vector or array with x-coordinates for each label
|
|
41
|
+
yValues, // vector or array with y-coordinates for each label
|
|
42
|
+
labels, // array with labels (strings)
|
|
43
|
+
pos = 0, // position of labels related to coordinates (middle, bottom, left, top, right)
|
|
44
|
+
faceColor = Colors.PRIMARY_TEXT, // face color of label symbols
|
|
45
|
+
lineColor = 'transparent', // border colors of label symbols
|
|
46
|
+
lineWidth = 0, // border width of label symbols
|
|
47
|
+
textSize = 1, // size of label symbols
|
|
48
|
+
rotateAngle = 0, // angle to rotate labels
|
|
49
|
+
onclick, // function to be called if onclick event fires
|
|
50
|
+
} = $props();
|
|
51
|
+
|
|
52
|
+
// array with text anchors for correct positioning
|
|
40
53
|
const textAnchors = ['middle', 'middle', 'start', 'middle', 'end'];
|
|
41
54
|
|
|
42
|
-
|
|
43
|
-
|
|
55
|
+
// sanity checks of coordinates provided by user
|
|
56
|
+
const xv = $derived(checkCoords(xValues, 'TextLabels'));
|
|
57
|
+
const yv = $derived(xv ? checkCoords(yValues, 'TextLabels', xv.length) : null);
|
|
44
58
|
|
|
45
|
-
//
|
|
46
|
-
|
|
59
|
+
// sanity check for labels
|
|
60
|
+
const l = $derived.by(() => {
|
|
61
|
+
if (!yv) return null;
|
|
47
62
|
|
|
48
|
-
if (
|
|
49
|
-
|
|
63
|
+
if (Array.isArray(labels) && labels.length !== yv.length) {
|
|
64
|
+
console.error('TextLabels: number of elements in "labels" does not match number of coordinates');
|
|
65
|
+
return null;
|
|
50
66
|
}
|
|
51
67
|
|
|
52
|
-
|
|
53
|
-
|
|
68
|
+
return labels;
|
|
69
|
+
});
|
|
54
70
|
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
}
|
|
71
|
+
// sanity check for position values
|
|
72
|
+
const p = $derived.by(() => {
|
|
73
|
+
if (!l) return null;
|
|
59
74
|
|
|
60
|
-
if (
|
|
61
|
-
|
|
75
|
+
if (typeof labels === 'string' && typeof pos !== 'number' ) {
|
|
76
|
+
console.error('TextLabels: parameter "labels" is provided as single string, so "pos" must be a single number.');
|
|
77
|
+
return null;
|
|
62
78
|
}
|
|
63
|
-
}
|
|
64
79
|
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
80
|
+
return pos;
|
|
81
|
+
})
|
|
82
|
+
|
|
83
|
+
// get axes context and compute coordinates
|
|
84
|
+
const axes = getContext('axes');
|
|
85
|
+
const x = $derived(xv && p !== null ? transformCoords(xv, axes.tX()) : null);
|
|
86
|
+
const y = $derived(yv && x ? transformCoords(yv, axes.tY()) : null);
|
|
87
|
+
|
|
88
|
+
// define x and y shifts for positioning
|
|
89
|
+
const m = $derived(LABELS_MARGIN[axes.scales().plot]);
|
|
90
|
+
const dx = $derived([0, 0, m, 0, -m, m]);
|
|
91
|
+
const dy = $derived([0, m, 0, -m, 0, m]);
|
|
71
92
|
|
|
72
93
|
// styles for the elements
|
|
73
|
-
|
|
94
|
+
const textStyleStr = $derived(`fill:${faceColor};stroke-width:${lineWidth}px;stroke:${lineColor};font-size:${textSize}em;`);
|
|
95
|
+
|
|
96
|
+
// local status
|
|
97
|
+
const isOk = $derived(x && y && p !== null && l && x.length === y.length);
|
|
74
98
|
</script>
|
|
75
99
|
|
|
76
|
-
{#if
|
|
77
|
-
|
|
100
|
+
{#if isOk}
|
|
101
|
+
<!-- svelte-ignore a11y_click_events_have_key_events -->
|
|
102
|
+
<!-- svelte-ignore a11y_no_static_element_interactions -->
|
|
103
|
+
<g class="series series-textlabels" style={textStyleStr} onclick={(e) => handleClick(e, 'text', onclick)} >
|
|
78
104
|
|
|
79
105
|
{#if typeof labels === 'string'}
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
106
|
+
|
|
107
|
+
{#each x as v, i}
|
|
108
|
+
<text data-id={i} x={x[i]} y={y[i]} dx={dx[pos]} dy={dy[pos]}
|
|
109
|
+
transform={rotateAngle !== undefined ? `rotate(${rotateAngle}, ${x[i] + dx[pos]}, ${y[i] + dy[pos]})` : ''}
|
|
110
|
+
text-anchor={textAnchors[pos[i]]}>{@html labels}</text>
|
|
111
|
+
{/each}
|
|
85
112
|
|
|
86
113
|
{:else if (Array.isArray(pos) || ArrayBuffer.isView(pos))}
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
114
|
+
|
|
115
|
+
{#each x as v, i}
|
|
116
|
+
<text data-id={i} x={x[i]} y={y[i]} dx={dx[pos[i]]} dy={dy[pos[i]]} text-anchor={textAnchors[pos[i]]}>{@html labels[i]}</text>
|
|
117
|
+
{/each}
|
|
90
118
|
|
|
91
119
|
{:else}
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
120
|
+
|
|
121
|
+
{#each x as v, i}
|
|
122
|
+
<text data-id={i} x={x[i]} y={y[i]} dx={dx[pos]} dy={dy[pos]} text-anchor={textAnchors[pos]}>{@html labels[i]}</text>
|
|
123
|
+
{/each}
|
|
124
|
+
|
|
95
125
|
{/if}
|
|
96
126
|
</g>
|
|
97
127
|
{/if}
|
package/src/2d/TextLegend.svelte
CHANGED
|
@@ -1,50 +1,28 @@
|
|
|
1
1
|
<script>
|
|
2
|
-
/****************************************************
|
|
3
|
-
* TextLegend *
|
|
4
|
-
* -------------------- *
|
|
5
|
-
* shows list of several formatted text elements *
|
|
6
|
-
*****************************************************/
|
|
7
|
-
|
|
8
2
|
import { getContext } from 'svelte';
|
|
9
|
-
import { Colors } from '../
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
export let borderColor = "transparent"; // border color of the text symbols
|
|
23
|
-
export let borderWidth = 0; // border width of the text symbols
|
|
24
|
-
export let textSize = 1; // size of the text symbols
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
/*****************************************/
|
|
28
|
-
/* Component code */
|
|
29
|
-
/*****************************************/
|
|
3
|
+
import { Colors } from '../constants';
|
|
4
|
+
|
|
5
|
+
let {
|
|
6
|
+
left, // vector/array with coordinates of lef side of each text box
|
|
7
|
+
top, // vector/array with coordinates of top side of each text box
|
|
8
|
+
dx = "0", // horizontal margin between left size and text in 'em' units
|
|
9
|
+
dy = "1.25em", // vertical margin between elements in 'em' units
|
|
10
|
+
elements, // array with text elements (svg tags are acceptable)
|
|
11
|
+
faceColor = Colors.PRIMARY_TEXT, // face color of the text symbols
|
|
12
|
+
lineColor = "transparent", // border color of the text symbols
|
|
13
|
+
lineWidth = 0, // border width of the text symbols
|
|
14
|
+
textSize = 1, // size of the text symbols
|
|
15
|
+
} = $props()
|
|
30
16
|
|
|
31
17
|
const axes = getContext('axes');
|
|
32
|
-
const
|
|
33
|
-
const
|
|
34
|
-
const isOk = axes.isOk;
|
|
35
|
-
|
|
36
|
-
// reactive variables for coordinates of data points in pixels
|
|
37
|
-
let x, y = undefined;
|
|
38
|
-
$: if ($isOk) {
|
|
39
|
-
x = axes.transform([left], $tX.coords);
|
|
40
|
-
y = axes.transform([top], $tY.coords);
|
|
41
|
-
}
|
|
18
|
+
const x = $derived(left ? transformCoords([left], axes.tX()) : null);
|
|
19
|
+
const y = $derived(top ? transformCoords([top], axes.tY()) : null );
|
|
42
20
|
|
|
43
21
|
// styles for bars and labels
|
|
44
|
-
|
|
22
|
+
const textStyleStr = $derived(`fill:${faceColor};stroke-width:${lineWidth}px;stroke:${lineColor};font-size:${textSize}em;`);
|
|
45
23
|
</script>
|
|
46
24
|
|
|
47
|
-
{#if
|
|
25
|
+
{#if x && y && elements.length > 0}
|
|
48
26
|
<text style={textStyleStr} x={x} y={y} dx={dx} dy={dy} dominant-baseline="middle" text-anchor="start">
|
|
49
27
|
{#each elements as el, i}
|
|
50
28
|
<tspan {x} {dx} dy={i === 0 ? 0 : dy}>{@html el}</tspan>
|
package/src/2d/XAxis.svelte
CHANGED
|
@@ -1,111 +1,61 @@
|
|
|
1
|
+
<!--
|
|
2
|
+
@component Adds x-axis with ticks, label, etc.
|
|
3
|
+
|
|
4
|
+
Main properties:
|
|
5
|
+
- `label` - text label for the axis (optional).
|
|
6
|
+
- `showGrid` - logical, show or not vertical grid lines, default: `false`.
|
|
7
|
+
- `ticks` - array or vector with tick positions (by default will be generated automatically based on `limX`).
|
|
8
|
+
- `tickLabels` - array with tick labels to show at each tick (by default will be generated automatically).
|
|
9
|
+
- `las` - orientation of tick labels (`1` - horizontal - default, `2` - vertical).
|
|
10
|
+
- `whole` - logical, show numeric tick labels as whole numbers (without decimals) or not, default: `false`.
|
|
11
|
+
|
|
12
|
+
Example:
|
|
13
|
+
```svelte
|
|
14
|
+
<script>
|
|
15
|
+
import {Axes, XAxis} from 'svelte-plots-basic/2d';
|
|
16
|
+
</script>
|
|
17
|
+
<Axes>
|
|
18
|
+
<XAxis label="x-axis label" showGrid={true} />
|
|
19
|
+
</Axes>
|
|
20
|
+
```
|
|
21
|
+
-->
|
|
1
22
|
<script>
|
|
2
|
-
/*****************************************************/
|
|
3
|
-
/* X-Axis */
|
|
4
|
-
/* -------------------- */
|
|
5
|
-
/* shows x-axis and its elements (ticks, grid, etc.) */
|
|
6
|
-
/*****************************************************/
|
|
7
23
|
|
|
8
24
|
import { getContext } from 'svelte';
|
|
9
|
-
import {
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
export let tickLabels = undefined; // vector with labels for each tick (by default tick values will be used)
|
|
22
|
-
export let showGrid = false; // logical, show or not grid lines
|
|
23
|
-
export let las = 1; // orientation of tick labels (1 - horizontal, 2 - vertical)
|
|
24
|
-
export let whole = false; // should the ticks be a whole number or not
|
|
25
|
-
|
|
26
|
-
export let lineColor = Colors.DARKGRAY; // color of axis and tick lines
|
|
27
|
-
export let gridColor = Colors.MIDDLEGRAY; // color og grid lines
|
|
28
|
-
export let textColor = Colors.DARKGRAY; // color of text tick labels
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
/*****************************************/
|
|
32
|
-
/* Component code */
|
|
33
|
-
/*****************************************/
|
|
34
|
-
|
|
35
|
-
// sanity checks of input parameters
|
|
36
|
-
if (slot !== 'xaxis') {
|
|
37
|
-
throw('Component XAxis must have "slot=\'xaxis\'" attribute.');
|
|
38
|
-
}
|
|
39
|
-
|
|
40
|
-
// get axes context and adjust x margins
|
|
25
|
+
import { text2svg, validateTicks, validateTickLabels } from '../methods';
|
|
26
|
+
|
|
27
|
+
let {
|
|
28
|
+
label, // text label
|
|
29
|
+
showGrid = false, // logical, show or not grid lines
|
|
30
|
+
ticks, // vector with numeric tick positions (by default is computed automatically)
|
|
31
|
+
tickLabels, // vector with labels for each tick (by default tick position values will be used)
|
|
32
|
+
las = 1, // orientation of tick labels (1 - horizontal, 2 - vertical)
|
|
33
|
+
whole = false // should the tick positions be a whole number or not.
|
|
34
|
+
} = $props();
|
|
35
|
+
|
|
36
|
+
// get axes context and send axis parameters to parent
|
|
41
37
|
const axes = getContext('axes');
|
|
42
38
|
|
|
43
|
-
//
|
|
44
|
-
|
|
45
|
-
const yLim = axes.yLim;
|
|
46
|
-
const scale = axes.scale;
|
|
47
|
-
const xscale = axes.xscale;
|
|
48
|
-
const isOk = axes.isOk;
|
|
49
|
-
const tY = axes.tY;
|
|
39
|
+
// reactive effects to validate and activate user input
|
|
40
|
+
$effect(() => {
|
|
50
41
|
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
let tickCoords = [];
|
|
55
|
-
let tfCoords = [];
|
|
56
|
-
let tickFactor = 0;
|
|
42
|
+
// process label text in case if there are sub or superscripts and adjust height of label correspondingly
|
|
43
|
+
const axisLabel = text2svg(label);
|
|
44
|
+
const labelHeight = axisLabel ? (axisLabel.length === label.length ? 1 : 1.4) : 0;
|
|
57
45
|
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
const ticksX = getAxisTicks(ticks, $xLim, axes.XTICK_NUM[$xscale], true, whole);
|
|
63
|
-
const tickNum = ticksX.length;
|
|
64
|
-
|
|
65
|
-
// compute tick y-coordinates (up and bottom)
|
|
66
|
-
const dY = axes.invTransform([axes.TICK_SIZE[$scale]], $tY.objects)[0];
|
|
67
|
-
const ticksY1 = Vector.fill($yLim[0], tickNum);
|
|
68
|
-
const ticksY2 = ticksY1.add(dY);
|
|
69
|
-
|
|
70
|
-
// compute coordinates for the ends of grid
|
|
71
|
-
const gridYEnd = Vector.fill($yLim[1], tickNum);
|
|
72
|
-
|
|
73
|
-
// tick labels and tick factor
|
|
74
|
-
if (ticks === undefined || tickLabels === undefined) {
|
|
75
|
-
[tickFactor, tickLabels] = getTickLabels(ticksX.v);
|
|
46
|
+
// if ticks are provided check if they are valid and preprocess them
|
|
47
|
+
let ticksProcessed, error = '';
|
|
48
|
+
if (ticks) {
|
|
49
|
+
[ticksProcessed, error] = validateTicks(ticks, axes.limX())
|
|
76
50
|
}
|
|
77
51
|
|
|
78
|
-
if
|
|
79
|
-
|
|
52
|
+
// if tick labels are provided check if they are valid and preprocess them
|
|
53
|
+
if (!error && tickLabels) {
|
|
54
|
+
[tickLabels, error] = validateTickLabels(tickLabels, ticksProcessed);
|
|
80
55
|
}
|
|
81
56
|
|
|
82
|
-
//
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
];
|
|
87
|
-
|
|
88
|
-
axisLine = [
|
|
89
|
-
[vector([$xLim[0]]), vector([$yLim[0]])],
|
|
90
|
-
[vector([$xLim[1]]), vector([$yLim[0]])]
|
|
91
|
-
]
|
|
92
|
-
|
|
93
|
-
tickCoords = [
|
|
94
|
-
[ticksX, ticksY2],
|
|
95
|
-
[ticksX, ticksY1]
|
|
96
|
-
];
|
|
97
|
-
|
|
98
|
-
tfCoords = [
|
|
99
|
-
[null, null],
|
|
100
|
-
[[$xLim[1]], [ticksY1.v[tickNum - 1] - 1.5 * dY]],
|
|
101
|
-
];
|
|
102
|
-
}
|
|
57
|
+
// activate axis
|
|
58
|
+
axes.setXAxis({show: true, error: error, label: axisLabel, ticks: ticksProcessed,
|
|
59
|
+
labelHeight, showGrid, las, whole, tickLabels});
|
|
60
|
+
});
|
|
103
61
|
</script>
|
|
104
|
-
|
|
105
|
-
{#if $isOk && axisLine.length > 0}
|
|
106
|
-
<Axis
|
|
107
|
-
className="mdaplot__xaxis" pos={1}
|
|
108
|
-
{lineColor} {gridColor} {textColor} {showGrid} {grid} {axisLine}
|
|
109
|
-
{tickCoords} {tickLabels} {tfCoords} {tickFactor} {las}
|
|
110
|
-
/>
|
|
111
|
-
{/if}
|
package/src/2d/YAxis.svelte
CHANGED
|
@@ -1,111 +1,64 @@
|
|
|
1
|
+
<!--
|
|
2
|
+
@component Adds y-axis with ticks, label, etc.
|
|
3
|
+
|
|
4
|
+
Main properties:
|
|
5
|
+
- `label` - text label for the axis (optional).
|
|
6
|
+
- `showGrid` - logical, show or not horizontal grid lines, default: `false`.
|
|
7
|
+
- `ticks` - array or vector with tick positions (by default will be generated automatically based on `limX`).
|
|
8
|
+
- `tickLabels` - array with tick labels to show at each tick (by default will be generated automatically).
|
|
9
|
+
- `las` - orientation of tick labels (`1` - horizontal - default, `2` - vertical).
|
|
10
|
+
- `whole` - logical, show numeric tick labels as whole numbers (without decimals) or not, default: `false`.
|
|
11
|
+
|
|
12
|
+
Example:
|
|
13
|
+
```svelte
|
|
14
|
+
<script>
|
|
15
|
+
import {Axes, YAxis} from 'svelte-plots-basic/2d';
|
|
16
|
+
</script>
|
|
17
|
+
<Axes>
|
|
18
|
+
<YAxis label="y-axis label" showGrid={true} />
|
|
19
|
+
</Axes>
|
|
20
|
+
```
|
|
21
|
+
-->
|
|
1
22
|
<script>
|
|
2
|
-
/****************************************************
|
|
3
|
-
* Y-Axis *
|
|
4
|
-
* -------------------- *
|
|
5
|
-
* shows y-axis and its elements (ticks, grid, etc.) *
|
|
6
|
-
*****************************************************/
|
|
7
23
|
|
|
8
24
|
import { getContext } from 'svelte';
|
|
9
|
-
import {
|
|
10
|
-
import { Colors } from '../Colors.js';
|
|
11
|
-
import { getAxisTicks, getTickLabels } from '../Utils.js';
|
|
12
|
-
import Axis from './Axis.svelte';
|
|
25
|
+
import { text2svg, validateTicks, validateTickLabels } from '../methods';
|
|
13
26
|
|
|
14
27
|
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
28
|
+
let {
|
|
29
|
+
label, // text label
|
|
30
|
+
ticks, // vector with numeric tick positions (by default is computed automatically)
|
|
31
|
+
tickLabels, // vector with labels for each tick (by default tick values will be used)
|
|
32
|
+
showGrid = false, // logical, show or not grid lines
|
|
33
|
+
las = 1, // orientation of tick labels (1 - horizontal, 2 - vertical)
|
|
34
|
+
whole = false // should the ticks be a whole number or not
|
|
35
|
+
} = $props();
|
|
18
36
|
|
|
19
|
-
export let slot = 'yaxis'; // slot the component must be placed in (must be "yaxis")
|
|
20
|
-
export let ticks = undefined; // vector with numeric tick positions (by default is computed automatically)
|
|
21
|
-
export let tickLabels = undefined; // vector with labels for each tick (by default tick values will be used)
|
|
22
|
-
export let showGrid = false; // logical, show or not grid lines
|
|
23
|
-
export let las = 1; // orientation of tick labels (1 - horizontal, 2 - vertical)
|
|
24
|
-
export let whole = false; // should the ticks be a whole number or not
|
|
25
37
|
|
|
26
|
-
|
|
27
|
-
export let gridColor = Colors.MIDDLEGRAY; // color og grid lines
|
|
28
|
-
export let textColor = Colors.DARKGRAY; // color of text tick labels
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
/*****************************************/
|
|
32
|
-
/* Component code */
|
|
33
|
-
/*****************************************/
|
|
34
|
-
|
|
35
|
-
// sanity checks of input parameters
|
|
36
|
-
if (slot !== 'yaxis') {
|
|
37
|
-
throw('Component YAxis must have "slot=\'yaxis\'" attribute.');
|
|
38
|
-
}
|
|
39
|
-
|
|
40
|
-
// get axes context and adjust x margins
|
|
38
|
+
// get axes context and send axis parameters to parent
|
|
41
39
|
const axes = getContext('axes');
|
|
42
40
|
|
|
43
|
-
//
|
|
44
|
-
|
|
45
|
-
const yLim = axes.yLim;
|
|
46
|
-
const scale = axes.scale;
|
|
47
|
-
const yscale = axes.yscale;
|
|
48
|
-
const isOk = axes.isOk;
|
|
49
|
-
const tX = axes.tX;
|
|
50
|
-
|
|
51
|
-
// prepare variables for coordinates
|
|
52
|
-
let grid = [];
|
|
53
|
-
let axisLine = [];
|
|
54
|
-
let tickCoords = [];
|
|
55
|
-
let tfCoords = [];
|
|
56
|
-
let tickFactor = 0;
|
|
57
|
-
|
|
58
|
-
// compute tick x-coordinates
|
|
59
|
-
$: if ($isOk) {
|
|
41
|
+
// reactive effects to validate and activate user input
|
|
42
|
+
$effect(() => {
|
|
60
43
|
|
|
61
|
-
//
|
|
62
|
-
const
|
|
63
|
-
const
|
|
44
|
+
// process label text in case if there are sub or superscripts and adjust height of label correspondingly
|
|
45
|
+
const axisLabel = text2svg(label);
|
|
46
|
+
const labelHeight = axisLabel ? (axisLabel.length === label.length ? 1.0 : 1.4) : 0;
|
|
64
47
|
|
|
65
|
-
// compute tick y-coordinates (up and bottom)
|
|
66
|
-
const dX = axes.invTransform([axes.TICK_SIZE[$scale]], $tX.objects)[0];
|
|
67
|
-
const ticksX1 = Vector.fill($xLim[0], tickNum)
|
|
68
|
-
const ticksX2 = ticksX1.add(dX)
|
|
69
48
|
|
|
70
|
-
//
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
if (ticks === undefined || tickLabels === undefined) {
|
|
75
|
-
[tickFactor, tickLabels] = getTickLabels(ticksY.v)
|
|
49
|
+
// if ticks are provided check if they are valid and preprocess them
|
|
50
|
+
let ticksProcessed, error = '';
|
|
51
|
+
if (ticks) {
|
|
52
|
+
[ticksProcessed, error] = validateTicks(ticks, axes.limY())
|
|
76
53
|
}
|
|
77
54
|
|
|
78
|
-
if
|
|
79
|
-
|
|
55
|
+
// if tick labels are provided check if they are valid and preprocess them
|
|
56
|
+
if (!error && tickLabels) {
|
|
57
|
+
[tickLabels, error] = validateTickLabels(tickLabels, ticksProcessed);
|
|
80
58
|
}
|
|
81
59
|
|
|
82
|
-
//
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
];
|
|
87
|
-
|
|
88
|
-
axisLine = [
|
|
89
|
-
[vector([$xLim[0]]), vector([$yLim[0]])],
|
|
90
|
-
[vector([$xLim[0]]), vector([$yLim[1]])]
|
|
91
|
-
]
|
|
92
|
-
|
|
93
|
-
tickCoords = [
|
|
94
|
-
[ticksX1, ticksY],
|
|
95
|
-
[ticksX2, ticksY]
|
|
96
|
-
];
|
|
97
|
-
|
|
98
|
-
tfCoords = [
|
|
99
|
-
[[], []],
|
|
100
|
-
[[ticksX2.v[tickNum - 1] + dX], [$yLim[1]]],
|
|
101
|
-
];
|
|
102
|
-
}
|
|
60
|
+
// activate axis
|
|
61
|
+
axes.setYAxis({show: true, error: error, label: axisLabel, ticks: ticksProcessed,
|
|
62
|
+
labelHeight, showGrid, las, whole, tickLabels, pos: 4});
|
|
63
|
+
});
|
|
103
64
|
</script>
|
|
104
|
-
|
|
105
|
-
{#if $isOk && axisLine.length > 0}
|
|
106
|
-
<Axis
|
|
107
|
-
className="mdaplot__yaxis" pos={4}
|
|
108
|
-
{lineColor} {gridColor} {textColor} {showGrid} {grid} {axisLine}
|
|
109
|
-
{tickCoords} {tickLabels} {tfCoords} {tickFactor} {las}
|
|
110
|
-
/>
|
|
111
|
-
{/if}
|
package/src/2d/index.js
CHANGED
|
@@ -18,4 +18,5 @@ export { default as Legend } from './Legend.svelte';
|
|
|
18
18
|
export { default as TextLegend } from './TextLegend.svelte';
|
|
19
19
|
export { default as ColormapLegend } from './ColormapLegend.svelte';
|
|
20
20
|
|
|
21
|
-
export { getcolmap
|
|
21
|
+
export { getcolmap } from '../methods.js';
|
|
22
|
+
export { Colors } from '../constants.js';
|