svelte-plots-basic 1.1.4 → 2.1.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.
Files changed (51) hide show
  1. package/README.md +59 -67
  2. package/package.json +10 -4
  3. package/src/2d/Area.svelte +61 -0
  4. package/src/2d/Axes.svelte +500 -0
  5. package/src/2d/Axis.svelte +67 -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/ColormapLegend.svelte +100 -0
  12. package/src/2d/Heatmap.svelte +109 -0
  13. package/src/2d/Legend.svelte +148 -0
  14. package/src/2d/Lines.svelte +49 -0
  15. package/src/2d/Multilines.svelte +67 -0
  16. package/src/2d/Points.svelte +79 -0
  17. package/src/2d/Rectangles.svelte +74 -0
  18. package/src/2d/Segments.svelte +65 -0
  19. package/src/2d/TextLabels.svelte +107 -0
  20. package/src/2d/TextLegend.svelte +53 -0
  21. package/src/2d/XAxis.svelte +109 -0
  22. package/src/2d/YAxis.svelte +109 -0
  23. package/src/2d/index.js +21 -0
  24. package/src/3d/Axes.svelte +445 -0
  25. package/src/3d/Axis.svelte +64 -0
  26. package/src/3d/AxisLines.svelte +50 -0
  27. package/src/3d/AxisTickLabels.svelte +54 -0
  28. package/src/3d/Lines.svelte +49 -0
  29. package/src/3d/Mesh.svelte +185 -0
  30. package/src/3d/Points.svelte +61 -0
  31. package/src/3d/Segments.svelte +69 -0
  32. package/src/3d/TextLabels.svelte +79 -0
  33. package/src/3d/XAxis.svelte +123 -0
  34. package/src/3d/YAxis.svelte +123 -0
  35. package/src/3d/ZAxis.svelte +123 -0
  36. package/src/3d/index.js +13 -0
  37. package/src/Colors.js +36 -1
  38. package/src/Utils.js +243 -0
  39. package/src/AreaSeries.svelte +0 -53
  40. package/src/Axes.svelte +0 -550
  41. package/src/BarSeries.svelte +0 -68
  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 -69
  46. package/src/Segments.svelte +0 -52
  47. package/src/TextLabels.svelte +0 -88
  48. package/src/TextLegend.svelte +0 -38
  49. package/src/XAxis.svelte +0 -69
  50. package/src/YAxis.svelte +0 -72
  51. package/src/index.js +0 -16
