svelte-plots-basic 2.5.7 → 3.0.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.
@@ -1,67 +1,94 @@
1
- <script>
2
- /****************************************************
3
- * Multilines *
4
- * -------------------- *
5
- * shows series of lines components *
6
- *****************************************************/
7
-
8
- import { getContext } from 'svelte';
9
- import { ismatrix } from 'mdatools/arrays';
10
- import { Colors } from '../Colors';
11
- import { val2p } from '../Utils';
1
+ <!--
2
+ @component Adds a series of polylines (like `<Lines>` but with several of those).
12
3
 
4
+ Main properties:
5
+ - `xValues` - array of vector with x-coordinates of the points.
6
+ - `yValues` - Matrix (object of class `Matrix` from `mdatools/array`) with y-coordinates of the points.
7
+ - `lineColor` - lines color, default: `Colors.PRIMARY`.
8
+ - `lineWidth` - width (thickness) of the lines in pixels, defailt: `1`.
9
+ - `lineType` - type of segment lines (`1` - solid, `2` - dashed, `3` - dotted, `4` - dashdot).
10
+ - `onclick` - function (callback) to be called when user clicks on a polyline.
13
11
 
14
- /*****************************************/
15
- /* Input parameters */
16
- /*****************************************/
12
+ Example:
17
13
 
18
- export let xValues; // vector or array with x-coordinates of the points
19
- export let yValues; // matrix (!) with y-coordinates of the points - every column is separate line
20
- export let lineWidth = 1; // width (thickness) of the lines
21
- export let lineColor = Colors.PRIMARY; // color of the lines
22
- export let lineType = 1; // type of the lines (1 - solid, 2 - dashed, 3 - dotted, 4 - dashdot)
23
- export let title = ''; // title of the bar series (reserved for future use)
14
+ ```svelte
15
+ <script>
16
+ import { Vector, cbind } from 'mdatools/arrays';
17
+ import { Axes, Multilines } from 'svelte-plots-basic/2d';
24
18
 
19
+ const xValues = Vector.seq(0, 100, 100);
20
+ const YY = cbind(
21
+ xValues.apply(v => Math.sin(v)),
22
+ xValues.apply(v => Math.sin(v + 0.5) + 0.1),
23
+ xValues.apply(v => Math.sin(v - 0.5) - 0.1),
24
+ xValues.apply(v => Math.sin(v + 1.0) + 0.2),
25
+ xValues.apply(v => Math.sin(v - 1.0) - 0.2)
26
+ )
27
+ </script>
25
28
 
26
- /*****************************************/
27
- /* Component code */
28
- /*****************************************/
29
+ <Axes limX={[0, 10]} limY={[-1.2, 1.2]}>
30
+ <Multilines {xValues} {yValues} />
31
+ </Axes>
32
+ ```
33
+ -->
34
+ <script>
35
+ import { getContext } from 'svelte';
36
+ import { ismatrix } from 'mdatools/arrays';
37
+ import { Colors } from '../constants';
38
+ import { val2p, checkCoords, handleClick } from '../methods';
39
+ import { LINE_STYLES } from '../constants';
29
40
 
30
- // get axes context
31
- const axes = getContext('axes');
41
+ /** @type {Props} */
42
+ let {
43
+ xValues, // vector or array with x-coordinates of the points
44
+ yValues, // vector or array with y-coordinates of the points
45
+ lineColor = Colors.PRIMARY, // color of segment lines
46
+ lineType = 1, // type of segment lines (1 - solid, 2 - dashed, 3 - dotted, 4 - dashdot)
47
+ lineWidth = 1, // width (thickness) of segment lines
48
+ onclick, // function to be called if onclick event fires
49
+ } = $props();
32
50
 
