svelte-plots-basic 1.1.3 → 2.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.
Files changed (50) hide show
  1. package/README.md +54 -69
  2. package/package.json +28 -33
  3. package/src/2d/Area.svelte +61 -0
  4. package/src/2d/Axes.svelte +500 -0
  5. package/src/2d/Axis.svelte +50 -0
  6. package/src/2d/AxisLines.svelte +54 -0
  7. package/src/2d/AxisTickLabels.svelte +82 -0
  8. package/src/2d/Bars.svelte +65 -0
  9. package/src/2d/Box.svelte +50 -0
  10. package/src/2d/ColorLegend.svelte +36 -0
  11. package/src/2d/Legend.svelte +148 -0
  12. package/src/2d/Lines.svelte +49 -0
  13. package/src/2d/Multilines.svelte +67 -0
  14. package/src/2d/Points.svelte +79 -0
  15. package/src/2d/Rectangles.svelte +74 -0
  16. package/src/2d/Segments.svelte +65 -0
  17. package/src/2d/TextLabels.svelte +107 -0
  18. package/src/2d/TextLegend.svelte +53 -0
  19. package/src/2d/XAxis.svelte +99 -0
  20. package/src/2d/YAxis.svelte +99 -0
  21. package/src/{index.js → 2d/index.js} +10 -6
  22. package/src/3d/Axes.svelte +445 -0
  23. package/src/3d/Axis.svelte +64 -0
  24. package/src/3d/AxisLines.svelte +50 -0
  25. package/src/3d/AxisTickLabels.svelte +54 -0
  26. package/src/3d/Lines.svelte +49 -0
  27. package/src/3d/Mesh.svelte +188 -0
  28. package/src/3d/Points.svelte +61 -0
  29. package/src/3d/Segments.svelte +69 -0
  30. package/src/3d/TextLabels.svelte +79 -0
  31. package/src/3d/XAxis.svelte +123 -0
  32. package/src/3d/YAxis.svelte +123 -0
  33. package/src/3d/ZAxis.svelte +123 -0
  34. package/src/3d/index.js +13 -0
  35. package/src/Colors.js +36 -1
  36. package/src/Utils.js +205 -0
  37. package/dist/index.js +0 -4209
  38. package/dist/index.mjs +0 -4187
  39. package/src/AreaSeries.svelte +0 -53
  40. package/src/Axes.svelte +0 -538
  41. package/src/BarSeries.svelte +0 -78
  42. package/src/Box.svelte +0 -35
  43. package/src/LineSeries.svelte +0 -50
  44. package/src/Rectangles.svelte +0 -64
  45. package/src/ScatterSeries.svelte +0 -85
  46. package/src/Segments.svelte +0 -48
  47. package/src/TextLabels.svelte +0 -78
  48. package/src/TextLegend.svelte +0 -38
  49. package/src/XAxis.svelte +0 -69
  50. package/src/YAxis.svelte +0 -72