@@ -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,69 +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
-
@@ -1,52 +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
- export let title = "";
14
- export let style = "series_seg";
15
-
16
- /* sanity check for input parameters */
17
- if (!Array.isArray(xStart) || !Array.isArray(xEnd) || !Array.isArray(yStart) || !Array.isArray(yEnd)) {
18
- throw("Segments: parameters 'xStart', 'yStart', 'xEnd' and 'yEnd' must be vectors.")
19
- }
20
-
21
- const n = xStart.length;
22
- if (xEnd.length !== n || yStart.length !== n || yEnd.length !== n) {
23
- throw("Segments: parameters 'xStart', 'yStart', 'xEnd' and 'yEnd' should have the same length.")
24
- }
25
-
26
- // get axes context and reactive variables needed to compute coordinates
27
- const axes = getContext('axes');
28
- const xLim = axes.xLim;
29
- const yLim = axes.yLim;
30
- const axesWidth = axes.width;
31
- const axesHeight = axes.height;
32
- const scale = axes.scale;
33
-
34
- // reactive variables for coordinates of data points in pixels (and line style)
35
- $: x1 = axes.scaleX(xStart, $xLim, $axesWidth);
36
- $: x2 = axes.scaleX(xEnd, $xLim, $axesWidth);
37
- $: y1 = axes.scaleY(yStart, $yLim, $axesHeight);
38
- $: y2 = axes.scaleY(yEnd, $yLim, $axesHeight);
39
- $: lineStyleStr = `stroke:${lineColor};stroke-width: ${lineWidth}px;stroke-dasharray:${axes.LINE_STYLES[$scale][lineType - 1]}`;
40
-
41
- </script>
42
-
43
- {#if x1 !== undefined && y1 !== undefined}
44
- <g class="series {style}" title={title} style={lineStyleStr}>
45
- {#each x1 as v, i}
46
- <line x1={x1[i]} x2={x2[i]} y1={y1[i]} y2={y2[i]}></line>
47
- {/each}
48
- </g>
49
- {/if}
50
-
51
- <style>
52
- </style>
@@ -1,88 +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
- import { subset } from 'mdatools/stat';
12
- import { vmult } from 'mdatools/matrix';
13
-
14
- // input parameters
15
- export let xValues;
16
- export let yValues;
17
- export let labels;
18
- export let pos = 0;
19
- export let faceColor = Colors.PRIMARY_TEXT;
20
- export let borderColor = "transparent";
21
- export let borderWidth = 0;
22
- export let textSize = 1;
23
- export let style = "";
24
- export let title = "series_text";
25
-
26
-
27
- // function to prepare values for variables 'label' and 'pos'
28
- function processValues(v, n, name) {
29
- if (!Array.isArray(v)) return Array(n).fill(v);
30
- if (v.length !== n) {
31
- throw(`TextLabels: parameter ${name} must be a single number or a vector of the same size as 'x' and 'y'.`)
32
- }
33
- return v;
34
- }
35
-
36
- // sanity check for input parameters
37
- if (!Array.isArray(xValues) || !Array.isArray(yValues) || xValues.length !== yValues.length) {
38
- throw("TextLabels: parameters 'xValues' and 'yValues' must be vectors of the same length.")
39
- }
40
-
41
- // prepare values for labels and positions
42
- $: labelsLocal = processValues(labels, xValues.length);
43
- $: posLocal = pos === 0 ? pos : processValues(pos, xValues.length);
44
-
45
- // get axes context and reactive variables needed to compute coordinates
46
- const axes = getContext('axes');
47
- const xLim = axes.xLim;
48
- const yLim = axes.yLim;
49
- const axesWidth = axes.width;
50
- const axesHeight = axes.height;
51
- const scale = axes.scale;
52
-
53
- // reactive variables for coordinates of data points in pixels
54
- $: x = axes.scaleX(xValues, $xLim, $axesWidth);
55
- $: y = axes.scaleY(yValues, $yLim, $axesHeight);
56
- $: dx = pos === 0 ? 0 : subset(vmult([0, 1, 0, -1], axes.LABELS_MARGIN[$scale]), posLocal)
57
- $: dy = pos === 0 ? 0 : subset(vmult([1, 0, -1, 0], axes.LABELS_MARGIN[$scale]), posLocal)
58
- $: textAnchors = pos === 0 ? "middle" : subset([ "middle", "start", "middle", "end"], posLocal);
59
-
60
- // styles for the elements
61
- $: textStyleStr = `dominant-baseline:middle;fill:${faceColor};stroke-width:${borderWidth}px;stroke:${borderColor};
62
- font-size:${textSize}em; text-anchor:middle;`;
63
- </script>
64
-
65
- {#if x !== undefined && y !== undefined}
66
- <g class="series {style}" title={title} style={textStyleStr} >
67
-
68
- {#if pos === 0}
69
- {#each x as v, i}
70
- <text data-id={i} x={x[i]} y={y[i]}>{@html labelsLocal[i]}</text>
71
- {/each}
72
-
73
- {:else}
74
- {#each x as v, i}
75
- <text data-id={i} x={x[i]} y={y[i]} dx={dx[i]} dy={dy[i]} text-anchor={textAnchors[i]}>{@html labelsLocal[i]}</text>
76
- {/each}
77
- {/if}
78
-
79
- </g>
80
- {/if}
81
-
82
- <style>
83
- text, text > :global(tspan) {
84
- cursor: default;
85
- user-select: none;
86
- dominant-baseline: middle;
87
- }
88
- </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>
package/src/index.js DELETED
@@ -1,16 +0,0 @@
1
- export { default as Axes } from './Axes.svelte';
2
- export { default as XAxis } from './XAxis.svelte';
3
- export { default as YAxis } from './YAxis.svelte';
4
- export { default as Box } from './Box.svelte';
5
-
6
- export { default as Rectangles } from './Rectangles.svelte';
7
- export { default as Segments } from './Segments.svelte';
8
- export { default as TextLabels } from './TextLabels.svelte';
9
- export { default as TextLegend } from './TextLegend.svelte';
10
-
11
- export { default as BarSeries } from './BarSeries.svelte';
12
- export { default as ScatterSeries } from './ScatterSeries.svelte';
13
- export { default as LineSeries } from './LineSeries.svelte';
14
- export { default as AreaSeries } from './AreaSeries.svelte';
15
-
16
- export { Colors } from './Colors.js';