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/README.md +533 -448
- package/package.json +5 -2
- package/src/2d/Area.svelte +56 -48
- package/src/2d/Axes.svelte +409 -390
- package/src/2d/AxisLines.svelte +37 -36
- package/src/2d/AxisTickLabels.svelte +41 -49
- package/src/2d/Bars.svelte +102 -47
- package/src/2d/Box.svelte +21 -41
- package/src/2d/ColormapLegend.svelte +47 -111
- package/src/2d/Heatmap.svelte +107 -85
- package/src/2d/Legend.svelte +115 -126
- package/src/2d/Lines.svelte +54 -40
- package/src/2d/Multilines.svelte +73 -46
- package/src/2d/Points.svelte +76 -58
- package/src/2d/Rectangles.svelte +67 -58
- package/src/2d/Segments.svelte +59 -46
- package/src/2d/TextLabels.svelte +102 -72
- package/src/2d/TextLegend.svelte +17 -39
- package/src/2d/XAxis.svelte +49 -99
- package/src/2d/YAxis.svelte +47 -94
- package/src/2d/index.js +2 -1
- package/src/3d/Axes.svelte +306 -348
- package/src/3d/AxisLines.svelte +19 -37
- package/src/3d/AxisTickLabels.svelte +15 -32
- package/src/3d/Lines.svelte +59 -32
- package/src/3d/Mesh.svelte +106 -128
- package/src/3d/Points.svelte +56 -36
- package/src/3d/Segments.svelte +68 -52
- package/src/3d/TextLabels.svelte +63 -49
- package/src/3d/XAxis.svelte +36 -113
- package/src/3d/YAxis.svelte +36 -113
- package/src/3d/ZAxis.svelte +37 -114
- package/src/3d/index.js +2 -1
- package/src/constants.js +103 -0
- package/src/methods.js +1484 -0
- package/src/2d/Axis.svelte +0 -60
- package/src/3d/Axis.svelte +0 -64
- package/src/Colors.js +0 -51
- package/src/Utils.js +0 -639
package/README.md
CHANGED
|
@@ -1,6 +1,9 @@
|
|
|
1
1
|
# Svelte components for creating plots
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
`svelte-plots-basic` is a [Svelte](https://svelte.dev) component library for making simple responsive 2D and 3D plots/charts. The plots are created by generating [SVG](https://en.wikipedia.org/wiki/Scalable_Vector_Graphics) inside HTML document.
|
|
4
|
+
|
|
5
|
+
One can think of this library as "Lego" bricks for plots. It has two groups of components: for 2D and for 3D plots. The 2D components are more developed while 3D components remain quite basic. In addition to `svelte`, the package 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
|
+
|
|
4
7
|
|
|
5
8
|
## Showcase
|
|
6
9
|
|
|
@@ -8,713 +11,793 @@ These websites and web-applications use `svelte-plots-basic` library:
|
|
|
8
11
|
|
|
9
12
|
* [graasta.com](https://graasta.com) — interactive web-apps for learning statistics and beyond.
|
|
10
13
|
* [mda.tools/ddsimca/](https://mda.tools/ddsimca/) — interactive web-app for one class classification using [DD-SIMCA](https://analyticalsciencejournals.onlinelibrary.wiley.com/doi/epdf/10.1002/cem.3556) method.
|
|
14
|
+
* [mda.tools/pca/](https://mda.tools/pca/) — interactive web-app for Principal Component Analysis method.
|
|
11
15
|
|
|
12
16
|
## News
|
|
13
17
|
|
|
14
|
-
###
|
|
15
|
-
|
|
16
|
-
* improvements to axis ticks and tick labels estimation.
|
|
17
|
-
* `<XAxis>` and `<YAxis>` got a new logical parameter `whole` to force using whole numbers as ticks.
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
### 2.4.0
|
|
21
|
-
|
|
22
|
-
* added possibility to copy a plot to clipboard.
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
### 2.3.0
|
|
26
|
-
|
|
27
|
-
* added possibility to save plot as SVG and PNG files (see details below).
|
|
28
|
-
* improved handling of markers in `Points` component.
|
|
29
|
-
* small improvements and bug fixes.
|
|
30
|
-
* added more detailed description with code example both here and Svelte REPL (see below).
|
|
18
|
+
### 3.0.0
|
|
31
19
|
|
|
32
|
-
|
|
20
|
+
New major release (v. 3.0.0, released 20/01/2025) introduces many breaking changes as the library has been re-written using Svelte 5. If you use previous versions of `svelte-plots-basic` in your projects, and do not want to change anything, stick to the latest 2.x.x version.
|
|
33
21
|
|
|
34
|
-
|
|
35
|
-
* Other small improvements and bug fixes.
|
|
22
|
+
In addition to Svelte 5 syntax and functionality, this release also introduces a lot of improvements, such as better handling of axis ticks, new syntax for axis elements, etc. See examples below for inspiration. Here is a short list what has been changed from 2.x.x:
|
|
36
23
|
|
|
37
|
-
|
|
24
|
+
* **No more slots** — components `<Box>`, `<XAxis>`, `<YAxis>`, and `<ZAxis>` (for 3D) do not require attribute `slot` anymore. Moreover, you have to remove this attribute from all your old code in order to use the new version.
|
|
38
25
|
|
|
39
|
-
*
|
|
40
|
-
* 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.
|
|
41
|
-
* Small improvements and bug fixes.
|
|
26
|
+
* **Axis labels** — in version 2.x.x. labels for x- and y-axis were a part of `<Axes>` component. From 3.x.x. they are part of corresponding axis components, for example:<br> `<XAxis label="x-axis label"/>`.
|
|
42
27
|
|
|
43
|
-
|
|
44
|
-
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).
|
|
28
|
+
* **Support for subscripts and superscripts** — you can now use simple syntax for subscripts (`_`) and superscripts (`^`) in axis labels, tick labels, legend labels and plot title. For example, such labels as `'x^2'`, `x^-1` or `x_(34)` — will be correctly transformed to corresponding SVG text elements in order to visualize them correctly. You can also use HTML symbols, such as `α`.
|
|
45
29
|
|
|
46
|
-
|
|
30
|
+
* **Mouse events** — previosly, handling mouse events was done through a coplex system of manual events dispatched by `<Axes>` component. From 3.x.x this is much easier, almost every 2D series component (`<Rectangles>`, `<Bars>`, `<Points>`, `<Segments>`, `<Lines>`, `<Areas>`) can handle its own `onclick` event. You just need to provide a callback — a function that will be run if this event is fired. The callback should have one argument — id (position) of an element the click was made on. For example `<Points ... onclick={(id) => console.log(id)}>`. Because of this modification, the property `title` has been removed from all components. 2D component `<Axes>` also supports `onclick` event, it provides coordinates of the clicked point as arguments for the callback function. 3D plots do not support mouse events.
|
|
47
31
|
|
|
48
|
-
|
|
32
|
+
* **Save and copy 3D plots** — from 3.x.x you can also save and copy to clipboard 3D plots (same way as for 2D).
|
|
49
33
|
|
|
50
|
-
|
|
34
|
+
* **Doc strings** — every component has a corresponding doc string with description of its properties and a simple code example. It should be available when you move your mouse over the component tag in your editor/IDE if it supports this option (works in VSCode).
|
|
51
35
|
|
|
36
|
+
* **Properites** — from 3.x.x the naming of the properties is more consistent. For example, before the library used properties `borderColor` and `borderWidth` for areas, rectangels, bars and markers, while for lines, segments and multilines the similar properties were named as `lineColor` and `lineWidth`. Now they all have prefix `line*` if it is something about lines or segments and `face*` if it is somthing inside a closed contour. So no more `border*` properties.
|
|
52
37
|
|
|
53
38
|
## Installation
|
|
54
39
|
|
|
55
40
|
The set up process is similar to any other Svelte component library. Just use:
|
|
56
41
|
|
|
57
42
|
```
|
|
58
|
-
npm
|
|
43
|
+
npm install svelte-plots-basic
|
|
59
44
|
```
|
|
60
45
|
|
|
61
46
|
or, to install it with yarn:
|
|
62
47
|
|
|
63
48
|
```
|
|
64
|
-
yarn add
|
|
49
|
+
yarn add svelte-plots-basic
|
|
65
50
|
```
|
|
66
51
|
|
|
67
52
|
|
|
68
|
-
##
|
|
53
|
+
## User tutorial (2D plots)
|
|
69
54
|
|
|
70
|
-
Below you will find several simple examples which help you to start with. It is assumed that you already know the basics of Svelte.
|
|
71
55
|
|
|
72
|
-
|
|
56
|
+
### Axes
|
|
57
|
+
|
|
58
|
+
Component `<Axes>` is a main component which does most of the job. It must be a parent element of all other components. It has many properties, full list with explanation is available at the end of this section, meanwhile we consider the three most important ones.
|
|
59
|
+
|
|
60
|
+
Properties `limX` and `limY` define limits of the coordinates system in *plot's coordinates*. Imagine you want to show a bar plot with developing of GDP per capita in Germany during five years — from 2020 to 2024. The values in US dollars are [47342, 52301, 49725, 53565, 55859]. So limits of the x-axis will be for example from 2019 to 2025 to have some space around: `limX={[2019, 2025]}` and the limits for y-axis will be from 0 to 60000: `limY={[0, 60000]}`. Here is a full example:
|
|
73
61
|
|
|
74
62
|
```svelte
|
|
75
63
|
<script>
|
|
76
|
-
import {Axes
|
|
77
|
-
|
|
78
|
-
// test data for the plot
|
|
79
|
-
const years = [2010, 2020, 2030, 2040, 2050];
|
|
80
|
-
const amount = [100, 200, 150, 300, -100];
|
|
64
|
+
import { Axes } from 'svelte-plots-basic/2d';
|
|
81
65
|
</script>
|
|
82
66
|
|
|
83
|
-
<div class="plot-
|
|
84
|
-
<Axes limX={[
|
|
85
|
-
xLabel="Years" yLabel="Income">
|
|
86
|
-
|
|
87
|
-
<!-- series of bars for the defined data -->
|
|
88
|
-
<Bars
|
|
89
|
-
faceColor="#e0e0e0"
|
|
90
|
-
edgeColor="#909090"
|
|
91
|
-
xValues={years}
|
|
92
|
-
yValues={amount}
|
|
93
|
-
/>
|
|
94
|
-
|
|
95
|
-
<!-- x and y axis with automatic ticks and grid lines -->
|
|
96
|
-
<XAxis slot="xaxis" />
|
|
97
|
-
<YAxis slot="yaxis" />
|
|
98
|
-
|
|
99
|
-
<!-- box around axes -->
|
|
100
|
-
<Box slot="box" />
|
|
101
|
-
|
|
67
|
+
<div class="plot-wrapper">
|
|
68
|
+
<Axes limX={[2019, 2025]} limY={[0, 60000]}>
|
|
102
69
|
</Axes>
|
|
103
70
|
</div>
|
|
104
71
|
|
|
105
72
|
<style>
|
|
106
|
-
.plot-
|
|
73
|
+
.plot-wrapper {
|
|
107
74
|
width: 100%;
|
|
108
75
|
height: 100%;
|
|
109
|
-
min-width: 200px;
|
|
110
|
-
min-height: 200px;
|
|
111
76
|
}
|
|
112
77
|
</style>
|
|
113
78
|
```
|
|
114
79
|
|
|
115
|
-
|
|
80
|
+
These two properties must always be provided as an array with two values, and the first value must be smaller than the second. Both properties have default value of `[0, 1]`.
|
|
116
81
|
|
|
117
|
-
|
|
82
|
+
Second important property is `margins`, it defines margins around the plotting area — the one which will be used for positioning of actual plot elements. Margins should be provided as an array with four values: relative margins for bottom, left, top and right parts of the plot.
|
|
118
83
|
|
|
119
|
-
|
|
84
|
+
The default values are `[1.0, 1.1, 0.6, 0.6]` and they will fit most of the cases, so it is not necessary to provide your own. These values are picked up assuming that there will be x- and y-axis with ticks and tick labels, thefore the bottom and left margins are bigger than the top and the right.
|
|
120
85
|
|
|
121
|
-
|
|
122
|
-
<script>
|
|
123
|
-
import { Axes, XAxis, YAxis, Box, Points } from 'svelte-plots-basic/2d';
|
|
124
|
-
import { Vector } from 'mdatools/arrays';
|
|
86
|
+
The real margins in pixels depend on size of the plot on the screen and vary from 30 (small plot) to 60 (extra large) pixels. For example if plot is small, then margin value of `0.6` will lead to `30 * 0.6 = 18` pixels of margin.
|
|
125
87
|
|
|
126
|
-
|
|
127
|
-
const x = Vector.randn(200, 0, 1);
|
|
128
|
-
const y = Vector.randn(200, 0, 2);
|
|
129
|
-
</script>
|
|
88
|
+
The `Axes` component works by mapping points from the plot coordinates to the screen coordinates. It is reactive, meaning if you change the plot size interactively (e.g. by resizing the browser window), the coordinates will be recomputed and all elements will be redrawn automatically. The figure below show schematically how it works (just remember that in SVG y-coordinates start from top, not from bottom, hence the difference between equations for x- and y-coordinates):
|
|
130
89
|
|
|
131
|
-
|
|
132
|
-
<Axes limX={[-6, 6]} limY={[-5, 6]} xLabel="x" yLabel="y">
|
|
133
|
-
<Points xValues={x} yValues={y} />
|
|
134
|
-
<XAxis slot="xaxis" />
|
|
135
|
-
<YAxis slot="yaxis" />
|
|
136
|
-
<Box slot="box" />
|
|
137
|
-
</Axes>
|
|
138
|
-
</div>
|
|
90
|
+

|
|
139
91
|
|
|
140
|
-
|
|
141
|
-
.plot-container {
|
|
142
|
-
width: 100%;
|
|
143
|
-
height: 100%;
|
|
144
|
-
min-width: 200px;
|
|
145
|
-
min-height: 200px;
|
|
146
|
-
}
|
|
147
|
-
</style>
|
|
148
|
-
```
|
|
92
|
+
Finally you can also provide `title` of the plot which is shown on the top outside the main area (shown as gray rectangle in the figure above) thereby it does not affect margins, ticks, etc.
|
|
149
93
|
|
|
150
|
-
|
|
94
|
+
Here is a table with main properties of `Axes` component.
|
|
151
95
|
|
|
96
|
+
Property name | Default value | Description
|
|
97
|
+
--|--|--
|
|
98
|
+
`title` | `''` | title of the plot, shown on top
|
|
99
|
+
`limX` | `[0, 1]` | x-axis limits in plot coordinates
|
|
100
|
+
`limY` | `[0, 1]` | y-axis limits in plot coordinates
|
|
101
|
+
`margins` | `[1.0, 1.1, 0.6, 0.6]` | relative margins (bottom, left, top, right)
|
|
152
102
|
|
|
153
|
-
Below is a brief description of available components for 2D plots.
|
|
154
103
|
|
|
104
|
+
The component also provides functionality for saving plots as SVG or PNG files and/or copying the plot to clipboard as PNG image. In order to activate it one has to set value for property `downloadLinks`. It can be one of the following: `'none'` (default value, hides download buttons), `'hover'` — the toolbar with buttons pops up when user hover mouse cursor over the plot area, and `'fixed'` — the toobar is always shown. By default the toolbar with buttons is shown in the right bottom corener of the plot. This can be changed by amending CSS styles of `.download-links` and `.download-links > button` elements.
|
|
155
105
|
|
|
156
|
-
|
|
106
|
+
By default the size of the PNG image is 8 x 8 cm. If plot on the screen is not squared (e.g. width is larger than height), it will keep the largest size at 8 cm and adjust the other side automatically to keep the actual aspect ratio. The default resoulition of PNG image is 300 ppi (so the default pixel size is 2400 x 2400 pixels). All three parameters (physical width, height and resolution) can be adjusted by setting corresponding properties of the component.
|
|
157
107
|
|
|
158
|
-
|
|
108
|
+
One can also adjust width and height of image, which is copied to clipboard. In this case both parameters are set in pixels.
|
|
159
109
|
|
|
160
|
-
Here is
|
|
110
|
+
Here is a table with properties of the component related to saving plot to a file and copying it to clipboard.
|
|
161
111
|
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
112
|
+
Property name | Default value | Description
|
|
113
|
+
--|--|--
|
|
114
|
+
`downloadLinks` | `'none'` | show toolbar with download buttons: `'none'`, `'hover'` or `'fixed'`
|
|
115
|
+
`fileName` | `'plot'` | filename for downloaded image file without extention
|
|
116
|
+
`pngWidth` | `8` | size of PNG image in cm
|
|
117
|
+
`pngHeight` | `8` | size of PNG image in cm
|
|
118
|
+
`pngRes` | `300` | resolution of PNG image (pixels per inch)
|
|
119
|
+
`clipboardWidth` | `1200` | width of image in clipboard in pixels
|
|
120
|
+
`clipboardHeight` | `800` | height of image in clipboard in pixels
|
|
166
121
|
|
|
167
|
-
|
|
168
|
-
limX={[0, 10]}
|
|
169
|
-
limY={[-100, 100]}
|
|
170
|
-
title="My super plot"
|
|
171
|
-
xLabel="X-axis label"
|
|
172
|
-
yLabel="Y-axis label"
|
|
173
|
-
margins={[1.0, 0.75, 0.5, 0.5]}
|
|
174
|
-
|
|
175
|
-
downloadLinks="hover"
|
|
176
|
-
fileName="plot"
|
|
177
|
-
pngWidth={8}
|
|
178
|
-
pngHeight={8}
|
|
179
|
-
pngRes={300}
|
|
180
|
-
>
|
|
181
|
-
</Axes>
|
|
182
|
-
```
|
|
122
|
+
Please pay attention that most of the browsers enable clipboard functionality only if website is available via HTTPS.
|
|
183
123
|
|
|
184
|
-
Parameter `margin` must contain four values, relative margins for bottom, left, top and right sides around main plotting area. The larger margin value is the more space available for axis ticks, labels, etc.
|
|
185
124
|
|
|
186
|
-
|
|
125
|
+
Finally, one can also provide callback functions for several mouse events (click, down, up and move). The event is triggered when someone clicks inside the plotting area but outside any other plotting element (e.g. marker, line, rectangle, etc).
|
|
187
126
|
|
|
188
|
-
|
|
127
|
+
`<Axes>` is the only component that supports several events, other components support only `onclick`. For every event the component calls a provided callback function with two arguments — x- and y- coordinates of the mouse pointer in plot coordinates (not pixels).
|
|
189
128
|
|
|
129
|
+
Here is a table with properties for handling mouse events.
|
|
190
130
|
|
|
191
|
-
|
|
131
|
+
Property name | Default value | Description
|
|
132
|
+
--|--|--
|
|
133
|
+
`onclick` | | callback for on mouse click event
|
|
134
|
+
`onmousemove` | | callback for on mouse move event
|
|
135
|
+
`onmousedown` | | callback for on mouse down
|
|
136
|
+
`onmouseup` | | callback for on mouse up
|
|
192
137
|
|
|
193
|
-
Two components, `XAxis` and `YAxis`, add corresponding elements to the plot. Both have one mandatory argument, `slot`, which must have values `"xaxis"` and `"yaxis"` correspondingly. Other parameters let you define manual ticks and tick labels as well as turn on/off grid lines.
|
|
194
138
|
|
|
195
|
-
|
|
139
|
+
### Box
|
|
196
140
|
|
|
197
|
-
|
|
198
|
-
<script>
|
|
199
|
-
import { Axes, XAxis, YAxis } from 'svelte-plots-basic/2d';
|
|
200
|
-
</script>
|
|
141
|
+
By default `Axes` does not show any elements, it simply creates an empty SVG image inside the wrapper element and implements all necessary functionality, like transformation of coordinates, download and copy functionality, etc. In order to add elements you need to put corresponding components inside the `<Axes>...</Axes>`.
|
|
201
142
|
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
showGrid={true}
|
|
208
|
-
las={2}
|
|
209
|
-
lineColor="#a0a0a0"
|
|
210
|
-
gridColor="#e0e0e0"
|
|
211
|
-
textColor="#ff6666"
|
|
212
|
-
/>
|
|
143
|
+
The simplest component is `<Box>` which creates a box/frame/rectangle around the plotting area. You can decide thickness and color of the box lines. Here is an example:
|
|
144
|
+
|
|
145
|
+
```svelte
|
|
146
|
+
<Axes limX={[2019, 2025]} limY={[0, 60000]}>
|
|
147
|
+
<Box />
|
|
213
148
|
</Axes>
|
|
214
149
|
```
|
|
215
150
|
|
|
216
|
-
The
|
|
151
|
+
The element has only two properties:
|
|
217
152
|
|
|
218
|
-
|
|
153
|
+
Property name | Default value | Description
|
|
154
|
+
--|--|--
|
|
155
|
+
`lineWidth` | `1` | width/thickness of box line in pixels
|
|
156
|
+
`lineColor` | `'#606060'` | color of the line
|
|
219
157
|
|
|
220
158
|
|
|
221
159
|
|
|
222
|
-
###
|
|
160
|
+
### Axis
|
|
223
161
|
|
|
224
|
-
|
|
162
|
+
There are two components to add x- and y-axis: `XAxis` and `YAxis`. Both have identicall set of parameters. Each component adds corresponding axis elements, such as axis line, tick lines located at tick positions/values and tick labels. By default everything is computed automatically based on axis limits. Here is an example:
|
|
225
163
|
|
|
226
164
|
```svelte
|
|
227
|
-
<
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
<Axes limX={[-6, 6]} limY={[-5, 6]}>
|
|
232
|
-
<Box slot="box" />
|
|
165
|
+
<Axes limX={[2019, 2025]} limY={[0, 60000]}>
|
|
166
|
+
<XAxis label="Year" />
|
|
167
|
+
<YAxis label="GDP per capita" />
|
|
233
168
|
</Axes>
|
|
234
169
|
```
|
|
235
170
|
|
|
236
|
-
|
|
237
|
-
|
|
171
|
+
You can also define tick positions and corresponding labels manually. Tick positions must be provided as array of numeric values. They must be unique, and all values must be inside the axis limits. You can also provide manual tick labels, in this case it should be array of text values. The number of labels should match the number of ticks, which means if you want to provide manual tick labels, you should provide corresponding tick values as well. Here is an example:
|
|
238
172
|
|
|
239
|
-
|
|
173
|
+
```svelte
|
|
174
|
+
<Axes limX={[2019, 2026]} limY={[0, 60000]}>
|
|
175
|
+
<Xaxis ticks={[2023, 2024, 2025]} tickLabels={['Past', 'Present', 'Future']} />
|
|
176
|
+
</Axes>
|
|
177
|
+
```
|
|
240
178
|
|
|
241
|
-
|
|
179
|
+
Tick and axis labels can include super and subscripts and specifal HTML symbols (e.g. `'α^2'` or `'γ_(34)'`).
|
|
242
180
|
|
|
243
|
-
Here is
|
|
181
|
+
Here is a full set of properties for both components:
|
|
244
182
|
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
|
|
248
|
-
|
|
183
|
+
Property name | Default value | Description
|
|
184
|
+
--|--|--
|
|
185
|
+
`label` | `''` | axis label
|
|
186
|
+
`showGrid` | `false` | show or not grid lines at tick positions
|
|
187
|
+
`ticks` | | tick positions, by default will be computed automatically
|
|
188
|
+
`tickLabels` | | tick labels, by default will be based on tick position values
|
|
189
|
+
`las` | `1` | orientation of tick labels: `1` — for horizontal, `2` — for vertical
|
|
190
|
+
`whole` | `false` | if `true` the automatic tick labels will be shown as whole numbers
|
|
249
191
|
|
|
250
|
-
<Axes limX={[-4, 4]} limY={[-1, 10]}>
|
|
251
|
-
<Points
|
|
252
|
-
xValues={[-3, -2, -1, 0, 1, 2, 3]}
|
|
253
|
-
yValues={[9, 4, 1, 0, 1, 4, 9]}
|
|
254
|
-
marker={1}
|
|
255
|
-
faceColor="transparent"
|
|
256
|
-
borderColor="#ff0000"
|
|
257
|
-
borderWidth={2}
|
|
258
|
-
markerSize={2}
|
|
259
|
-
title="series1"
|
|
260
|
-
/>
|
|
261
|
-
</Axes>
|
|
262
|
-
```
|
|
263
192
|
|
|
264
|
-
|
|
193
|
+
Check these Svelte REPL examples covering the use of all four components: [plots-axes-simple](https://svelte.dev/playground/d818a241c85844249b34e75196ac308c), [plots-axes-advanced](https://svelte.dev/playground/d06edc89a2d341faba03e0d03f43839f), [plots-axes-mouse](https://svelte.dev/playground/706b66ce008d40c9bcad1e4c89434e23). The last example also uses other elements which are described below.
|
|
265
194
|
|
|
266
|
-
The size of markers is defined in `"em"` units.
|
|
267
195
|
|
|
268
|
-
Parameter `title` is needed only if you want to handle click events, for example, to select a particular point. See specific section below with more details below.
|
|
269
196
|
|
|
270
|
-
[This example](https://svelte.dev/repl/edc090cb1c184fee88aedebd8731a87e?version=4.2.18) in Svelte REPL.
|
|
271
197
|
|
|
198
|
+
### Series
|
|
272
199
|
|
|
273
|
-
|
|
200
|
+
Series are components that add several items of the same nature (hence series) on the plot. There are seven components in this group, below you will find their short description. Each component has a property `onclick` whose value should be a callback function that will be called when a user clicks on any item of the series. The callback function should have one argument — the index of the item.
|
|
274
201
|
|
|
275
|
-
|
|
202
|
+
#### Points
|
|
276
203
|
|
|
277
|
-
|
|
204
|
+
This component shows a set of points using one of eight pre-defined markers. Also known as scatter series. Here is an example:
|
|
278
205
|
|
|
279
206
|
```svelte
|
|
280
207
|
<script>
|
|
281
|
-
import { Axes,
|
|
208
|
+
import { Axes, XAxis, YAxis, Box, Points } from 'svelte-plots-basic/2d';
|
|
209
|
+
const height = [1.68, 1.72, 1.88, 1.54, 1.79];
|
|
210
|
+
const weight = [70, 69, 90, 56, 74];
|
|
282
211
|
</script>
|
|
283
212
|
|
|
284
|
-
<
|
|
285
|
-
|
|
286
|
-
|
|
287
|
-
|
|
288
|
-
|
|
289
|
-
|
|
290
|
-
lineColor="#ff0000"
|
|
291
|
-
lineType={3}
|
|
213
|
+
<div class="plot-wrapper">
|
|
214
|
+
<Axes limX={[1, 2]} limY={[50, 100]} title="People">
|
|
215
|
+
|
|
216
|
+
<Points xValues={height} yValues={weight}
|
|
217
|
+
marker={2}
|
|
218
|
+
lineColor="#3344ff"
|
|
292
219
|
lineWidth={2}
|
|
220
|
+
faceColor="#3344ff80"
|
|
293
221
|
/>
|
|
222
|
+
|
|
223
|
+
<XAxis label="Height, m", showGrid={true} />
|
|
224
|
+
<YAxis label="Weight, kg" showGrid={true} />
|
|
225
|
+
<Box />
|
|
294
226
|
</Axes>
|
|
227
|
+
</div>
|
|
228
|
+
|
|
229
|
+
<style>
|
|
230
|
+
.plot-wrapper {
|
|
231
|
+
width: 100%;
|
|
232
|
+
height: 100%;
|
|
233
|
+
}
|
|
234
|
+
</style>
|
|
295
235
|
```
|
|
296
236
|
|
|
297
|
-
|
|
237
|
+
Check more advanced example in Svelte REPL: [plots-points](https://svelte.dev/playground/edc090cb1c184fee88aedebd8731a87e).
|
|
298
238
|
|
|
299
|
-
[This example](https://svelte.dev/repl/41285c86fe6e4e6c9abc50fb08faa7d9?version=4.2.18) in Svelte REPL.
|
|
300
239
|
|
|
240
|
+
Here is a table with all properties:
|
|
301
241
|
|
|
302
|
-
|
|
242
|
+
Property name | Default value | Description
|
|
243
|
+
--|--|--
|
|
244
|
+
`xValues` | | array or vector with x-coordinates of the points
|
|
245
|
+
`yValues` | | array or vector with y-coordinates of the points
|
|
246
|
+
`marker` | `1` | value between 1 and 8 defininng markers: `●, ◼, ▲, ▼, ⬥, +, *, ✕`
|
|
247
|
+
`markerSize` | `1` | size of the marker symbol in em
|
|
248
|
+
`lineColor` | `'#2679B2'` | color of marker border
|
|
249
|
+
`lineWidth` | `1` | thickness/width of the border in pixels
|
|
250
|
+
`faceColor` | `'transparent'` | color of the face of the marker (only for the first five)
|
|
251
|
+
`onclick` | `null` | callback function for on mouse click event
|
|
303
252
|
|
|
304
|
-
Use this component if you want to show a series of rectangles. The component has four mandatory parameters — coordinates of left-top corner, width and height of each rectangle. The coordinates and sizes can be provided as Javascript array or as a `Vector` instance.
|
|
305
253
|
|
|
306
|
-
|
|
254
|
+
Last three markers (`+, *, ✕`) do not have face, hence `faceColor` has no effect for them.
|
|
255
|
+
|
|
256
|
+
#### Text labels
|
|
257
|
+
|
|
258
|
+
This component is similar to `<Points>` but lets you put any text at the specified positions. Here is an example, based on the previous case but here we also use `<TextLabels>` to add labels for each point:
|
|
307
259
|
|
|
308
260
|
|
|
309
261
|
```svelte
|
|
310
262
|
<script>
|
|
311
|
-
import { Axes,
|
|
263
|
+
import { Axes, XAxis, YAxis, Box, Points } from 'svelte-plots-basic/2d';
|
|
264
|
+
const height = [1.68, 1.72, 1.88, 1.54, 1.79];
|
|
265
|
+
const weight = [70, 69, 90, 56, 74];
|
|
266
|
+
const labels = ['Bob', 'Eva', 'John', 'Leya', 'Peter'];
|
|
312
267
|
</script>
|
|
313
268
|
|
|
314
|
-
<
|
|
315
|
-
|
|
316
|
-
|
|
317
|
-
|
|
318
|
-
|
|
319
|
-
|
|
320
|
-
|
|
321
|
-
|
|
322
|
-
|
|
323
|
-
/>
|
|
269
|
+
<div class="plot-wrapper">
|
|
270
|
+
<Axes limX={[1, 2]} limY={[50, 100]} title="People">
|
|
271
|
+
<Points xValues={height} yValues={weight} />
|
|
272
|
+
|
|
273
|
+
<TextLabels xValues={height} yValues={weight} {labels} pos={3} />
|
|
274
|
+
|
|
275
|
+
<XAxis label="Height, m", showGrid={true} />
|
|
276
|
+
<YAxis label="Weight, kg" showGrid={true} />
|
|
277
|
+
<Box />
|
|
324
278
|
</Axes>
|
|
325
|
-
|
|
279
|
+
</div>
|
|
326
280
|
|
|
327
|
-
|
|
281
|
+
<style>
|
|
282
|
+
.plot-wrapper {
|
|
283
|
+
width: 100%;
|
|
284
|
+
height: 100%;
|
|
285
|
+
}
|
|
286
|
+
</style>
|
|
287
|
+
```
|
|
328
288
|
|
|
329
|
-
|
|
289
|
+
Check this example in Svelte REPL: [plots-textlabels](https://svelte.dev/playground/ae58b8d4e92f4e748a679b60c4876347).
|
|
330
290
|
|
|
331
|
-
Use this component if you want to show a filled area of arbitrary shape. The component has two mandatory parameters — x- and y-coordinates of the corner points of the area. The coordinates can be provided as Javascript array or as a `Vector` instance.
|
|
332
291
|
|
|
333
|
-
Here is
|
|
292
|
+
Here is a full set of properties:
|
|
334
293
|
|
|
335
|
-
|
|
336
|
-
|
|
337
|
-
|
|
338
|
-
|
|
294
|
+
Property name | Default value | Description
|
|
295
|
+
--|--|--
|
|
296
|
+
`xValues` | | array or vector with x-coordinates of the labels position
|
|
297
|
+
`yValues` | | array or vector with y-coordinates of the labels position
|
|
298
|
+
`labels` | | text labels (either array or single value for all positions)
|
|
299
|
+
`pos` | `0` | positions of labels related to coordinates (see details)
|
|
300
|
+
`lineColor` | `'transparent'` | color of border of the labels' symbols
|
|
301
|
+
`lineWidth` | `1` | thickness/width of the border in pixels
|
|
302
|
+
`faceColor` | `'#2679B2'` | color of the face of the symbols
|
|
303
|
+
`textSize` | `1` | size of the labels' symbols in em
|
|
304
|
+
`rotateAngle` | `0` | angle in degrees to rotate the labels
|
|
305
|
+
`onclick` | `null` | callback function for on mouse click event
|
|
339
306
|
|
|
340
|
-
|
|
341
|
-
|
|
342
|
-
xValues={[3, 2, 1, 4, 5]}
|
|
343
|
-
yValues={[-5, 10, 20, 10, -2]}
|
|
344
|
-
fillColor="#ffc00080"
|
|
345
|
-
lineColor="#ff0000"
|
|
346
|
-
lineWidth={2}
|
|
347
|
-
lineType={3}
|
|
348
|
-
/>
|
|
349
|
-
</Axes>
|
|
350
|
-
```
|
|
307
|
+
Both `labels` and `pos` properties can be provided as array or as a single value. The number of elements in array should match the number of coordinates. If single value is provided it will be
|
|
308
|
+
replicated for all coordinates.
|
|
351
309
|
|
|
352
|
-
[This example](https://svelte.dev/repl/478b77501386469b93a1160e46809644?version=4.2.18) in Svelte REPL.
|
|
353
310
|
|
|
354
|
-
### Text labels
|
|
355
311
|
|
|
356
|
-
|
|
312
|
+
#### Segments
|
|
357
313
|
|
|
358
|
-
|
|
314
|
+
This component shows a series of line segments connecting two points (start and end). Hence it has four mandatory arguments: x- and y-coordinates of start and end points.
|
|
359
315
|
|
|
360
316
|
```svelte
|
|
361
317
|
<script>
|
|
362
|
-
import { Axes,
|
|
318
|
+
import { Axes, XAxis, YAxis, Box, Segments } from 'svelte-plots-basic/2d';
|
|
319
|
+
const xStart = [1, 2, 3, 4, 5];
|
|
320
|
+
const yStart = [1, 2, 3, 2, 1];
|
|
321
|
+
const xEnd = [1, 2, 3, 4, 5];
|
|
322
|
+
const yEnd = [9, 7, 6, 7, 9];
|
|
363
323
|
</script>
|
|
364
324
|
|
|
365
|
-
<
|
|
366
|
-
|
|
367
|
-
|
|
368
|
-
|
|
369
|
-
|
|
370
|
-
|
|
371
|
-
|
|
372
|
-
|
|
373
|
-
borderWidth={1}
|
|
374
|
-
textSize={4}
|
|
375
|
-
/>
|
|
325
|
+
<div class="plot-wrapper">
|
|
326
|
+
<Axes limX={[0, 6]} limY={[0, 10]} >
|
|
327
|
+
|
|
328
|
+
<Segments {xStart} {yStart} {xEnd} {yEnd} lineWidth={2} lineColor="#ff4422" />
|
|
329
|
+
|
|
330
|
+
<XAxis showGrid={true} />
|
|
331
|
+
<YAxis showGrid={true} />
|
|
332
|
+
<Box />
|
|
376
333
|
</Axes>
|
|
334
|
+
</div>
|
|
335
|
+
|
|
336
|
+
<style>
|
|
337
|
+
.plot-wrapper {
|
|
338
|
+
width: 100%;
|
|
339
|
+
height: 100%;
|
|
340
|
+
}
|
|
341
|
+
</style>
|
|
377
342
|
```
|
|
378
343
|
|
|
379
|
-
|
|
344
|
+
Check this example in Svelte REPL: [plots-segments](https://svelte.dev/playground/41285c86fe6e4e6c9abc50fb08faa7d9).
|
|
380
345
|
|
|
381
|
-
It can be specified as a single value, like in the example above, or as array of values — individual for each label.
|
|
382
346
|
|
|
383
|
-
|
|
347
|
+
Here is a full set of properties:
|
|
384
348
|
|
|
385
|
-
|
|
349
|
+
Property name | Default value | Description
|
|
350
|
+
--|--|--
|
|
351
|
+
`xStart` | | array or vector with x-coordinates of the start points
|
|
352
|
+
`yStart` | | array or vector with y-coordinates of the start points
|
|
353
|
+
`xEnd` | | array or vector with x-coordinates of the end points
|
|
354
|
+
`yEnd` | | array or vector with y-coordinates of the end points
|
|
355
|
+
`lineColor` | `'#2679B2'` | color of the lines
|
|
356
|
+
`lineWidth` | `1` | thickness/width of the lines in pixels
|
|
357
|
+
`lineType` | `1` | line type (`1`- solid, `2` - dashed, `3` - dotted, `4` - dashdot)
|
|
358
|
+
`onclick` | `null` | callback function for on mouse click event
|
|
386
359
|
|
|
387
360
|
|
|
388
|
-
### Bars
|
|
389
361
|
|
|
390
|
-
|
|
362
|
+
#### Lines
|
|
363
|
+
|
|
364
|
+
Lines components is similar to `<Points>` but instead of showing markers, it connects the points with line segments. If the points are close to each other, the result looks like a smooth curve.
|
|
391
365
|
|
|
392
|
-
Here is an example of using the component with all available parameters:
|
|
393
366
|
|
|
394
367
|
```svelte
|
|
395
368
|
<script>
|
|
396
|
-
import { Axes,
|
|
397
|
-
</script>
|
|
369
|
+
import { Axes, XAxis, YAxis, Box, Lines } from 'svelte-plots-basic/2d';
|
|
398
370
|
|
|
399
|
-
|
|
400
|
-
|
|
401
|
-
|
|
402
|
-
yValues={[9, 4, 1, 0.1, 1, 4, 9]}
|
|
403
|
-
faceColor="#ffcc0080"
|
|
404
|
-
borderColor="#ff0000"
|
|
405
|
-
borderWidth={2}
|
|
406
|
-
/>
|
|
407
|
-
</Axes>
|
|
408
|
-
```
|
|
371
|
+
const xValues = [-4, -3, -2, -1, 0, 1, 2, 3, 4];
|
|
372
|
+
const yValues = [16, 9, 4, 1, 0, 1, 4, 9, 16];
|
|
373
|
+
</script>
|
|
409
374
|
|
|
410
|
-
|
|
375
|
+
<div class="plot-wrapper">
|
|
376
|
+
<Axes limX={[-10, 10]} limY={[-2, 20]} >
|
|
411
377
|
|
|
378
|
+
<Lines {xValues} {yValues} lineWidth={2} lineColor="#ff4422" />
|
|
412
379
|
|
|
413
|
-
|
|
380
|
+
<XAxis showGrid={true} />
|
|
381
|
+
<YAxis showGrid={true} />
|
|
382
|
+
<Box />
|
|
383
|
+
</Axes>
|
|
384
|
+
</div>
|
|
414
385
|
|
|
415
|
-
|
|
386
|
+
<style>
|
|
387
|
+
.plot-wrapper {
|
|
388
|
+
width: 100%;
|
|
389
|
+
height: 100%;
|
|
390
|
+
}
|
|
391
|
+
</style>
|
|
392
|
+
```
|
|
416
393
|
|
|
417
|
-
|
|
394
|
+
Check this example in Svelte REPL: [plots-lines](https://svelte.dev/playground/2006ca1441f845eb9ed40b3e583d0094).
|
|
418
395
|
|
|
419
|
-
```svelte
|
|
420
|
-
<script>
|
|
421
|
-
import { Axes, Lines } from 'svelte-plots-basic/2d';
|
|
422
|
-
</script>
|
|
423
396
|
|
|
424
|
-
|
|
425
|
-
<Lines
|
|
426
|
-
xValues={[-3, -2, -1, 0, 1, 2, 3]}
|
|
427
|
-
yValues={[9, 4, 1, 0, 1, 4, 9]}
|
|
428
|
-
lineColor="#ff0000"
|
|
429
|
-
lineWidth={2}
|
|
430
|
-
lineType={3}
|
|
431
|
-
/>
|
|
432
|
-
</Axes>
|
|
433
|
-
```
|
|
397
|
+
Here is a full set of properties:
|
|
434
398
|
|
|
435
|
-
|
|
399
|
+
Property name | Default value | Description
|
|
400
|
+
--|--|--
|
|
401
|
+
`xValues` | | array or vector with x-coordinates of the points
|
|
402
|
+
`yValues` | | array or vector with y-coordinates of the points
|
|
403
|
+
`lineColor` | `'#2679B2'` | color of the lines
|
|
404
|
+
`lineWidth` | `1` | thickness/width of the lines in pixels
|
|
405
|
+
`lineType` | `1` | line type similar to `Segments`
|
|
406
|
+
`onclick` | `null` | callback function for on mouse click event
|
|
436
407
|
|
|
437
|
-
[This example](https://svelte.dev/repl/2006ca1441f845eb9ed40b3e583d0094?version=4.2.18) in Svelte REPL.
|
|
438
408
|
|
|
439
409
|
|
|
440
|
-
|
|
410
|
+
#### Multilines
|
|
441
411
|
|
|
442
|
-
This component is similar to `Lines` but it
|
|
412
|
+
This component is similar to `Lines` but it can show multiple lines whose x-coordinates are the same and each line has its own y-coordinates. Most of the properties are identical the properties of `Lines` except one — `yValues` must be provided as an instance of `Matrix` class (from `mdatools` package). Every column of this matrix contains y-coordinates of corresponding line.
|
|
443
413
|
|
|
444
414
|
Here is an example of using the component with all available parameters:
|
|
445
415
|
|
|
446
416
|
```svelte
|
|
447
417
|
<script>
|
|
448
|
-
import { Axes, Multilines } from 'svelte-plots-basic/2d';
|
|
418
|
+
import { Axes, XAxis, YAxis, Box, Multilines } from 'svelte-plots-basic/2d';
|
|
449
419
|
import { Vector, cbind } from 'mdatools/arrays';
|
|
450
420
|
|
|
451
421
|
// create x-values
|
|
452
|
-
const
|
|
422
|
+
const xValues = Vector.seq(0, 15, 0.1);
|
|
453
423
|
|
|
454
424
|
// compute y-values for four lines
|
|
455
|
-
const y1 =
|
|
425
|
+
const y1 = xValues.apply(v => Math.sin(v));
|
|
456
426
|
const y2 = y1.add(0.2);
|
|
457
427
|
const y3 = y2.add(0.2);
|
|
458
428
|
const y4 = y3.add(0.2);
|
|
459
429
|
|
|
460
|
-
// combine y-values
|
|
461
|
-
const
|
|
430
|
+
// combine y-values so they form columns of matrix Y
|
|
431
|
+
const yValues = cbind(y1, y2, y3, y4);
|
|
462
432
|
</script>
|
|
463
433
|
|
|
464
|
-
<
|
|
465
|
-
|
|
466
|
-
|
|
467
|
-
|
|
468
|
-
|
|
469
|
-
|
|
470
|
-
|
|
471
|
-
/>
|
|
434
|
+
<div class="plot-wrapper">
|
|
435
|
+
<Axes limX={[-1, 16]} limY={[-2,2]} >
|
|
436
|
+
|
|
437
|
+
<Multilines {xValues} {yValues} lineWidth={2} lineColor="#ff4422" />
|
|
438
|
+
|
|
439
|
+
<XAxis showGrid={true} />
|
|
440
|
+
<YAxis showGrid={true} />
|
|
441
|
+
<Box />
|
|
472
442
|
</Axes>
|
|
443
|
+
</div>
|
|
444
|
+
|
|
445
|
+
<style>
|
|
446
|
+
.plot-wrapper {
|
|
447
|
+
width: 100%;
|
|
448
|
+
height: 100%;
|
|
449
|
+
}
|
|
450
|
+
</style>
|
|
473
451
|
```
|
|
474
452
|
|
|
475
|
-
|
|
453
|
+
Check this example in Svelte REPL: [plots-multilines](https://svelte.dev/playground/d642a8fb78fd4a4f8381438d9bf427a2).
|
|
476
454
|
|
|
477
455
|
|
|
478
|
-
|
|
456
|
+
#### Rectangles
|
|
479
457
|
|
|
480
|
-
|
|
458
|
+
Use this component if you want to show a series of rectangles. The component has four mandatory parameters — coordinates of left-top corner, width and height of each rectangle.
|
|
481
459
|
|
|
482
|
-
Here is an example:
|
|
483
460
|
|
|
484
461
|
```svelte
|
|
485
462
|
<script>
|
|
486
|
-
import { Axes,
|
|
487
|
-
import {Vector} from 'mdatools/arrays';
|
|
463
|
+
import { Axes, XAxis, YAxis, Box, Rectangles } from 'svelte-plots-basic/2d';
|
|
488
464
|
|
|
489
|
-
|
|
490
|
-
const
|
|
465
|
+
const left = [-3, -2, -1, 0, 1, 2, 3];
|
|
466
|
+
const top = [9, 8, 7, 6, 7, 8, 9];
|
|
467
|
+
const width = [0.75, 0.75, 0.75, 0.75, 0.75, 0.75, 0.75];
|
|
468
|
+
const height = [8, 6, 4, 2, 4, 6, 8];
|
|
491
469
|
|
|
492
|
-
// compute y-values
|
|
493
|
-
const y1 = x.apply(v => Math.pow(v, 2));
|
|
494
|
-
const y2 = x.apply(v => Math.pow(v, 3));
|
|
495
|
-
const y3 = x.apply(v => Math.pow(v, 4));
|
|
496
470
|
</script>
|
|
497
471
|
|
|
498
|
-
<
|
|
499
|
-
|
|
500
|
-
<!-- series 1: dotted red line -->
|
|
501
|
-
<Lines xValues={x} yValues={y1} lineColor="red" lineType={3} />
|
|
502
|
-
|
|
503
|
-
<!-- series 2: solid blue line and circle markers -->
|
|
504
|
-
<Lines xValues={x} yValues={y2} lineColor="blue" lineType={1} />
|
|
505
|
-
<Points xValues={x} yValues={y2} borderColor="blue" faceColor="white"/>
|
|
506
|
-
|
|
507
|
-
<!-- series 3: markers in form of diamonds with green stroke and yellow fill -->
|
|
508
|
-
<Points xValues={x} yValues={y3} marker={5} faceColor="yellow" borderColor="green" />
|
|
472
|
+
<div class="plot-wrapper">
|
|
473
|
+
<Axes limX={[-4, 4]} limY={[0, 10]}>
|
|
509
474
|
|
|
510
|
-
|
|
511
|
-
|
|
512
|
-
|
|
513
|
-
|
|
514
|
-
{"label": 'y=x^2', "lineType": 3, "lineColor": "red"},
|
|
515
|
-
{"label": "y=x^3", "lineType": 1, "lineColor": "blue", "marker": 1, "faceColor": "white", "borderColor": "blue"},
|
|
516
|
-
{"label": "y=x^4", "marker": 5, "faceColor": "yellow", "borderColor": "green"},
|
|
517
|
-
]}
|
|
475
|
+
<Rectangles {left} {top} {width} {height}
|
|
476
|
+
faceColor="#ff000080"
|
|
477
|
+
lineColor="#ff0000"
|
|
478
|
+
lineWidth={2}
|
|
518
479
|
/>
|
|
480
|
+
|
|
481
|
+
<XAxis showGrid={true} />
|
|
482
|
+
<YAxis showGrid={true} />
|
|
483
|
+
<Box />
|
|
519
484
|
</Axes>
|
|
485
|
+
</div>
|
|
486
|
+
|
|
487
|
+
<style>
|
|
488
|
+
.plot-wrapper {
|
|
489
|
+
width: 100%;
|
|
490
|
+
height: 100%;
|
|
491
|
+
}
|
|
492
|
+
</style>
|
|
520
493
|
```
|
|
494
|
+
Check more advanced example in Svelte REPL: [plots-rectangles](https://svelte.dev/playground/8ef24c0380474d2a9ff15ab66f2d0572).
|
|
521
495
|
|
|
522
|
-
The `position` parameter can be one of the follows: `"topleft"`, `"top"`, `"topright"`, `"right"`, `"bottomright"` and so on.
|
|
523
496
|
|
|
524
|
-
|
|
497
|
+
Here is a table with all properties:
|
|
525
498
|
|
|
526
|
-
|
|
499
|
+
Property name | Default value | Description
|
|
500
|
+
--|--|--
|
|
501
|
+
`left` | | array or vector with coordinates of the left side of the rectangles
|
|
502
|
+
`top` | | array or vector with coordinates of the top side of the rectangles
|
|
503
|
+
`height` | | single value, array or vector with height values of the rectangle
|
|
504
|
+
`width` | | single value, array or vector with width values of the rectangle
|
|
505
|
+
`lineColor` | `'#2679B2'` | color of border line
|
|
506
|
+
`lineWidth` | `1` | thickness/width of the border in pixels
|
|
507
|
+
`faceColor` | `'transparent'` | color of the face of the marker (only for first five)
|
|
508
|
+
`onclick` | `null` | callback function for on mouse click event
|
|
527
509
|
|
|
528
|
-
|
|
510
|
+
The `left` and `top` properties must be provided as vector or array. But `height` or/and `width` can be provided as single values, in this case all rectangles will have the same height and width.
|
|
529
511
|
|
|
530
|
-
```svelte
|
|
531
|
-
<script>
|
|
532
|
-
import { Axes, Heatmap } from 'svelte-plots-basic/2d';
|
|
533
|
-
import { matrix, vector } from 'mdatools/arrays';
|
|
534
512
|
|
|
535
|
-
|
|
536
|
-
|
|
513
|
+
#### Bars
|
|
514
|
+
|
|
515
|
+
Adds series of bars. The properties are similar to `Rectangles` but you have to provide only x- and y-coordinates of the top (or bottom if value is negative) sides of the bar.
|
|
537
516
|
|
|
538
|
-
// create vector of breaks
|
|
539
|
-
const breaks = vector([0.0, 0.2, 0.4, 0.6, 0.8, 1.0]);
|
|
540
517
|
|
|
541
|
-
|
|
542
|
-
|
|
518
|
+
```svelte
|
|
519
|
+
<script>
|
|
520
|
+
import { Axes, XAxis, YAxis, Box, Bars } from 'svelte-plots-basic/2d';
|
|
521
|
+
|
|
522
|
+
const xValues = [2020, 2021, 2022, 2023, 2024];
|
|
523
|
+
const yValues = [47342, 52301, 49725, 53565, 55859];
|
|
543
524
|
</script>
|
|
544
525
|
|
|
545
|
-
<
|
|
546
|
-
|
|
547
|
-
|
|
548
|
-
|
|
549
|
-
|
|
526
|
+
<div class="plot-wrapper">
|
|
527
|
+
<Axes limX={[2019, 2025]} limY={[0, 60000]} title="GDP per capita">
|
|
528
|
+
|
|
529
|
+
<Bars {xValues} {yValues}
|
|
530
|
+
faceColor="#0000ff80"
|
|
531
|
+
lineColor="#0000ff"
|
|
532
|
+
lineWidth={1}
|
|
550
533
|
/>
|
|
534
|
+
|
|
535
|
+
<XAxis label="Year" />
|
|
536
|
+
<YAxis label="GDP per capita, USD" />
|
|
537
|
+
<Box />
|
|
551
538
|
</Axes>
|
|
539
|
+
</div>
|
|
540
|
+
|
|
541
|
+
<style>
|
|
542
|
+
.plot-wrapper {
|
|
543
|
+
width: 100%;
|
|
544
|
+
height: 100%;
|
|
545
|
+
}
|
|
546
|
+
</style>
|
|
552
547
|
```
|
|
548
|
+
Check more advanced example in Svelte REPL: [plots-bars](https://svelte.dev/playground/3afb9d8acd824d64ba1c42226fb01fa9).
|
|
553
549
|
|
|
554
|
-
|
|
550
|
+
Here is a table with all properties:
|
|
555
551
|
|
|
556
|
-
|
|
552
|
+
Property name | Default value | Description
|
|
553
|
+
--|--|--
|
|
554
|
+
`xValues` | | array or vector with coordinates of the middle point of the bars
|
|
555
|
+
`yValues` | | array or vector with coordinates of the top/bottom point of the bars
|
|
556
|
+
`lineColor` | `'#2679B2'` | color of border line
|
|
557
|
+
`lineWidth` | `1` | thickness/width of the border in pixels
|
|
558
|
+
`faceColor` | `'#2679B2'` | color of the face of the marker (only for first five)
|
|
559
|
+
`barWidth` | `0.8` | width of each par as per cent of maximum possible width
|
|
560
|
+
`onclick` | `null` | callback function for on mouse click event
|
|
557
561
|
|
|
558
|
-
[This example](https://svelte.dev/repl/fde87fe5dad24c419314eeb7b9fa469f?version=4.2.18) in Svelte REPL.
|
|
559
562
|
|
|
563
|
+
### Elements
|
|
560
564
|
|
|
561
|
-
|
|
565
|
+
Elements are components that add one single item to the plot. There are four of them, `<Area>` and `<Heatmap>` are used to add plotting elements based on data values, while `<Legend>` and `<ColormapLegend>` are service elements. Here is a short description of each.
|
|
562
566
|
|
|
563
|
-
|
|
567
|
+
#### Area
|
|
564
568
|
|
|
565
|
-
|
|
569
|
+
This component shows a polygon defined by a series of points. The points are connected by lines (including the connection between the first and the last points) and one can define the characteristics of the line (width, color, type), as well as color of the polygon face. The coordinates can be provided as an array or vector of values.
|
|
570
|
+
|
|
571
|
+
Here is an example:
|
|
566
572
|
|
|
567
573
|
```svelte
|
|
568
574
|
<script>
|
|
569
|
-
import { Axes,
|
|
570
|
-
import { matrix, vector } from 'mdatools/arrays';
|
|
571
|
-
|
|
572
|
-
// create values for a matrix
|
|
573
|
-
const values = matrix([0.9, 0.7, 0.5, 0.3, 0.9, 0.1, 0.1, 0.5, 0.9], 3, 3);
|
|
575
|
+
import { Axes, Xaxis, YAxis, Box, Area } from 'svelte-plots-basic/2d';
|
|
574
576
|
|
|
575
|
-
|
|
576
|
-
|
|
577
|
+
const xValues = [3, 2, 2.5, 4, 5.5, 6, 5];
|
|
578
|
+
const yValues = [1, 3, 4, 5, 4, 3, 1];
|
|
577
579
|
|
|
578
|
-
// colmap
|
|
579
|
-
const colmap = ["blue", "green", "yellow", "orange", "red"]
|
|
580
580
|
</script>
|
|
581
581
|
|
|
582
|
-
<
|
|
583
|
-
|
|
584
|
-
|
|
585
|
-
|
|
586
|
-
{colmap}
|
|
587
|
-
/>
|
|
582
|
+
<div class="plot-wrapper">
|
|
583
|
+
<Axes limX={[0, 6]} limY={[0, 7]}>
|
|
584
|
+
|
|
585
|
+
<Area {xValues} {yValues} lineColor="blue" lineWidth={2} faceColor="#3344ff80" />
|
|
588
586
|
|
|
589
|
-
|
|
587
|
+
<XAxis showGrid={true} />
|
|
588
|
+
<YAxis showGrid={true} />
|
|
589
|
+
<Box />
|
|
590
590
|
</Axes>
|
|
591
|
+
</div>
|
|
592
|
+
|
|
593
|
+
<style>
|
|
594
|
+
.plot-wrapper {
|
|
595
|
+
width: 100%;
|
|
596
|
+
height: 100%;
|
|
597
|
+
}
|
|
598
|
+
</style>
|
|
591
599
|
```
|
|
592
600
|
|
|
593
|
-
|
|
601
|
+
Check this example in Svelte REPL: [plots-area](https://svelte.dev/playground/478b77501386469b93a1160e46809644).
|
|
602
|
+
|
|
594
603
|
|
|
595
|
-
|
|
604
|
+
Here is a table with parameters:
|
|
596
605
|
|
|
597
|
-
|
|
598
|
-
|
|
599
|
-
|
|
600
|
-
|
|
606
|
+
Property name | Default value | Description
|
|
607
|
+
--|--|--
|
|
608
|
+
`xValues` | | array or vector with x-coordinates of points
|
|
609
|
+
`yValues` | | array or vector with y-coordinates of points
|
|
610
|
+
`lineColor` | `'#2679B2'` | color of line connecting the points
|
|
611
|
+
`lineWidth` | `2` | thickness/width of the line in pixels
|
|
612
|
+
`lineType` | `1` | number from 1 to 4 defining solid, dashed, dotted and dashdotted line
|
|
613
|
+
`faceColor` | `'transparent'` | color of the face inside the polygon
|
|
614
|
+
`opacity` | `1` | opacity value for both face and line color
|
|
615
|
+
`onclick` | `null` | callback function for on mouse click event
|
|
601
616
|
|
|
617
|
+
The opacity of separate line or face colors can be defined in the color value, e.g. `#ff000050`.
|
|
602
618
|
|
|
603
|
-
### Handle mouse events
|
|
604
619
|
|
|
605
|
-
|
|
620
|
+
#### Heatmap
|
|
606
621
|
|
|
607
|
-
|
|
622
|
+
Heatmap is used to visualze values of a matrix, hence the values for this component must be provided as object/instance of class `Matrix` from `mdatools` package.
|
|
608
623
|
|
|
609
|
-
|
|
624
|
+
Optionally you can also provide property `breaks` — list of interval boundaries you want to bin the values into, and `colmap` — list of colors associated with each interval. If you do not provide breaks, they will be computed automatically based on the provided values (the component splits them into 12 intervals evenly distrubuted between the smallest and the largest values).
|
|
625
|
+
|
|
626
|
+
Then the component visualizes the values as a rectangular grid/table, where every cell has a color depending on which interval a value corresponding to this cell is fallen into.
|
|
627
|
+
|
|
628
|
+
Here is an example:
|
|
610
629
|
|
|
611
630
|
```svelte
|
|
612
631
|
<script>
|
|
613
|
-
import { Axes,
|
|
632
|
+
import { Axes, Xaxis, YAxis, Box, Heatmap } from 'svelte-plots-basic/2d';
|
|
633
|
+
import { Matrix } from 'mdatools/arrays';
|
|
614
634
|
|
|
615
|
-
|
|
616
|
-
const
|
|
617
|
-
const y = [179.7, 185.3, 189.0, 193.5]
|
|
635
|
+
// create 5 x 10 matrix filled with normally distributed random values
|
|
636
|
+
const values = Matrix.randn(5, 10);
|
|
618
637
|
|
|
619
|
-
//
|
|
620
|
-
|
|
638
|
+
// create list with boundaries for 6 intervals
|
|
639
|
+
const breaks = [-3, -2, -1, 0, 1, 2, 3];
|
|
621
640
|
|
|
622
|
-
|
|
623
|
-
|
|
624
|
-
|
|
625
|
-
if (id >= 0) {
|
|
626
|
-
selected = Number.parseFloat(e.detail.elementID);
|
|
627
|
-
}
|
|
628
|
-
}
|
|
641
|
+
// create a list with colors for the intervals
|
|
642
|
+
const colmap = ['blue', 'cyan', 'green', 'yellow', 'orange', 'red'];
|
|
643
|
+
</script>
|
|
629
644
|
|
|
630
|
-
|
|
631
|
-
|
|
632
|
-
|
|
645
|
+
<div class="plot-wrapper">
|
|
646
|
+
<Axes limX={[0.5, 15.5]} limY={[0.5, 5.5]}>
|
|
647
|
+
|
|
648
|
+
<Heatmap {values} {breaks} {colmap} />
|
|
649
|
+
|
|
650
|
+
<XAxis whole={true} />
|
|
651
|
+
<YAxis whole={true} />
|
|
652
|
+
<Box />
|
|
653
|
+
</Axes>
|
|
654
|
+
</div>
|
|
655
|
+
|
|
656
|
+
<style>
|
|
657
|
+
.plot-wrapper {
|
|
658
|
+
width: 100%;
|
|
659
|
+
height: 100%;
|
|
633
660
|
}
|
|
634
|
-
</
|
|
661
|
+
</style>
|
|
662
|
+
```
|
|
635
663
|
|
|
636
|
-
|
|
637
|
-
xLabel="Years" yLabel="bn US$ PPP" title="GDP of Denmark"
|
|
638
|
-
on:markerclick={selectMarker}
|
|
639
|
-
on:axesclick={resetSelection}
|
|
640
|
-
>
|
|
664
|
+
Check this example in Svelte REPL: [plots-heatmap](https://svelte.dev/playground/fde87fe5dad24c419314eeb7b9fa469f).
|
|
641
665
|
|
|
642
|
-
|
|
643
|
-
<Points xValues={x} yValues={y} markerSize="1.5" faceColor="#fff" borderWidth="2"/>
|
|
666
|
+
Here is a table with parameters:
|
|
644
667
|
|
|
645
|
-
|
|
646
|
-
|
|
647
|
-
|
|
648
|
-
|
|
649
|
-
|
|
668
|
+
Property name | Default value | Description
|
|
669
|
+
--|--|--
|
|
670
|
+
`values` | | matrix with values (mandatory)
|
|
671
|
+
`breaks` | | optional, list or vector with interval boundaries
|
|
672
|
+
`colmap` | | optional, list of colors for each interval
|
|
650
673
|
|
|
651
|
-
|
|
652
|
-
<YAxis slot="yaxis" showGrid={true} />
|
|
653
|
-
<Box slot="box" />
|
|
674
|
+
Naturally, number of breaks is by one larger than the number of intervals and hence number of colors.
|
|
654
675
|
|
|
655
|
-
</Axes>
|
|
656
|
-
```
|
|
657
676
|
|
|
658
|
-
|
|
677
|
+
#### Colormap legend
|
|
678
|
+
|
|
679
|
+
Colormap legend can be used together with `<Heatmap>` or any other color grouping case, when you need to show a legend which match colors and corresponding values. Here is an example how it can be used with `<Heatmap>` (note that y-limits were adjusted to give place for the legend):
|
|
680
|
+
|
|
659
681
|
|
|
682
|
+
```svelte
|
|
683
|
+
<script>
|
|
684
|
+
import { Axes, Xaxis, YAxis, Box, Heatmap, ColormapLegend } from 'svelte-plots-basic/2d';
|
|
685
|
+
import { Matrix } from 'mdatools/arrays';
|
|
686
|
+
|
|
687
|
+
// create 5 x 10 matrix filled with normally distributed random values
|
|
688
|
+
const values = Matrix.randn(5, 10);
|
|
689
|
+
|
|
690
|
+
// create list with boundaries for 6 intervals
|
|
691
|
+
const breaks = [-3, -2, -1, 0, 1, 2, 3];
|
|
692
|
+
|
|
693
|
+
// create a list with colors for the intervals
|
|
694
|
+
const colmap = ['blue', 'cyan', 'green', 'yellow', 'orange', 'red'];
|
|
695
|
+
</script>
|
|
660
696
|
|
|
661
|
-
|
|
697
|
+
<div class="plot-wrapper">
|
|
698
|
+
<Axes limX={[0.5, 15.5]} limY={[0.5, 6.5]}>
|
|
699
|
+
|
|
700
|
+
<ColormapLegend {breaks} {colmap} />
|
|
701
|
+
|
|
702
|
+
<Heatmap {values} {breaks} {colmap} />
|
|
703
|
+
<XAxis whole={true} />
|
|
704
|
+
<YAxis whole={true} />
|
|
705
|
+
<Box />
|
|
706
|
+
</Axes>
|
|
707
|
+
</div>
|
|
708
|
+
|
|
709
|
+
<style>
|
|
710
|
+
.plot-wrapper {
|
|
711
|
+
width: 100%;
|
|
712
|
+
height: 100%;
|
|
713
|
+
}
|
|
714
|
+
</style>
|
|
715
|
+
```
|
|
662
716
|
|
|
663
|
-
|
|
717
|
+
By default labels are set based on the interval boundaries (breaks) but you can also provide array with manual labels. If number of labels is the same as number of breaks (by one larger than number of colors), they will be shown between the colored rectangles. If the number is the same, then they will be shown in the middle.
|
|
664
718
|
|
|
665
|
-
|
|
666
|
-
* `"hover"` — buttons with download options appear when user hovers mouse over the plot.
|
|
667
|
-
* `"fixed"` — buttons with download options are always shown.
|
|
719
|
+
Check these two Svelte REPL apps for example: [plots-heatmap-colormap](https://svelte.dev/playground/52ba71622f554155a56755b987598ce5), [plots-colormap](https://svelte.dev/playground/637311032f584f9d9fc4b324db4f9f49)
|
|
668
720
|
|
|
669
|
-
You can also specify parameter `fileName` which should be the desired filename for the plot without extension (e.g. `fileName="myplot"`).
|
|
670
721
|
|
|
671
|
-
|
|
722
|
+
#### Legend
|
|
672
723
|
|
|
673
|
-
|
|
674
|
-
* `pngHeight` — height of PNG image in cm (default is 8 cm).
|
|
675
|
-
* `pngRes` — resolution of the PNG image in pixels per inch (default is 300).
|
|
724
|
+
This component makes sense to use if you show several series on the same plot and want to annotate them. It has two main arguments — a position of the legend and array with JSON objects specifying legend text and parameters of the corresponding series.
|
|
676
725
|
|
|
677
|
-
|
|
726
|
+
Every item should contain a mandatory field `label` — with text label for the legend item, and, optionally two additional fields: `point` — with properties of point and `line` — with properties of line. If you have other elements, e.g. segments, bars, areas, in the legend they can be still defined by a marker (`point`) or/and by a line (`line`).
|
|
678
727
|
|
|
679
|
-
Here is
|
|
728
|
+
Here is an example:
|
|
680
729
|
|
|
681
730
|
```svelte
|
|
682
731
|
<script>
|
|
683
|
-
import { Axes, XAxis, YAxis, Box,
|
|
684
|
-
|
|
732
|
+
import { Axes, Points, XAxis, YAxis, Box, Lines, Legend } from 'svelte-plots-basic/2d';
|
|
733
|
+
import {Vector} from 'mdatools/arrays';
|
|
685
734
|
|
|
686
|
-
//
|
|
687
|
-
const x = Vector.
|
|
688
|
-
|
|
735
|
+
// create x-values
|
|
736
|
+
const x = Vector.seq(-5, 5, 0.1);
|
|
737
|
+
|
|
738
|
+
// compute y-values
|
|
739
|
+
const y1 = x.apply(v => Math.pow(v, 2));
|
|
740
|
+
const y2 = x.apply(v => Math.pow(v, 3));
|
|
741
|
+
const y3 = x.apply(v => Math.pow(v, 4));
|
|
742
|
+
|
|
743
|
+
// define properties of each series
|
|
744
|
+
line1Props = {lineType: 3, lineColor: 'red'};
|
|
745
|
+
line2Props = {lineColor: 'blue', lineType: 1};
|
|
746
|
+
point2Props = {lineColor: 'blue', faceColor: 'white'};
|
|
747
|
+
point3Props = {marker: 5, lineColor: 'green', faceColor: 'yellow'};
|
|
748
|
+
|
|
749
|
+
// legend items
|
|
750
|
+
items = [
|
|
751
|
+
{label: 'y=x^2', line: lines1Props },
|
|
752
|
+
{label: "y=x^3", line: lines2Props, point: points2Props },
|
|
753
|
+
{label: "y=x^4", point: points2Props },
|
|
754
|
+
];
|
|
689
755
|
</script>
|
|
690
756
|
|
|
691
|
-
<
|
|
692
|
-
|
|
693
|
-
|
|
694
|
-
|
|
695
|
-
|
|
696
|
-
|
|
697
|
-
|
|
698
|
-
|
|
699
|
-
|
|
700
|
-
|
|
701
|
-
|
|
702
|
-
|
|
703
|
-
|
|
704
|
-
|
|
705
|
-
|
|
706
|
-
|
|
707
|
-
|
|
757
|
+
<Axes limX={[-5, 5]} limY={[-20, 20]}>
|
|
758
|
+
|
|
759
|
+
<!-- series 1: dotted red line -->
|
|
760
|
+
<Lines xValues={x} yValues={y1} {...line1Props} />
|
|
761
|
+
|
|
762
|
+
<!-- series 2: solid blue line and circle markers -->
|
|
763
|
+
<Lines xValues={x} yValues={y2} {...line2Props} />
|
|
764
|
+
<Points xValues={x} yValues={y2} {...point2Props} />
|
|
765
|
+
|
|
766
|
+
<!-- series 3: markers in form of diamonds with green stroke and yellow fill -->
|
|
767
|
+
<Points xValues={x} yValues={y3} {...points3Props} />
|
|
768
|
+
|
|
769
|
+
<!-- legend with one JSON for each series -->
|
|
770
|
+
<Legend position="right" {items} />
|
|
771
|
+
|
|
772
|
+
<XAxis showGrid={true} />
|
|
773
|
+
<YAxis showGrid={true} />
|
|
774
|
+
<Box />
|
|
775
|
+
</Axes>
|
|
708
776
|
```
|
|
777
|
+
Check this example in Svelte REPL: [plots-legend](https://svelte.dev/playground/1c5e21e2153a4229afb630809fe545bd) in Svelte REPL.
|
|
709
778
|
|
|
710
|
-
|
|
779
|
+
The `position` parameter can be one of the follows: `'topleft'`, `'top'`, `'topright'`, `'right'`, `'bottomright'` and so on.
|
|
711
780
|
|
|
712
|
-
|
|
781
|
+
The component has also properties which changes it apearance, here is the full list:
|
|
782
|
+
|
|
783
|
+
Property name | Default value | Description
|
|
784
|
+
--|--|--
|
|
785
|
+
`items` | | array with JSON properties of legend items
|
|
786
|
+
`position` | `'topleft'` | position of the legend element inside plotting area
|
|
787
|
+
`lineColor` | `'#303030'` | color of the legend box line
|
|
788
|
+
`lineWidth` | `1` | width (thickness) of the legend box line
|
|
789
|
+
`faceColor` | `'#fff'` | background color of the legend box
|
|
790
|
+
`fontSize` | `0.85` | font size for labels in em
|
|
791
|
+
|
|
792
|
+
|
|
793
|
+
## User tutorial (3D plots)
|
|
713
794
|
|
|
714
795
|
3D plots can be created similar to 2D plots but its components must be imported from `svelte-plots-basic/3d`. Plus all elements must have three coordinates (x, y and z). Plot elements include axes pane, x-, y- and z-axis, as well as points, lines, segments and mesh series.
|
|
715
796
|
|
|
716
797
|
The plots are also made as SVG elements, by using [isometric projection](https://en.wikipedia.org/wiki/Isometric_projection). The orientation of the projection plane is defined by parameters `phi` and `theta`. Parameter `zoom` defines the distance between the plane and the scene.
|
|
717
798
|
|
|
799
|
+
The 3D plots can also be downloaded or copied to clipboard, however mouse events are directly not supported yet. However you can add mouse and keyboard support extrentally.
|
|
800
|
+
|
|
718
801
|
Here is an example of simple 3D scatter plot:
|
|
719
802
|
|
|
720
803
|
```svelte
|
|
@@ -735,14 +818,16 @@ Here is an example of simple 3D scatter plot:
|
|
|
735
818
|
|
|
736
819
|
<Axes limX={[-10, 10]} limY={[-10, 10]} limZ={[-10, 10]} {zoom} {phi} {theta}>
|
|
737
820
|
<Points {xValues} {yValues} {zValues} />
|
|
738
|
-
|
|
739
|
-
<
|
|
740
|
-
<
|
|
821
|
+
|
|
822
|
+
<XAxis showGrid={true} title="X" />
|
|
823
|
+
<YAxis showGrid={true} title="Y" />
|
|
824
|
+
<ZAxis showGrid={true} title="Z" />
|
|
741
825
|
</Axes>
|
|
742
826
|
```
|
|
743
827
|
|
|
744
|
-
[This example](https://svelte.dev/repl/2294eecba7d7477ab9a09c1734d32ac2?version=4.2.18) in Svelte REPL.
|
|
745
|
-
|
|
746
828
|
You can also add `Lines`, `Segments` and `Mesh` series to 3D plots.
|
|
747
829
|
|
|
830
|
+
Check more advanced example in Svelte REPL with mouse and keyboard support, and more: [plots-3d]()
|
|
831
|
+
|
|
832
|
+
|
|
748
833
|
|