33
- const scale = axes.scale;
34
- const isOk = axes.isOk;
35
- const tX = axes.tX;
36
- const tY = axes.tY;
51
+ // check user provided coordinates
52
+ const xv = $derived(checkCoords(xValues, 'Multilines'));
53
+ const yv = $derived.by(() => {
37
54
 
38
- // compute polynomial coordinates as string
39
- let pp;
40
- $: if ($isOk) {
55
+ if (!xv) return null;
41
56
 
42
57
  if (!ismatrix(yValues)) {
43
- throw Error('Multilines: parameter "yValues" must be a matrix.');
58
+ console.error('Multilines: parameter "yValues" must be a matrix.');
59
+ return null;
44
60
  }
45
61
 
46
- if (xValues.length !== yValues.nrows) {
47
- throw Error('Multilines: parameter "xValues" must have the same number of values as number of rows in "yValues".');
62
+ if (xv.length !== yValues.nrows) {
63
+ console.error('Multilines: parameter "xValues" must have the same number of values as number of rows in "yValues".');
48
64
  }
49
65
 
50
- pp = new Array(yValues.ncols);
51
- for (let i = 0; i < pp.length; i++) {
52
- pp[i] = val2p(xValues, yValues.getcolumn(i + 1), $tX, $tY, axes);
66
+ return yValues;
67
+ });
68
+
69
+
70
+ // get axes context and compute coordinates of the polylines
71
+ const axes = getContext('axes');
72
+ const pp = $derived.by(() => {
73
+ if (!xv || !yv) return null;
74
+ const lpp = new Array(yv.ncols);
75
+ for (let i = 0; i < lpp.length; i++) {
76
+ lpp[i] = val2p(xv, yv.getcolumn(i + 1), axes.tX(), axes.tY());
53
77
  }
54
- }
78
+ return lpp;
79
+ });
55
80
 
56
81
  // reactive variables for coordinates of data points in pixels
57
- $: lineStyleStr = `fill:transparent;stroke:${lineColor};stroke-width: ${lineWidth}px; stroke-dasharray:${axes.LINE_STYLES[$scale][lineType-1]}`;
82
+ const lineStyleStr = $derived(`fill:transparent;stroke:${lineColor};stroke-width: ${lineWidth}px; stroke-dasharray:${LINE_STYLES[axes.scales().plot][lineType-1]}`);
58
83
  </script>
59
84
 
60
- {#if $isOk && pp !== undefined && pp.length > 0}
61
- <g class="series series-multiline" style={lineStyleStr} title={title}>
62
- {#each pp as p}
63
- <polyline class="line" points={p}/>
64
- {/each}
65
- </g>
85
+ {#if pp && pp.length > 0}
86
+ <!-- svelte-ignore a11y_click_events_have_key_events -->
87
+ <!-- svelte-ignore a11y_no_static_element_interactions -->
88
+ <g onclick={(e) => handleClick(e, 'polyline', onclick)} class="series series-multiline" style={lineStyleStr}>
89
+ {#each pp as p}
90
+ <polyline class="line" points={p}/>
91
+ {/each}
92
+ </g>
66
93
  {/if}
67
94
 
@@ -1,68 +1,86 @@
1
+ <!--
2
+ @component Adds a series of points.
3
+
4
+ Main properties:
5
+ - `xValues` - array of vector with x-coordinates of the points.
6
+ - `yValues` - array of vector with y-coordinates of the points.
7
+ - `marker` - index for a point symbol (from 1 - default - to 8): `"●", "◼", "▲", "▼", "⬥", "+", "*", "✕"`.
8
+ - `faceColor` - face (fill) color of the markers, default: `'transparent'`.
9
+ - `lineColor` - border color of the markers, default: `Colors.PRIMARY`.
10
+ - `lineWidth` - width (thickness) of the markers' border in pixels, defailt: `1`.
11
+ - `markerSize` - size of the markers in em, defailt: `1`.
12
+ - `onclick` - function (callback) to be called when user clicks on a marker.
13
+
14
+ Example:
15
+
16
+ ```svelte
17
+ <script>
18
+ import { Axes, Points } from 'svelte-plots-basic/2d';
19
+
20
+ const xValues = [-3, -2, -1, 0, 1, 2, 3];
21
+ const yValues = [9, 4, 1, 0, 1, 4, 9];
22
+ </script>
23
+
24
+ <Axes limX={[-4, 4]} limY={[-2, 10]}>
25
+ <Points {xValues} {yValues} />
26
+ </Axes>
27
+ ```
28
+ -->
1
29
  <script>
2
- /****************************************************
3
- * Points *
4
- * -------------------- *
5
- * shows series of points/markers on a plot *
6
- *****************************************************/
7
-
8
30
  import { getContext } from 'svelte';
9
- import { Colors } from '../Colors';
10
- import { checkCoords } from '../Utils';
11
-
12
-
13
- /*****************************************/
14
- /* Input parameters */
15
- /*****************************************/
16
-
17
- export let xValues; // array of vector with x-coordinates of points
18
- export let yValues; // array of vector with y-coordinates of points
19
- export let marker = 1 // index for point symbol (from 1 to 8): "●", "◼", "▲", "▼", "⬥", "+", "*", "✕"
20
- export let faceColor = 'transparent'; // face (fill) color of the points
21
- export let borderColor = Colors.PRIMARY; // border color of the points
22
- export let borderWidth = 1; // width (thickness) of the points
23
- export let markerSize = 1; // size of the marker symbols
24
- export let title = ''; // title of the point series - required for handling mouse click events
25
-
26
-
27
- /*****************************************/
28
- /* Component code */
29
- /*****************************************/
30
-
31
- // get axes context and adjust x margins
32
- const axes = getContext('axes');
33
- const tX = axes.tX;
34
- const tY = axes.tY;
35
- const isOk = axes.isOk;
36
-
37
- let x, y, markerSymbol;
38
-
39
- // reactive calculations triggered by changes in coordinates and plot parameters
40
- $: if ($isOk) {
41
-
42
- if (typeof(marker) !== "number" || marker < 1 || marker > axes.MARKER_SYMBOLS.length) {
43
- throw Error(`Points: parameter "marker" must be a number from 1 to ${axes.MARKER_SYMBOLS.length}.`);
44
- }
45
-
46
- markerSymbol = axes.MARKER_SYMBOLS[marker - 1];
47
-
48
- x = axes.transform(checkCoords(xValues, 'Points'), $tX.coords);
49
- y = axes.transform(checkCoords(yValues, 'Points'), $tY.coords);
50
-
51
- // sanity check for input parameters
52
- if (x.length !== y.length) {
53
- throw Error('Points: parameters "xValues" and "yValues" must be vectors of the same length.')
31
+ import { Colors, MARKER_SYMBOLS } from '../constants';
32
+ import { checkCoords, transformCoords, handleClick } from '../methods';
33
+
34
+ /** @type {Props} */
35
+ let {
36
+ xValues, // array of vector with x-coordinates of points
37
+ yValues, // array of vector with y-coordinates of points
38
+ marker = 1, // index for point symbol (from 1 to 8): "●", "◼", "▲", "▼", "⬥", "+", "*", "✕"
39
+ faceColor = 'transparent', // face (fill) color of the points
40
+ lineColor = Colors.PRIMARY, // border color of the points
41
+ lineWidth = 1, // width (thickness) of the points
42
+ markerSize = 1, // size of the marker symbols
43
+ onclick, // function to be called if onclick event fires
44
+ } = $props();
45
+
46
+
47
+ // select which symbol to use as a marker
48
+ const markerSymbol = $derived.by(() => {
49
+ if (typeof(marker) !== 'number' || marker < 1 || marker > MARKER_SYMBOLS.length) {
50
+ console.error('Points: parameter "marker" must be a number from 1 to ' + MARKER_SYMBOLS.length + '.');
51
+ return null;
54
52
  }
55
- }
53
+ return MARKER_SYMBOLS[marker - 1]
54
+ });
56
55
 
57
- // styles for the elements
58
- $: textStyleStr = ['+', '✳', '✕'].includes(markerSymbol) ?
59
- `fill:${borderColor && borderColor !== 'transparent' ? borderColor : faceColor};stroke-width:0px;font-size:${markerSize}em;` :
60
- `fill:${faceColor};stroke-width:${borderWidth}px;stroke:${borderColor}; font-size:${markerSize}em;`;
56
+ // check user provided coordinates
57
+ const xv = $derived(checkCoords(xValues, 'Points'));
58
+ const yv = $derived(xv ? checkCoords(yValues, 'Points', xv.length) : null);
61
59
 
60
+ // get axes context and compute screen coordinates
61
+ const axes = getContext('axes');
62
+ const x = $derived(xv ? transformCoords(xv, axes.tX()) : null);
63
+ const y = $derived(x && yv ? transformCoords(yv, axes.tY()) : null);
64
+
65
+ // styles for the markers
66
+ const textStyleStr = $derived(['+', '✳', '✕'].includes(markerSymbol) ?
67
+ `fill:${lineColor && lineColor !== 'transparent' ? lineColor : faceColor};stroke-width:0px;font-size:${markerSize}em;` :
68
+ `fill:${faceColor};stroke-width:${lineWidth}px;stroke:${lineColor}; font-size:${markerSize}em;`
69
+ );
70
+
71
+ const alignStyleStr = $derived(['◼', '⬥', '+', '✳', '✕'].includes(markerSymbol) ?
72
+ `dominant-baseline:central;alignment-baseline:mathematical;` :
73
+ `dominant-baseline:middle;alignment-baseline:middle;`
74
+ );
75
+ // check if all coordinates are correct
76
+ const isOk = $derived(x && y && x.length === y.length);
62
77
  </script>
63
78
 
64
- {#if $isOk && x !== undefined && y !== undefined}
65
- <g class="series series-points" title={title} style={textStyleStr} dominant-baseline="middle" text-anchor="middle">
79
+ {#if isOk}
80
+ <!-- svelte-ignore a11y_click_events_have_key_events -->
81
+ <!-- svelte-ignore a11y_no_static_element_interactions -->
82
+ <g onclick={(e) => handleClick(e, 'text', onclick)}
83
+ class="series series-points" style={textStyleStr + alignStyleStr} text-anchor="middle">
66
84
  {#each x as v, i}
67
85
  <text x={x[i]} y={y[i]}>{markerSymbol}</text>
68
86
  {/each}
@@ -1,70 +1,79 @@
1
+ <!--
2
+ @component Adds a series of rectangles.
3
+
4
+ Main properties:
5
+ - `left` - array or vector with world coordinates of left side of the rectangles.
6
+ - `top` - array or vector with world coordinates of left side of the rectangles.
7
+ - `width` - width of the rectangles (one value or array/vector with individual values).
8
+ - `height` - height of the rectangles (one value or array/vector with individual values).
9
+ - `facColor` - face color of the rectangles (same for all).
10
+ - `lineColor` - border color of each rectangle.
11
+ - `lineWidth` - width (thickness) of border lines in pixels.
12
+ - `className`- CSS class name for the labels group, default: `'series-rect'`.
13
+ - `onclick` - function (callback) to be called when user clicks on any rectangle.
14
+
15
+
16
+ Example:
17
+ ```jsx
18
+ <script>
19
+ import { Matrix } from 'mdatools/arrays';
20
+ import { Axes, Rectangles } from 'svelte-plots-basic/2d';
21
+
22
+ const left = [0, 10, 20, 30];
23
+ const top = [10, 20, 30, 40];
24
+ const width = 5;
25
+ const height = 10;
26
+ </script>
27
+
28
+ <Axes limX={[0, 40]} limY={[0, 50]}>
29
+ <Rectangles {left} {top} {width} {height} lineColor="red" faceColor="pink" />
30
+ </Axes>
31
+ ```
32
+ -->
1
33
  <script>
2
- /****************************************************
3
- * Rectangles *
4
- * -------------------- *
5
- * shows series of rectangles *
6
- *****************************************************/
7
-
8
34
  import { getContext } from 'svelte';
9
35
  import { Vector } from 'mdatools/arrays';
10
- import { Colors } from '../Colors';
11
- import { checkCoords } from '../Utils';
12
-
13
-
14
- /*****************************************/
15
- /* Input parameters */
16
- /*****************************************/
17
-
18
- export let left; // array of vector with coordinates of left sides of the bars
19
- export let top; // array of vector with coordinates of top sides of the bars
20
- export let width; // single value (same for all) or vector/array with bar width
21
- export let height; // single value (same for all) or vector/array with bar height
22
- export let faceColor = Colors.PRIMARY; // color of bar faces (fill)
23
- export let borderColor = faceColor; // color of bar borders
24
- export let lineWidth = 1; // width (thickness) of bar border lines
25
- export let className = 'series-rect'; // CSS class name of the SVG group
26
- export let title = ''; // title of the rectangle series (reserved for future use)
27
-
28
-
29
- /*****************************************/
30
- /* Component code */
31
- /*****************************************/
32
-
33
- // get axes context and reactive variables needed to compute coordinates
36
+ import { Colors } from '../constants';
37
+ import { checkCoords, transformCoords, transformObjects, handleClick } from '../methods';
38
+
39
+ /** @type {Props} */
40
+ let {
41
+ left, // array of vector with coordinates of left sides of the bars
42
+ top, // array of vector with coordinates of top sides of the bars
43
+ width, // single value (same for all) or vector/array with bar width
44
+ height, // single value (same for all) or vector/array with bar height
45
+ faceColor = Colors.PRIMARY, // color of bar faces (fill)
46
+ lineColor = faceColor, // color of bar borders
47
+ lineWidth = 1, // width (thickness) of bar border lines
48
+ className = 'series-rect', // CSS class name of the SVG group
49
+ onclick, // function to be called if onclick event fires
50
+ } = $props();
51
+
52
+ // process provided values and compute world coordinates of rectangles
53
+ const l = $derived(checkCoords(left, 'Rectangles (left)'));
54
+ const t = $derived(l ? checkCoords(top, 'Rectangles (top)', l.length) : null);
55
+ const w = $derived(t ? (typeof width !== 'object' ? Vector.fill(width, l.length) : width) : null);
56
+ const h = $derived(t ? (typeof height !== 'object' ? Vector.fill(height, l.length) : height) : null);
57
+
58
+ // get axes context and compute screen coordinates
34
59
  const axes = getContext('axes');
35
- const tX = axes.tX;
36
- const tY = axes.tY;
37
- const isOk = axes.isOk;
38
-
39
- let rx, ry, rw, rh = undefined;
40
- $: {
41
- if ($isOk) {
42
- rx = axes.transform(checkCoords(left, 'Rectangles'), $tX.coords);
43
- ry = axes.transform(checkCoords(top, 'Rectangles'), $tY.coords);
44
-
45
- if (rx.length !== ry.length) {
46
- throw Error('Rectangles: parameters "left" and "top" must be vectors of the same length.');
47
- }
48
60
 
49
- if (typeof width !== 'object') {
50
- width = Vector.fill(width, left.length);
51
- }
52
-
53
- if (typeof height !== 'object') {
54
- height = Vector.fill(height, left.length);
55
- }
56
-
57
- rw = axes.transform(width, $tX.objects);
58
- rh = axes.transform(height, $tY.objects);
59
- }
60
- }
61
+ const rx = $derived(l ? transformCoords(l, axes.tX()) : null);
62
+ const ry = $derived(t ? transformCoords(t, axes.tY()) : null);
63
+ const rw = $derived(w ? transformObjects(w, axes.tX()) : null);
64
+ const rh = $derived(h ? transformObjects(h, axes.tY()) : null);
61
65
 
62
66
  // styles for bars and labels
63
- $: barsStyleStr = `fill:${faceColor};stroke:${borderColor};stroke-width:${lineWidth}px;`;
67
+ const barsStyleStr = $derived(`fill:${faceColor};stroke:${lineColor};stroke-width:${lineWidth}px;`);
68
+
69
+ // check if all coordinates are correct
70
+ const isOk = $derived(rx && ry && rw && rh && (rw.length === rx.length) && (rh.length === rx.length));
64
71
  </script>
65
72
 
66
- {#if $isOk}
67
- <g class="series {className}" title={title} style={barsStyleStr}>
73
+ {#if isOk}
74
+ <!-- svelte-ignore a11y_click_events_have_key_events -->
75
+ <!-- svelte-ignore a11y_no_static_element_interactions -->
76
+ <g class="series {className}" style={barsStyleStr} onclick={(e) => handleClick(e, 'rect', onclick)} >
68
77
  {#each left as v, i}
69
78
  <rect x={rx[i]} y={ry[i]} width={rw[i]} height={rh[i]} />
70
79
  {/each}
@@ -1,62 +1,75 @@
1
- <script>
2
- /****************************************************
3
- * Segments *
4
- * -------------------- *
5
- * shows series of line segments *
6
- *****************************************************/
1
+ <!--
2
+ @component Adds a series with line segments.
7
3
 
8
- import { getContext } from 'svelte';
9
- import { Colors } from '../Colors';
10
- import { checkCoords } from '../Utils';
4
+ Main properties:
5
+ - `xStart` - vector or array with x-coordinates of starting points.
6
+ - `xEnd` - or array with x-coordinates of end points.
7
+ - `yStart` - vector or array with y-coordinates of starting points.
8
+ - `yEnd` - vector or array with y-coordinates of end points.
9
+ - `lineColor` - color of segment lines, default: `Colors.PRIMARY`.
10
+ - `lineType` - type of segment lines (`1` - solid, `2` - dashed, `3` - dotted, `4` - dashdot).
11
+ - `lineWidth` - width (thickness) of segment lines in pixels, default: `1`.
12
+ - `onclick` - function (callback) to be called when user clicks on any segment.
11
13
 
12
- /*****************************************/
13
- /* Input parameters */
14
- /*****************************************/
14
+ Example:
15
15
 
16
- export let xStart; // vector or array with x-coordinates of starting points
17
- export let xEnd; // vector or array with x-coordinates of end points
18
- export let yStart; // vector or array with y-coordinates of starting points
19
- export let yEnd; // vector or array with y-coordinates of end points
20
- export let lineColor = Colors.PRIMARY; // color of segment lines
21
- export let lineType = 1; // type of segment lines (1 - solid, 2 - dashed, 3 - dotted, 4 - dashdot)
22
- export let lineWidth = 1; // width (thickness) of segment lines
23
- export let className = 'series-seg'; // CSS class name of the SVG group
24
- export let title = ''; // title of the segment series (reserved for future use)
16
+ ```svelte
17
+ <script>
18
+ import { Axes, Segments } from 'svelte-plots-basic/2d';
25
19
 
20
+ const xStart = [1, 2, 3, 4];
21
+ const xEnd = [1, 2, 3, 4];
22
+ const yStart = [-2, -1, 0, -1, -2];
23
+ const yEnd = [2, 1, 0, 1, 2];
24
+ </script>
26
25
 
27
- /*****************************************/
28
- /* Component code */
29
- /*****************************************/
26
+ <Axes limX={[0, 5]} limY={[-3, 3]} >
27
+ <Segments {xStart} {yStart} {xEnd} {yEnd} lineColor="red" lineType={2} />
28
+ </Axes>
29
+ ```
30
+ -->
31
+ <script>
32
+ import { getContext } from 'svelte';
33
+ import { Colors } from '../constants';
34
+ import { checkCoords, transformCoords, handleClick } from '../methods';
35
+ import { LINE_STYLES } from '../constants';
30
36
 
31
- // get axes context and reactive variables needed to compute coordinates
32
- const axes = getContext('axes');
33
- const scale = axes.scale;
34
- const isOk = axes.isOk;
35
- const tX = axes.tX;
36
- const tY = axes.tY;
37
+ let {
38
+ xStart, // vector or array with x-coordinates of starting points
39
+ xEnd, // vector or array with x-coordinates of end points
40
+ yStart, // vector or array with y-coordinates of starting points
41
+ yEnd, // vector or array with y-coordinates of end points
42
+ lineColor = Colors.PRIMARY, // color of segment lines
43
+ lineType = 1, // type of segment lines (1 - solid, 2 - dashed, 3 - dotted, 4 - dashdot)
44
+ lineWidth = 1, // width (thickness) of segment lines
45
+ onclick, // function to be called if onclick event fires
46
+ } = $props();
37
47
 
38
- let x1, x2, y1, y2 = undefined;
39
- $: {
40
- if ($isOk) {
41
48
 
42
- x1 = axes.transform(checkCoords(xStart, 'Segments'), $tX.coords);
43
- y1 = axes.transform(checkCoords(yStart, 'Segments'), $tY.coords);
44
- x2 = axes.transform(checkCoords(xEnd, 'Segments'), $tX.coords);
45
- y2 = axes.transform(checkCoords(yEnd, 'Segments'), $tY.coords);
49
+ // check user defined coordinates
50
+ const sx = $derived(checkCoords(xStart, 'Segments'));
51
+ const sy = $derived(sx ? checkCoords(yStart, 'Segments', sx.length) : null);
52
+ const ex = $derived(sy ? checkCoords(xEnd, 'Segments', sx.length) : null);
53
+ const ey = $derived(ex ? checkCoords(yEnd, 'Segments', sx.length) : null);
46
54
 
47
- const n = x1.length;
48
- if (x2.length !== n || y1.length !== n || y2.length !== n) {
49
- throw Error('Segments: parameters "xStart", "yStart", "xEnd" and "yEnd" should have the same length.');
50
- }
51
- }
52
- }
55
+ // get axes context and reactive variables needed to compute coordinates
56
+ const axes = getContext('axes');
57
+ const x1 = $derived(sx ? transformCoords(sx, axes.tX()) : null);
58
+ const x2 = $derived(ex ? transformCoords(ex, axes.tX()) : null);
59
+ const y1 = $derived(sy ? transformCoords(sy, axes.tY()) : null);
60
+ const y2 = $derived(ey ? transformCoords(ey, axes.tY()) : null);
53
61
 
54
62
  // reactive variables for coordinates of data points in pixels (and line style)
55
- $: lineStyleStr = `stroke:${lineColor};stroke-width: ${lineWidth}px;stroke-dasharray:${axes.LINE_STYLES[$scale][lineType-1]}`;
63
+ const lineStyleStr = $derived(`stroke:${lineColor};stroke-width: ${lineWidth}px;stroke-dasharray:${LINE_STYLES[axes.scales().plot][lineType-1]}`);
64
+
65
+ // local status
66
+ const isOk = $derived(x1 && x2 && y1 && y2);
56
67
  </script>
57
68
 
58
- {#if $isOk}
59
- <g class="series {className}" title={title} style={lineStyleStr}>
69
+ {#if isOk}
70
+ <!-- svelte-ignore a11y_click_events_have_key_events -->
71
+ <!-- svelte-ignore a11y_no_static_element_interactions -->
72
+ <g class="series series-segment" style={lineStyleStr} onclick={(e) => handleClick(e, 'line', onclick)}>
60
73
  {#each x1 as v, i}
61
74
  <line x1={x1[i]} x2={x2[i]} y1={y1[i]} y2={y2[i]} />
62
75
  {/each}