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/package.json CHANGED
@@ -1,17 +1,20 @@
1
1
  {
2
2
  "name": "svelte-plots-basic",
3
3
  "description": "Svelte components for creating simple plots",
4
- "version": "2.5.8",
4
+ "version": "3.0.1",
5
5
  "author": "Sergey Kucheryavskiy",
6
6
  "license": "MIT",
7
7
  "private": false,
8
8
  "exports": {
9
- "./utils": "./src/Utils.js",
9
+ "./utils": "./src/methods.js",
10
10
  "./2d": "./src/2d/index.js",
11
11
  "./2d/*.svelte": "./src/2d/*.svelte",
12
12
  "./3d": "./src/3d/index.js",
13
13
  "./3d/*.svelte": "./src/3d/*.svelte"
14
14
  },
15
+ "devDependencies": {
16
+ "svelte": "^5.x.x"
17
+ },
15
18
  "dependencies": {
16
19
  "mdatools": "^1.x.x"
17
20
  },
@@ -1,61 +1,69 @@
1
- <script>
2
- /****************************************************
3
- * Area *
4
- * -------------------- *
5
- * shows area of a polygon defined by set of points *
6
- *****************************************************/
1
+ <!--
2
+ @component Adds a closed polygon (area).
7
3
 
8
- import { getContext } from 'svelte';
9
- import { vector } from 'mdatools/arrays';
10
- import { Colors } from '../Colors';
11
- import { checkCoords, val2p } from '../Utils';
4
+ Main properties:
5
+ - `xValues` - array of vector with x-coordinates of the polygon's points.
6
+ - `yValues` - array of vector with y-coordinates of the polygon's points.
7
+ - `lineWidth` - width (thickness) of the polygon's line in pixels, defailt: `1`.
8
+ - `lineColor` - line color, default: `Colors.PRIMARY`.
9
+ - `lineType` - type of line (`1` - solid, `2` - dashed, `3` - dotted, `4` - dashdot).
10
+ - `facColor` - face (fill) color of the polygon, default: `'transparent'`.
11
+ - `opacity` - opacity of the face color, default: `1`.
12
+ - `onclick` - function (callback) to be called when user clicks on a polygon.
13
+
14
+ Example:
12
15
 
16
+ ```svelte
17
+ <script>
18
+ import { Axes, Area } from 'svelte-plots-basic/2d';
13
19
 
14
- /*****************************************/
15
- /* Input parameters */
16
- /*****************************************/
20
+ const xValues = [-3, -2, -1, 0, 1, 2, 3];
21
+ const yValues = [9, 4, 1, 0, 1, 4, 9];
22
+ </script>
17
23
 
18
- export let xValues; // x-coordinates of polygon (array or vector)
19
- export let yValues; // y-coordinates of polygon (array or vector)
20
- export let lineWidth = 1; // width of polygon lines
21
- export let lineColor = Colors.PRIMARY; // color for polygon lines
22
- export let fillColor = Colors.PRIMARY; // color of area inside polygon
23
- export let opacity = 1; // opacity of the area color
24
- export let lineType = 1; // type of lines connected the points (1 - solid, 2 - dashed, 3 - dotted)
25
- export let title = ''; // title of this element (reserved for future use)
24
+ <Axes limX={[-4, 4]} limY={[-2, 10]}>
25
+ <Area {xValues} {yValues} />
26
+ </Axes>
27
+ ```
28
+ -->
29
+ <script>
30
+ import { getContext } from 'svelte';
31
+ import { Colors } from '../constants';
32
+ import { val2p, checkCoords, handleClick, transformCoords } from '../methods';
33
+ import { LINE_STYLES } from '../constants';
26
34
 
35
+ /** @type {Props} */
36
+ let {
37
+ xValues, // vector or array with x-coordinates of the points
38
+ yValues, // vector or array with y-coordinates of the points
39
+ lineColor = Colors.PRIMARY, // color of segment lines
40
+ lineType = 1, // type of segment lines (1 - solid, 2 - dashed, 3 - dotted, 4 - dashdot)
41
+ lineWidth = 1, // width (thickness) of segment lines
42
+ faceColor = 'transparent', // color of segment lines
43
+ opacity = 1, // opacity of the area color
44
+ onclick, // function to be called if onclick event fires
45
+ } = $props();
27
46
 
28
- /*****************************************/
29
- /* Component code */
30
- /*****************************************/
47
+ // check user provided coordinates
48
+ const xv = $derived(checkCoords(xValues, 'Area'));
49
+ const yv = $derived(xv ? checkCoords(yValues, 'Area', xv.length) : null);
31
50
 
32
- // get axes context
51
+ // get axes context and compute coordinates of polyline
33
52
  const axes = getContext('axes');
53
+ const p = $derived(xv && yv ? val2p(xv, yv, axes.tX(), axes.tY()) : null);
54
+
55
+ const ys = $derived(yv ? transformCoords(yv.subset(1), axes.tY()) : null);
56
+ const xs = $derived(xv ? transformCoords(xv.subset(1), axes.tX()) : null);
57
+ const pa = $derived(p && xs && ys ? xs + ',' + ys + ' ' + p + ' ' + xs + ',' + ys : null);
34
58
 
35
- const scale = axes.scale;
36
- const isOk = axes.isOk;
37
- const tX = axes.tX;
38
- const tY = axes.tY;
39
-
40
- // reactive variables for coordinates of data points in pixels
41
- let p, xs, y0, xe = undefined;
42
- $: {
43
-
44
- if ($isOk) {
45
- const xv = checkCoords(xValues, 'AreaSeries');
46
- p = val2p(xv, yValues, $tX, $tY, axes);
47
- y0 = axes.transform(vector([0]), $tY.coords)[0];
48
- xs = axes.transform(xv.subset(1), $tX.coords)[0];
49
- xe = axes.transform(xv.subset(xValues.length), $tX.coords)[0];
50
- }
51
- }
52
-
53
- $: areaStyleStr = `opacity:${opacity};fill:${fillColor};stroke:${lineColor};stroke-width: ${lineWidth}px;stroke-dasharray:${axes.LINE_STYLES[$scale][lineType - 1]}`;
59
+ const areaStyleStr = $derived(`opacity:${opacity};fill:${faceColor};stroke:${lineColor};stroke-width: ${lineWidth}px;stroke-dasharray:${LINE_STYLES[axes.scales().plot][lineType - 1]}`);
54
60
  </script>
55
61
 
56
- {#if $isOk && p !== undefined}
57
- <g class="series series_area" style={areaStyleStr} title={title}>
58
- <polygon points="{xs + "," + y0 + " " + p + " " + xs + "," + y0}"/>
59
- </g>
62
+ {#if pa}
63
+ <!-- svelte-ignore a11y_click_events_have_key_events -->
64
+ <!-- svelte-ignore a11y_no_static_element_interactions -->
65
+ <g onclick={(e) => handleClick(e, 'polygon', onclick)} class="series series-area" style={areaStyleStr}>
66
+ <polygon points={pa}/>
67
+ </g>
60
68
  {/if}
61
69