@@ -1,50 +0,0 @@
1
- <script>
2
- import { getContext } from 'svelte';
3
- import { mrange } from 'mdatools/stat';
4
- import { Colors } from './Colors';
5
-
6
- // input parameters
7
- export let xValues;
8
- export let yValues;
9
- export let title = "";
10
- export let lineWidth = 1;
11
- export let lineColor = Colors.PRIMARY;
12
- export let lineType = 1;
13
-
14
- /* sanity check of input parameters */
15
- if (!Array.isArray(xValues) || !Array.isArray(yValues) || xValues.length != yValues.length) {
16
- throw("LineSeries: parameters 'xValues' and 'yValues' must be numeric vectors of the same length.");
17
- }
18
-
19
- // compute ranges for x and y values
20
- const xValuesRange = mrange(xValues, 0.05);
21
- const yValuesRange = mrange(yValues, 0.05);
22
-
23
- // get axes context and adjust axes limits
24
- const axes = getContext('axes');
25
- axes.adjustXAxisLimits(xValuesRange);
26
- axes.adjustYAxisLimits(yValuesRange);
27
-
28
- // get reactive variables needed to compute coordinates
29
- const xLim = axes.xLim;
30
- const yLim = axes.yLim;
31
- const axesWidth = axes.width;
32
- const axesHeight = axes.height;
33
- const scale = axes.scale;
34
-
35
- // reactive variables for coordinates of data points in pixels
36
- $: x = axes.scaleX(xValues, $xLim, $axesWidth);
37
- $: y = axes.scaleY(yValues, $yLim, $axesHeight);
38
- $: p = x !== undefined && y !== undefined ? x.map((v, i) => `${v},${y[i]}`).join(' ') : undefined;
39
- $: lineStyleStr = `fill:transparent;stroke:${lineColor};stroke-width: ${lineWidth}px;
40
- stroke-dasharray:${axes.LINE_STYLES[$scale][lineType - 1]}`;
41
- </script>
42
-
43
- {#if p !== undefined}
44
- <g class="series series_line" style={lineStyleStr} title={title}>
45
- <polyline class="line" points={p}/>
46
- </g>
47
- {/if}
48
-
49
- <style>
50
- </style>
@@ -1,64 +0,0 @@
1
- <script>
2
- import { getContext } from 'svelte';
3
- import { Colors } from './Colors';
4
-
5
- /* input parameters */
6
- export let left;
7
- export let top;
8
- export let width;
9
- export let height;
10
- export let labels = undefined;
11
- export let faceColor = Colors.PRIMARY;
12
- export let borderColor = faceColor;
13
- export let lineWidth = 1;
14
- export let title = "";
15
- export let style = "series_rect";
16
-
17
-
18
- // multiply width and height values if needed
19
- if (!Array.isArray(left) || !Array.isArray(top) || left.length < 1 || left.length != top.length) {
20
- throw("Rectangles: parameters 'left' and 'top' must be vectors of the same size.")
21
- }
22
-
23
- // we make this reactive in case if left and right has been changed but not width
24
- const n = left.length;
25
- if (!Array.isArray(height)) height = Array(n).fill(height);
26
- if (!Array.isArray(width)) width = Array(n).fill(width);
27
-
28
- /* sanity check for input parameters */
29
- if (top.length !== n || width.length !== n || height.length !== n) {
30
- throw("Rectangles: x, y, w and h should have the same length (w and h can be single values).")
31
- }
32
-
33
- if (labels !== undefined && (!Array.isArray(labels) || labels.length !== n)) {
34
- throw("Rectangles: vector with labels should have the same length as vectors with x and y coordinates.")
35
- }
36
-
37
- // get axes context and reactive variables needed to compute coordinates
38
- const axes = getContext('axes');
39
- const xLim = axes.xLim;
40
- const yLim = axes.yLim;
41
- const axesWidth = axes.width;
42
- const axesHeight = axes.height;
43
-
44
- // reactive variables for coordinates of data points in pixels
45
- $: rx = axes.scaleX(left, $xLim, $axesWidth);
46
- $: ry = axes.scaleY(top, $yLim, $axesHeight);
47
- $: rw = axes.scaleX(width, $xLim, $axesWidth, true);
48
- $: rh = axes.scaleY(height, $yLim, $axesHeight, true);
49
-
50
- // styles for bars and labels
51
- $: barsStyleStr = `fill:${faceColor};stroke:${borderColor};stroke-width: ${lineWidth}px;`;
52
-
53
- </script>
54
-
55
- {#if rx !== undefined && ry !== undefined}
56
- <g class="series {style}" title={title} style={barsStyleStr}>
57
- {#each left as v, i}
58
- <rect x={rx[i]} y={ry[i]} width={rw[i]} height={rh[i]} />
59
- {/each}
60
- </g>
61
- {/if}
62
-
63
- <style>
64
- </style>
@@ -1,85 +0,0 @@
1
- <script>
2
-
3
- import { getContext } from 'svelte';
4
- import { mrange } from 'mdatools/stat';
5
- import { Colors } from './Colors';
6
- import TextLabels from './TextLabels.svelte';
7
-
8
- // input parameters
9
- export let xValues;
10
- export let yValues;
11
- export let marker = 1
12
- export let title = "";
13
- export let faceColor = "transparent";
14
- export let borderColor = Colors.PRIMARY;
15
- export let borderWidth = 1;
16
- export let markerSize = 1;
17
-
18
- // TODO: implement later
19
- //export let labels = yValues;
20
- //export let showLabels = "no"; // can be "no", "hover", "always"
21
-
22
- /* constants for internal use */
23
- const markers = ["●", "◼", "▲", "▼", "⬥", "+", "*", "⨯"];
24
- let markerSymbol;
25
-
26
- /* sanity check of input parameters */
27
- if (typeof(marker) !== "number" || marker < 1 || marker > markers.length) {
28
- throw(`ScatterSeries: parameter 'marker' must be a number from 1 to ${markers.length}."`);
29
- }
30
-
31
- // to access shared parameters and methods from Axes
32
- const axes = getContext('axes');
33
-
34
- /* reactive actions related to x-values, fires when there are changes in:
35
- * - xValues
36
- * - marker
37
- */
38
- $: {
39
- if (!Array.isArray(xValues)) {
40
- throw("ScatterSeries: parameter 'xValues' must be a numeric vector.");
41
- }
42
-
43
- const xValuesRange = mrange(xValues, 0.05);
44
- axes.adjustXAxisLimits(xValuesRange);
45
-
46
- markerSymbol = "";
47
- markerSymbol = markers[marker - 1];
48
- }
49
-
50
- /* reactive actions related to y-values, fires when there are changes in:
51
- * - yValues
52
- */
53
- $: {
54
- if (!Array.isArray(yValues) || xValues.length != yValues.length) {
55
- throw("BarSeries: parameter 'yValues' must be a numeric vector of the same length as 'xValues'.");
56
- }
57
-
58
- const yValuesRange = mrange(yValues, 0.05);
59
- axes.adjustYAxisLimits(yValuesRange);
60
- }
61
- </script>
62
-
63
- <TextLabels
64
- {xValues} {yValues} {faceColor} {borderColor} {borderWidth} {title}
65
- style="series_scatter"
66
- labels={markerSymbol}
67
- textSize={markerSize}
68
- />
69
-
70
- <style>
71
-
72
- :global(.series_scatter > text) {
73
- font-size: 1em;
74
- cursor: default;
75
- }
76
-
77
- :global(.series_scatter > text:hover) {
78
- opacity: 90%;
79
- }
80
-
81
- :global(.series_scatter > *:hover + .labels_hover) {
82
- visibility: visible;
83
- opacity: 1;
84
- }
85
- </style>
@@ -1,48 +0,0 @@
1
- <script>
2
- import { getContext } from 'svelte';
3
- import { Colors } from './Colors';
4
-
5
- /* input parameters */
6
- export let xStart;
7
- export let xEnd;
8
- export let yStart;
9
- export let yEnd;
10
- export let lineColor = Colors.PRIMARY;
11
- export let lineType = 1;
12
- export let lineWidth = 1;
13
-
14
- /* sanity check for input parameters */
15
- if (!Array.isArray(xStart) || !Array.isArray(xEnd) || !Array.isArray(yStart) || !Array.isArray(yEnd)) {
16
- throw("Segments: parameters 'xStart', 'yStart', 'xEnd' and 'yEnd' must be vectors.")
17
- }
18
-
19
- const n = xStart.length;
20
- if (xEnd.length !== n || yStart.length !== n || yEnd.length !== n) {
21
- throw("Segments: parameters 'xStart', 'yStart', 'xEnd' and 'yEnd' should have the same length.")
22
- }
23
-
24
- // get axes context and reactive variables needed to compute coordinates
25
- const axes = getContext('axes');
26
- const xLim = axes.xLim;
27
- const yLim = axes.yLim;
28
- const axesWidth = axes.width;
29
- const axesHeight = axes.height;
30
- const scale = axes.scale;
31
-
32
- // reactive variables for coordinates of data points in pixels (and line style)
33
- $: x1 = axes.scaleX(xStart, $xLim, $axesWidth);
34
- $: x2 = axes.scaleX(xEnd, $xLim, $axesWidth);
35
- $: y1 = axes.scaleY(yStart, $yLim, $axesHeight);
36
- $: y2 = axes.scaleY(yEnd, $yLim, $axesHeight);
37
- $: lineStyleStr = `stroke:${lineColor};stroke-width: ${lineWidth}px;stroke-dasharray:${axes.LINE_STYLES[$scale][lineType - 1]}`;
38
-
39
- </script>
40
-
41
- {#if x1 !== undefined && y1 !== undefined}
42
- {#each x1 as v, i}
43
- <line x1="{x1[i]}" x2="{x2[i]}" y1="{y1[i]}" y2="{y2[i]}" style="{lineStyleStr}"></line>
44
- {/each}
45
- {/if}
46
-
47
- <style>
48
- </style>
@@ -1,78 +0,0 @@
1
- <script>
2
- /****************************************************
3
- * TextLabels component *
4
- * -------------------- *
5
- * shows a series of text labels on the plot *
6
- * can be used as basis for marker plot *
7
- *****************************************************/
8
-
9
- import { getContext } from 'svelte';
10
- import { Colors } from './Colors';
11
-
12
- // input parameters
13
- export let xValues;
14
- export let yValues;
15
- export let labels;
16
- export let pos = 0;
17
- export let faceColor = Colors.PRIMARY_TEXT;
18
- export let borderColor = "transparent";
19
- export let borderWidth = 0;
20
- export let textSize = 1;
21
- export let style = "";
22
- export let title = "series_text";
23
-
24
- // text-anchor values depending on position
25
- const textAnchors = ["middle", "middle", "start", "middle", "end"];
26
-
27
- // sanity check for input parameters
28
- if (!Array.isArray(xValues) || !Array.isArray(yValues) || xValues.length !== yValues.length) {
29
- throw("TextLabels: parameters 'xValues' and 'yValues' must be vectors of the same length.")
30
- }
31
-
32
- // multiply label values if needed
33
- $: {
34
- const n = xValues.length;
35
- if (!Array.isArray(labels)) labels = Array(n).fill(labels);
36
-
37
- // workaround for an issue when xValues and yValues are changed in parent app
38
- // but array of labels is still the same as in the
39
- if (labels.length != n) labels = Array(n).fill(labels[0]);
40
-
41
- // check that the length of labels vector is correct
42
- if (labels.length !== n) {
43
- throw("TextLabels: parameter 'labels' must be a single text value or a vector of the same size as 'x' and 'y'.")
44
- }
45
- }
46
-
47
- // get axes context and reactive variables needed to compute coordinates
48
- const axes = getContext('axes');
49
- const xLim = axes.xLim;
50
- const yLim = axes.yLim;
51
- const axesWidth = axes.width;
52
- const axesHeight = axes.height;
53
- const scale = axes.scale;
54
-
55
- // reactive variables for coordinates of data points in pixels
56
- $: x = axes.scaleX(xValues, $xLim, $axesWidth);
57
- $: y = axes.scaleY(yValues, $yLim, $axesHeight);
58
- $: dx = [0, 0, 1, 0, -1][pos] * axes.LABELS_MARGIN[$scale];
59
- $: dy = [0, 1, 0, -1, 0][pos] * axes.LABELS_MARGIN[$scale];
60
-
61
- // styles for the elements
62
- $: textStyleStr = `fill:${faceColor};stroke-width:${borderWidth}px;stroke:${borderColor};
63
- font-size:${textSize}em; text-anchor:${textAnchors[pos]};`;
64
- </script>
65
-
66
- {#if x !== undefined && y !== undefined}
67
- <g class="series {style}" title={title} style={textStyleStr} >
68
- {#each x as v, i}
69
- <text data-id={i} x={x[i]} y={y[i]} dx={dx} dy={dy}>{@html labels[i]}</text>
70
- {/each}
71
- </g>
72
- {/if}
73
-
74
- <style>
75
- text, text > :global(tspan) {
76
- dominant-baseline: middle;
77
- }
78
- </style>
@@ -1,38 +0,0 @@
1
- <script>
2
- import { getContext } from 'svelte';
3
- import { Colors } from './Colors';
4
-
5
- /* input parameters */
6
- export let left;
7
- export let top;
8
- export let dx = "0";
9
- export let dy = "1.25em";
10
- export let elements;
11
- export let faceColor = Colors.PRIMARY_TEXT;
12
- export let borderColor = "transparent";
13
- export let borderWidth = 0;
14
- export let textSize = 1;
15
-
16
- // get axes context and reactive variables needed to compute coordinates
17
- const axes = getContext('axes');
18
- const xLim = axes.xLim;
19
- const yLim = axes.yLim;
20
- const axesWidth = axes.width;
21
- const axesHeight = axes.height;
22
- const scale = axes.scale;
23
-
24
- // reactive variables for coordinates of data points in pixels
25
- $: x = axes.scaleX([left], $xLim, $axesWidth);
26
- $: y = axes.scaleY([top], $yLim, $axesHeight);
27
-
28
- // styles for bars and labels
29
- $: textStyleStr = `fill:${faceColor};stroke-width:${borderWidth}px;stroke:${borderColor};font-size:${textSize}em;`;
30
- </script>
31
-
32
- {#if x !== undefined && y !== undefined && elements.length > 0}
33
- <text style={textStyleStr} x={x} y={y} dx={dx} dy={dy} dominant-baseline="middle" text-anchor="start">
34
- {#each elements as el, i}
35
- <tspan {x} {dx} dy={i === 0 ? 0 : dy}>{@html el}</tspan>
36
- {/each}
37
- </text>
38
- {/if}
package/src/XAxis.svelte DELETED
@@ -1,69 +0,0 @@
1
- <script>
2
- import { getContext } from 'svelte';
3
- import { Colors } from './Colors';
4
-
5
- // input parameters
6
- export let slot = "xaxis"; // slot the component must be placed in
7
- export let ticks = undefined; // vector with numeric tick positions in plot units
8
- export let tickLabels = ticks; // vector with labels for each tick
9
- export let showGrid = false; // logical, show or not grid lines
10
-
11
- // set up tick mode
12
- const tickMode = ticks === undefined ? "auto" : "manual";
13
-
14
- /* sanity checks of input parameters */
15
- if (slot !== "xaxis") {
16
- throw("Component XAxis must have \"slot='xaxis'\" attribute.")
17
- }
18
-
19
- if (ticks !== undefined && !Array.isArray(ticks)) {
20
- throw("XAxis: 'ticks' must be a vector of numbers.")
21
- }
22
-
23
- if (ticks !== undefined && !(Array.isArray(tickLabels) && tickLabels.length == ticks.length)) {
24
- throw("XAxis: 'tickLabels' must be a vector of the same size as ticks.")
25
- }
26
-
27
- /* styles for axis and grid lines */
28
- const axisLineStyleStr = `stroke:${Colors.DARKGRAY};line-width:1px;`;
29
- const gridLineStyleStr = `stroke:${Colors.MIDDLEGRAY};stroke-opacity:${showGrid?1:0};stroke-dasharray:2px;`;
30
-
31
-
32
- // get axes context and adjust x margins
33
- const axes = getContext('axes');
34
- axes.addXAxisMargins();
35
-
36
- // get reactive variables needed to compute coordinates
37
- const xLim = axes.xLim;
38
- const yLim = axes.yLim;
39
- const axesWidth = axes.width;
40
- const axesHeight = axes.height;
41
- const scale = axes.scale;
42
- const isOk = axes.isOk;
43
-
44
- // reactive variables for coordinates of axis lines
45
- $: x = axes.scaleX($xLim, $xLim, $axesWidth);
46
- $: y = axes.scaleY($yLim, $yLim, $axesHeight);
47
-
48
- // reactive variables for ticks and their coordinates
49
- $: dy = $scale === "small" ? 7 : 10;
50
- $: tickNum = axes.TICK_NUM[$scale];
51
- $: ticks = tickMode === "auto" ? axes.getAxisTicks(undefined, $xLim, tickNum, true) : ticks;
52
- $: tickLabels = tickMode === "auto" ? ticks : tickLabels;
53
- $: ticksX = axes.scaleX(ticks, $xLim, $axesWidth);
54
- $: ticksY = y === undefined ? undefined : [y[0], y[0] + dy];
55
- </script>
56
-
57
- {#if $isOk && x !== undefined && y !== undefined }
58
- <g class="mdaplot__axis mdaplot__xaxis">
59
- {#each ticksX as tx, i}
60
- <line x1="{tx}" x2="{tx}" y1="{y[0]}" y2="{y[1]}" style="{gridLineStyleStr}"></line>
61
- <line x1="{tx}" x2="{tx}" y1="{ticksY[0]}" y2="{ticksY[1]}" style="{axisLineStyleStr}"></line>
62
- <text x="{tx}" y="{ticksY[1]}" dx="0" dy="{dy}" class="mdaplot__axis-labels" dominant-baseline="middle" text-anchor="middle">{tickLabels[i]}</text>
63
- {/each}
64
- <line x1="{x[0]}" x2="{x[1]}" y1="{y[0]}" y2="{y[0]}" style="{axisLineStyleStr}"></line>
65
- </g>
66
- {/if}
67
-
68
- <style>
69
- </style>
package/src/YAxis.svelte DELETED
@@ -1,72 +0,0 @@
1
- <script>
2
- import { getContext } from 'svelte';
3
- import { Colors } from './Colors';
4
-
5
- // input parameters
6
- export let slot = "yaxis"; // slot the component must be placed in
7
- export let ticks = undefined; // vector with numeric tick positions in plot units
8
- export let tickLabels = ticks; // vector with labels for each tick
9
- export let showGrid = false; // logical, show or not grid lines
10
- export let las = 1;
11
-
12
- // set up tick mode
13
- const tickMode = ticks === undefined ? "auto" : "manual";
14
-
15
- // TODO: set up tick text translation
16
- const transform = las > 1 ? "" : "";
17
-
18
- /* sanity checks of input parameters */
19
- if (slot !== "yaxis") {
20
- throw("Component YAxis must have \"slot='yaxis'\" attribute.")
21
- }
22
-
23
- if (ticks !== undefined && !Array.isArray(ticks)) {
24
- throw("YAxis: 'ticks' must be a vector of numbers.")
25
- }
26
-
27
- if (ticks !== undefined && !(Array.isArray(tickLabels) && tickLabels.length == ticks.length)) {
28
- throw("YAxis: 'tickLabels' must be a vector of the same size as ticks.")
29
- }
30
-
31
- /* styles for axis and grid lines */
32
- const axisLineStyleStr = `stroke:${Colors.DARKGRAY};line-width:1px;`;
33
- const gridLineStyleStr = `stroke:${Colors.MIDDLEGRAY};stroke-opacity:${showGrid?1:0};stroke-dasharray:2px;`;
34
-
35
-
36
- // get axes context and adjust x margins
37
- const axes = getContext('axes');
38
- axes.addYAxisMargins();
39
-
40
- // get reactive variables needed to compute coordinates
41
- const xLim = axes.xLim;
42
- const yLim = axes.yLim;
43
- const axesWidth = axes.width;
44
- const axesHeight = axes.height;
45
- const scale = axes.scale;
46
-
47
- // reactive variables for coordinates of axis lines
48
- $: x = axes.scaleX($xLim, $xLim, $axesWidth);
49
- $: y = axes.scaleY($yLim, $yLim, $axesHeight);
50
-
51
- // reactive variables for ticks and their coordinates
52
- $: dx = $scale === "small" ? -4 : -6;
53
- $: tickNum = axes.TICK_NUM[$scale];
54
- $: ticks = tickMode === "auto" ? axes.getAxisTicks(undefined, $yLim, tickNum, true) : ticks;
55
- $: tickLabels = tickMode === "auto" ? ticks : tickLabels;
56
- $: ticksY = axes.scaleY(ticks, $yLim, $axesHeight);
57
- $: ticksX = x === undefined ? undefined : [x[0] + dx, x[0]];
58
- </script>
59
-
60
- {#if x !== undefined && y !== undefined }
61
- <g class="mdaplot__axis mdaplot__yaxis">
62
- {#each ticksY as ty, i}
63
- <line x1="{x[0]}" x2="{x[1]}" y1="{ty}" y2="{ty}" style="{gridLineStyleStr}"></line>
64
- <line x1="{ticksX[0]}" x2="{ticksX[1]}" y1="{ty}" y2="{ty}" style="{axisLineStyleStr}" ></line>
65
- <text x="{ticksX[0]}" y="{ty}" dx="{dx}" dy="{0}" {transform} style="background:red" class="mdaplot__axis-labels" dominant-baseline="middle" text-anchor="end">{tickLabels[i]}</text>
66
- {/each}
67
- <line x1="{x[0]}" x2="{x[0]}" y1="{y[0]}" y2="{y[1]}" style="{axisLineStyleStr}" ></line>
68
- </g>
69
- {/if}
70
-
71
- <style>
72
- </style>