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
package/README.md CHANGED
@@ -1,10 +1,17 @@
1
- # Svelte plots
1
+ # Svelte components for creating plots
2
+
3
+ The library is under development and breaking changes may occur in the coming versions.
4
+
5
+ ## News
6
+
7
+ New release (v. 2.0.0) introduces many breaking changes as the library was completely re-written. If you use previous versions of `svelte-plots-basic` in your projects, and do not want to change anything, stick to the latest 1.x.x version (v. 1.1.4).
2
8
 
3
9
  ## Description
4
10
 
5
- `svelte-plots-basic` is a [Svelte](https://svelte.dev) component library for creating very simple 2D plots/charts. It is currently under active development and breaking changes may occur. In its current state (v. 1.1.3) it allows to create bar, scatter, area and line charts as well as any combination of the four. The charts are created using [SVG](https://en.wikipedia.org/wiki/Scalable_Vector_Graphics) and are re-scalable.
11
+ `svelte-plots-basic` is a [Svelte](https://svelte.dev) component library for creating simple 2D and 3D plots/charts. The plots are created by generating [SVG](https://en.wikipedia.org/wiki/Scalable_Vector_Graphics) inside HTML document and are re-scalable.
12
+
13
+ The library provides building blocks for creating plots, one can think of this library as "Lego" bricks for plots. It has two groups of components: for 2D and for 3D plots. In addition to `svelte` the library has one direct dependence, [mdatools-js](https://github.com/svkucheryavski/mdatools-js) library which is used for vector/matrix operations, statistics, and other manipulations with data values.
6
14
 
7
- The description is not complete yet and will be updated during February-March of 2022.
8
15
 
9
16
  ## Installation
10
17
 
@@ -20,113 +27,91 @@ or, to install it with yarn:
20
27
  yarn add -D svelte-plots-basic
21
28
  ```
22
29
 
30
+ ## Demo
23
31
 
32
+ There is on-line demo site with some examples of what you can do with the library: [https://mda.tools/svelteplotsdemo/](https://mda.tools/svelteplotsdemo/). All examples from this demo are available in corresponding GitHub repo: [](). Use code of the examples for inspiration and as a tutorial, although some basic information is also available below.
24
33
 
25
- ## Quick start
34
+ ## Quick start (2D plots)
26
35
 
27
36
  It is assumed that you already know the basics of Svelte.
28
37
 
29
- Just create a new Svelte app following the [quick start guide](https://svelte.dev/blog/the-easiest-way-to-get-started). Then open `App.svelte` file, delete everything and write the following code, which creates a simple bar chart:
38
+ Just create a new Svelte app following the [quick start guide](https://svelte.dev/blog/the-easiest-way-to-get-started). Then open `App.svelte` file, delete everything and write the following code, which creates a simple 2D bar chart:
30
39
 
31
- ```js
40
+ ```svelte
32
41
  <script>
33
- import {Axes, BarSeries} from 'svelte-plots';
42
+ import {Axes, XAxis, YAxis, Box, BarSeries} from 'svelte-plots-basic';
34
43
 
35
44
  // test data for the plot
36
45
  const years = [2010, 2020, 2030, 2040, 2050];
37
46
  const amount = [100, 200, 150, 300, -100];
38
47
  </script>
39
48
 
40
- <div class="plot">
41
- <Axes>
49
+ <div class="plot-container">
50
+ <Axes limX={[2005, 2055]} limY={[-150, 350]} margins={[1, 1, 0.5, 0.5]}xLabel="Years" yLabel="Income">
51
+
42
52
  <BarSeries
43
- title="Economy"
44
- showLabels="no"
45
53
  faceColor="#e0e0e0"
46
- edgeColor="#e0e0e0"
47
- labels={years}
54
+ edgeColor="#909090"
48
55
  xValues={years}
49
56
  yValues={amount}
50
57
  />
58
+
59
+ // x and y axis with automatic ticks and grid lines
60
+ <XAxis slot="xaxis" />
61
+ <YAxis slot="yaxis" />
62
+
63
+ // box around the axes
64
+ <Box slot="box" />
65
+
51
66
  </Axes>
52
67
  </div>
68
+
69
+ <style>
70
+ .plot-container {
71
+ width: 100%;
72
+ height: 100%;
73
+ min-width: 200px;
74
+ min-height: 200px;
75
+ }
76
+ </style>
53
77
  ```
54
78
 
55
- Then run `npm run dev` in terminal and open `localhost:5000` in browser. That is it.
79
+ Then run `npm run dev` in terminal and open the URL provided by npm in browser. That is it.
56
80
 
57
- Example below shows how to create a plot with axis, box and grid as well as how to combine several plot series together.
81
+ You can also use all capabilities of the `mdatools` package, e.g. generated random numbers:
58
82
 
59
- ```js
83
+ ```svelte
60
84
  <script>
61
- import {Axes, BarSeries} from 'svelte-plots';
85
+ import { Axes, BarSeries } from 'svelte-plots-basic';
86
+ import { Vector } from 'mdatools/arrays';
62
87
 
63
- // test data for the plot
64
- const years = [2010, 2020, 2030, 2040, 2050];
65
- const amount = [100, 200, 150, 300, -100];
88
+ // generate random values from normal distribution
89
+ const x = Vector.randn(200, 0, 1);
90
+ const y = Vector.randn(200, 0, 2);
66
91
  </script>
67
92
 
68
- <div class="plot">
69
- <Axes>
93
+ <div class="plot-container">
94
+ <Axes limX={[-6, 6]} limY={[-5, 6]} xLabel="x" yLabel="y">
70
95
 
71
- // bar, line and marker series with the same data values
72
- <BarSeries
73
- showLabels="no"
74
- faceColor="#eee"
75
- edgeColor="#eee"
76
- xValues={years}
77
- yValues={amount}
78
- />
79
- <LineSeries
80
- lineColor="blue"
81
- lineWidth={2}
82
- xValues={years}
83
- yValues={amount}
84
- />
85
96
  <ScatterSeries
86
- marker="✺"
87
- showLabels="hover"
88
- markerColor="blue"
89
- labels={years}
90
- xValues={years}
91
- yValues={amount}
97
+ xValues={x}
98
+ yValues={y}
92
99
  />
93
100
 
94
101
  // x and y axis with automatic ticks and grid lines
95
- <XAxis showGrid={true} slot="xaxis" />
96
- <YAxis showGrid={true} slot="yaxis" />
102
+ <XAxis slot="xaxis" />
103
+ <YAxis slot="yaxis" />
97
104
 
98
105
  // box around the axes
99
106
  <Box slot="box" />
107
+
100
108
  </Axes>
101
109
  </div>
102
110
  ```
103
111
 
104
- ## Details
105
-
106
- The library has following components:
107
-
108
- ### Axes
109
-
110
- `Axes` this is a main component, which is responsible for showing all other plot elements (axis, labels, titles, primitives, series, etc.). The `Axes` component must be always a parent for all other components. It has four optional parameters:
111
-
112
- * `limX` — vector with two values - x-axis limits (min and max) in plot units.
113
- * `limY` — vector with two values - y-axis limits (min and max) in plot units.
114
- * `title` — a text to be shown as a plot title (on top of the plot).
115
- * `xLabel` — a label for x-axis (shown under the axis).
116
- * `yLabel` — a label for y-axis (shown on the right side of the axis).
117
-
118
- If limits are not provided, the component will compute them automatically when/if you add one or several plot series. If you want to show axes without any series or use primitives instead, you need to provide the values for limits explicitly.
119
-
120
- The `Axes` operates in two coordinate systems. One is related to plot units. For example if you make a scatter plot for Height vs Weight of people, then the x-units will be cm and y-units will be kg. The x-axis will be directed from left to right and y-axis from bottom to up, like in conventional plots. The component then will automatically recompute the coordinates of all plot elements from the plot units to pixels, depending on the current size of the plot shown in browser.
121
-
122
- ### XAxis and YAxis
112
+ See demo for more details.
123
113
 
124
- Each component is responsible for visualization of the corresponding axis. The axis is shown in form of a line with outer ticks and related labels. Number of ticks and their values are computed automatically until you do not specify them as a component parameter. Each component has two optional parameters:
114
+ ## Quick start (3D plots)
125
115
 
126
- * `ticks` — vector with ticks
127
- * `tickLabels` — vector with values to be shown as labels beside each tick
128
- * `showGrid` — a logical parameter, which allows to show grid lines on the plot
129
- * `slot` — name of the slot, `"xaxis"` or `"yaxis"`
130
116
 
131
- The components must be placed in a particular slot of the parent `Axes` component.
132
117
 
package/package.json CHANGED
@@ -1,35 +1,30 @@
1
1
  {
2
- "name": "svelte-plots-basic",
3
- "svelte": "src/index.js",
4
- "module": "dist/index.mjs",
5
- "main": "dist/index.js",
6
- "scripts": {
7
- "build": "rollup -c",
8
- "prepublishOnly": "npm run build"
9
- },
10
- "devDependencies": {
11
- "@rollup/plugin-node-resolve": "^9.0.0",
12
- "rollup": "^2.0.0",
13
- "rollup-plugin-svelte": "^6.0.0",
14
- "svelte": "^3.0.0"
15
- },
16
- "dependencies": {
17
- "mdatools": "^0.5.3"
18
- },
19
- "keywords": [
20
- "svelte",
21
- "svelte plots"
22
- ],
23
- "files": [
24
- "src",
25
- "dist"
26
- ],
27
- "description": "Svelte components for creating simple plots",
28
- "version": "1.1.3",
29
- "repository": {
30
- "type": "git",
31
- "url": "svkucheryavski/svelte-plots-basic"
32
- },
33
- "author": "Sergey Kucheryavskiy",
34
- "license": "MIT"
2
+ "name": "svelte-plots-basic",
3
+ "description": "Svelte components for creating simple plots",
4
+ "version": "2.0.1",
5
+ "author": "Sergey Kucheryavskiy",
6
+ "license": "MIT",
7
+ "private": false,
8
+ "exports": {
9
+ "./2d": "./src/2d/index.js",
10
+ "./2d/*.svelte": "./src/2d/*.svelte",
11
+ "./3d": "./src/3d/index.js",
12
+ "./3d/*.svelte": "./src/3d/*.svelte"
13
+ },
14
+ "dependencies": {
15
+ "mdatools": "^1.x.x"
16
+ },
17
+ "keywords": [
18
+ "svelte",
19
+ "plots",
20
+ "charts",
21
+ "svelte plots"
22
+ ],
23
+ "files": [
24
+ "src"
25
+ ],
26
+ "repository": {
27
+ "type": "git",
28
+ "url": "svkucheryavski/svelte-plots-basic"
29
+ }
35
30
  }
@@ -0,0 +1,61 @@
1
+ <script>
2
+ /****************************************************
3
+ * Area *
4
+ * -------------------- *
5
+ * shows area of a polygon defined by set of points *
6
+ *****************************************************/
7
+
8
+ import { getContext } from 'svelte';
9
+ import { vector } from 'mdatools/arrays';
10
+ import { Colors } from '../Colors';
11
+ import { checkCoords, val2p } from '../Utils';
12
+
13
+
14
+ /*****************************************/
15
+ /* Input parameters */
16
+ /*****************************************/
17
+
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)
26
+
27
+
28
+ /*****************************************/
29
+ /* Component code */
30
+ /*****************************************/
31
+
32
+ // get axes context
33
+ const axes = getContext('axes');
34
+
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]}`;
54
+ </script>
55
+
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>
60
+ {/if}
61
+