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
package/README.md CHANGED
@@ -1,8 +1,22 @@
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 major 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).
8
+
9
+ ### 2.1.0
10
+
11
+ * Added new elements, `Heatmap` and `ColormapLegend`.
12
+ * Improvements to tick labels, in particular if values are too small (< 0.01) or too large (>99) the values are adjusted and a common factor is shown at the end of axis. This is applied only to automatic ticks, manually provided ticks and tick labels are shown as is.
13
+ * Small improvements and bug fixes.
2
14
 
3
15
  ## Description
4
16
 
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.4) 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.
17
+ `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.
18
+
19
+ 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
20
 
7
21
 
8
22
  ## Installation
@@ -19,113 +33,91 @@ or, to install it with yarn:
19
33
  yarn add -D svelte-plots-basic
20
34
  ```
21
35
 
36
+ ## Demo
22
37
 
38
+ 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.
23
39
 
24
- ## Quick start
40
+ ## Quick start (2D plots)
25
41
 
26
42
  It is assumed that you already know the basics of Svelte.
27
43
 
28
- 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:
44
+ 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:
29
45
 
30
- ```js
46
+ ```svelte
31
47
  <script>
32
- import {Axes, BarSeries} from 'svelte-plots';
48
+ import {Axes, XAxis, YAxis, Box, BarSeries} from 'svelte-plots-basic';
33
49
 
34
50
  // test data for the plot
35
51
  const years = [2010, 2020, 2030, 2040, 2050];
36
52
  const amount = [100, 200, 150, 300, -100];
37
53
  </script>
38
54
 
39
- <div class="plot">
40
- <Axes>
55
+ <div class="plot-container">
56
+ <Axes limX={[2005, 2055]} limY={[-150, 350]} margins={[1, 1, 0.5, 0.5]}xLabel="Years" yLabel="Income">
57
+
41
58
  <BarSeries
42
- title="Economy"
43
- showLabels="no"
44
59
  faceColor="#e0e0e0"
45
- edgeColor="#e0e0e0"
46
- labels={years}
60
+ edgeColor="#909090"
47
61
  xValues={years}
48
62
  yValues={amount}
49
63
  />
64
+
65
+ // x and y axis with automatic ticks and grid lines
66
+ <XAxis slot="xaxis" />
67
+ <YAxis slot="yaxis" />
68
+
69
+ // box around the axes
70
+ <Box slot="box" />
71
+
50
72
  </Axes>
51
73
  </div>
74
+
75
+ <style>
76
+ .plot-container {
77
+ width: 100%;
78
+ height: 100%;
79
+ min-width: 200px;
80
+ min-height: 200px;
81
+ }
82
+ </style>
52
83
  ```
53
84
 
54
85
  Then run `npm run dev` in terminal and open the URL provided by npm in browser. That is it.
55
86
 
56
- Example below shows how to create a plot with axis, box and grid as well as how to combine several plot series together.
87
+ You can also use all capabilities of the `mdatools` package, e.g. generated random numbers:
57
88
 
58
- ```js
89
+ ```svelte
59
90
  <script>
60
- import {Axes, BarSeries} from 'svelte-plots';
91
+ import { Axes, BarSeries } from 'svelte-plots-basic';
92
+ import { Vector } from 'mdatools/arrays';
61
93
 
62
- // test data for the plot
63
- const years = [2010, 2020, 2030, 2040, 2050];
64
- const amount = [100, 200, 150, 300, -100];
94
+ // generate random values from normal distribution
95
+ const x = Vector.randn(200, 0, 1);
96
+ const y = Vector.randn(200, 0, 2);
65
97
  </script>
66
98
 
67
- <div class="plot">
68
- <Axes>
99
+ <div class="plot-container">
100
+ <Axes limX={[-6, 6]} limY={[-5, 6]} xLabel="x" yLabel="y">
69
101
 
70
- // bar, line and marker series with the same data values
71
- <BarSeries
72
- showLabels="no"
73
- faceColor="#eee"
74
- edgeColor="#eee"
75
- xValues={years}
76
- yValues={amount}
77
- />
78
- <LineSeries
79
- lineColor="blue"
80
- lineWidth={2}
81
- xValues={years}
82
- yValues={amount}
83
- />
84
102
  <ScatterSeries
85
- marker="✺"
86
- showLabels="hover"
87
- markerColor="blue"
88
- labels={years}
89
- xValues={years}
90
- yValues={amount}
103
+ xValues={x}
104
+ yValues={y}
91
105
  />
92
106
 
93
107
  // x and y axis with automatic ticks and grid lines
94
- <XAxis showGrid={true} slot="xaxis" />
95
- <YAxis showGrid={true} slot="yaxis" />
108
+ <XAxis slot="xaxis" />
109
+ <YAxis slot="yaxis" />
96
110
 
97
111
  // box around the axes
98
112
  <Box slot="box" />
113
+
99
114
  </Axes>
100
115
  </div>
101
116
  ```
102
117
 
103
- ## Details
104
-
105
- The library has following components:
106
-
107
- ### Axes
108
-
109
- `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:
110
-
111
- * `limX` — vector with two values - x-axis limits (min and max) in plot units.
112
- * `limY` — vector with two values - y-axis limits (min and max) in plot units.
113
- * `title` — a text to be shown as a plot title (on top of the plot).
114
- * `xLabel` — a label for x-axis (shown under the axis).
115
- * `yLabel` — a label for y-axis (shown on the right side of the axis).
116
-
117
- 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.
118
-
119
- 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.
120
-
121
- ### XAxis and YAxis
118
+ See demo for more details.
122
119
 
123
- 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:
120
+ ## Quick start (3D plots)
124
121
 
125
- * `ticks` — vector with ticks
126
- * `tickLabels` — vector with values to be shown as labels beside each tick
127
- * `showGrid` — a logical parameter, which allows to show grid lines on the plot
128
- * `slot` — name of the slot, `"xaxis"` or `"yaxis"`
129
122
 
130
- The components must be placed in a particular slot of the parent `Axes` component.
131
123
 
package/package.json CHANGED
@@ -1,17 +1,23 @@
1
1
  {
2
2
  "name": "svelte-plots-basic",
3
3
  "description": "Svelte components for creating simple plots",
4
- "version": "1.1.4",
4
+ "version": "2.1.0",
5
5
  "author": "Sergey Kucheryavskiy",
6
6
  "license": "MIT",
7
7
  "private": false,
8
- "main": "src/index.js",
9
-
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
+ },
10
14
  "dependencies": {
11
- "mdatools": "^0.6.1"
15
+ "mdatools": "^1.x.x"
12
16
  },
13
17
  "keywords": [
14
18
  "svelte",
19
+ "plots",
20
+ "charts",
15
21
  "svelte plots"
16
22
  ],
17
23
  "files": [
@@ -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
+